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

Commit 840ff5f

Browse files
committed
Get rid of recursion-marker values in enum AlterTableType
During ALTER TABLE execution, when prep-time handling of subcommands of certain types determine that execution-time handling requires recursion, they signal this by changing the subcommand type to a special value. This can be done in a simpler way by using a separate flag introduced by commit ec0925c, so do that. Catversion bumped. It's not clear to me that ALTER TABLE subcommands are stored anywhere in catalogs (CREATE FUNCTION rejects it in BEGIN ATOMIC function bodies), but we do have both write and read support for them, so be safe. Discussion: https://postgr.es/m/20220929090033.zxuaezcdwh2fgfjb@alvherre.pgsql
1 parent 9d0cf57 commit 840ff5f

File tree

5 files changed

+18
-78
lines changed

5 files changed

+18
-78
lines changed

src/backend/commands/tablecmds.c

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,8 +4070,7 @@ AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode)
40704070
* For most subcommand types, phases 2 and 3 do no explicit recursion,
40714071
* since phase 1 already does it. However, for certain subcommand types
40724072
* it is only possible to determine how to recurse at phase 2 time; for
4073-
* those cases, phase 1 sets the cmd->recurse flag (or, in some older coding,
4074-
* changes the command subtype of a "Recurse" variant XXX to be cleaned up.)
4073+
* those cases, phase 1 sets the cmd->recurse flag.
40754074
*
40764075
* Thanks to the magic of MVCC, an error anywhere along the way rolls back
40774076
* the whole operation; we don't have to do anything special to clean up.
@@ -4276,7 +4275,6 @@ AlterTableGetLockLevel(List *cmds)
42764275
break;
42774276

42784277
case AT_AddConstraint:
4279-
case AT_AddConstraintRecurse: /* becomes AT_AddConstraint */
42804278
case AT_ReAddConstraint: /* becomes AT_AddConstraint */
42814279
case AT_ReAddDomainConstraint: /* becomes AT_AddConstraint */
42824280
if (IsA(cmd->def, Constraint))
@@ -4628,7 +4626,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
46284626
/* Recursion occurs during execution phase */
46294627
/* No command-specific prep needed except saving recurse flag */
46304628
if (recurse)
4631-
cmd->subtype = AT_AddConstraintRecurse;
4629+
cmd->recurse = true;
46324630
pass = AT_PASS_ADD_CONSTR;
46334631
break;
46344632
case AT_AddIndexConstraint: /* ADD CONSTRAINT USING INDEX */
@@ -4643,7 +4641,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
46434641
/* Other recursion occurs during execution phase */
46444642
/* No command-specific prep needed except saving recurse flag */
46454643
if (recurse)
4646-
cmd->subtype = AT_DropConstraintRecurse;
4644+
cmd->recurse = true;
46474645
pass = AT_PASS_DROP;
46484646
break;
46494647
case AT_AlterColumnType: /* ALTER COLUMN TYPE */
@@ -4765,7 +4763,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
47654763
/* Recursion occurs during execution phase */
47664764
/* No command-specific prep needed except saving recurse flag */
47674765
if (recurse)
4768-
cmd->subtype = AT_ValidateConstraintRecurse;
4766+
cmd->recurse = true;
47694767
pass = AT_PASS_MISC;
47704768
break;
47714769
case AT_ReplicaIdentity: /* REPLICA IDENTITY ... */
@@ -4930,12 +4928,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
49304928
case AT_AddColumn: /* ADD COLUMN */
49314929
case AT_AddColumnToView: /* add column via CREATE OR REPLACE VIEW */
49324930
address = ATExecAddColumn(wqueue, tab, rel, &cmd,
4933-
false, false,
4934-
lockmode, cur_pass, context);
4935-
break;
4936-
case AT_AddColumnRecurse:
4937-
address = ATExecAddColumn(wqueue, tab, rel, &cmd,
4938-
true, false,
4931+
cmd->recurse, false,
49394932
lockmode, cur_pass, context);
49404933
break;
49414934
case AT_ColumnDefault: /* ALTER COLUMN DEFAULT */
@@ -4989,13 +4982,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
49894982
break;
49904983
case AT_DropColumn: /* DROP COLUMN */
49914984
address = ATExecDropColumn(wqueue, rel, cmd->name,
4992-
cmd->behavior, false, false,
4993-
cmd->missing_ok, lockmode,
4994-
NULL);
4995-
break;
4996-
case AT_DropColumnRecurse: /* DROP COLUMN with recursion */
4997-
address = ATExecDropColumn(wqueue, rel, cmd->name,
4998-
cmd->behavior, true, false,
4985+
cmd->behavior, cmd->recurse, false,
49994986
cmd->missing_ok, lockmode,
50004987
NULL);
50014988
break;
@@ -5015,27 +5002,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
50155002
/* Transform the command only during initial examination */
50165003
if (cur_pass == AT_PASS_ADD_CONSTR)
50175004
cmd = ATParseTransformCmd(wqueue, tab, rel, cmd,
5018-
false, lockmode,
5005+
cmd->recurse, lockmode,
50195006
cur_pass, context);
50205007
/* Depending on constraint type, might be no more work to do now */
50215008
if (cmd != NULL)
50225009
address =
50235010
ATExecAddConstraint(wqueue, tab, rel,
50245011
(Constraint *) cmd->def,
5025-
false, false, lockmode);
5026-
break;
5027-
case AT_AddConstraintRecurse: /* ADD CONSTRAINT with recursion */
5028-
/* Transform the command only during initial examination */
5029-
if (cur_pass == AT_PASS_ADD_CONSTR)
5030-
cmd = ATParseTransformCmd(wqueue, tab, rel, cmd,
5031-
true, lockmode,
5032-
cur_pass, context);
5033-
/* Depending on constraint type, might be no more work to do now */
5034-
if (cmd != NULL)
5035-
address =
5036-
ATExecAddConstraint(wqueue, tab, rel,
5037-
(Constraint *) cmd->def,
5038-
true, false, lockmode);
5012+
cmd->recurse, false, lockmode);
50395013
break;
50405014
case AT_ReAddConstraint: /* Re-add pre-existing check constraint */
50415015
address =
@@ -5060,22 +5034,12 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
50605034
address = ATExecAlterConstraint(rel, cmd, false, false, lockmode);
50615035
break;
50625036
case AT_ValidateConstraint: /* VALIDATE CONSTRAINT */
5063-
address = ATExecValidateConstraint(wqueue, rel, cmd->name, false,
5064-
false, lockmode);
5065-
break;
5066-
case AT_ValidateConstraintRecurse: /* VALIDATE CONSTRAINT with
5067-
* recursion */
5068-
address = ATExecValidateConstraint(wqueue, rel, cmd->name, true,
5037+
address = ATExecValidateConstraint(wqueue, rel, cmd->name, cmd->recurse,
50695038
false, lockmode);
50705039
break;
50715040
case AT_DropConstraint: /* DROP CONSTRAINT */
50725041
ATExecDropConstraint(rel, cmd->name, cmd->behavior,
5073-
false, false,
5074-
cmd->missing_ok, lockmode);
5075-
break;
5076-
case AT_DropConstraintRecurse: /* DROP CONSTRAINT with recursion */
5077-
ATExecDropConstraint(rel, cmd->name, cmd->behavior,
5078-
true, false,
5042+
cmd->recurse, false,
50795043
cmd->missing_ok, lockmode);
50805044
break;
50815045
case AT_AlterColumnType: /* ALTER COLUMN TYPE */
@@ -5351,7 +5315,7 @@ ATParseTransformCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
53515315
case AT_AddConstraint:
53525316
/* Recursion occurs during execution phase */
53535317
if (recurse)
5354-
cmd2->subtype = AT_AddConstraintRecurse;
5318+
cmd2->recurse = true;
53555319
switch (castNode(Constraint, cmd2->def)->contype)
53565320
{
53575321
case CONSTR_PRIMARY:
@@ -6110,7 +6074,6 @@ alter_table_type_to_string(AlterTableType cmdtype)
61106074
switch (cmdtype)
61116075
{
61126076
case AT_AddColumn:
6113-
case AT_AddColumnRecurse:
61146077
case AT_AddColumnToView:
61156078
return "ADD COLUMN";
61166079
case AT_ColumnDefault:
@@ -6135,24 +6098,20 @@ alter_table_type_to_string(AlterTableType cmdtype)
61356098
case AT_SetCompression:
61366099
return "ALTER COLUMN ... SET COMPRESSION";
61376100
case AT_DropColumn:
6138-
case AT_DropColumnRecurse:
61396101
return "DROP COLUMN";
61406102
case AT_AddIndex:
61416103
case AT_ReAddIndex:
61426104
return NULL; /* not real grammar */
61436105
case AT_AddConstraint:
6144-
case AT_AddConstraintRecurse:
61456106
case AT_ReAddConstraint:
61466107
case AT_ReAddDomainConstraint:
61476108
case AT_AddIndexConstraint:
61486109
return "ADD CONSTRAINT";
61496110
case AT_AlterConstraint:
61506111
return "ALTER CONSTRAINT";
61516112
case AT_ValidateConstraint:
6152-
case AT_ValidateConstraintRecurse:
61536113
return "VALIDATE CONSTRAINT";
61546114
case AT_DropConstraint:
6155-
case AT_DropConstraintRecurse:
61566115
return "DROP CONSTRAINT";
61576116
case AT_ReAddComment:
61586117
return NULL; /* not real grammar */
@@ -6671,7 +6630,7 @@ ATPrepAddColumn(List **wqueue, Relation rel, bool recurse, bool recursing,
66716630
ATTypedTableRecursion(wqueue, rel, cmd, lockmode, context);
66726631

66736632
if (recurse && !is_view)
6674-
cmd->subtype = AT_AddColumnRecurse;
6633+
cmd->recurse = true;
66756634
}
66766635

66776636
/*
@@ -8376,7 +8335,7 @@ ATPrepDropColumn(List **wqueue, Relation rel, bool recurse, bool recursing,
83768335
ATTypedTableRecursion(wqueue, rel, cmd, lockmode, context);
83778336

83788337
if (recurse)
8379-
cmd->subtype = AT_DropColumnRecurse;
8338+
cmd->recurse = true;
83808339
}
83818340

83828341
/*

src/backend/parser/parse_utilcmd.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,7 +3362,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
33623362
switch (cmd->subtype)
33633363
{
33643364
case AT_AddColumn:
3365-
case AT_AddColumnRecurse:
33663365
{
33673366
ColumnDef *def = castNode(ColumnDef, cmd->def);
33683367

@@ -3386,7 +3385,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
33863385
}
33873386

33883387
case AT_AddConstraint:
3389-
case AT_AddConstraintRecurse:
33903388

33913389
/*
33923390
* The original AddConstraint cmd node doesn't go to newcmds

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202212092
60+
#define CATALOG_VERSION_NO 202212121
6161

6262
#endif

src/include/nodes/parsenodes.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,6 @@ typedef struct AlterTableStmt
19791979
typedef enum AlterTableType
19801980
{
19811981
AT_AddColumn, /* add column */
1982-
AT_AddColumnRecurse, /* internal to commands/tablecmds.c */
19831982
AT_AddColumnToView, /* implicitly via CREATE OR REPLACE VIEW */
19841983
AT_ColumnDefault, /* alter column default */
19851984
AT_CookedColumnDefault, /* add a pre-cooked column default */
@@ -1993,19 +1992,15 @@ typedef enum AlterTableType
19931992
AT_SetStorage, /* alter column set storage */
19941993
AT_SetCompression, /* alter column set compression */
19951994
AT_DropColumn, /* drop column */
1996-
AT_DropColumnRecurse, /* internal to commands/tablecmds.c */
19971995
AT_AddIndex, /* add index */
19981996
AT_ReAddIndex, /* internal to commands/tablecmds.c */
19991997
AT_AddConstraint, /* add constraint */
2000-
AT_AddConstraintRecurse, /* internal to commands/tablecmds.c */
20011998
AT_ReAddConstraint, /* internal to commands/tablecmds.c */
20021999
AT_ReAddDomainConstraint, /* internal to commands/tablecmds.c */
20032000
AT_AlterConstraint, /* alter constraint */
20042001
AT_ValidateConstraint, /* validate constraint */
2005-
AT_ValidateConstraintRecurse, /* internal to commands/tablecmds.c */
20062002
AT_AddIndexConstraint, /* add constraint using existing index */
20072003
AT_DropConstraint, /* drop constraint */
2008-
AT_DropConstraintRecurse, /* internal to commands/tablecmds.c */
20092004
AT_ReAddComment, /* internal to commands/tablecmds.c */
20102005
AT_AlterColumnType, /* alter column type */
20112006
AT_AlterColumnGenericOptions, /* alter column OPTIONS (...) */

src/test/modules/test_ddl_deparse/test_ddl_deparse.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
114114
case AT_AddColumn:
115115
strtype = "ADD COLUMN";
116116
break;
117-
case AT_AddColumnRecurse:
118-
strtype = "ADD COLUMN (and recurse)";
119-
break;
120117
case AT_AddColumnToView:
121118
strtype = "ADD COLUMN TO VIEW";
122119
break;
@@ -156,9 +153,6 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
156153
case AT_DropColumn:
157154
strtype = "DROP COLUMN";
158155
break;
159-
case AT_DropColumnRecurse:
160-
strtype = "DROP COLUMN (and recurse)";
161-
break;
162156
case AT_AddIndex:
163157
strtype = "ADD INDEX";
164158
break;
@@ -168,9 +162,6 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
168162
case AT_AddConstraint:
169163
strtype = "ADD CONSTRAINT";
170164
break;
171-
case AT_AddConstraintRecurse:
172-
strtype = "ADD CONSTRAINT (and recurse)";
173-
break;
174165
case AT_ReAddConstraint:
175166
strtype = "(re) ADD CONSTRAINT";
176167
break;
@@ -183,18 +174,12 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
183174
case AT_ValidateConstraint:
184175
strtype = "VALIDATE CONSTRAINT";
185176
break;
186-
case AT_ValidateConstraintRecurse:
187-
strtype = "VALIDATE CONSTRAINT (and recurse)";
188-
break;
189177
case AT_AddIndexConstraint:
190178
strtype = "ADD CONSTRAINT (using index)";
191179
break;
192180
case AT_DropConstraint:
193181
strtype = "DROP CONSTRAINT";
194182
break;
195-
case AT_DropConstraintRecurse:
196-
strtype = "DROP CONSTRAINT (and recurse)";
197-
break;
198183
case AT_ReAddComment:
199184
strtype = "(re) ADD COMMENT";
200185
break;
@@ -326,7 +311,10 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
326311
break;
327312
}
328313

329-
values[0] = CStringGetTextDatum(strtype);
314+
if (subcmd->recurse)
315+
values[0] = CStringGetTextDatum(psprintf("%s (and recurse)", strtype));
316+
else
317+
values[0] = CStringGetTextDatum(strtype);
330318
if (OidIsValid(sub->address.objectId))
331319
{
332320
char *objdesc;

0 commit comments

Comments
 (0)