Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 47de6ac

Browse files
committed
Fix ALTER TABLE's scheduling rules for AT_AddConstraint subcommands.
Commit 1281a5c rearranged the logic in this area rather drastically, and it broke the case of adding a foreign key constraint in the same ALTER that adds the pkey or unique constraint it depends on. While self-referential fkeys are surely a pretty niche case, this used to work so we shouldn't break it. To fix, reorganize the scheduling rules in ATParseTransformCmd so that a transformed AT_AddConstraint subcommand will be delayed into a later pass in all cases, not only when it's been spit out as a side-effect of parsing some other command type. Also tweak the logic so that we won't run ATParseTransformCmd twice while doing this. It seems to work even without that, but it's surely wasting cycles to do so. Per bug #16589 from Jeremy Evans. Back-patch to v13 where the new code was introduced. Discussion: https://postgr.es/m/16589-31c8d981ca503896@postgresql.org
1 parent 610394c commit 47de6ac

File tree

3 files changed

+135
-66
lines changed

3 files changed

+135
-66
lines changed

src/backend/commands/tablecmds.c

+85-66
Original file line numberDiff line numberDiff line change
@@ -4513,19 +4513,25 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
45134513
lockmode);
45144514
break;
45154515
case AT_AddConstraint: /* ADD CONSTRAINT */
4516-
cmd = ATParseTransformCmd(wqueue, tab, rel, cmd, false, lockmode,
4517-
cur_pass, context);
4518-
/* Might not have gotten AddConstraint back from parse transform */
4516+
/* Transform the command only during initial examination */
4517+
if (cur_pass == AT_PASS_ADD_CONSTR)
4518+
cmd = ATParseTransformCmd(wqueue, tab, rel, cmd,
4519+
false, lockmode,
4520+
cur_pass, context);
4521+
/* Depending on constraint type, might be no more work to do now */
45194522
if (cmd != NULL)
45204523
address =
45214524
ATExecAddConstraint(wqueue, tab, rel,
45224525
(Constraint *) cmd->def,
45234526
false, false, lockmode);
45244527
break;
45254528
case AT_AddConstraintRecurse: /* ADD CONSTRAINT with recursion */
4526-
cmd = ATParseTransformCmd(wqueue, tab, rel, cmd, true, lockmode,
4527-
cur_pass, context);
4528-
/* Might not have gotten AddConstraint back from parse transform */
4529+
/* Transform the command only during initial examination */
4530+
if (cur_pass == AT_PASS_ADD_CONSTR)
4531+
cmd = ATParseTransformCmd(wqueue, tab, rel, cmd,
4532+
true, lockmode,
4533+
cur_pass, context);
4534+
/* Depending on constraint type, might be no more work to do now */
45294535
if (cmd != NULL)
45304536
address =
45314537
ATExecAddConstraint(wqueue, tab, rel,
@@ -4787,75 +4793,88 @@ ATParseTransformCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
47874793
foreach(lc, atstmt->cmds)
47884794
{
47894795
AlterTableCmd *cmd2 = lfirst_node(AlterTableCmd, lc);
4796+
int pass;
4797+
4798+
/*
4799+
* This switch need only cover the subcommand types that can be added
4800+
* by parse_utilcmd.c; otherwise, we'll use the default strategy of
4801+
* executing the subcommand immediately, as a substitute for the
4802+
* original subcommand. (Note, however, that this does cause
4803+
* AT_AddConstraint subcommands to be rescheduled into later passes,
4804+
* which is important for index and foreign key constraints.)
4805+
*
4806+
* We assume we needn't do any phase-1 checks for added subcommands.
4807+
*/
4808+
switch (cmd2->subtype)
4809+
{
4810+
case AT_SetNotNull:
4811+
/* Need command-specific recursion decision */
4812+
ATPrepSetNotNull(wqueue, rel, cmd2,
4813+
recurse, false,
4814+
lockmode, context);
4815+
pass = AT_PASS_COL_ATTRS;
4816+
break;
4817+
case AT_AddIndex:
4818+
/* This command never recurses */
4819+
/* No command-specific prep needed */
4820+
pass = AT_PASS_ADD_INDEX;
4821+
break;
4822+
case AT_AddIndexConstraint:
4823+
/* This command never recurses */
4824+
/* No command-specific prep needed */
4825+
pass = AT_PASS_ADD_INDEXCONSTR;
4826+
break;
4827+
case AT_AddConstraint:
4828+
/* Recursion occurs during execution phase */
4829+
if (recurse)
4830+
cmd2->subtype = AT_AddConstraintRecurse;
4831+
switch (castNode(Constraint, cmd2->def)->contype)
4832+
{
4833+
case CONSTR_PRIMARY:
4834+
case CONSTR_UNIQUE:
4835+
case CONSTR_EXCLUSION:
4836+
pass = AT_PASS_ADD_INDEXCONSTR;
4837+
break;
4838+
default:
4839+
pass = AT_PASS_ADD_OTHERCONSTR;
4840+
break;
4841+
}
4842+
break;
4843+
case AT_AlterColumnGenericOptions:
4844+
/* This command never recurses */
4845+
/* No command-specific prep needed */
4846+
pass = AT_PASS_MISC;
4847+
break;
4848+
default:
4849+
pass = cur_pass;
4850+
break;
4851+
}
47904852

4791-
if (newcmd == NULL &&
4792-
(cmd->subtype == cmd2->subtype ||
4793-
(cmd->subtype == AT_AddConstraintRecurse &&
4794-
cmd2->subtype == AT_AddConstraint)))
4853+
if (pass < cur_pass)
4854+
{
4855+
/* Cannot schedule into a pass we already finished */
4856+
elog(ERROR, "ALTER TABLE scheduling failure: too late for pass %d",
4857+
pass);
4858+
}
4859+
else if (pass > cur_pass)
47954860
{
4796-
/* Found the transformed version of our subcommand */
4797-
cmd2->subtype = cmd->subtype; /* copy recursion flag */
4798-
newcmd = cmd2;
4861+
/* OK, queue it up for later */
4862+
tab->subcmds[pass] = lappend(tab->subcmds[pass], cmd2);
47994863
}
48004864
else
48014865
{
4802-
int pass;
4803-
48044866
/*
4805-
* Schedule added subcommand appropriately. We assume we needn't
4806-
* do any phase-1 checks for it. This switch only has to cover
4807-
* the subcommand types that can be added by parse_utilcmd.c.
4867+
* We should see at most one subcommand for the current pass,
4868+
* which is the transformed version of the original subcommand.
48084869
*/
4809-
switch (cmd2->subtype)
4870+
if (newcmd == NULL && cmd->subtype == cmd2->subtype)
48104871
{
4811-
case AT_SetNotNull:
4812-
/* Need command-specific recursion decision */
4813-
ATPrepSetNotNull(wqueue, rel, cmd2,
4814-
recurse, false,
4815-
lockmode, context);
4816-
pass = AT_PASS_COL_ATTRS;
4817-
break;
4818-
case AT_AddIndex:
4819-
/* This command never recurses */
4820-
/* No command-specific prep needed */
4821-
pass = AT_PASS_ADD_INDEX;
4822-
break;
4823-
case AT_AddIndexConstraint:
4824-
/* This command never recurses */
4825-
/* No command-specific prep needed */
4826-
pass = AT_PASS_ADD_INDEXCONSTR;
4827-
break;
4828-
case AT_AddConstraint:
4829-
/* Recursion occurs during execution phase */
4830-
if (recurse)
4831-
cmd2->subtype = AT_AddConstraintRecurse;
4832-
switch (castNode(Constraint, cmd2->def)->contype)
4833-
{
4834-
case CONSTR_PRIMARY:
4835-
case CONSTR_UNIQUE:
4836-
case CONSTR_EXCLUSION:
4837-
pass = AT_PASS_ADD_INDEXCONSTR;
4838-
break;
4839-
default:
4840-
pass = AT_PASS_ADD_OTHERCONSTR;
4841-
break;
4842-
}
4843-
break;
4844-
case AT_AlterColumnGenericOptions:
4845-
/* This command never recurses */
4846-
/* No command-specific prep needed */
4847-
pass = AT_PASS_MISC;
4848-
break;
4849-
default:
4850-
elog(ERROR, "unexpected AlterTableType: %d",
4851-
(int) cmd2->subtype);
4852-
pass = AT_PASS_UNSET;
4853-
break;
4872+
/* Found the transformed version of our subcommand */
4873+
newcmd = cmd2;
48544874
}
4855-
/* Must be for a later pass than we're currently doing */
4856-
if (pass <= cur_pass)
4857-
elog(ERROR, "ALTER TABLE scheduling failure");
4858-
tab->subcmds[pass] = lappend(tab->subcmds[pass], cmd2);
4875+
else
4876+
elog(ERROR, "ALTER TABLE scheduling failure: bogus item for pass %d",
4877+
pass);
48594878
}
48604879
}
48614880

src/test/regress/expected/alter_table.out

+36
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,42 @@ ALTER TABLE ataddindex
36783678
Indexes:
36793679
"ataddindex_expr_excl" EXCLUDE USING btree ((f1 ~~ 'a'::text) WITH =)
36803680

3681+
DROP TABLE ataddindex;
3682+
CREATE TABLE ataddindex(id int, ref_id int);
3683+
ALTER TABLE ataddindex
3684+
ADD PRIMARY KEY (id),
3685+
ADD FOREIGN KEY (ref_id) REFERENCES ataddindex;
3686+
\d ataddindex
3687+
Table "public.ataddindex"
3688+
Column | Type | Collation | Nullable | Default
3689+
--------+---------+-----------+----------+---------
3690+
id | integer | | not null |
3691+
ref_id | integer | | |
3692+
Indexes:
3693+
"ataddindex_pkey" PRIMARY KEY, btree (id)
3694+
Foreign-key constraints:
3695+
"ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
3696+
Referenced by:
3697+
TABLE "ataddindex" CONSTRAINT "ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
3698+
3699+
DROP TABLE ataddindex;
3700+
CREATE TABLE ataddindex(id int, ref_id int);
3701+
ALTER TABLE ataddindex
3702+
ADD UNIQUE (id),
3703+
ADD FOREIGN KEY (ref_id) REFERENCES ataddindex (id);
3704+
\d ataddindex
3705+
Table "public.ataddindex"
3706+
Column | Type | Collation | Nullable | Default
3707+
--------+---------+-----------+----------+---------
3708+
id | integer | | |
3709+
ref_id | integer | | |
3710+
Indexes:
3711+
"ataddindex_id_key" UNIQUE CONSTRAINT, btree (id)
3712+
Foreign-key constraints:
3713+
"ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
3714+
Referenced by:
3715+
TABLE "ataddindex" CONSTRAINT "ataddindex_ref_id_fkey" FOREIGN KEY (ref_id) REFERENCES ataddindex(id)
3716+
36813717
DROP TABLE ataddindex;
36823718
-- unsupported constraint types for partitioned tables
36833719
CREATE TABLE partitioned (

src/test/regress/sql/alter_table.sql

+14
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,20 @@ ALTER TABLE ataddindex
22522252
\d ataddindex
22532253
DROP TABLE ataddindex;
22542254

2255+
CREATE TABLE ataddindex(id int, ref_id int);
2256+
ALTER TABLE ataddindex
2257+
ADD PRIMARY KEY (id),
2258+
ADD FOREIGN KEY (ref_id) REFERENCES ataddindex;
2259+
\d ataddindex
2260+
DROP TABLE ataddindex;
2261+
2262+
CREATE TABLE ataddindex(id int, ref_id int);
2263+
ALTER TABLE ataddindex
2264+
ADD UNIQUE (id),
2265+
ADD FOREIGN KEY (ref_id) REFERENCES ataddindex (id);
2266+
\d ataddindex
2267+
DROP TABLE ataddindex;
2268+
22552269
-- unsupported constraint types for partitioned tables
22562270
CREATE TABLE partitioned (
22572271
a int,

0 commit comments

Comments
 (0)