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

Commit dffc82a

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 502898192 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 5a1d1b9 commit dffc82a

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
@@ -3419,6 +3419,7 @@ _copyTableLikeClause(const TableLikeClause *from)
34193419

34203420
COPY_NODE_FIELD(relation);
34213421
COPY_SCALAR_FIELD(options);
3422+
COPY_SCALAR_FIELD(relationOid);
34223423

34233424
return newnode;
34243425
}

src/backend/nodes/equalfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,7 @@ _equalTableLikeClause(const TableLikeClause *a, const TableLikeClause *b)
12601260
{
12611261
COMPARE_NODE_FIELD(relation);
12621262
COMPARE_SCALAR_FIELD(options);
1263+
COMPARE_SCALAR_FIELD(relationOid);
12631264

12641265
return true;
12651266
}

src/backend/nodes/outfuncs.c

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

28112811
WRITE_NODE_FIELD(relation);
28122812
WRITE_UINT_FIELD(options);
2813+
WRITE_OID_FIELD(relationOid);
28132814
}
28142815

28152816
static void

src/backend/parser/gram.y

+1
Original file line numberDiff line numberDiff line change
@@ -3629,6 +3629,7 @@ TableLikeClause:
36293629
TableLikeClause *n = makeNode(TableLikeClause);
36303630
n->relation = $2;
36313631
n->options = $3;
3632+
n->relationOid = InvalidOid;
36323633
$$ = (Node *)n;
36333634
}
36343635
;

src/backend/parser/parse_utilcmd.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
11021102
* we don't yet know what column numbers the copied columns will have in
11031103
* the finished table. If any of those options are specified, add the
11041104
* LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1105-
* called after we do know that.
1105+
* called after we do know that. Also, remember the relation OID so that
1106+
* expandTableLikeClause is certain to open the same table.
11061107
*/
11071108
if (table_like_clause->options &
11081109
(CREATE_TABLE_LIKE_DEFAULTS |
11091110
CREATE_TABLE_LIKE_GENERATED |
11101111
CREATE_TABLE_LIKE_CONSTRAINTS |
11111112
CREATE_TABLE_LIKE_INDEXES))
1113+
{
1114+
table_like_clause->relationOid = RelationGetRelid(relation);
11121115
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
1116+
}
11131117

11141118
/*
11151119
* We may copy extended statistics if requested, since the representation
@@ -1182,9 +1186,13 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
11821186
* Open the relation referenced by the LIKE clause. We should still have
11831187
* the table lock obtained by transformTableLikeClause (and this'll throw
11841188
* an assertion failure if not). Hence, no need to recheck privileges
1185-
* etc.
1189+
* etc. We must open the rel by OID not name, to be sure we get the same
1190+
* table.
11861191
*/
1187-
relation = relation_openrv(table_like_clause->relation, NoLock);
1192+
if (!OidIsValid(table_like_clause->relationOid))
1193+
elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause");
1194+
1195+
relation = relation_open(table_like_clause->relationOid, NoLock);
11881196

11891197
tupleDesc = RelationGetDescr(relation);
11901198
constr = tupleDesc->constr;

src/include/nodes/parsenodes.h

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

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