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

Commit2b0ee12

Browse files
committed
Fix usage of "tableoid" in GENERATED expressions.
We consider this supported (though I've got my doubts that it's agood idea, because tableoid is not immutable). However, severalcode paths failed to fill the field in soon enough, causing sucha GENERATED expression to see zero or the wrong value. Thisoccurred when ALTER TABLE adds a new GENERATED column to a tablewith existing rows, and during regular INSERT or UPDATE on aforeign 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
1 parent84f5c29 commit2b0ee12

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5761,6 +5761,14 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
57615761
foreach(lc, dropped_attrs)
57625762
newslot->tts_isnull[lfirst_int(lc)] = true;
57635763

5764+
/*
5765+
* Constraints and GENERATED expressions might reference the
5766+
* tableoid column, so fill tts_tableOid with the desired
5767+
* value. (We must do this each time, because it gets
5768+
* overwritten with newrel's OID during storing.)
5769+
*/
5770+
newslot->tts_tableOid = RelationGetRelid(oldrel);
5771+
57645772
/*
57655773
* Process supplied expressions to replace selected columns.
57665774
*
@@ -5804,11 +5812,6 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
58045812
&newslot->tts_isnull[ex->attnum - 1]);
58055813
}
58065814

5807-
/*
5808-
* Constraints might reference the tableoid column, so
5809-
* initialize t_tableOid before evaluating them.
5810-
*/
5811-
newslot->tts_tableOid = RelationGetRelid(oldrel);
58125815
insertslot = newslot;
58135816
}
58145817
else

‎src/backend/executor/nodeModifyTable.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ ExecInsert(ModifyTableState *mtstate,
658658
}
659659
elseif (resultRelInfo->ri_FdwRoutine)
660660
{
661+
/*
662+
* GENERATED expressions might reference the tableoid column, so
663+
* (re-)initialize tts_tableOid before evaluating them.
664+
*/
665+
slot->tts_tableOid=RelationGetRelid(resultRelInfo->ri_RelationDesc);
666+
661667
/*
662668
* Compute stored generated columns
663669
*/
@@ -729,7 +735,7 @@ ExecInsert(ModifyTableState *mtstate,
729735
/*
730736
* AFTER ROW Triggers or RETURNING expressions might reference the
731737
* tableoid column, so (re-)initialize tts_tableOid before evaluating
732-
* them.
738+
* them. (This covers the case where the FDW replaced the slot.)
733739
*/
734740
slot->tts_tableOid=RelationGetRelid(resultRelInfo->ri_RelationDesc);
735741
}
@@ -738,8 +744,8 @@ ExecInsert(ModifyTableState *mtstate,
738744
WCOKindwco_kind;
739745

740746
/*
741-
* Constraints might reference the tableoid column, so (re-)initialize
742-
* tts_tableOid before evaluating them.
747+
* Constraintsand GENERATED expressionsmight reference the tableoid
748+
*column, so (re-)initializetts_tableOid before evaluating them.
743749
*/
744750
slot->tts_tableOid=RelationGetRelid(resultRelationDesc);
745751

@@ -1629,6 +1635,12 @@ ExecUpdate(ModifyTableState *mtstate,
16291635
}
16301636
elseif (resultRelInfo->ri_FdwRoutine)
16311637
{
1638+
/*
1639+
* GENERATED expressions might reference the tableoid column, so
1640+
* (re-)initialize tts_tableOid before evaluating them.
1641+
*/
1642+
slot->tts_tableOid=RelationGetRelid(resultRelInfo->ri_RelationDesc);
1643+
16321644
/*
16331645
* Compute stored generated columns
16341646
*/
@@ -1651,7 +1663,7 @@ ExecUpdate(ModifyTableState *mtstate,
16511663
/*
16521664
* AFTER ROW Triggers or RETURNING expressions might reference the
16531665
* tableoid column, so (re-)initialize tts_tableOid before evaluating
1654-
* them.
1666+
* them. (This covers the case where the FDW replaced the slot.)
16551667
*/
16561668
slot->tts_tableOid=RelationGetRelid(resultRelationDesc);
16571669
}
@@ -1662,8 +1674,8 @@ ExecUpdate(ModifyTableState *mtstate,
16621674
boolupdate_indexes;
16631675

16641676
/*
1665-
* Constraints might reference the tableoid column, so (re-)initialize
1666-
* tts_tableOid before evaluating them.
1677+
* Constraintsand GENERATED expressionsmight reference the tableoid
1678+
*column, so (re-)initializetts_tableOid before evaluating them.
16671679
*/
16681680
slot->tts_tableOid=RelationGetRelid(resultRelationDesc);
16691681

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ ERROR: both identity and generation expression specified for column "b" of tabl
6464
LINE 1: ...t PRIMARY KEY, b int GENERATED ALWAYS AS identity GENERATED ...
6565
^
6666
-- reference to system column not allowed in generated column
67+
-- (except tableoid, which we test below)
6768
CREATE TABLE gtest_err_6a (a int PRIMARY KEY, b bool GENERATED ALWAYS AS (xmin <> 37) STORED);
6869
ERROR: cannot use system column "xmin" in column generation expression
6970
LINE 1: ...a (a int PRIMARY KEY, b bool GENERATED ALWAYS AS (xmin <> 37...
@@ -455,14 +456,16 @@ DROP TYPE double_int;
455456
-- using tableoid is allowed
456457
CREATE TABLE gtest_tableoid (
457458
a int PRIMARY KEY,
458-
b bool GENERATED ALWAYS AS (tableoid<> 0) STORED
459+
b bool GENERATED ALWAYS AS (tableoid= 'gtest_tableoid'::regclass) STORED
459460
);
460461
INSERT INTO gtest_tableoid VALUES (1), (2);
462+
ALTER TABLE gtest_tableoid ADD COLUMN
463+
c regclass GENERATED ALWAYS AS (tableoid) STORED;
461464
SELECT * FROM gtest_tableoid;
462-
a | b
463-
---+---
464-
1 | t
465-
2 | t
465+
a | b| c
466+
---+---+----------------
467+
1 | t | gtest_tableoid
468+
2 | t | gtest_tableoid
466469
(2 rows)
467470

468471
-- drop column behavior

‎src/test/regress/sql/generated.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CREATE TABLE gtest_err_5a (a int PRIMARY KEY, b int DEFAULT 5 GENERATED ALWAYS A
2929
CREATETABLEgtest_err_5b (aintPRIMARY KEY, bint GENERATED ALWAYSAS identity GENERATED ALWAYSAS (a*2) STORED);
3030

3131
-- reference to system column not allowed in generated column
32+
-- (except tableoid, which we test below)
3233
CREATETABLEgtest_err_6a (aintPRIMARY KEY, b bool GENERATED ALWAYSAS (xmin<>37) STORED);
3334

3435
-- various prohibited constructs
@@ -218,9 +219,11 @@ DROP TYPE double_int;
218219
-- using tableoid is allowed
219220
CREATETABLEgtest_tableoid (
220221
aintPRIMARY KEY,
221-
b bool GENERATED ALWAYSAS (tableoid<>0) STORED
222+
b bool GENERATED ALWAYSAS (tableoid='gtest_tableoid'::regclass) STORED
222223
);
223224
INSERT INTO gtest_tableoidVALUES (1), (2);
225+
ALTERTABLE gtest_tableoid ADD COLUMN
226+
c regclass GENERATED ALWAYSAS (tableoid) STORED;
224227
SELECT*FROM gtest_tableoid;
225228

226229
-- drop column behavior

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp