aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/tablecmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-05-21 15:02:07 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-05-21 15:02:07 -0400
commit77e3204ecbf15ab5dfd295bbc66eeeec4d9ade19 (patch)
treed05a40262c896f41b3cc33712a002e54835b5c96 /src/backend/commands/tablecmds.c
parentd18ee6f92d9a22b4fae57f515797b2196bf385c7 (diff)
downloadpostgresql-77e3204ecbf15ab5dfd295bbc66eeeec4d9ade19.tar.gz
postgresql-77e3204ecbf15ab5dfd295bbc66eeeec4d9ade19.zip
Fix usage of "tableoid" in GENERATED expressions.
We consider this supported (though I've got my doubts that it's a good idea, because tableoid is not immutable). However, several code paths failed to fill the field in soon enough, causing such a GENERATED expression to see zero or the wrong value. This occurred when ALTER TABLE adds a new GENERATED column to a table with existing rows, and during regular INSERT or UPDATE on a foreign table with GENERATED columns. Noted during investigation of a report from Vitaly Ustinov. Back-patch to v12 where GENERATED came in. Discussion: https://postgr.es/m/CAM_DEiWR2DPT6U4xb-Ehigozzd3n3G37ZB1+867zbsEVtYoJww@mail.gmail.com
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r--src/backend/commands/tablecmds.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 84e6e9dbc58..70337c7c072 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5374,6 +5374,14 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
newslot->tts_isnull[lfirst_int(lc)] = true;
/*
+ * Constraints and GENERATED expressions might reference the
+ * tableoid column, so fill tts_tableOid with the desired
+ * value. (We must do this each time, because it gets
+ * overwritten with newrel's OID during storing.)
+ */
+ newslot->tts_tableOid = RelationGetRelid(oldrel);
+
+ /*
* Process supplied expressions to replace selected columns.
*
* First, evaluate expressions whose inputs come from the old
@@ -5416,11 +5424,6 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
&newslot->tts_isnull[ex->attnum - 1]);
}
- /*
- * Constraints might reference the tableoid column, so
- * initialize t_tableOid before evaluating them.
- */
- newslot->tts_tableOid = RelationGetRelid(oldrel);
insertslot = newslot;
}
else