Skip to content

Commit 19f5a37

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 03c7f1f commit 19f5a37

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/backend/tcop/utility.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,7 @@ ProcessUtilitySlow(ParseState *pstate,
11391139
{
11401140
List *stmts;
11411141
ListCell *l;
1142+
RangeVar *table_rv = NULL;
11421143

11431144
/* Run parse analysis ... */
11441145
stmts = transformCreateStmt((CreateStmt *) parsetree,
@@ -1151,11 +1152,15 @@ ProcessUtilitySlow(ParseState *pstate,
11511152

11521153
if (IsA(stmt, CreateStmt))
11531154
{
1155+
CreateStmt *cstmt = (CreateStmt *) stmt;
11541156
Datum toast_options;
11551157
static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
11561158

1159+
/* Remember transformed RangeVar for LIKE */
1160+
table_rv = cstmt->relation;
1161+
11571162
/* Create the table itself */
1158-
address = DefineRelation((CreateStmt *) stmt,
1163+
address = DefineRelation(cstmt,
11591164
RELKIND_RELATION,
11601165
InvalidOid, NULL,
11611166
queryString);
@@ -1174,7 +1179,7 @@ ProcessUtilitySlow(ParseState *pstate,
11741179
* table
11751180
*/
11761181
toast_options = transformRelOptions((Datum) 0,
1177-
((CreateStmt *) stmt)->options,
1182+
cstmt->options,
11781183
"toast",
11791184
validnsps,
11801185
true,
@@ -1188,12 +1193,17 @@ ProcessUtilitySlow(ParseState *pstate,
11881193
}
11891194
else if (IsA(stmt, CreateForeignTableStmt))
11901195
{
1196+
CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) stmt;
1197+
1198+
/* Remember transformed RangeVar for LIKE */
1199+
table_rv = cstmt->base.relation;
1200+
11911201
/* Create the table itself */
1192-
address = DefineRelation((CreateStmt *) stmt,
1202+
address = DefineRelation(&cstmt->base,
11931203
RELKIND_FOREIGN_TABLE,
11941204
InvalidOid, NULL,
11951205
queryString);
1196-
CreateForeignTable((CreateForeignTableStmt *) stmt,
1206+
CreateForeignTable(cstmt,
11971207
address.objectId);
11981208
EventTriggerCollectSimpleCommand(address,
11991209
secondaryObject,
@@ -1208,10 +1218,11 @@ ProcessUtilitySlow(ParseState *pstate,
12081218
* to-do list.
12091219
*/
12101220
TableLikeClause *like = (TableLikeClause *) stmt;
1211-
RangeVar *rv = ((CreateStmt *) parsetree)->relation;
12121221
List *morestmts;
12131222

1214-
morestmts = expandTableLikeClause(rv, like);
1223+
Assert(table_rv != NULL);
1224+
1225+
morestmts = expandTableLikeClause(table_rv, like);
12151226
stmts = list_concat(stmts, morestmts);
12161227

12171228
/*

src/test/regress/expected/create_table_like.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,24 @@ CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
421421
NOTICE: merging column "a" with inherited definition
422422
ERROR: column "a" has a storage parameter conflict
423423
DETAIL: MAIN versus EXTENDED
424+
-- Check that LIKE isn't confused by a system catalog of the same name
425+
CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
426+
\d+ public.pg_attrdef
427+
Table "public.pg_attrdef"
428+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
429+
--------+------+-----------+----------+---------+----------+--------------+-------------
430+
a | text | | not null | | main | | A
431+
b | text | | | | extended | | B
432+
Indexes:
433+
"pg_attrdef_pkey" PRIMARY KEY, btree (a)
434+
"pg_attrdef_b_idx" btree (b)
435+
"pg_attrdef_expr_idx" btree ((a || b))
436+
Check constraints:
437+
"ctlt1_a_check" CHECK (length(a) > 2)
438+
Statistics objects:
439+
"public"."pg_attrdef_a_b_stat" (ndistinct, dependencies, mcv) ON a, b FROM public.pg_attrdef
440+
441+
DROP TABLE public.pg_attrdef;
424442
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
425443
NOTICE: drop cascades to table inhe
426444
-- LIKE must respect NO INHERIT property of constraints

src/test/regress/sql/create_table_like.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ SELECT s.stxname, objsubid, description FROM pg_description, pg_statistic_ext s
163163
CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
164164
CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
165165

166+
-- Check that LIKE isn't confused by a system catalog of the same name
167+
CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
168+
\d+ public.pg_attrdef
169+
DROP TABLE public.pg_attrdef;
170+
166171
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
167172

168173
-- LIKE must respect NO INHERIT property of constraints

0 commit comments

Comments
 (0)