Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb142068

Browse files
committed
Allow CREATE FOREIGN TABLE to include SERIAL columns.
The behavior is that the required sequence is created locally, which isappropriate because the default expression will be evaluated locally.Per gripe from Brad Nicholson that this case was refused with a confusingerror message. We could have improved the error message but it seemsbetter to just allow the case.Also, remove ALTER TABLE's arbitrary prohibition against being applied toforeign tables, which was pretty inconsistent considering we allow it forviews, sequences, and other relation types that aren't even called tables.This is needed to avoid breaking pg_dump, which sometimes emits columndefaults using separate ALTER TABLE commands. (I think this can happeneven when the default is not associated with a sequence, so that was apre-existing bug once we allowed column defaults for foreign tables.)
1 parenta2a480a commitb142068

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

‎contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,3 +2339,37 @@ select c2, count(*) from "S 1"."T 1" where c2 < 500 group by 1 order by 1;
23392339
407 | 100
23402340
(13 rows)
23412341

2342+
-- ===================================================================
2343+
-- test serial columns (ie, sequence-based defaults)
2344+
-- ===================================================================
2345+
create table loc1 (f1 serial, f2 text);
2346+
create foreign table rem1 (f1 serial, f2 text)
2347+
server loopback options(table_name 'loc1');
2348+
select pg_catalog.setval('rem1_f1_seq', 10, false);
2349+
setval
2350+
--------
2351+
10
2352+
(1 row)
2353+
2354+
insert into loc1(f2) values('hi');
2355+
insert into rem1(f2) values('hi remote');
2356+
insert into loc1(f2) values('bye');
2357+
insert into rem1(f2) values('bye remote');
2358+
select * from loc1;
2359+
f1 | f2
2360+
----+------------
2361+
1 | hi
2362+
10 | hi remote
2363+
2 | bye
2364+
11 | bye remote
2365+
(4 rows)
2366+
2367+
select * from rem1;
2368+
f1 | f2
2369+
----+------------
2370+
1 | hi
2371+
10 | hi remote
2372+
2 | bye
2373+
11 | bye remote
2374+
(4 rows)
2375+

‎contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,17 @@ select c2, count(*) from "S 1"."T 1" where c2 < 500 group by 1 order by 1;
369369
commit;
370370
select c2,count(*)from ft2where c2<500group by1order by1;
371371
select c2,count(*)from"S 1"."T 1"where c2<500group by1order by1;
372+
373+
-- ===================================================================
374+
-- test serial columns (ie, sequence-based defaults)
375+
-- ===================================================================
376+
createtableloc1 (f1serial, f2text);
377+
create foreign table rem1 (f1serial, f2text)
378+
server loopback options(table_name'loc1');
379+
selectpg_catalog.setval('rem1_f1_seq',10, false);
380+
insert into loc1(f2)values('hi');
381+
insert into rem1(f2)values('hi remote');
382+
insert into loc1(f2)values('bye');
383+
insert into rem1(f2)values('bye remote');
384+
select*from loc1;
385+
select*from rem1;

‎src/backend/commands/sequence.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,11 +1440,12 @@ process_owned_by(Relation seqrel, List *owned_by)
14401440
rel=makeRangeVarFromNameList(relname);
14411441
tablerel=relation_openrv(rel,AccessShareLock);
14421442

1443-
/* Must be a regular table */
1444-
if (tablerel->rd_rel->relkind!=RELKIND_RELATION)
1443+
/* Must be a regular or foreign table */
1444+
if (!(tablerel->rd_rel->relkind==RELKIND_RELATION||
1445+
tablerel->rd_rel->relkind==RELKIND_FOREIGN_TABLE))
14451446
ereport(ERROR,
14461447
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1447-
errmsg("referenced relation \"%s\" is not a table",
1448+
errmsg("referenced relation \"%s\" is not a table or foreign table",
14481449
RelationGetRelationName(tablerel))));
14491450

14501451
/* We insist on same owner and schema */

‎src/backend/commands/tablecmds.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10518,19 +10518,16 @@ RangeVarCallbackForAlterRelation(const RangeVar *rv, Oid relid, Oid oldrelid,
1051810518
errmsg("\"%s\" is a composite type",rv->relname),
1051910519
errhint("Use ALTER TYPE instead.")));
1052010520

10521-
if (reltype!=OBJECT_FOREIGN_TABLE&&relkind==RELKIND_FOREIGN_TABLE)
10522-
ereport(ERROR,
10523-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
10524-
errmsg("\"%s\" is a foreign table",rv->relname),
10525-
errhint("Use ALTER FOREIGN TABLE instead.")));
10526-
1052710521
/*
1052810522
* Don't allow ALTER TABLE .. SET SCHEMA on relations that can't be moved
1052910523
* to a different schema, such as indexes and TOAST tables.
1053010524
*/
10531-
if (IsA(stmt,AlterObjectSchemaStmt)&&relkind!=RELKIND_RELATION
10532-
&&relkind!=RELKIND_VIEW&&relkind!=RELKIND_MATVIEW
10533-
&&relkind!=RELKIND_SEQUENCE&&relkind!=RELKIND_FOREIGN_TABLE)
10525+
if (IsA(stmt,AlterObjectSchemaStmt)&&
10526+
relkind!=RELKIND_RELATION&&
10527+
relkind!=RELKIND_VIEW&&
10528+
relkind!=RELKIND_MATVIEW&&
10529+
relkind!=RELKIND_SEQUENCE&&
10530+
relkind!=RELKIND_FOREIGN_TABLE)
1053410531
ereport(ERROR,
1053510532
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1053610533
errmsg("\"%s\" is not a table, view, sequence, or foreign table",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp