BUG #19770: CREATE TABLE (LIKE ...) PARTITION BY copies a NO INHERIT not-null constraint; NULLs then go into par

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

BUG #19770: CREATE TABLE (LIKE ...) PARTITION BY copies a NO INHERIT not-null constraint; NULLs then go into par

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
The following bug has been logged on the website:

Bug reference:      19770
Logged by:          Ann C
Email address:      anncalla@163.com
PostgreSQL version: 18.6
Operating system:   Ubuntu 24.04 x86_64
Description:        

A not-null constraint marked NO INHERIT is rejected on a partitioned table
when it is declared directly:

  ERROR:  not-null constraints on partitioned tables cannot be NO INHERIT

But CREATE TABLE pp (LIKE p) PARTITION BY ... copies such a constraint from
p without an error. pg_constraint then has the constraint on pp with
connoinherit = true. The partitions do not get a not-null constraint, so
INSERT INTO pp VALUES (NULL) succeeds and the row is stored in the default
partition.

Queries on the parent are planned with c0 known to be NOT NULL, so "WHERE c0
IS NULL" becomes a constant false and returns 0 rows, although SELECT
count(*) FROM pp returns the NULL row and the partition itself finds it.

Expected: CREATE TABLE ... (LIKE p) PARTITION BY should fail with the same
error as the direct declaration (or copy the constraint without NO INHERIT).

Reproduced on 18.6 and 19beta4 (official Docker images). The ALTER
CONSTRAINT ... NO INHERIT path to the same state was fixed on 2026-05-22
("Prevent setting NO INHERIT on partitioned NOT NULL constraints"); this is
another path.

CREATE TABLE p (c0 int, CONSTRAINT p_nn NOT NULL c0 NO INHERIT);

CREATE TABLE q (c0 int NOT NULL NO INHERIT) PARTITION BY RANGE (c0);
-- ERROR:  not-null constraints on partitioned tables cannot be NO INHERIT

CREATE TABLE pp (LIKE p) PARTITION BY RANGE (c0);
-- CREATE TABLE (no error)

SELECT conname, conrelid::regclass, contype, connoinherit
FROM pg_constraint WHERE conrelid = 'pp'::regclass;
--  p_nn | pp | n | t

CREATE TABLE pp_def PARTITION OF pp DEFAULT;   -- column c0 is nullable in
pp_def
INSERT INTO pp VALUES (NULL);                  -- INSERT 0 1

SELECT count(*) FROM pp;                       -- 1
SELECT count(*) FROM pp WHERE c0 IS NULL;      -- 0, expected 1
SELECT count(*) FROM pp_def WHERE c0 IS NULL;  -- 1

EXPLAIN (COSTS OFF) SELECT count(*) FROM pp WHERE c0 IS NULL;
--  Aggregate
--    ->  Result
--          One-Time Filter: false




Re: BUG #19770: CREATE TABLE (LIKE ...) PARTITION BY copies a NO INHERIT not-null constraint; NULLs then go into par

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Hi,

Thanks for the report. I have proposed a fix to this in
pgsql-hackers[1]. Please take a look.

Regards,
Nitin Motiani
Google


[1] https://www.postgresql.org/message-id/CAH5HC96YCzOy+9JZLQ4n0sMchVnharTOXrr4sbYuh=_d+ffjAA@mail.gmail.com


FAQ