Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4f10e7e

Browse files
alvherreitscaro
andcommitted
Set ActiveSnapshot when logically replaying inserts
Input functions for the inserted tuples may require a snapshot, when they are replayed by native logical replication. An example is a domain with a constraint using a SQL-language function, which prior to this commit failed to apply on the subscriber side. Reported-by: Mai Peng <maily.peng@webedia-group.com> Co-authored-by: Minh-Quan TRAN <qtran@itscaro.me> Co-authored-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/4EB4BD78-BFC3-4D04-B8DA-D53DF7160354@webedia-group.com Discussion: https://postgr.es/m/153211336163.1404.11721804383024050689@wrigleys.postgresql.org
1 parent 70de0ab commit 4f10e7e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/backend/replication/logical/worker.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,15 @@ apply_handle_insert(StringInfo s)
610610
remoteslot = ExecInitExtraTupleSlot(estate,
611611
RelationGetDescr(rel->localrel));
612612

613+
/* Input functions may need an active snapshot, so get one */
614+
PushActiveSnapshot(GetTransactionSnapshot());
615+
613616
/* Process and store remote tuple in the slot */
614617
oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
615618
slot_store_cstrings(remoteslot, rel, newtup.values);
616619
slot_fill_defaults(rel, estate, remoteslot);
617620
MemoryContextSwitchTo(oldctx);
618621

619-
PushActiveSnapshot(GetTransactionSnapshot());
620622
ExecOpenIndices(estate->es_result_relation_info, false);
621623

622624
/* Do the insert. */

src/test/subscription/t/002_types.pl

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use warnings;
55
use PostgresNode;
66
use TestLib;
7-
use Test::More tests => 3;
7+
use Test::More tests => 4;
88

99
# Initialize publisher node
1010
my $node_publisher = get_new_node('publisher');
@@ -90,7 +90,13 @@
9090
CREATE TABLE public.tst_hstore (
9191
a INTEGER PRIMARY KEY,
9292
b public.hstore
93-
););
93+
);
94+
95+
SET check_function_bodies=off;
96+
CREATE FUNCTION public.monot_incr(int) RETURNS bool LANGUAGE sql
97+
AS ' select \$1 > max(a) from public.tst_dom_constr; ';
98+
CREATE DOMAIN monot_int AS int CHECK (monot_incr(VALUE));
99+
CREATE TABLE public.tst_dom_constr (a monot_int););
94100

95101
# Setup structure on both nodes
96102
$node_publisher->safe_psql('postgres', $ddl);
@@ -240,6 +246,9 @@
240246
(2, '"zzz"=>"foo"'),
241247
(3, '"123"=>"321"'),
242248
(4, '"yellow horse"=>"moaned"');
249+
250+
-- tst_dom_constr
251+
INSERT INTO tst_dom_constr VALUES (10);
243252
));
244253

245254
$node_publisher->wait_for_catchup($appname);
@@ -541,5 +550,16 @@
541550
4|"yellow horse"=>"moaned"',
542551
'check replicated deletes on subscriber');
543552

553+
# Test a domain with a constraint backed by a SQL-language function,
554+
# which needs an active snapshot in order to operate.
555+
$node_publisher->safe_psql('postgres', "INSERT INTO tst_dom_constr VALUES (11)");
556+
557+
$node_subscriber->poll_query_until('postgres', $synced_query)
558+
or die "Timed out while waiting for subscriber to synchronize data";
559+
560+
$result =
561+
$node_subscriber->safe_psql('postgres', "SELECT sum(a) FROM tst_dom_constr");
562+
is($result, '21', 'sql-function constraint on domain');
563+
544564
$node_subscriber->stop('fast');
545565
$node_publisher->stop('fast');

0 commit comments

Comments
 (0)