test_include_bug.sql

application/octet-stream

Filename: test_include_bug.sql
Type: application/octet-stream
Part: 0
Message: Re: [Patch] Add WHERE clause support to REFRESH MATERIALIZED VIEW
DROP TABLE IF EXISTS t_include CASCADE;
CREATE TABLE t_include (
    id INT PRIMARY KEY,
    data TEXT,
    extra TEXT
);

INSERT INTO t_include VALUES
    (1, 'data1', 'extra1'),
    (2, 'data2', 'extra2');

CREATE MATERIALIZED VIEW mv_include AS SELECT * FROM t_include;

-- Create index with INCLUDE column
CREATE UNIQUE INDEX idx_include ON mv_include(id) INCLUDE (extra);

\echo 'Index structure:'
SELECT
    indnatts as total_attrs,
    indnkeyatts as key_attrs
FROM pg_index
WHERE indexrelid = 'idx_include'::regclass;

\echo 'Attempting partial refresh with INCLUDE index...'
UPDATE t_include SET data = 'updated1', extra = 'updated_extra1' WHERE id = 1;

\set ON_ERROR_STOP 0
REFRESH MATERIALIZED VIEW mv_include WHERE id = 1;
\set ON_ERROR_STOP 1

\echo ''
\echo 'Expected error: "no unique or exclusion constraint matching ON CONFLICT"'
\echo 'Root cause: Code uses indnatts (2) instead of indnkeyatts (1)'
\echo 'Generates: ON CONFLICT (id, extra) but constraint is only on (id)'