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

Commit3f244d0

Browse files
committed
Make new GENERATED-expressions code more bulletproof.
In commit8bf6ec3 I assumed that no code path could reachExecGetExtraUpdatedCols without having gone throughExecInitStoredGenerated. That turns out not to be the case inlogical replication: if there's an ON UPDATE trigger on the targettable, trigger.c will call this code before anybody has set up itsgenerated columns. Having seen that, I don't have a lot of faith inthere not being other such paths. ExecGetExtraUpdatedCols can callExecInitStoredGenerated for itself, as long as we are willing toassume that it is only called in CMD_UPDATE operations, which onthe whole seems like a safer leap of faith.Per report from Vitaly Davydov.Discussion:https://postgr.es/m/d259d69652b8c2ff50e14cda3c236c7f@postgrespro.ru
1 parent1334b79 commit3f244d0

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

‎src/backend/executor/execUtils.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include"access/transam.h"
5353
#include"executor/executor.h"
5454
#include"executor/execPartition.h"
55+
#include"executor/nodeModifyTable.h"
5556
#include"jit/jit.h"
5657
#include"mb/pg_wchar.h"
5758
#include"miscadmin.h"
@@ -1342,15 +1343,9 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
13421343
Bitmapset*
13431344
ExecGetExtraUpdatedCols(ResultRelInfo*relinfo,EState*estate)
13441345
{
1345-
#ifdefUSE_ASSERT_CHECKING
1346-
/* Verify that ExecInitStoredGenerated has been called if needed. */
1347-
Relationrel=relinfo->ri_RelationDesc;
1348-
TupleDesctupdesc=RelationGetDescr(rel);
1349-
1350-
if (tupdesc->constr&&tupdesc->constr->has_generated_stored)
1351-
Assert(relinfo->ri_GeneratedExprs!=NULL);
1352-
#endif
1353-
1346+
/* In some code paths we can reach here before initializing the info */
1347+
if (relinfo->ri_GeneratedExprs==NULL)
1348+
ExecInitStoredGenerated(relinfo,estate,CMD_UPDATE);
13541349
returnrelinfo->ri_extraUpdatedCols;
13551350
}
13561351

‎src/backend/executor/nodeModifyTable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ ExecCheckTIDVisible(EState *estate,
360360
* fields. (Currently, ri_extraUpdatedCols is consulted only in UPDATE,
361361
* but we might as well fill it for INSERT too.)
362362
*/
363-
staticvoid
363+
void
364364
ExecInitStoredGenerated(ResultRelInfo*resultRelInfo,
365365
EState*estate,
366366
CmdTypecmdtype)

‎src/include/executor/nodeModifyTable.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include"nodes/execnodes.h"
1717

18+
externvoidExecInitStoredGenerated(ResultRelInfo*resultRelInfo,
19+
EState*estate,
20+
CmdTypecmdtype);
21+
1822
externvoidExecComputeStoredGenerated(ResultRelInfo*resultRelInfo,
1923
EState*estate,TupleTableSlot*slot,
2024
CmdTypecmdtype);

‎src/test/subscription/t/011_generated.pl

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
);
2626

2727
$node_subscriber->safe_psql('postgres',
28-
"CREATE TABLE tab1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 22) STORED)"
28+
"CREATE TABLE tab1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 22) STORED, c int)"
2929
);
3030

3131
# data for initial sync
@@ -55,11 +55,45 @@
5555

5656
$node_publisher->wait_for_catchup('sub1');
5757

58-
$result =$node_subscriber->safe_psql('postgres',"SELECT a, b FROM tab1");
59-
is($result,qq(1|22
60-
2|44
61-
3|66
62-
4|88
63-
6|132),'generated columns replicated');
58+
$result =$node_subscriber->safe_psql('postgres',"SELECT * FROM tab1");
59+
is($result,qq(1|22|
60+
2|44|
61+
3|66|
62+
4|88|
63+
6|132|),'generated columns replicated');
64+
65+
# try it with a subscriber-side trigger
66+
67+
$node_subscriber->safe_psql(
68+
'postgres',q{
69+
CREATE FUNCTION tab1_trigger_func() RETURNS trigger
70+
LANGUAGE plpgsql AS $$
71+
BEGIN
72+
NEW.c := NEW.a + 10;
73+
RETURN NEW;
74+
END $$;
75+
76+
CREATE TRIGGER test1 BEFORE INSERT OR UPDATE ON tab1
77+
FOR EACH ROW
78+
EXECUTE PROCEDURE tab1_trigger_func();
79+
80+
ALTER TABLE tab1 ENABLE REPLICA TRIGGER test1;
81+
});
82+
83+
$node_publisher->safe_psql('postgres',"INSERT INTO tab1 VALUES (7), (8)");
84+
85+
$node_publisher->safe_psql('postgres',"UPDATE tab1 SET a = 9 WHERE a = 7");
86+
87+
$node_publisher->wait_for_catchup('sub1');
88+
89+
$result =
90+
$node_subscriber->safe_psql('postgres',"SELECT * FROM tab1 ORDER BY 1");
91+
is($result,qq(1|22|
92+
2|44|
93+
3|66|
94+
4|88|
95+
6|132|
96+
8|176|18
97+
9|198|19),'generated columns replicated with trigger');
6498

6599
done_testing();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp