Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitf7f83a5

Browse files
committed
Ensure that expandTableLikeClause() re-examines the same table.
As it stood, expandTableLikeClause() re-did the same relation_openrvcall that transformTableLikeClause() had done. However there arescenarios where this would not find the same table as expected.We hold lock on the LIKE source table, so it can't be renamed ordropped, but another table could appear before it in the search path.This explains the odd behavior reported in bug #16758 when cloning atable as a temp table of the same name. This case worked as expectedbefore commit5028981 introduced the need to open the source tabletwice, so we should fix it.To make really sure we get the same table, let's re-open it by OID notname. That requires adding an OID field to struct TableLikeClause,which is a little nervous-making from an ABI standpoint, but as longas 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 parent677f74e commitf7f83a5

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
@@ -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
returnnewnode;
34163417
}

‎src/backend/nodes/equalfuncs.c

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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
staticvoid

‎src/backend/parser/gram.y

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ typedef struct TableLikeClause
673673
NodeTagtype;
674674
RangeVar*relation;
675675
bits32options;/* OR of TableLikeOption flags */
676+
OidrelationOid;/* If table has been looked up, its OID */
676677
}TableLikeClause;
677678

678679
typedefenumTableLikeOption

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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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
DROPTABLEpublic.pg_attrdef;
175175

176+
-- Check that LIKE isn't confused when new table masks the old, either
177+
BEGIN;
178+
CREATESCHEMActl_schema;
179+
SET LOCAL search_path= ctl_schema, public;
180+
CREATETABLEctlt1 (LIKE ctlt1 INCLUDING ALL);
181+
\d+ ctlt1
182+
ROLLBACK;
183+
176184
DROPTABLE 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)

[8]ページ先頭

©2009-2025 Movatter.jp