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

Commita8b88c2

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 parent547e60b commita8b88c2

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

‎src/backend/executor/execUtils.c

Lines changed: 4 additions & 2 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"
@@ -1332,8 +1333,9 @@ ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate)
13321333
{
13331334
ListCell*lc;
13341335

1335-
/* Assert that ExecInitStoredGenerated has been called. */
1336-
Assert(relinfo->ri_GeneratedExprs!=NULL);
1336+
/* In some code paths we can reach here before initializing the info */
1337+
if (relinfo->ri_GeneratedExprs==NULL)
1338+
ExecInitStoredGenerated(relinfo,estate,CMD_UPDATE);
13371339
foreach(lc,estate->es_resultrelinfo_extra)
13381340
{
13391341
ResultRelInfoExtra*rextra= (ResultRelInfoExtra*)lfirst(lc);

‎src/backend/executor/nodeModifyTable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ ExecCheckTIDVisible(EState *estate,
260260
* (Currently, ri_extraUpdatedCols is consulted only in UPDATE, but we might
261261
* as well fill it for INSERT too.)
262262
*/
263-
staticvoid
263+
void
264264
ExecInitStoredGenerated(ResultRelInfo*resultRelInfo,
265265
EState*estate,
266266
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: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use warnings;
77
use PostgresNode;
88
use TestLib;
9-
use Test::Moretests=>2;
9+
use Test::Moretests=>3;
1010

1111
# setup
1212

@@ -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,9 +55,43 @@
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');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp