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

Commitdffc82a

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 commit 502898192 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 parent5a1d1b9 commitdffc82a

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

‎src/backend/nodes/equalfuncs.c

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

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

‎src/backend/parser/gram.y

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

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

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

677678
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