Logical replication failing when foreign key present

Started by Thom Brownover 9 years ago3 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrytests failedCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t35898
psql -h localhost -U postgres

Built from patchset v2 (message #2), July 28, 2026 at 08:37 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t35898_2 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t35898_2 && git checkout t35898_2

Patchset v2 (message #2) is on t35898_2

Jump to latest
#1Thom Brown
thom@linux.com

Hi,

There's an issue which I haven't seen documented as expected
behaviour, where replicating data to a table which has a foreign key
results in a replication failure. This produces the following log
entries:

LOG: starting logical replication worker for subscription "contacts_sub"
LOG: logical replication apply for subscription "contacts_sub" has started
ERROR: AfterTriggerSaveEvent() called outside of query
LOG: worker process: logical replication worker for subscription
16408 (PID 19201) exited with exit code 1

Reproducible test case:

On both instances:

CREATE TABLE b (bid int PRIMARY KEY);
CREATE TABLE a (id int PRIMARY KEY, bid int REFERENCES b (bid));
INSERT INTO b (bid) VALUES (1);

First instance:
CREATE PUBLICATION a_pub FOR TABLE a;

Second instance:
CREATE SUBSCRIPTION a_sub CONNECTION 'host=127.0.0.1 port=5530
user=thom dbname=postgres' PUBLICATION a_pub;

First instance:
INSERT INTO a (id, bid) VALUES (1,1);

Regards

Thom

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#2Petr Jelinek
petr@2ndquadrant.com
In reply to: Thom Brown (#1)
Re: Logical replication failing when foreign key present

On 22/01/17 18:50, Thom Brown wrote:

Hi,

There's an issue which I haven't seen documented as expected
behaviour, where replicating data to a table which has a foreign key
results in a replication failure. This produces the following log
entries:

LOG: starting logical replication worker for subscription "contacts_sub"
LOG: logical replication apply for subscription "contacts_sub" has started
ERROR: AfterTriggerSaveEvent() called outside of query
LOG: worker process: logical replication worker for subscription
16408 (PID 19201) exited with exit code 1

Hi, thanks for report.

Looks like I missed AfterTriggerBeginQuery/AfterTriggerEndQuery when
moving the executor stuff around. Attached should fix it.

--
Petr Jelinek http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Attachments:

t35898_2
0001-Fix-after-trigger-execution-in-logical-replication.patchapplication/x-patch; name=0001-Fix-after-trigger-execution-in-logical-replication.patchDownload+15-1
#3Peter Eisentraut
peter_e@gmx.net
In reply to: Petr Jelinek (#2)
Re: Logical replication failing when foreign key present

On 1/22/17 18:07, Petr Jelinek wrote:

On 22/01/17 18:50, Thom Brown wrote:

There's an issue which I haven't seen documented as expected
behaviour, where replicating data to a table which has a foreign key
results in a replication failure. This produces the following log
entries:

Hi, thanks for report.

Looks like I missed AfterTriggerBeginQuery/AfterTriggerEndQuery when
moving the executor stuff around. Attached should fix it.

This has been fixed.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers