Skip to content

Commit 86b8ef1

Browse files
committed
Use the properly transformed RangeVar for expandTableLikeClause().
transformCreateStmt() adjusts the transformed statement's RangeVar to specify the target schema explicitly, for the express reason of making sure that auxiliary statements derived by parse transformation operate on the right table. But the refactoring I did in commit 5028981 got this wrong and passed the untransformed RangeVar to expandTableLikeClause(). This could lead to assertion failures or weird misbehavior if the wrong table was accessed. Per report from Alexander Lakhin. Like the previous patch, back-patch to all supported branches. Discussion: https://postgr.es/m/05051f9d-b32b-cb35-6735-0e9f2ab86b5f@gmail.com
1 parent e2c9bed commit 86b8ef1

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/backend/tcop/utility.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ ProcessUtilitySlow(Node *parsetree,
949949
{
950950
List *stmts;
951951
ListCell *l;
952+
RangeVar *table_rv = NULL;
952953

953954
/* Run parse analysis ... */
954955
stmts = transformCreateStmt((CreateStmt *) parsetree,
@@ -961,11 +962,15 @@ ProcessUtilitySlow(Node *parsetree,
961962

962963
if (IsA(stmt, CreateStmt))
963964
{
965+
CreateStmt *cstmt = (CreateStmt *) stmt;
964966
Datum toast_options;
965967
static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
966968

969+
/* Remember transformed RangeVar for LIKE */
970+
table_rv = cstmt->relation;
971+
967972
/* Create the table itself */
968-
address = DefineRelation((CreateStmt *) stmt,
973+
address = DefineRelation(cstmt,
969974
RELKIND_RELATION,
970975
InvalidOid, NULL);
971976
EventTriggerCollectSimpleCommand(address,
@@ -983,7 +988,7 @@ ProcessUtilitySlow(Node *parsetree,
983988
* table
984989
*/
985990
toast_options = transformRelOptions((Datum) 0,
986-
((CreateStmt *) stmt)->options,
991+
cstmt->options,
987992
"toast",
988993
validnsps,
989994
true,
@@ -997,11 +1002,16 @@ ProcessUtilitySlow(Node *parsetree,
9971002
}
9981003
else if (IsA(stmt, CreateForeignTableStmt))
9991004
{
1005+
CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) stmt;
1006+
1007+
/* Remember transformed RangeVar for LIKE */
1008+
table_rv = cstmt->base.relation;
1009+
10001010
/* Create the table itself */
1001-
address = DefineRelation((CreateStmt *) stmt,
1011+
address = DefineRelation(&cstmt->base,
10021012
RELKIND_FOREIGN_TABLE,
10031013
InvalidOid, NULL);
1004-
CreateForeignTable((CreateForeignTableStmt *) stmt,
1014+
CreateForeignTable(cstmt,
10051015
address.objectId);
10061016
EventTriggerCollectSimpleCommand(address,
10071017
secondaryObject,
@@ -1016,10 +1026,11 @@ ProcessUtilitySlow(Node *parsetree,
10161026
* to-do list.
10171027
*/
10181028
TableLikeClause *like = (TableLikeClause *) stmt;
1019-
RangeVar *rv = ((CreateStmt *) parsetree)->relation;
10201029
List *morestmts;
10211030

1022-
morestmts = expandTableLikeClause(rv, like);
1031+
Assert(table_rv != NULL);
1032+
1033+
morestmts = expandTableLikeClause(table_rv, like);
10231034
stmts = list_concat(stmts, morestmts);
10241035

10251036
/*

src/test/regress/expected/create_table_like.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,22 @@ CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
243243
NOTICE: merging column "a" with inherited definition
244244
ERROR: column "a" has a storage parameter conflict
245245
DETAIL: MAIN versus EXTENDED
246+
-- Check that LIKE isn't confused by a system catalog of the same name
247+
CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
248+
\d+ public.pg_attrdef
249+
Table "public.pg_attrdef"
250+
Column | Type | Modifiers | Storage | Stats target | Description
251+
--------+------+-----------+----------+--------------+-------------
252+
a | text | not null | main | | A
253+
b | text | | extended | | B
254+
Indexes:
255+
"pg_attrdef_pkey" PRIMARY KEY, btree (a)
256+
"pg_attrdef_b_idx" btree (b)
257+
"pg_attrdef_expr_idx" btree ((a || b))
258+
Check constraints:
259+
"ctlt1_a_check" CHECK (length(a) > 2)
260+
261+
DROP TABLE public.pg_attrdef;
246262
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
247263
NOTICE: drop cascades to table inhe
248264
/* LIKE with other relation kinds */

src/test/regress/sql/create_table_like.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ SELECT c.relname, objsubid, description FROM pg_description, pg_index i, pg_clas
112112
CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
113113
CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
114114

115+
-- Check that LIKE isn't confused by a system catalog of the same name
116+
CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
117+
\d+ public.pg_attrdef
118+
DROP TABLE public.pg_attrdef;
119+
115120
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
116121

117122

0 commit comments

Comments
 (0)