Skip to content

Commit 28bb8c4

Browse files
committed
Ensure that expandTableLikeClause() re-examines the same table.
As it stood, expandTableLikeClause() re-did the same relation_openrv call that transformTableLikeClause() had done. However there are scenarios where this would not find the same table as expected. We hold lock on the LIKE source table, so it can't be renamed or dropped, but another table could appear before it in the search path. This explains the odd behavior reported in bug #16758 when cloning a table as a temp table of the same name. This case worked as expected before commit 5028981 introduced the need to open the source table twice, so we should fix it. To make really sure we get the same table, let's re-open it by OID not name. That requires adding an OID field to struct TableLikeClause, which is a little nervous-making from an ABI standpoint, but as long as it's at the end I don't think there's any serious risk. Per bug #16758 from Marc Boeren. Like the previous patch, back-patch to all supported branches. Discussion: https://postgr.es/m/16758-840e84a6cfab276d@postgresql.org
1 parent 49aaabd commit 28bb8c4

File tree

8 files changed

+45
-3
lines changed

8 files changed

+45
-3
lines changed

src/backend/nodes/copyfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,7 @@ _copyTableLikeClause(const TableLikeClause *from)
33563356

33573357
COPY_NODE_FIELD(relation);
33583358
COPY_SCALAR_FIELD(options);
3359+
COPY_SCALAR_FIELD(relationOid);
33593360

33603361
return newnode;
33613362
}

src/backend/nodes/equalfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ _equalTableLikeClause(const TableLikeClause *a, const TableLikeClause *b)
12491249
{
12501250
COMPARE_NODE_FIELD(relation);
12511251
COMPARE_SCALAR_FIELD(options);
1252+
COMPARE_SCALAR_FIELD(relationOid);
12521253

12531254
return true;
12541255
}

src/backend/nodes/outfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,7 @@ _outTableLikeClause(StringInfo str, const TableLikeClause *node)
28352835

28362836
WRITE_NODE_FIELD(relation);
28372837
WRITE_UINT_FIELD(options);
2838+
WRITE_OID_FIELD(relationOid);
28382839
}
28392840

28402841
static void

src/backend/parser/gram.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,7 @@ TableLikeClause:
36273627
TableLikeClause *n = makeNode(TableLikeClause);
36283628
n->relation = $2;
36293629
n->options = $3;
3630+
n->relationOid = InvalidOid;
36303631
$$ = (Node *)n;
36313632
}
36323633
;

src/backend/parser/parse_utilcmd.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,12 +1100,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
11001100
* yet know what column numbers the copied columns will have in the
11011101
* finished table. If any of those options are specified, add the LIKE
11021102
* clause to cxt->likeclauses so that expandTableLikeClause will be called
1103-
* after we do know that.
1103+
* after we do know that. Also, remember the relation OID so that
1104+
* expandTableLikeClause is certain to open the same table.
11041105
*/
11051106
if (table_like_clause->options &
11061107
(CREATE_TABLE_LIKE_CONSTRAINTS |
11071108
CREATE_TABLE_LIKE_INDEXES))
1109+
{
1110+
table_like_clause->relationOid = RelationGetRelid(relation);
11081111
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
1112+
}
11091113

11101114
/*
11111115
* We may copy extended statistics if requested, since the representation
@@ -1177,9 +1181,13 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
11771181
* Open the relation referenced by the LIKE clause. We should still have
11781182
* the table lock obtained by transformTableLikeClause (and this'll throw
11791183
* an assertion failure if not). Hence, no need to recheck privileges
1180-
* etc.
1184+
* etc. We must open the rel by OID not name, to be sure we get the same
1185+
* table.
11811186
*/
1182-
relation = relation_openrv(table_like_clause->relation, NoLock);
1187+
if (!OidIsValid(table_like_clause->relationOid))
1188+
elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause");
1189+
1190+
relation = relation_open(table_like_clause->relationOid, NoLock);
11831191

11841192
tupleDesc = RelationGetDescr(relation);
11851193
constr = tupleDesc->constr;

src/include/nodes/parsenodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ typedef struct TableLikeClause
672672
NodeTag type;
673673
RangeVar *relation;
674674
bits32 options; /* OR of TableLikeOption flags */
675+
Oid relationOid; /* If table has been looked up, its OID */
675676
} TableLikeClause;
676677

677678
typedef enum TableLikeOption

src/test/regress/expected/create_table_like.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,27 @@ Statistics objects:
334334
"public"."pg_attrdef_a_b_stat" (ndistinct, dependencies) ON a, b FROM public.pg_attrdef
335335

336336
DROP TABLE public.pg_attrdef;
337+
-- Check that LIKE isn't confused when new table masks the old, either
338+
BEGIN;
339+
CREATE SCHEMA ctl_schema;
340+
SET LOCAL search_path = ctl_schema, public;
341+
CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
342+
\d+ ctlt1
343+
Table "ctl_schema.ctlt1"
344+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
345+
--------+------+-----------+----------+---------+----------+--------------+-------------
346+
a | text | | not null | | main | | A
347+
b | text | | | | extended | | B
348+
Indexes:
349+
"ctlt1_pkey" PRIMARY KEY, btree (a)
350+
"ctlt1_b_idx" btree (b)
351+
"ctlt1_expr_idx" btree ((a || b))
352+
Check constraints:
353+
"ctlt1_a_check" CHECK (length(a) > 2)
354+
Statistics objects:
355+
"ctl_schema"."ctlt1_a_b_stat" (ndistinct, dependencies) ON a, b FROM ctlt1
356+
357+
ROLLBACK;
337358
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
338359
NOTICE: drop cascades to table inhe
339360
/* LIKE with other relation kinds */

src/test/regress/sql/create_table_like.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
139139
\d+ public.pg_attrdef
140140
DROP TABLE public.pg_attrdef;
141141

142+
-- Check that LIKE isn't confused when new table masks the old, either
143+
BEGIN;
144+
CREATE SCHEMA ctl_schema;
145+
SET LOCAL search_path = ctl_schema, public;
146+
CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
147+
\d+ ctlt1
148+
ROLLBACK;
149+
142150
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
143151

144152

0 commit comments

Comments
 (0)