aboutsummaryrefslogtreecommitdiff
path: root/src/backend/parser/parse_utilcmd.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-12-01 14:02:27 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2020-12-01 14:02:27 -0500
commitf7f83a55bf6051818a0e4387d718867ecfa8561b (patch)
tree6bd3c317472c0ac4ef499394a16f943ce5fe0e4d /src/backend/parser/parse_utilcmd.c
parent677f74e5bb8360122ebf2f473d7169ed65ce4dba (diff)
downloadpostgresql-f7f83a55bf6051818a0e4387d718867ecfa8561b.tar.gz
postgresql-f7f83a55bf6051818a0e4387d718867ecfa8561b.zip
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
Diffstat (limited to 'src/backend/parser/parse_utilcmd.c')
-rw-r--r--src/backend/parser/parse_utilcmd.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index c709abad2b0..89ee9905991 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -1105,14 +1105,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
* we don't yet know what column numbers the copied columns will have in
* the finished table. If any of those options are specified, add the
* LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
- * called after we do know that.
+ * called 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 &
(CREATE_TABLE_LIKE_DEFAULTS |
CREATE_TABLE_LIKE_GENERATED |
CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_INDEXES))
+ {
+ table_like_clause->relationOid = RelationGetRelid(relation);
cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause);
+ }
/*
* We may copy extended statistics if requested, since the representation
@@ -1185,9 +1189,13 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
* Open the relation referenced by the LIKE clause. We should still have
* the table lock obtained by transformTableLikeClause (and this'll throw
* 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);
constr = tupleDesc->constr;