mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-03 10:47:19 +02:00
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
This commit is contained in:
parent
49aaabdf8d
commit
28bb8c4966
@ -3356,6 +3356,7 @@ _copyTableLikeClause(const TableLikeClause *from)
|
|||||||
|
|
||||||
COPY_NODE_FIELD(relation);
|
COPY_NODE_FIELD(relation);
|
||||||
COPY_SCALAR_FIELD(options);
|
COPY_SCALAR_FIELD(options);
|
||||||
|
COPY_SCALAR_FIELD(relationOid);
|
||||||
|
|
||||||
return newnode;
|
return newnode;
|
||||||
}
|
}
|
||||||
|
@ -1249,6 +1249,7 @@ _equalTableLikeClause(const TableLikeClause *a, const TableLikeClause *b)
|
|||||||
{
|
{
|
||||||
COMPARE_NODE_FIELD(relation);
|
COMPARE_NODE_FIELD(relation);
|
||||||
COMPARE_SCALAR_FIELD(options);
|
COMPARE_SCALAR_FIELD(options);
|
||||||
|
COMPARE_SCALAR_FIELD(relationOid);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -2835,6 +2835,7 @@ _outTableLikeClause(StringInfo str, const TableLikeClause *node)
|
|||||||
|
|
||||||
WRITE_NODE_FIELD(relation);
|
WRITE_NODE_FIELD(relation);
|
||||||
WRITE_UINT_FIELD(options);
|
WRITE_UINT_FIELD(options);
|
||||||
|
WRITE_OID_FIELD(relationOid);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -3627,6 +3627,7 @@ TableLikeClause:
|
|||||||
TableLikeClause *n = makeNode(TableLikeClause);
|
TableLikeClause *n = makeNode(TableLikeClause);
|
||||||
n->relation = $2;
|
n->relation = $2;
|
||||||
n->options = $3;
|
n->options = $3;
|
||||||
|
n->relationOid = InvalidOid;
|
||||||
$$ = (Node *)n;
|
$$ = (Node *)n;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
@ -1100,12 +1100,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
|
|||||||
* yet know what column numbers the copied columns will have in the
|
* yet know what column numbers the copied columns will have in the
|
||||||
* finished table. If any of those options are specified, add the LIKE
|
* finished table. If any of those options are specified, add the LIKE
|
||||||
* clause to cxt->likeclauses so that expandTableLikeClause will be called
|
* clause to cxt->likeclauses so that expandTableLikeClause will be called
|
||||||
* after we do know that.
|
* after we do know that. Also, remember the relation OID so that
|
||||||
|
* expandTableLikeClause is certain to open the same table.
|
||||||
*/
|
*/
|
||||||
if (table_like_clause->options &
|
if (table_like_clause->options &
|
||||||
(CREATE_TABLE_LIKE_CONSTRAINTS |
|
(CREATE_TABLE_LIKE_CONSTRAINTS |
|
||||||
CREATE_TABLE_LIKE_INDEXES))
|
CREATE_TABLE_LIKE_INDEXES))
|
||||||
|
{
|
||||||
|
table_like_clause->relationOid = RelationGetRelid(relation);
|
||||||
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
|
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We may copy extended statistics if requested, since the representation
|
* We may copy extended statistics if requested, since the representation
|
||||||
@ -1177,9 +1181,13 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
|
|||||||
* Open the relation referenced by the LIKE clause. We should still have
|
* Open the relation referenced by the LIKE clause. We should still have
|
||||||
* the table lock obtained by transformTableLikeClause (and this'll throw
|
* the table lock obtained by transformTableLikeClause (and this'll throw
|
||||||
* an assertion failure if not). Hence, no need to recheck privileges
|
* an assertion failure if not). Hence, no need to recheck privileges
|
||||||
* etc.
|
* etc. We must open the rel by OID not name, to be sure we get the same
|
||||||
|
* table.
|
||||||
*/
|
*/
|
||||||
relation = relation_openrv(table_like_clause->relation, NoLock);
|
if (!OidIsValid(table_like_clause->relationOid))
|
||||||
|
elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause");
|
||||||
|
|
||||||
|
relation = relation_open(table_like_clause->relationOid, NoLock);
|
||||||
|
|
||||||
tupleDesc = RelationGetDescr(relation);
|
tupleDesc = RelationGetDescr(relation);
|
||||||
constr = tupleDesc->constr;
|
constr = tupleDesc->constr;
|
||||||
|
@ -672,6 +672,7 @@ typedef struct TableLikeClause
|
|||||||
NodeTag type;
|
NodeTag type;
|
||||||
RangeVar *relation;
|
RangeVar *relation;
|
||||||
bits32 options; /* OR of TableLikeOption flags */
|
bits32 options; /* OR of TableLikeOption flags */
|
||||||
|
Oid relationOid; /* If table has been looked up, its OID */
|
||||||
} TableLikeClause;
|
} TableLikeClause;
|
||||||
|
|
||||||
typedef enum TableLikeOption
|
typedef enum TableLikeOption
|
||||||
|
@ -334,6 +334,27 @@ Statistics objects:
|
|||||||
"public"."pg_attrdef_a_b_stat" (ndistinct, dependencies) ON a, b FROM public.pg_attrdef
|
"public"."pg_attrdef_a_b_stat" (ndistinct, dependencies) ON a, b FROM public.pg_attrdef
|
||||||
|
|
||||||
DROP TABLE public.pg_attrdef;
|
DROP TABLE public.pg_attrdef;
|
||||||
|
-- Check that LIKE isn't confused when new table masks the old, either
|
||||||
|
BEGIN;
|
||||||
|
CREATE SCHEMA ctl_schema;
|
||||||
|
SET LOCAL search_path = ctl_schema, public;
|
||||||
|
CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
|
||||||
|
\d+ ctlt1
|
||||||
|
Table "ctl_schema.ctlt1"
|
||||||
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
||||||
|
--------+------+-----------+----------+---------+----------+--------------+-------------
|
||||||
|
a | text | | not null | | main | | A
|
||||||
|
b | text | | | | extended | | B
|
||||||
|
Indexes:
|
||||||
|
"ctlt1_pkey" PRIMARY KEY, btree (a)
|
||||||
|
"ctlt1_b_idx" btree (b)
|
||||||
|
"ctlt1_expr_idx" btree ((a || b))
|
||||||
|
Check constraints:
|
||||||
|
"ctlt1_a_check" CHECK (length(a) > 2)
|
||||||
|
Statistics objects:
|
||||||
|
"ctl_schema"."ctlt1_a_b_stat" (ndistinct, dependencies) ON a, b FROM ctlt1
|
||||||
|
|
||||||
|
ROLLBACK;
|
||||||
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
|
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
|
||||||
NOTICE: drop cascades to table inhe
|
NOTICE: drop cascades to table inhe
|
||||||
/* LIKE with other relation kinds */
|
/* LIKE with other relation kinds */
|
||||||
|
@ -139,6 +139,14 @@ CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
|
|||||||
\d+ public.pg_attrdef
|
\d+ public.pg_attrdef
|
||||||
DROP TABLE public.pg_attrdef;
|
DROP TABLE public.pg_attrdef;
|
||||||
|
|
||||||
|
-- Check that LIKE isn't confused when new table masks the old, either
|
||||||
|
BEGIN;
|
||||||
|
CREATE SCHEMA ctl_schema;
|
||||||
|
SET LOCAL search_path = ctl_schema, public;
|
||||||
|
CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
|
||||||
|
\d+ ctlt1
|
||||||
|
ROLLBACK;
|
||||||
|
|
||||||
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
|
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user