Skip to content

Commit fbef434

Browse files
committed
Make CREATE OR REPLACE VIEW internally more consistent
The way that columns are added to a view is by calling AlterTableInternal with special subtype AT_AddColumnToView; but that subtype is changed to AT_AddColumnRecurse by ATPrepAddColumn. This has no visible effect in the current code, since views cannot have inheritance children (thus the recursion step is a no-op) and adding a column to a view is executed identically to doing it to a table; but it does make a difference for future event trigger code keeping track of commands, because the current situation leads to confusing the case with a normal ALTER TABLE ADD COLUMN. Fix the problem by passing a flag to ATPrepAddColumn to prevent it from changing the command subtype. The event trigger code can then properly ignore the subcommand. (We could remove the call to ATPrepAddColumn, since views are never typed, and there is never a need for recursion, which are the two conditions that are checked by ATPrepAddColumn; but it seems more future-proof to keep the call in place.)
1 parent f65e827 commit fbef434

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/backend/commands/tablecmds.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static void ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cm
325325
static List *find_typed_table_dependencies(Oid typeOid, const char *typeName,
326326
DropBehavior behavior);
327327
static void ATPrepAddColumn(List **wqueue, Relation rel, bool recurse, bool recursing,
328-
AlterTableCmd *cmd, LOCKMODE lockmode);
328+
bool is_view, AlterTableCmd *cmd, LOCKMODE lockmode);
329329
static void ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
330330
ColumnDef *colDef, bool isOid,
331331
bool recurse, bool recursing, LOCKMODE lockmode);
@@ -3085,14 +3085,16 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
30853085
case AT_AddColumn: /* ADD COLUMN */
30863086
ATSimplePermissions(rel,
30873087
ATT_TABLE | ATT_COMPOSITE_TYPE | ATT_FOREIGN_TABLE);
3088-
ATPrepAddColumn(wqueue, rel, recurse, recursing, cmd, lockmode);
3088+
ATPrepAddColumn(wqueue, rel, recurse, recursing, false, cmd,
3089+
lockmode);
30893090
/* Recursion occurs during execution phase */
30903091
pass = AT_PASS_ADD_COL;
30913092
break;
30923093
case AT_AddColumnToView: /* add column via CREATE OR REPLACE
30933094
* VIEW */
30943095
ATSimplePermissions(rel, ATT_VIEW);
3095-
ATPrepAddColumn(wqueue, rel, recurse, recursing, cmd, lockmode);
3096+
ATPrepAddColumn(wqueue, rel, recurse, recursing, true, cmd,
3097+
lockmode);
30963098
/* Recursion occurs during execution phase */
30973099
pass = AT_PASS_ADD_COL;
30983100
break;
@@ -4576,7 +4578,7 @@ check_of_type(HeapTuple typetuple)
45764578
*/
45774579
static void
45784580
ATPrepAddColumn(List **wqueue, Relation rel, bool recurse, bool recursing,
4579-
AlterTableCmd *cmd, LOCKMODE lockmode)
4581+
bool is_view, AlterTableCmd *cmd, LOCKMODE lockmode)
45804582
{
45814583
if (rel->rd_rel->reloftype && !recursing)
45824584
ereport(ERROR,
@@ -4586,7 +4588,7 @@ ATPrepAddColumn(List **wqueue, Relation rel, bool recurse, bool recursing,
45864588
if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
45874589
ATTypedTableRecursion(wqueue, rel, cmd, lockmode);
45884590

4589-
if (recurse)
4591+
if (recurse && !is_view)
45904592
cmd->subtype = AT_AddColumnRecurse;
45914593
}
45924594

@@ -5026,7 +5028,7 @@ ATPrepAddOids(List **wqueue, Relation rel, bool recurse, AlterTableCmd *cmd, LOC
50265028
cdef->location = -1;
50275029
cmd->def = (Node *) cdef;
50285030
}
5029-
ATPrepAddColumn(wqueue, rel, recurse, false, cmd, lockmode);
5031+
ATPrepAddColumn(wqueue, rel, recurse, false, false, cmd, lockmode);
50305032

50315033
if (recurse)
50325034
cmd->subtype = AT_AddOidsRecurse;

0 commit comments

Comments
 (0)