The logical replication subscription side does not fire per-column
update triggers when applying an update change. (Per-table update
triggers work fine.) This patch fixes that. Should be backpatched to PG10.
A patch along these lines is also necessary to handle triggers involving
generated columns in the apply worker. I'll work on that separately.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 94249746b2b633a1ece333344527060d0de18dc1 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Fri, 13 Dec 2019 14:23:25 +0100
Subject: [PATCH] Have logical replication subscriber fire column triggers
The logical replication apply worker did not fire per-column update
triggers because the updatedCols bitmap in the RTE was not populated.
This fixes that.
---
src/backend/replication/logical/worker.c | 10 ++++++++++
src/test/subscription/t/003_constraints.pl | 21 +++++++++++++++++----
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/src/backend/replication/logical/worker.c
b/src/backend/replication/logical/worker.c
index ced0d191c2..08b71ddfcb 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -720,6 +720,16 @@ apply_handle_update(StringInfo s)
&estate->es_tupleTable);
EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1);
+ /* Populate updatedCols for trigger manager */
+ for (int i = 0; i < remoteslot->tts_tupleDescriptor->natts; i++)
+ {
+ RangeTblEntry *target_rte = list_nth(estate->es_range_table, 0);
+
+ if (newtup.changed)
+ target_rte->updatedCols =
bms_add_member(target_rte->updatedCols,
+
i + 1 - FirstLowInvalidHeapAttributeNumber);
+ }
+
PushActiveSnapshot(GetTransactionSnapshot());
ExecOpenIndices(estate->es_result_relation_info, false);
diff --git a/src/test/subscription/t/003_constraints.pl
b/src/test/subscription/t/003_constraints.pl
index 81547f65fa..daad79cf48 100644
--- a/src/test/subscription/t/003_constraints.pl
+++ b/src/test/subscription/t/003_constraints.pl
@@ -3,7 +3,7 @@
use warnings;
use PostgresNode;
use TestLib;
-use Test::More tests => 4;
+use Test::More tests => 5;
# Initialize publisher node
my $node_publisher = get_new_node('publisher');
@@ -81,6 +81,8 @@ BEGIN
ELSE
RETURN NULL;
END IF;
+ ELSIF (TG_OP = 'UPDATE') THEN
+ RETURN NULL;
ELSE
RAISE WARNING 'Unknown action';
RETURN NULL;
@@ -88,7 +90,7 @@ BEGIN
END;
\$\$ LANGUAGE plpgsql;
CREATE TRIGGER filter_basic_dml_trg
- BEFORE INSERT ON tab_fk_ref
+ BEFORE INSERT OR UPDATE OF bid ON tab_fk_ref
FOR EACH ROW EXECUTE PROCEDURE filter_basic_dml_fn();
ALTER TABLE tab_fk_ref ENABLE REPLICA TRIGGER filter_basic_dml_trg;
});
@@ -99,10 +101,21 @@ BEGIN
$node_publisher->wait_for_catchup('tap_sub');
-# The row should be skipped on subscriber
+# The trigger should cause the insert to be skipped on subscriber
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;");
+is($result, qq(2|1|2), 'check replica insert trigger applied on subscriber');
+
+# Update data
+$node_publisher->safe_psql('postgres',
+ "UPDATE tab_fk_ref SET bid = 2 WHERE bid = 1;");
+
+$node_publisher->wait_for_catchup('tap_sub');
+
+# The trigger should cause the update to be skipped on subscriber
$result = $node_subscriber->safe_psql('postgres',
"SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;");
-is($result, qq(2|1|2), 'check replica trigger applied on subscriber');
+is($result, qq(2|1|2), 'check replica update column trigger applied on
subscriber');
$node_subscriber->stop('fast');
$node_publisher->stop('fast');
--
2.24.0