[PATCH] Check domain constraints for NULLs in logical replication

From: mingjie(dot)gmj(at)gmail(dot)com
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] Check domain constraints for NULLs in logical replication
Date: 2026-11-17 12:04:38
Message-ID: CAL5_ZS1JZRxYbctLeRowbH9cLfB674caBAfBPxjgr=ZsxP1hPA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I found that logical replication can store a NULL in a subscriber
column whose domain is defined as NOT NULL. A regular UPDATE on the
subscriber rejects the same value.

I reproduced this on master at
11591f273e6a26fb55728b410538fa2627b19b28. Here is a small example with
two instances and wal_level=logical on the publisher.

On the publisher:

CREATE TABLE orders (id integer PRIMARY KEY, quantity integer);
INSERT INTO orders VALUES (1, 10);
CREATE PUBLICATION orders_publication FOR TABLE orders;

On the subscriber, replace the connection string and run:

CREATE DOMAIN required_int AS integer NOT NULL;
CREATE TABLE orders (id integer PRIMARY KEY, quantity required_int);
CREATE SUBSCRIPTION orders_subscription
CONNECTION '<publisher connection string>'
PUBLICATION orders_publication WITH (disable_on_error = true);

After the initial sync finishes, run this on the publisher:

UPDATE orders SET quantity = NULL WHERE id = 1;

The subscriber stores NULL and the subscription remains enabled. I
expected the apply worker to reject the value and disable the
subscription, leaving the original value unchanged.

It looks like the apply worker sets tts_isnull directly for incoming
NULLs, without calling the subscriber type's input function. Non-NULL
values go through input conversion, which checks the domain
constraints.

The attached patch passes explicit NULLs through the type input
function for rows being inserted or updated. It doesn't check NULL
placeholders in old replica identity key tuples, since those don't
represent values being written. This is needed for valid DELETEs and
key updates to work.

The patch also adds tests for domain NOT NULL and CHECK constraints in
text and binary modes, along with key updates, unchanged TOAST values
and subscriber-only defaults. The full subscription test suite passed
with the patch.

I'm not sure if this is the right way to fix it, or if there are cases
I've missed. I'd appreciate your thoughts on the approach. If it looks
reasonable, I can continue to improve the patch and add more tests.

Thanks,
mingjiegao

Attachment Content-Type Size
v1-0001-Check-domain-constraints-for-logical-replication-NULLs.patch application/octet-stream 13.6 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Matthew Mochalkin 2026-11-17 11:21:50 Re: libpq: SCRAM client proofs serialize across threads on OpenSSL 3 (re-init of the HMAC per PBKDF2 iteration)