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

Commit b380484

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 502898192 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 e6bbe07 commit b380484

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/backend/tcop/utility.c

+17-6
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ ProcessUtilitySlow(ParseState *pstate,
11371137
{
11381138
List *stmts;
11391139
ListCell *l;
1140+
RangeVar *table_rv = NULL;
11401141

11411142
/* Run parse analysis ... */
11421143
stmts = transformCreateStmt((CreateStmt *) parsetree,
@@ -1149,11 +1150,15 @@ ProcessUtilitySlow(ParseState *pstate,
11491150

11501151
if (IsA(stmt, CreateStmt))
11511152
{
1153+
CreateStmt *cstmt = (CreateStmt *) stmt;
11521154
Datum toast_options;
11531155
static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
11541156

1157+
/* Remember transformed RangeVar for LIKE */
1158+
table_rv = cstmt->relation;
1159+
11551160
/* Create the table itself */
1156-
address = DefineRelation((CreateStmt *) stmt,
1161+
address = DefineRelation(cstmt,
11571162
RELKIND_RELATION,
11581163
InvalidOid, NULL,
11591164
queryString);
@@ -1172,7 +1177,7 @@ ProcessUtilitySlow(ParseState *pstate,
11721177
* table
11731178
*/
11741179
toast_options = transformRelOptions((Datum) 0,
1175-
((CreateStmt *) stmt)->options,
1180+
cstmt->options,
11761181
"toast",
11771182
validnsps,
11781183
true,
@@ -1186,12 +1191,17 @@ ProcessUtilitySlow(ParseState *pstate,
11861191
}
11871192
else if (IsA(stmt, CreateForeignTableStmt))
11881193
{
1194+
CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) stmt;
1195+
1196+
/* Remember transformed RangeVar for LIKE */
1197+
table_rv = cstmt->base.relation;
1198+
11891199
/* Create the table itself */
1190-
address = DefineRelation((CreateStmt *) stmt,
1200+
address = DefineRelation(&cstmt->base,
11911201
RELKIND_FOREIGN_TABLE,
11921202
InvalidOid, NULL,
11931203
queryString);
1194-
CreateForeignTable((CreateForeignTableStmt *) stmt,
1204+
CreateForeignTable(cstmt,
11951205
address.objectId);
11961206
EventTriggerCollectSimpleCommand(address,
11971207
secondaryObject,
@@ -1206,10 +1216,11 @@ ProcessUtilitySlow(ParseState *pstate,
12061216
* to-do list.
12071217
*/
12081218
TableLikeClause *like = (TableLikeClause *) stmt;
1209-
RangeVar *rv = ((CreateStmt *) parsetree)->relation;
12101219
List *morestmts;
12111220

1212-
morestmts = expandTableLikeClause(rv, like);
1221+
Assert(table_rv != NULL);
1222+
1223+
morestmts = expandTableLikeClause(table_rv, like);
12131224
stmts = list_concat(stmts, morestmts);
12141225

12151226
/*

src/test/regress/expected/create_table_like.out

+18
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

+5
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)