A tidbit I spotted while playing in tablecmds.c

Started by Bruce Momjianover 7 years ago2 messageshackers
Jump to latest
#1Bruce Momjian
bruce@momjian.us

I think this is a bug:

postgres=# alter table only x2 add if not exists i integer;
ERROR: 42701: column "i" of relation "x2" already exists

Note that it does not occur without the ONLY:

postgres=# alter table x2 add if not exists i integer;
NOTICE: 42701: column "i" of relation "x2" already exists, skipping
ALTER TABLE

And I think this would fix it:

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index eef3b3a26c..ad8c176793 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -4054,7 +4054,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
Relation rel,
  case AT_AddColumnToView: /* add column via CREATE OR REPLACE VIEW */
  address = ATExecAddColumn(wqueue, tab, rel, (ColumnDef *) cmd->def,
    false, false,
-   false, lockmode);
+   cmd->missing_ok, lockmode);
  break;
  case AT_AddColumnRecurse:
  address = ATExecAddColumn(wqueue, tab, rel, (ColumnDef *) cmd->def,

--
greg

#2Andres Freund
andres@anarazel.de
In reply to: Bruce Momjian (#1)
Re: A tidbit I spotted while playing in tablecmds.c

Hi,

On 2018-12-19 18:22:28 -0500, Greg Stark wrote:

I think this is a bug:

postgres=# alter table only x2 add if not exists i integer;
ERROR: 42701: column "i" of relation "x2" already exists

Note that it does not occur without the ONLY:

postgres=# alter table x2 add if not exists i integer;
NOTICE: 42701: column "i" of relation "x2" already exists, skipping
ALTER TABLE

And I think this would fix it:

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index eef3b3a26c..ad8c176793 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -4054,7 +4054,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
Relation rel,
case AT_AddColumnToView: /* add column via CREATE OR REPLACE VIEW */
address = ATExecAddColumn(wqueue, tab, rel, (ColumnDef *) cmd->def,
false, false,
-   false, lockmode);
+   cmd->missing_ok, lockmode);
break;
case AT_AddColumnRecurse:
address = ATExecAddColumn(wqueue, tab, rel, (ColumnDef *) cmd->def,

Cursorily this seems right, but obviously need tests.

Greetings,

Andres Freund