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

Appearance settings

Commit 1a9c93e

Browse filesBrowse 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 dc71c64 commit 1a9c93e
Copy full SHA for 1a9c93e

File tree

Expand file treeCollapse file tree

3 files changed

+38
-6
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+38
-6
lines changed

‎src/backend/tcop/utility.c

Copy file name to clipboardExpand all lines: src/backend/tcop/utility.c
+17-6Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ ProcessUtilitySlow(Node *parsetree,
964964
{
965965
List *stmts;
966966
ListCell *l;
967+
RangeVar *table_rv = NULL;
967968

968969
/* Run parse analysis ... */
969970
stmts = transformCreateStmt((CreateStmt *) parsetree,
@@ -976,11 +977,15 @@ ProcessUtilitySlow(Node *parsetree,
976977

977978
if (IsA(stmt, CreateStmt))
978979
{
980+
CreateStmt *cstmt = (CreateStmt *) stmt;
979981
Datum toast_options;
980982
static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
981983

984+
/* Remember transformed RangeVar for LIKE */
985+
table_rv = cstmt->relation;
986+
982987
/* Create the table itself */
983-
address = DefineRelation((CreateStmt *) stmt,
988+
address = DefineRelation(cstmt,
984989
RELKIND_RELATION,
985990
InvalidOid, NULL);
986991
EventTriggerCollectSimpleCommand(address,
@@ -998,7 +1003,7 @@ ProcessUtilitySlow(Node *parsetree,
9981003
* table
9991004
*/
10001005
toast_options = transformRelOptions((Datum) 0,
1001-
((CreateStmt *) stmt)->options,
1006+
cstmt->options,
10021007
"toast",
10031008
validnsps,
10041009
true,
@@ -1012,11 +1017,16 @@ ProcessUtilitySlow(Node *parsetree,
10121017
}
10131018
else if (IsA(stmt, CreateForeignTableStmt))
10141019
{
1020+
CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) stmt;
1021+
1022+
/* Remember transformed RangeVar for LIKE */
1023+
table_rv = cstmt->base.relation;
1024+
10151025
/* Create the table itself */
1016-
address = DefineRelation((CreateStmt *) stmt,
1026+
address = DefineRelation(&cstmt->base,
10171027
RELKIND_FOREIGN_TABLE,
10181028
InvalidOid, NULL);
1019-
CreateForeignTable((CreateForeignTableStmt *) stmt,
1029+
CreateForeignTable(cstmt,
10201030
address.objectId);
10211031
EventTriggerCollectSimpleCommand(address,
10221032
secondaryObject,
@@ -1031,10 +1041,11 @@ ProcessUtilitySlow(Node *parsetree,
10311041
* to-do list.
10321042
*/
10331043
TableLikeClause *like = (TableLikeClause *) stmt;
1034-
RangeVar *rv = ((CreateStmt *) parsetree)->relation;
10351044
List *morestmts;
10361045

1037-
morestmts = expandTableLikeClause(rv, like);
1046+
Assert(table_rv != NULL);
1047+
1048+
morestmts = expandTableLikeClause(table_rv, like);
10381049
stmts = list_concat(stmts, morestmts);
10391050

10401051
/*

‎src/test/regress/expected/create_table_like.out

Copy file name to clipboardExpand all lines: src/test/regress/expected/create_table_like.out
+16Lines 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

Copy file name to clipboardExpand all lines: src/test/regress/sql/create_table_like.sql
+5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.