Skip to content

Commit 9695ecf

Browse files
committed
Fix ALTER COLUMN TYPE failure with a partial exclusion constraint.
ATExecAlterColumnType failed to consider the possibility that an index that needs to be rebuilt might be a child of a constraint that needs to be rebuilt. We missed this so far because usually a constraint index doesn't have a direct dependency on its table, just on the constraint object. But if there's a WHERE clause, then dependency analysis of the WHERE clause results in direct dependencies on the column(s) mentioned in WHERE. This led to trying to drop and rebuild both the constraint and its underlying index. In v11/HEAD, we successfully drop both the index and the constraint, and then try to rebuild both, and of course the second rebuild hits a duplicate-index-name problem. Before v11, it fails with obscure messages about a missing relation OID, due to trying to drop the index twice. This is essentially the same kind of problem noted in commit 20bef2c: the possible dependency linkages are broader than what ATExecAlterColumnType was designed for. It was probably OK when written, but it's certainly been broken since the introduction of partial exclusion constraints. Fix by adding an explicit check for whether any of the indexes-to-be-rebuilt belong to any of the constraints-to-be-rebuilt, and ignoring any that do. In passing, fix a latent bug introduced by commit 8b08f7d: in get_constraint_index() we must "continue" not "break" when rejecting a relation of a wrong relkind. This is harmless today because we don't expect that code path to be taken anyway; but if there ever were any relations to be ignored, the existing coding would have an extremely undesirable dependency on the order of pg_depend entries. Also adjust a couple of obsolete comments. Per bug #15835 from Yaroslav Schekin. Back-patch to all supported branches. Discussion: https://postgr.es/m/15835-32d9b7a76c06a7a9@postgresql.org
1 parent 69f3220 commit 9695ecf

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

src/backend/catalog/pg_depend.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ getOwnedSequences(Oid relid)
611611

612612
/*
613613
* get_constraint_index
614-
* Given the OID of a unique or primary-key constraint, return the
615-
* OID of the underlying unique index.
614+
* Given the OID of a unique, primary-key, or exclusion constraint,
615+
* return the OID of the underlying index.
616616
*
617617
* Return InvalidOid if the index couldn't be found; this suggests the
618618
* given OID is bogus, but we leave it to caller to decide what to do.
@@ -672,8 +672,9 @@ get_constraint_index(Oid constraintId)
672672

673673
/*
674674
* get_index_constraint
675-
* Given the OID of an index, return the OID of the owning unique or
676-
* primary-key constraint, or InvalidOid if no such constraint.
675+
* Given the OID of an index, return the OID of the owning unique,
676+
* primary-key, or exclusion constraint, or InvalidOid if there
677+
* is no owning constraint.
677678
*/
678679
Oid
679680
get_index_constraint(Oid indexId)

src/backend/commands/tablecmds.c

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7787,6 +7787,9 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
77877787
ScanKeyData key[3];
77887788
SysScanDesc scan;
77897789
HeapTuple depTup;
7790+
ListCell *lc;
7791+
ListCell *prev;
7792+
ListCell *next;
77907793

77917794
attrelation = heap_open(AttributeRelationId, RowExclusiveLock);
77927795

@@ -7900,14 +7903,20 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
79007903

79017904
if (relKind == RELKIND_INDEX)
79027905
{
7906+
/*
7907+
* Indexes that are directly dependent on the table
7908+
* might be regular indexes or constraint indexes.
7909+
* Constraint indexes typically have only indirect
7910+
* dependencies; but there are exceptions, notably
7911+
* partial exclusion constraints. Hence we must check
7912+
* whether the index depends on any constraint that's
7913+
* due to be rebuilt, which we'll do below after we've
7914+
* found all such constraints.
7915+
*/
79037916
Assert(foundObject.objectSubId == 0);
7904-
if (!list_member_oid(tab->changedIndexOids, foundObject.objectId))
7905-
{
7906-
tab->changedIndexOids = lappend_oid(tab->changedIndexOids,
7907-
foundObject.objectId);
7908-
tab->changedIndexDefs = lappend(tab->changedIndexDefs,
7909-
pg_get_indexdef_string(foundObject.objectId));
7910-
}
7917+
tab->changedIndexOids =
7918+
list_append_unique_oid(tab->changedIndexOids,
7919+
foundObject.objectId);
79117920
}
79127921
else if (relKind == RELKIND_SEQUENCE)
79137922
{
@@ -8043,6 +8052,41 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
80438052

80448053
systable_endscan(scan);
80458054

8055+
/*
8056+
* Check the collected index OIDs to see which ones belong to the
8057+
* constraint(s) of the table, and drop those from the list of indexes
8058+
* that we need to process; rebuilding the constraints will handle them.
8059+
*/
8060+
prev = NULL;
8061+
for (lc = list_head(tab->changedIndexOids); lc; lc = next)
8062+
{
8063+
Oid indexoid = lfirst_oid(lc);
8064+
Oid conoid;
8065+
8066+
next = lnext(lc);
8067+
8068+
conoid = get_index_constraint(indexoid);
8069+
if (OidIsValid(conoid) &&
8070+
list_member_oid(tab->changedConstraintOids, conoid))
8071+
tab->changedIndexOids = list_delete_cell(tab->changedIndexOids,
8072+
lc, prev);
8073+
else
8074+
prev = lc;
8075+
}
8076+
8077+
/*
8078+
* Now collect the definitions of the indexes that must be rebuilt. (We
8079+
* could merge this into the previous loop, but it'd be more complicated
8080+
* for little gain.)
8081+
*/
8082+
foreach(lc, tab->changedIndexOids)
8083+
{
8084+
Oid indexoid = lfirst_oid(lc);
8085+
8086+
tab->changedIndexDefs = lappend(tab->changedIndexDefs,
8087+
pg_get_indexdef_string(indexoid));
8088+
}
8089+
80468090
/*
80478091
* Now scan for dependencies of this column on other things. The only
80488092
* thing we should find is the dependency on the column datatype, which we

src/test/regress/expected/alter_table.out

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,46 @@ select * from anothertab;
18981898
f | IT WAS NULL!
18991899
(3 rows)
19001900

1901+
drop table anothertab;
1902+
-- Test alter table column type with constraint indexes (cf. bug #15835)
1903+
create table anothertab(f1 int primary key, f2 int unique, f3 int, f4 int);
1904+
alter table anothertab
1905+
add exclude using btree (f3 with =);
1906+
alter table anothertab
1907+
add exclude using btree (f4 with =) where (f4 is not null);
1908+
\d anothertab
1909+
Table "public.anothertab"
1910+
Column | Type | Modifiers
1911+
--------+---------+-----------
1912+
f1 | integer | not null
1913+
f2 | integer |
1914+
f3 | integer |
1915+
f4 | integer |
1916+
Indexes:
1917+
"anothertab_pkey" PRIMARY KEY, btree (f1)
1918+
"anothertab_f2_key" UNIQUE CONSTRAINT, btree (f2)
1919+
"anothertab_f3_excl" EXCLUDE USING btree (f3 WITH =)
1920+
"anothertab_f4_excl" EXCLUDE USING btree (f4 WITH =) WHERE (f4 IS NOT NULL)
1921+
1922+
alter table anothertab alter column f1 type bigint;
1923+
alter table anothertab
1924+
alter column f2 type bigint,
1925+
alter column f3 type bigint,
1926+
alter column f4 type bigint;
1927+
\d anothertab
1928+
Table "public.anothertab"
1929+
Column | Type | Modifiers
1930+
--------+--------+-----------
1931+
f1 | bigint | not null
1932+
f2 | bigint |
1933+
f3 | bigint |
1934+
f4 | bigint |
1935+
Indexes:
1936+
"anothertab_pkey" PRIMARY KEY, btree (f1)
1937+
"anothertab_f2_key" UNIQUE CONSTRAINT, btree (f2)
1938+
"anothertab_f3_excl" EXCLUDE USING btree (f3 WITH =)
1939+
"anothertab_f4_excl" EXCLUDE USING btree (f4 WITH =) WHERE (f4 IS NOT NULL)
1940+
19011941
drop table anothertab;
19021942
create table another (f1 int, f2 text);
19031943
insert into another values(1, 'one');

src/test/regress/sql/alter_table.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,23 @@ select * from anothertab;
12921292

12931293
drop table anothertab;
12941294

1295+
-- Test alter table column type with constraint indexes (cf. bug #15835)
1296+
create table anothertab(f1 int primary key, f2 int unique, f3 int, f4 int);
1297+
alter table anothertab
1298+
add exclude using btree (f3 with =);
1299+
alter table anothertab
1300+
add exclude using btree (f4 with =) where (f4 is not null);
1301+
1302+
\d anothertab
1303+
alter table anothertab alter column f1 type bigint;
1304+
alter table anothertab
1305+
alter column f2 type bigint,
1306+
alter column f3 type bigint,
1307+
alter column f4 type bigint;
1308+
\d anothertab
1309+
1310+
drop table anothertab;
1311+
12951312
create table another (f1 int, f2 text);
12961313

12971314
insert into another values(1, 'one');

0 commit comments

Comments
 (0)