Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2981e5a

Browse files
committed
Fix ALTER COLUMN TYPE failure with a partial exclusion constraint.
ATExecAlterColumnType failed to consider the possibility that an indexthat needs to be rebuilt might be a child of a constraint that needs to berebuilt. We missed this so far because usually a constraint index doesn'thave a direct dependency on its table, just on the constraint object.But if there's a WHERE clause, then dependency analysis of the WHEREclause results in direct dependencies on the column(s) mentioned in WHERE.This led to trying to drop and rebuild both the constraint and itsunderlying 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 aduplicate-index-name problem. Before v11, it fails with obscure messagesabout a missing relation OID, due to trying to drop the index twice.This is essentially the same kind of problem noted in commit20bef2c: the possible dependency linkages are broader than whatATExecAlterColumnType was designed for. It was probably OK whenwritten, but it's certainly been broken since the introduction ofpartial exclusion constraints. Fix by adding an explicit checkfor whether any of the indexes-to-be-rebuilt belong to any of theconstraints-to-be-rebuilt, and ignoring any that do.In passing, fix a latent bug introduced by commit8b08f7d: inget_constraint_index() we must "continue" not "break" when rejectinga relation of a wrong relkind. This is harmless today because we don'texpect that code path to be taken anyway; but if there ever were anyrelations to be ignored, the existing coding would have an extremelyundesirable 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 supportedbranches.Discussion:https://postgr.es/m/15835-32d9b7a76c06a7a9@postgresql.org
1 parent56a9325 commit2981e5a

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
@@ -616,8 +616,8 @@ getOwnedSequence(Oid relid, AttrNumber attnum)
616616

617617
/*
618618
* get_constraint_index
619-
*Given the OID of a unique orprimary-key constraint, return the
620-
*OID of the underlying unique index.
619+
*Given the OID of a unique,primary-key, or exclusion constraint,
620+
*return theOID of the underlying index.
621621
*
622622
* Return InvalidOid if the index couldn't be found; this suggests the
623623
* given OID is bogus, but we leave it to caller to decide what to do.
@@ -677,8 +677,9 @@ get_constraint_index(Oid constraintId)
677677

678678
/*
679679
* get_index_constraint
680-
*Given the OID of an index, return the OID of the owning unique or
681-
*primary-key constraint, or InvalidOid if no such constraint.
680+
*Given the OID of an index, return the OID of the owning unique,
681+
*primary-key, or exclusion constraint, or InvalidOid if there
682+
*is no owning constraint.
682683
*/
683684
Oid
684685
get_index_constraint(OidindexId)

‎src/backend/commands/tablecmds.c

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9021,6 +9021,9 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
90219021
SysScanDescscan;
90229022
HeapTupledepTup;
90239023
ObjectAddressaddress;
9024+
ListCell*lc;
9025+
ListCell*prev;
9026+
ListCell*next;
90249027

90259028
attrelation=heap_open(AttributeRelationId,RowExclusiveLock);
90269029

@@ -9134,14 +9137,20 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
91349137

91359138
if (relKind==RELKIND_INDEX)
91369139
{
9140+
/*
9141+
* Indexes that are directly dependent on the table
9142+
* might be regular indexes or constraint indexes.
9143+
* Constraint indexes typically have only indirect
9144+
* dependencies; but there are exceptions, notably
9145+
* partial exclusion constraints. Hence we must check
9146+
* whether the index depends on any constraint that's
9147+
* due to be rebuilt, which we'll do below after we've
9148+
* found all such constraints.
9149+
*/
91379150
Assert(foundObject.objectSubId==0);
9138-
if (!list_member_oid(tab->changedIndexOids,foundObject.objectId))
9139-
{
9140-
tab->changedIndexOids=lappend_oid(tab->changedIndexOids,
9141-
foundObject.objectId);
9142-
tab->changedIndexDefs=lappend(tab->changedIndexDefs,
9143-
pg_get_indexdef_string(foundObject.objectId));
9144-
}
9151+
tab->changedIndexOids=
9152+
list_append_unique_oid(tab->changedIndexOids,
9153+
foundObject.objectId);
91459154
}
91469155
elseif (relKind==RELKIND_SEQUENCE)
91479156
{
@@ -9313,6 +9322,41 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
93139322

93149323
systable_endscan(scan);
93159324

9325+
/*
9326+
* Check the collected index OIDs to see which ones belong to the
9327+
* constraint(s) of the table, and drop those from the list of indexes
9328+
* that we need to process; rebuilding the constraints will handle them.
9329+
*/
9330+
prev=NULL;
9331+
for (lc=list_head(tab->changedIndexOids);lc;lc=next)
9332+
{
9333+
Oidindexoid=lfirst_oid(lc);
9334+
Oidconoid;
9335+
9336+
next=lnext(lc);
9337+
9338+
conoid=get_index_constraint(indexoid);
9339+
if (OidIsValid(conoid)&&
9340+
list_member_oid(tab->changedConstraintOids,conoid))
9341+
tab->changedIndexOids=list_delete_cell(tab->changedIndexOids,
9342+
lc,prev);
9343+
else
9344+
prev=lc;
9345+
}
9346+
9347+
/*
9348+
* Now collect the definitions of the indexes that must be rebuilt. (We
9349+
* could merge this into the previous loop, but it'd be more complicated
9350+
* for little gain.)
9351+
*/
9352+
foreach(lc,tab->changedIndexOids)
9353+
{
9354+
Oidindexoid=lfirst_oid(lc);
9355+
9356+
tab->changedIndexDefs=lappend(tab->changedIndexDefs,
9357+
pg_get_indexdef_string(indexoid));
9358+
}
9359+
93169360
/*
93179361
* Now scan for dependencies of this column on other things. The only
93189362
* 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
@@ -1933,6 +1933,46 @@ select * from anothertab;
19331933
f | IT WAS NULL!
19341934
(3 rows)
19351935

1936+
drop table anothertab;
1937+
-- Test alter table column type with constraint indexes (cf. bug #15835)
1938+
create table anothertab(f1 int primary key, f2 int unique, f3 int, f4 int);
1939+
alter table anothertab
1940+
add exclude using btree (f3 with =);
1941+
alter table anothertab
1942+
add exclude using btree (f4 with =) where (f4 is not null);
1943+
\d anothertab
1944+
Table "public.anothertab"
1945+
Column | Type | Collation | Nullable | Default
1946+
--------+---------+-----------+----------+---------
1947+
f1 | integer | | not null |
1948+
f2 | integer | | |
1949+
f3 | integer | | |
1950+
f4 | integer | | |
1951+
Indexes:
1952+
"anothertab_pkey" PRIMARY KEY, btree (f1)
1953+
"anothertab_f2_key" UNIQUE CONSTRAINT, btree (f2)
1954+
"anothertab_f3_excl" EXCLUDE USING btree (f3 WITH =)
1955+
"anothertab_f4_excl" EXCLUDE USING btree (f4 WITH =) WHERE (f4 IS NOT NULL)
1956+
1957+
alter table anothertab alter column f1 type bigint;
1958+
alter table anothertab
1959+
alter column f2 type bigint,
1960+
alter column f3 type bigint,
1961+
alter column f4 type bigint;
1962+
\d anothertab
1963+
Table "public.anothertab"
1964+
Column | Type | Collation | Nullable | Default
1965+
--------+--------+-----------+----------+---------
1966+
f1 | bigint | | not null |
1967+
f2 | bigint | | |
1968+
f3 | bigint | | |
1969+
f4 | bigint | | |
1970+
Indexes:
1971+
"anothertab_pkey" PRIMARY KEY, btree (f1)
1972+
"anothertab_f2_key" UNIQUE CONSTRAINT, btree (f2)
1973+
"anothertab_f3_excl" EXCLUDE USING btree (f3 WITH =)
1974+
"anothertab_f4_excl" EXCLUDE USING btree (f4 WITH =) WHERE (f4 IS NOT NULL)
1975+
19361976
drop table anothertab;
19371977
create table another (f1 int, f2 text);
19381978
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
@@ -1307,6 +1307,23 @@ select * from anothertab;
13071307

13081308
droptable anothertab;
13091309

1310+
-- Test alter table column type with constraint indexes (cf. bug #15835)
1311+
createtableanothertab(f1intprimary key, f2int unique, f3int, f4int);
1312+
altertable anothertab
1313+
add exclude using btree (f3 with=);
1314+
altertable anothertab
1315+
add exclude using btree (f4 with=)where (f4is not null);
1316+
1317+
\d anothertab
1318+
altertable anothertab alter column f1 typebigint;
1319+
altertable anothertab
1320+
alter column f2 typebigint,
1321+
alter column f3 typebigint,
1322+
alter column f4 typebigint;
1323+
\d anothertab
1324+
1325+
droptable anothertab;
1326+
13101327
createtableanother (f1int, f2text);
13111328

13121329
insert into anothervalues(1,'one');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp