[32m[1m       Using[0m[39m [37m[1mDefaultFeature("pg18")[0m[39m and `pg_config` from [36m/usr/bin/pg_config[39m
[32m[1m    Building[0m[39m for SQL generation with features `pg18`
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
[32m[1m  Discovered[0m[39m [36m[1m36[0m[39m SQL entities: [36m[1m0[0m[39m schemas ([36m[1m0[0m[39m unique), [36m[1m28[0m[39m functions, [36m[1m1[0m[39m types, [36m[1m0[0m[39m enums, [36m[1m4[0m[39m sqls, [36m[1m0[0m[39m ords, [36m[1m0[0m[39m hashes, [36m[1m0[0m[39m aggregates, [36m[1m3[0m[39m triggers
[32m[1m  Rebuilding[0m[39m [36mpgrx_embed[39m, in debug mode, for SQL generation with features [36mpg18[39m
   Compiling pg_tviews v0.1.0-beta.11 (/home/lionel/code/pg_tviews)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s
[32m[1m     Writing[0m[39m SQL entities to /dev/stdout
/* <begin connected objects> */
/*
This file is auto generated by pgrx.

The ordering of items is not stable, it is driven by a dependency graph.
*/
/* </end connected objects> */

/* <begin connected objects> */
-- src/metadata.rs:134

CREATE TABLE IF NOT EXISTS @extschema@.pg_tview_audit_log (
    log_id BIGSERIAL PRIMARY KEY,
    operation TEXT NOT NULL,  -- CREATE, DROP, REFRESH
    entity TEXT NOT NULL,
    performed_by TEXT NOT NULL DEFAULT current_user,
    performed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    transaction_id BIGINT DEFAULT pg_current_xact_id()::text::bigint,
    rows_affected BIGINT,
    details JSONB,
    client_addr INET DEFAULT inet_client_addr(),
    client_port INTEGER DEFAULT inet_client_port()
);

CREATE INDEX IF NOT EXISTS idx_audit_log_entity_time ON public.pg_tview_audit_log(entity, performed_at);

COMMENT ON TABLE public.pg_tview_audit_log IS 'Audit log for TVIEW operations';
/* </end connected objects> */

/* <begin connected objects> */
-- src/metadata.rs:35

    CREATE TABLE IF NOT EXISTS @extschema@.pg_tview_meta (
        entity TEXT NOT NULL PRIMARY KEY,
        view_oid OID NOT NULL,
        table_oid OID NOT NULL,
        definition TEXT NOT NULL,
        cascade_paths TEXT[] NOT NULL DEFAULT '{}',
        fk_columns TEXT[] NOT NULL DEFAULT '{}',
        uuid_fk_columns TEXT[] NOT NULL DEFAULT '{}',
        dependency_types TEXT[] NOT NULL DEFAULT '{}',
        dependency_paths TEXT[]  NOT NULL DEFAULT '{}',
        array_match_keys TEXT[] NOT NULL DEFAULT '{}',
        distinct_on_keys TEXT[] NOT NULL DEFAULT '{}',
        is_union BOOLEAN NOT NULL DEFAULT FALSE,
        created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
    );

    CREATE TABLE IF NOT EXISTS @extschema@.pg_tview_helpers (
        helper_name TEXT NOT NULL PRIMARY KEY,
        is_helper BOOLEAN NOT NULL DEFAULT TRUE,
        used_by TEXT[] NOT NULL DEFAULT '{}',
        depends_on TEXT[] NOT NULL DEFAULT '{}',
        created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
    );

    COMMENT ON TABLE @extschema@.pg_tview_meta IS 'Metadata for TVIEW materialized tables';
    COMMENT ON TABLE @extschema@.pg_tview_helpers IS 'Tracks helper views used by TVIEWs';

    -- Indexes for catalog lookup performance (entity PK already has a unique index)
    CREATE INDEX IF NOT EXISTS idx_pg_tview_meta_table_oid
        ON @extschema@.pg_tview_meta(table_oid);
/* </end connected objects> */

/* <begin connected objects> */
-- src/metadata.rs:157
-- requires:
--   create_metadata_tables


-- Queue monitoring view
CREATE OR REPLACE VIEW @extschema@.pg_tviews_queue_realtime AS
SELECT
    current_setting('application_name') as session,
    pg_backend_pid() as backend_pid,
    txid_current() as transaction_id,
    0 as queue_size,
    ARRAY[]::TEXT[] as entities,
    NOW() as last_enqueued;

-- Cache statistics view
CREATE OR REPLACE VIEW @extschema@.pg_tviews_cache_stats AS
SELECT
    'graph_cache' as cache_type,
    0::BIGINT as entries,
    '0 bytes' as estimated_size
UNION ALL
SELECT
    'table_cache' as cache_type,
    0::BIGINT as entries,
    '0 bytes' as estimated_size;

-- Performance summary view
-- Note: public.pg_tview_meta is hardcoded (not @extschema@) because pgrx strips
-- @extschema@. from generated SQL, causing the reference to be unqualified and
-- fail during extension installation (search_path does not include public at
-- install time). The rest of the extension uses public.* explicitly.
CREATE OR REPLACE VIEW public.pg_tviews_performance_summary AS
SELECT
    entity,
    COUNT(*) as total_refreshes,
    0.0 as avg_refresh_ms,
    NOW() as last_refresh
FROM public.pg_tview_meta
GROUP BY entity;
/* </end connected objects> */

/* <begin connected objects> */
-- src/schema/mod.rs:38
-- pg_tviews::schema::TViewSchema
CREATE TYPE TViewSchema;

-- src/schema/mod.rs:38
-- pg_tviews::schema::tviewschema_in
CREATE  FUNCTION "tviewschema_in"(
	"input" cstring /* core::option::Option<&core::ffi::c_str::CStr> */
) RETURNS TViewSchema /* core::option::Option<pg_tviews::schema::TViewSchema> */
IMMUTABLE PARALLEL SAFE
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'tviewschema_in_wrapper';

-- src/schema/mod.rs:38
-- pg_tviews::schema::tviewschema_out
CREATE  FUNCTION "tviewschema_out"(
	"input" TViewSchema /* pg_tviews::schema::TViewSchema */
) RETURNS cstring /* alloc::ffi::c_str::CString */
IMMUTABLE STRICT PARALLEL SAFE
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'tviewschema_out_wrapper';

-- src/schema/mod.rs:38
-- pg_tviews::schema::TViewSchema
CREATE TYPE TViewSchema (
	INTERNALLENGTH = variable,
	INPUT = tviewschema_in, /* pg_tviews::schema::tviewschema_in */
	OUTPUT = tviewschema_out, /* pg_tviews::schema::tviewschema_out */
	STORAGE = extended
);
/* </end connected objects> */

/* <begin connected objects> */
-- src/admin.rs:13
-- pg_tviews::admin::pg_tviews_analyze_select
CREATE  FUNCTION "pg_tviews_analyze_select"(
	"sql" TEXT /* &str */
) RETURNS jsonb /* pgrx::datum::json::JsonB */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_analyze_select_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/cascade.rs:14
-- pg_tviews::cascade::pg_tviews_cascade
CREATE  FUNCTION "pg_tviews_cascade"(
	"base_table_oid" oid, /* pgrx_pg_sys::submodules::oids::Oid */
	"pk_value" bigint /* i64 */
) RETURNS void
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_cascade_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lifecycle.rs:175
-- pg_tviews::lifecycle::pg_tviews_check_jsonb_delta
CREATE  FUNCTION "pg_tviews_check_jsonb_delta"() RETURNS bool /* bool */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_check_jsonb_delta_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/ddl/mod.rs:70
-- pg_tviews::ddl::pg_tviews_convert_existing_table
CREATE  FUNCTION "pg_tviews_convert_existing_table"(
	"table_name" TEXT /* &str */
) RETURNS TEXT /* core::result::Result<alloc::string::String, alloc::string::String> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_convert_existing_table_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/event_trigger.rs:27
-- pg_tviews::event_trigger::pg_tviews_convert_table
CREATE  FUNCTION "pg_tviews_convert_table"(
	"table_name" TEXT /* alloc::string::String */
) RETURNS VOID /* core::result::Result<(), alloc::boxed::Box<dyn core::error::Error>> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_convert_table_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/ddl/mod.rs:30
-- pg_tviews::ddl::pg_tviews_create
CREATE  FUNCTION "pg_tviews_create"(
	"tview_name" TEXT, /* &str */
	"select_sql" TEXT /* &str */
) RETURNS TEXT /* core::result::Result<alloc::string::String, alloc::string::String> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_create_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/health.rs:149
-- pg_tviews::health::pg_tviews_debug_queue
CREATE  FUNCTION "pg_tviews_debug_queue"() RETURNS jsonb /* pgrx::datum::json::JsonB */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_debug_queue_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/cascade.rs:57
-- pg_tviews::cascade::pg_tviews_delete
CREATE  FUNCTION "pg_tviews_delete"(
	"base_table_oid" oid, /* pgrx_pg_sys::submodules::oids::Oid */
	"pk_value" bigint /* i64 */
) RETURNS void
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_delete_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/ddl/mod.rs:50
-- pg_tviews::ddl::pg_tviews_drop
CREATE  FUNCTION "pg_tviews_drop"(
	"tview_name" TEXT, /* &str */
	"if_exists" bool DEFAULT false, /* bool */
	"cascade" bool DEFAULT false /* bool */
) RETURNS TEXT /* core::result::Result<alloc::string::String, alloc::string::String> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_drop_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/health.rs:13
-- pg_tviews::health::pg_tviews_health_check
CREATE  FUNCTION "pg_tviews_health_check"() RETURNS TABLE (
	"status" TEXT,  /* alloc::string::String */
	"component" TEXT,  /* alloc::string::String */
	"message" TEXT,  /* alloc::string::String */
	"severity" TEXT  /* alloc::string::String */
)
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_health_check_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lifecycle.rs:24
-- pg_tviews::lifecycle::pg_tviews_hook_status
CREATE  FUNCTION "pg_tviews_hook_status"() RETURNS TEXT /* &str */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_hook_status_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/admin.rs:27
-- pg_tviews::admin::pg_tviews_infer_types
CREATE  FUNCTION "pg_tviews_infer_types"(
	"table_name" TEXT, /* &str */
	"columns" TEXT[] /* alloc::vec::Vec<alloc::string::String> */
) RETURNS jsonb /* pgrx::datum::json::JsonB */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_infer_types_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/cascade.rs:50
-- pg_tviews::cascade::pg_tviews_insert
CREATE  FUNCTION "pg_tviews_insert"(
	"base_table_oid" oid, /* pgrx_pg_sys::submodules::oids::Oid */
	"pk_value" bigint /* i64 */
) RETURNS void
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_insert_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lib.rs:73
-- pg_tviews::pg_tviews_is_suspended
CREATE  FUNCTION "pg_tviews_is_suspended"() RETURNS bool /* bool */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_is_suspended_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/admin.rs:129
-- pg_tviews::admin::pg_tviews_migrate_triggers
CREATE  FUNCTION "pg_tviews_migrate_triggers"() RETURNS void
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_migrate_triggers_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/health.rs:169
-- pg_tviews::health::pg_tviews_performance_stats
CREATE  FUNCTION "pg_tviews_performance_stats"() RETURNS TABLE (
	"entity" TEXT,  /* alloc::string::String */
	"table_size" TEXT,  /* alloc::string::String */
	"total_size" TEXT,  /* alloc::string::String */
	"row_count" bigint,  /* i64 */
	"index_count" INT  /* i32 */
)
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_performance_stats_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/health.rs:126
-- pg_tviews::health::pg_tviews_queue_stats
CREATE  FUNCTION "pg_tviews_queue_stats"() RETURNS jsonb /* pgrx::datum::json::JsonB */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_queue_stats_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lifecycle.rs:73
-- pg_tviews::lifecycle::pg_tviews_recover_after_crash
CREATE  FUNCTION "pg_tviews_recover_after_crash"(
	"entity_name" TEXT /* &str */
) RETURNS bool /* core::result::Result<bool, pg_tviews::error::TViewError> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_recover_after_crash_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/admin.rs:58
-- pg_tviews::admin::pg_tviews_refresh
CREATE  FUNCTION "pg_tviews_refresh"(
	"entity" TEXT /* &str */
) RETURNS VOID /* core::result::Result<(), pg_tviews::error::TViewError> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_refresh_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lib.rs:102
-- pg_tviews::pg_tviews_refresh_all
CREATE  FUNCTION "pg_tviews_refresh_all"() RETURNS jsonb /* core::result::Result<pgrx::datum::json::JsonB, alloc::string::String> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_refresh_all_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/admin.rs:100
-- pg_tviews::admin::pg_tviews_refresh_all_entities
CREATE  FUNCTION "pg_tviews_refresh_all_entities"() RETURNS VOID /* core::result::Result<(), pg_tviews::error::TViewError> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_refresh_all_entities_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lib.rs:85
-- pg_tviews::pg_tviews_resume_triggers
CREATE  FUNCTION "pg_tviews_resume_triggers"() RETURNS void
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_resume_triggers_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/admin.rs:139
-- pg_tviews::admin::pg_tviews_show_cascade_path
CREATE  FUNCTION "pg_tviews_show_cascade_path"(
	"entity" TEXT /* &str */
) RETURNS TABLE (
	"depth" INT,  /* i32 */
	"entity_name" TEXT,  /* alloc::string::String */
	"depends_on" TEXT  /* alloc::string::String */
)
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_show_cascade_path_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lib.rs:78
-- pg_tviews::pg_tviews_suspend_triggers
CREATE  FUNCTION "pg_tviews_suspend_triggers"() RETURNS void
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_suspend_triggers_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lib.rs:174
-- pg_tviews::pg_tviews_suspended_entities
CREATE  FUNCTION "pg_tviews_suspended_entities"() RETURNS TEXT[] /* alloc::vec::Vec<alloc::string::String> */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_suspended_entities_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/lifecycle.rs:17
-- pg_tviews::lifecycle::pg_tviews_version
CREATE  FUNCTION "pg_tviews_version"() RETURNS TEXT /* &str */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'pg_tviews_version_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/trigger.rs:268
-- pg_tviews::trigger::pg_tview_flush_trigger
CREATE FUNCTION "pg_tview_flush_trigger"()
	RETURNS TRIGGER
	LANGUAGE c
	AS 'MODULE_PATHNAME', 'pg_tview_flush_trigger_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/trigger.rs:289
-- pg_tviews::trigger::pg_tview_stmt_trigger_handler
CREATE FUNCTION "pg_tview_stmt_trigger_handler"()
	RETURNS TRIGGER
	LANGUAGE c
	AS 'MODULE_PATHNAME', 'pg_tview_stmt_trigger_handler_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/trigger.rs:68
-- pg_tviews::trigger::pg_tview_trigger_handler
CREATE FUNCTION "pg_tview_trigger_handler"()
	RETURNS TRIGGER
	LANGUAGE c
	AS 'MODULE_PATHNAME', 'pg_tview_trigger_handler_wrapper';
/* </end connected objects> */

/* <begin connected objects> */
-- src/metadata.rs:76
-- requires:
--   create_metadata_tables

-- finalize

-- Event trigger handler: PL/pgSQL wrapper that calls the Rust C function pg_tviews_convert_table().
-- Using PL/pgSQL (not a direct C function) because pgrx cannot generate RETURNS event_trigger
-- for #[pg_extern] functions — it always emits RETURNS VOID, which PostgreSQL rejects for
-- event trigger handlers.  The Rust logic lives in src/event_trigger.rs::handle_ddl_event_internal.
CREATE OR REPLACE FUNCTION pg_tviews_handle_ddl_event()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
    obj record;
BEGIN
    FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands()
    LOOP
        -- Only process table-creation commands
        IF obj.command_tag IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO') THEN
            -- Only intercept tv_* tables
            IF obj.object_identity LIKE '%.tv_%' OR obj.object_identity LIKE 'tv_%' THEN
                DECLARE
                    table_name_only TEXT;
                BEGIN
                    table_name_only := CASE
                        WHEN obj.object_identity LIKE '%.%'
                        THEN split_part(obj.object_identity, '.', 2)
                        ELSE obj.object_identity
                    END;

                    PERFORM pg_tviews_convert_table(table_name_only);
                EXCEPTION
                    WHEN OTHERS THEN
                        -- pg_tviews_convert_table raises its own error; re-raise here.
                        RAISE;
                END;
            END IF;
        END IF;
    END LOOP;
END;
$$;

-- Create the event trigger (fires after CREATE TABLE completes — safe SPI context)
DROP EVENT TRIGGER IF EXISTS pg_tviews_ddl_end;
CREATE EVENT TRIGGER pg_tviews_ddl_end
    ON ddl_command_end
    WHEN TAG IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')
    EXECUTE FUNCTION pg_tviews_handle_ddl_event();

COMMENT ON EVENT TRIGGER pg_tviews_ddl_end IS
'Intercepts CREATE TABLE tv_* commands and converts them to TVIEWs';
/* </end connected objects> */

