[PATCH] Check domain constraints for NULLs in logical replication

Поиск
Список
Период
Сортировка
Искать

[PATCH] Check domain constraints for NULLs in logical replication

От:
mingjie.gmj@gmail.com
Дата:
Вложения:
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 ''
      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

Re: [PATCH] Check domain constraints for NULLs in logical replication

От:
vignesh C <vignesh21@gmail.com>
Дата:
On Tue, 17 Nov 2026 at 17:34,  wrote:
>
> 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 for the patch.

The issue is fixed only when the publisher sends values for the NOT
NULL domain columns. However, if the publisher does not have the
column and the subscriber needs to assign its default value, the NOT
NULL constraint is not validated.
Here is a test for the same:
--pub
CREATE TABLE tab_req (a int PRIMARY KEY);
INSERT INTO tab_req (a) VALUES (1)
CREATE PUBLICATION pub1 FOR TABLE tab_req;

--sub
CREATE DOMAIN required_int AS int NOT NULL;
CREATE TABLE tab_req (a int PRIMARY KEY, extra required_int);


--subscriber insert fails
INSERT INTO tab_req (a) VALUES (1)

--sub
CREATE SUBSCRIPTION orders_subscription
  CONNECTION ''
  PUBLICATION pub1 WITH (disable_on_error = true);

-- sub
postgres=# select * from tab_req ;
 a | extra
---+-------
 1 |
(1 row)

Should we call domain_check to see if the domain type does not allow
null values in these cases?

Regards,
Vignesh


FAQ