From f8408756e69a8341edd21aa6c8c4275460762ea9 Mon Sep 17 00:00:00 2001 From: Seongjun Shin Date: Wed, 25 Nov 2026 14:35:21 +0900 Subject: [PATCH v1] Preserve tablespace of exclusion constraint indexes in ALTER TYPE ALTER TABLE ... ALTER COLUMN TYPE rebuilds an exclusion constraint from the output of pg_get_constraintdef_command(), which never included the index tablespace. The index was therefore recreated in the default tablespace, and when its storage was reused without a rewrite, the index files could no longer be found. Print USING INDEX TABLESPACE for exclusion constraints when a full command is requested, as is already done for primary key and unique constraints. pg_get_constraintdef() output is unchanged. Author: Seongjun Shin --- src/backend/utils/adt/ruleutils.c | 3 +- src/test/regress/expected/tablespace.out | 41 ++++++++++++++++++++++++ src/test/regress/sql/tablespace.sql | 6 ++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index b2d5327cc20..352a78fe765 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -2577,14 +2577,13 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, operators[i] = DatumGetObjectId(elems[i]); /* pg_get_indexdef_worker does the rest */ - /* suppress tablespace because pg_dump wants it that way */ appendStringInfoString(&buf, pg_get_indexdef_worker(indexOid, 0, operators, false, false, - false, + fullCommand, false, prettyFlags, false)); diff --git a/src/test/regress/expected/tablespace.out b/src/test/regress/expected/tablespace.out index f0dd25cdf0c..b7e01853efe 100644 --- a/src/test/regress/expected/tablespace.out +++ b/src/test/regress/expected/tablespace.out @@ -420,6 +420,7 @@ CREATE INDEX test_index1 on testschema.test_default_tab (id); CREATE INDEX test_index2 on testschema.test_default_tab (id) TABLESPACE regress_tblspace; ALTER TABLE testschema.test_default_tab ADD CONSTRAINT test_index3 PRIMARY KEY (id); ALTER TABLE testschema.test_default_tab ADD CONSTRAINT test_index4 UNIQUE (id) USING INDEX TABLESPACE regress_tblspace; +ALTER TABLE testschema.test_default_tab ADD CONSTRAINT test_index5 EXCLUDE (id WITH =) USING INDEX TABLESPACE regress_tblspace; \d testschema.test_index1 Index "testschema.test_index1" Column | Type | Key? | Definition @@ -450,6 +451,14 @@ primary key, btree, for table "testschema.test_default_tab" unique, btree, for table "testschema.test_default_tab" Tablespace: "regress_tblspace" +\d testschema.test_index5 + Index "testschema.test_index5" + Column | Type | Key? | Definition +--------+--------+------+------------ + id | bigint | yes | id +btree, for table "testschema.test_default_tab" +Tablespace: "regress_tblspace" + -- use a custom tablespace for default_tablespace SET default_tablespace TO regress_tblspace; -- tablespace should not change if no rewrite @@ -484,6 +493,14 @@ primary key, btree, for table "testschema.test_default_tab" unique, btree, for table "testschema.test_default_tab" Tablespace: "regress_tblspace" +\d testschema.test_index5 + Index "testschema.test_index5" + Column | Type | Key? | Definition +--------+--------+------+------------ + id | bigint | yes | id +btree, for table "testschema.test_default_tab" +Tablespace: "regress_tblspace" + SELECT * FROM testschema.test_default_tab; id ---- @@ -522,6 +539,14 @@ primary key, btree, for table "testschema.test_default_tab" unique, btree, for table "testschema.test_default_tab" Tablespace: "regress_tblspace" +\d testschema.test_index5 + Index "testschema.test_index5" + Column | Type | Key? | Definition +--------+---------+------+------------ + id | integer | yes | id +btree, for table "testschema.test_default_tab" +Tablespace: "regress_tblspace" + SELECT * FROM testschema.test_default_tab; id ---- @@ -562,6 +587,14 @@ primary key, btree, for table "testschema.test_default_tab" unique, btree, for table "testschema.test_default_tab" Tablespace: "regress_tblspace" +\d testschema.test_index5 + Index "testschema.test_index5" + Column | Type | Key? | Definition +--------+---------+------+------------ + id | integer | yes | id +btree, for table "testschema.test_default_tab" +Tablespace: "regress_tblspace" + -- tablespace should not change even if there is an index rewrite ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint; \d testschema.test_index1 @@ -594,6 +627,14 @@ primary key, btree, for table "testschema.test_default_tab" unique, btree, for table "testschema.test_default_tab" Tablespace: "regress_tblspace" +\d testschema.test_index5 + Index "testschema.test_index5" + Column | Type | Key? | Definition +--------+--------+------+------------ + id | bigint | yes | id +btree, for table "testschema.test_default_tab" +Tablespace: "regress_tblspace" + DROP TABLE testschema.test_default_tab; -- check that default_tablespace doesn't affect ALTER TABLE index rebuilds -- (this time with a partitioned table) diff --git a/src/test/regress/sql/tablespace.sql b/src/test/regress/sql/tablespace.sql index c43a59e5957..595c917ffae 100644 --- a/src/test/regress/sql/tablespace.sql +++ b/src/test/regress/sql/tablespace.sql @@ -250,11 +250,13 @@ CREATE INDEX test_index1 on testschema.test_default_tab (id); CREATE INDEX test_index2 on testschema.test_default_tab (id) TABLESPACE regress_tblspace; ALTER TABLE testschema.test_default_tab ADD CONSTRAINT test_index3 PRIMARY KEY (id); ALTER TABLE testschema.test_default_tab ADD CONSTRAINT test_index4 UNIQUE (id) USING INDEX TABLESPACE regress_tblspace; +ALTER TABLE testschema.test_default_tab ADD CONSTRAINT test_index5 EXCLUDE (id WITH =) USING INDEX TABLESPACE regress_tblspace; \d testschema.test_index1 \d testschema.test_index2 \d testschema.test_index3 \d testschema.test_index4 +\d testschema.test_index5 -- use a custom tablespace for default_tablespace SET default_tablespace TO regress_tblspace; -- tablespace should not change if no rewrite @@ -263,6 +265,7 @@ ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint; \d testschema.test_index2 \d testschema.test_index3 \d testschema.test_index4 +\d testschema.test_index5 SELECT * FROM testschema.test_default_tab; -- tablespace should not change even if there is an index rewrite ALTER TABLE testschema.test_default_tab ALTER id TYPE int; @@ -270,6 +273,7 @@ ALTER TABLE testschema.test_default_tab ALTER id TYPE int; \d testschema.test_index2 \d testschema.test_index3 \d testschema.test_index4 +\d testschema.test_index5 SELECT * FROM testschema.test_default_tab; -- now use the default tablespace for default_tablespace SET default_tablespace TO ''; @@ -279,12 +283,14 @@ ALTER TABLE testschema.test_default_tab ALTER id TYPE int; \d testschema.test_index2 \d testschema.test_index3 \d testschema.test_index4 +\d testschema.test_index5 -- tablespace should not change even if there is an index rewrite ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint; \d testschema.test_index1 \d testschema.test_index2 \d testschema.test_index3 \d testschema.test_index4 +\d testschema.test_index5 DROP TABLE testschema.test_default_tab; -- check that default_tablespace doesn't affect ALTER TABLE index rebuilds -- 2.50.1 (Apple Git-155)