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

Commit a8b7408

Browse files
committed
Fix failure of ALTER FOREIGN TABLE SET SCHEMA to move sequences.
Ordinary ALTER TABLE SET SCHEMA will also move any owned sequences into the new schema. We failed to do likewise for foreign tables, because AlterTableNamespaceInternal believed that only certain relkinds could have indexes, owned sequences, or constraints. We could simply add foreign tables to that relkind list, but it seems likely that the same oversight could be made again in future. Instead let's remove the relkind filter altogether. These functions shouldn't cost much when there are no objects that they need to process, and surely this isn't an especially performance-critical case anyway. Per bug #18407 from Vidushi Gupta. Back-patch to all supported branches. Discussion: https://postgr.es/m/18407-4fd07373d252c6a0@postgresql.org
1 parent 7124e7d commit a8b7408

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/backend/commands/tablecmds.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15051,16 +15051,11 @@ AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid,
1505115051
nspOid, false, false, objsMoved);
1505215052

1505315053
/* Fix other dependent stuff */
15054-
if (rel->rd_rel->relkind == RELKIND_RELATION ||
15055-
rel->rd_rel->relkind == RELKIND_MATVIEW ||
15056-
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
15057-
{
15058-
AlterIndexNamespaces(classRel, rel, oldNspOid, nspOid, objsMoved);
15059-
AlterSeqNamespaces(classRel, rel, oldNspOid, nspOid,
15060-
objsMoved, AccessExclusiveLock);
15061-
AlterConstraintNamespaces(RelationGetRelid(rel), oldNspOid, nspOid,
15062-
false, objsMoved);
15063-
}
15054+
AlterIndexNamespaces(classRel, rel, oldNspOid, nspOid, objsMoved);
15055+
AlterSeqNamespaces(classRel, rel, oldNspOid, nspOid,
15056+
objsMoved, AccessExclusiveLock);
15057+
AlterConstraintNamespaces(RelationGetRelid(rel), oldNspOid, nspOid,
15058+
false, objsMoved);
1506415059

1506515060
table_close(classRel, RowExclusiveLock);
1506615061
}

src/test/regress/expected/foreign_data.out

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -877,26 +877,31 @@ ERROR: column "no_column" of relation "ft1" does not exist
877877
ALTER FOREIGN TABLE ft1 DROP COLUMN IF EXISTS no_column;
878878
NOTICE: column "no_column" of relation "ft1" does not exist, skipping
879879
ALTER FOREIGN TABLE ft1 DROP COLUMN c9;
880+
ALTER FOREIGN TABLE ft1 ADD COLUMN c11 serial;
880881
ALTER FOREIGN TABLE ft1 SET SCHEMA foreign_schema;
881882
ALTER FOREIGN TABLE ft1 SET TABLESPACE ts; -- ERROR
882883
ERROR: relation "ft1" does not exist
883884
ALTER FOREIGN TABLE foreign_schema.ft1 SET TABLESPACE ts; -- ERROR
884885
ERROR: "ft1" is not a table, materialized view, index, or partitioned index
886+
ALTER SEQUENCE foreign_schema.ft1_c11_seq SET SCHEMA public; -- ERROR
887+
ERROR: cannot move an owned sequence into another schema
888+
DETAIL: Sequence "ft1_c11_seq" is linked to table "ft1".
885889
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
886890
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
887891
\d foreign_schema.foreign_table_1
888-
Foreign table "foreign_schema.foreign_table_1"
889-
Column | Type | Collation | Nullable | Default | FDW options
890-
------------------+---------+-----------+----------+---------+--------------------------------
891-
foreign_column_1 | integer | | not null | | ("param 1" 'val1')
892-
c2 | text | | | | (param2 'val2', param3 'val3')
893-
c3 | date | | | |
894-
c4 | integer | | | 0 |
895-
c5 | integer | | | |
896-
c6 | integer | | not null | |
897-
c7 | integer | | | | (p1 'v1', p2 'v2')
898-
c8 | text | | | | (p2 'V2')
899-
c10 | integer | | | | (p1 'v1')
892+
Foreign table "foreign_schema.foreign_table_1"
893+
Column | Type | Collation | Nullable | Default | FDW options
894+
------------------+---------+-----------+----------+-------------------------------------------------+--------------------------------
895+
foreign_column_1 | integer | | not null | | ("param 1" 'val1')
896+
c2 | text | | | | (param2 'val2', param3 'val3')
897+
c3 | date | | | |
898+
c4 | integer | | | 0 |
899+
c5 | integer | | | |
900+
c6 | integer | | not null | |
901+
c7 | integer | | | | (p1 'v1', p2 'v2')
902+
c8 | text | | | | (p2 'V2')
903+
c10 | integer | | | | (p1 'v1')
904+
c11 | integer | | not null | nextval('foreign_schema.ft1_c11_seq'::regclass) |
900905
Check constraints:
901906
"ft1_c2_check" CHECK (c2 <> ''::text)
902907
"ft1_c3_check" CHECK (c3 >= '01-01-1994'::date AND c3 <= '01-31-1994'::date)

src/test/regress/sql/foreign_data.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,11 @@ ALTER FOREIGN TABLE ft1 OPTIONS (DROP delimiter, SET quote '~', ADD escape '@');
406406
ALTER FOREIGN TABLE ft1 DROP COLUMN no_column; -- ERROR
407407
ALTER FOREIGN TABLE ft1 DROP COLUMN IF EXISTS no_column;
408408
ALTER FOREIGN TABLE ft1 DROP COLUMN c9;
409+
ALTER FOREIGN TABLE ft1 ADD COLUMN c11 serial;
409410
ALTER FOREIGN TABLE ft1 SET SCHEMA foreign_schema;
410411
ALTER FOREIGN TABLE ft1 SET TABLESPACE ts; -- ERROR
411412
ALTER FOREIGN TABLE foreign_schema.ft1 SET TABLESPACE ts; -- ERROR
413+
ALTER SEQUENCE foreign_schema.ft1_c11_seq SET SCHEMA public; -- ERROR
412414
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
413415
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
414416
\d foreign_schema.foreign_table_1

0 commit comments

Comments
 (0)