Skip to content

Commit f7f83a5

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 677f74e commit f7f83a5

File tree

8 files changed

+45
-3
lines changed

8 files changed

+45
-3
lines changed

src/backend/nodes/copyfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -3411,6 +3411,7 @@ _copyTableLikeClause(const TableLikeClause *from)
34113411

34123412
COPY_NODE_FIELD(relation);
34133413
COPY_SCALAR_FIELD(options);
3414+
COPY_SCALAR_FIELD(relationOid);
34143415

34153416
return newnode;
34163417
}

src/backend/nodes/equalfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ _equalTableLikeClause(const TableLikeClause *a, const TableLikeClause *b)
12541254
{
12551255
COMPARE_NODE_FIELD(relation);
12561256
COMPARE_SCALAR_FIELD(options);
1257+
COMPARE_SCALAR_FIELD(relationOid);
12571258

12581259
return true;
12591260
}

src/backend/nodes/outfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,7 @@ _outTableLikeClause(StringInfo str, const TableLikeClause *node)
28112811

28122812
WRITE_NODE_FIELD(relation);
28132813
WRITE_UINT_FIELD(options);
2814+
WRITE_OID_FIELD(relationOid);
28142815
}
28152816

28162817
static void

src/backend/parser/gram.y

+1
Original file line numberDiff line numberDiff line change
@@ -3643,6 +3643,7 @@ TableLikeClause:
36433643
TableLikeClause *n = makeNode(TableLikeClause);
36443644
n->relation = $2;
36453645
n->options = $3;
3646+
n->relationOid = InvalidOid;
36463647
$$ = (Node *)n;
36473648
}
36483649
;

src/backend/parser/parse_utilcmd.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -1105,14 +1105,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
11051105
* we don't yet know what column numbers the copied columns will have in
11061106
* the finished table. If any of those options are specified, add the
11071107
* LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1108-
* called after we do know that.
1108+
* called after we do know that. Also, remember the relation OID so that
1109+
* expandTableLikeClause is certain to open the same table.
11091110
*/
11101111
if (table_like_clause->options &
11111112
(CREATE_TABLE_LIKE_DEFAULTS |
11121113
CREATE_TABLE_LIKE_GENERATED |
11131114
CREATE_TABLE_LIKE_CONSTRAINTS |
11141115
CREATE_TABLE_LIKE_INDEXES))
1116+
{
1117+
table_like_clause->relationOid = RelationGetRelid(relation);
11151118
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
1119+
}
11161120

11171121
/*
11181122
* We may copy extended statistics if requested, since the representation
@@ -1185,9 +1189,13 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
11851189
* Open the relation referenced by the LIKE clause. We should still have
11861190
* the table lock obtained by transformTableLikeClause (and this'll throw
11871191
* an assertion failure if not). Hence, no need to recheck privileges
1188-
* etc.
1192+
* etc. We must open the rel by OID not name, to be sure we get the same
1193+
* table.
11891194
*/
1190-
relation = relation_openrv(table_like_clause->relation, NoLock);
1195+
if (!OidIsValid(table_like_clause->relationOid))
1196+
elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause");
1197+
1198+
relation = relation_open(table_like_clause->relationOid, NoLock);
11911199

11921200
tupleDesc = RelationGetDescr(relation);
11931201
constr = tupleDesc->constr;

src/include/nodes/parsenodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ typedef struct TableLikeClause
673673
NodeTag type;
674674
RangeVar *relation;
675675
bits32 options; /* OR of TableLikeOption flags */
676+
Oid relationOid; /* If table has been looked up, its OID */
676677
} TableLikeClause;
677678

678679
typedef enum TableLikeOption

src/test/regress/expected/create_table_like.out

+21
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,27 @@ Statistics objects:
455455
"public"."pg_attrdef_a_b_stat" (ndistinct, dependencies, mcv) ON a, b FROM public.pg_attrdef
456456

457457
DROP TABLE public.pg_attrdef;
458+
-- Check that LIKE isn't confused when new table masks the old, either
459+
BEGIN;
460+
CREATE SCHEMA ctl_schema;
461+
SET LOCAL search_path = ctl_schema, public;
462+
CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
463+
\d+ ctlt1
464+
Table "ctl_schema.ctlt1"
465+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
466+
--------+------+-----------+----------+---------+----------+--------------+-------------
467+
a | text | | not null | | main | | A
468+
b | text | | | | extended | | B
469+
Indexes:
470+
"ctlt1_pkey" PRIMARY KEY, btree (a)
471+
"ctlt1_b_idx" btree (b)
472+
"ctlt1_expr_idx" btree ((a || b))
473+
Check constraints:
474+
"ctlt1_a_check" CHECK (length(a) > 2)
475+
Statistics objects:
476+
"ctl_schema"."ctlt1_a_b_stat" (ndistinct, dependencies, mcv) ON a, b FROM ctlt1
477+
478+
ROLLBACK;
458479
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
459480
NOTICE: drop cascades to table inhe
460481
-- LIKE must respect NO INHERIT property of constraints

src/test/regress/sql/create_table_like.sql

+8
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
173173
\d+ public.pg_attrdef
174174
DROP TABLE public.pg_attrdef;
175175

176+
-- Check that LIKE isn't confused when new table masks the old, either
177+
BEGIN;
178+
CREATE SCHEMA ctl_schema;
179+
SET LOCAL search_path = ctl_schema, public;
180+
CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
181+
\d+ ctlt1
182+
ROLLBACK;
183+
176184
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
177185

178186
-- LIKE must respect NO INHERIT property of constraints

0 commit comments

Comments
 (0)