Thread

  1. [PATCH] Cover some errors and corner conditions in repack.c

    Álvaro Herrera <alvherre@kurilemu.de> — 2026-05-28T09:20:19Z

    ---
     src/test/regress/expected/cluster.out | 33 +++++++++++++++++++++++++++
     src/test/regress/sql/cluster.sql      | 28 +++++++++++++++++++++++
     2 files changed, 61 insertions(+)
    
    diff --git a/src/test/regress/expected/cluster.out b/src/test/regress/expected/cluster.out
    index 23f312c62a3..cfbe2764427 100644
    --- a/src/test/regress/expected/cluster.out
    +++ b/src/test/regress/expected/cluster.out
    @@ -697,6 +697,39 @@ SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
     (4 rows)
     
     COMMIT;
    +-- verify some error cases
    +CREATE TABLE clstr_table_one (id int, val text);
    +CREATE TABLE clstr_table_two (id int, val text);
    +CREATE INDEX clstr_idx_b ON clstr_table_two (id);
    +CLUSTER clstr_table_one USING clstr_idx_b;
    +ERROR:  "clstr_idx_b" is not an index for table "clstr_table_one"
    +CLUSTER clstr_table_one USING nonexistant;
    +ERROR:  index "nonexistant" for table "clstr_table_one" does not exist
    +CREATE INDEX clstr_hash_idx ON clstr_table_one USING hash (id);
    +CLUSTER clstr_table_one USING clstr_hash_idx;
    +ERROR:  cannot cluster on index "clstr_hash_idx" because access method does not support clustering
    +CREATE INDEX clstr_partial_idx ON clstr_table_one (id) WHERE id > 0;
    +CLUSTER clstr_table_one USING clstr_partial_idx;
    +ERROR:  cannot cluster on partial index "clstr_partial_idx"
    +REPACK pg_class USING INDEX pg_class_oid_index;
    +ERROR:  permission denied: "pg_class" is a system catalog
    +DETAIL:  System catalogs can only be clustered by the index they're already clustered on, if any, unless "allow_system_table_mods" is enabled.
    +DROP TABLE clstr_table_one, clstr_table_two;
    +-- verify that CLUSTER/REPACK don't touch a NO DATA matview
    +CREATE MATERIALIZED VIEW clstr_matview AS
    +    SELECT i FROM generate_series(1, 5) i
    +    WITH NO DATA;
    +CREATE INDEX clstr_matview_idx ON clstr_matview (i);
    +SELECT relfilenode FROM pg_class WHERE oid = 'clstr_matview'::regclass \gset
    +CLUSTER clstr_matview USING clstr_matview_idx;
    +REPACK clstr_matview USING INDEX clstr_matview_idx;
    +SELECT relfilenode = :relfilenode FROM pg_class WHERE oid = 'clstr_matview'::regclass;
    + ?column? 
    +----------
    + t
    +(1 row)
    +
    +DROP MATERIALIZED VIEW clstr_matview;
     ----------------------------------------------------------------------
     --
     -- REPACK
    diff --git a/src/test/regress/sql/cluster.sql b/src/test/regress/sql/cluster.sql
    index c2f329ecd1b..1cb1942263c 100644
    --- a/src/test/regress/sql/cluster.sql
    +++ b/src/test/regress/sql/cluster.sql
    @@ -328,6 +328,34 @@ EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
     SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
     COMMIT;
     
    +-- verify some error cases
    +CREATE TABLE clstr_table_one (id int, val text);
    +CREATE TABLE clstr_table_two (id int, val text);
    +CREATE INDEX clstr_idx_b ON clstr_table_two (id);
    +CLUSTER clstr_table_one USING clstr_idx_b;
    +CLUSTER clstr_table_one USING nonexistant;
    +
    +CREATE INDEX clstr_hash_idx ON clstr_table_one USING hash (id);
    +CLUSTER clstr_table_one USING clstr_hash_idx;
    +
    +CREATE INDEX clstr_partial_idx ON clstr_table_one (id) WHERE id > 0;
    +CLUSTER clstr_table_one USING clstr_partial_idx;
    +
    +REPACK pg_class USING INDEX pg_class_oid_index;
    +
    +DROP TABLE clstr_table_one, clstr_table_two;
    +
    +-- verify that CLUSTER/REPACK don't touch a NO DATA matview
    +CREATE MATERIALIZED VIEW clstr_matview AS
    +    SELECT i FROM generate_series(1, 5) i
    +    WITH NO DATA;
    +CREATE INDEX clstr_matview_idx ON clstr_matview (i);
    +SELECT relfilenode FROM pg_class WHERE oid = 'clstr_matview'::regclass \gset
    +CLUSTER clstr_matview USING clstr_matview_idx;
    +REPACK clstr_matview USING INDEX clstr_matview_idx;
    +SELECT relfilenode = :relfilenode FROM pg_class WHERE oid = 'clstr_matview'::regclass;
    +DROP MATERIALIZED VIEW clstr_matview;
    +
     ----------------------------------------------------------------------
     --
     -- REPACK
    -- 
    2.47.3
    
    
    --yrl6wzxda7bokrre--