| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | anncalla(at)163(dot)com |
| Subject: | BUG #19770: CREATE TABLE (LIKE ...) PARTITION BY copies a NO INHERIT not-null constraint; NULLs then go into par |
| Date: | 2026-10-18 13:58:03 |
| Message-ID: | 19770-7c6e75fc30df7e06@postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
The following bug has been logged on the website:
Bug reference: 19770
Logged by: Ann C
Email address: anncalla(at)163(dot)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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | PG Bug reporting form | 2026-10-18 20:40:57 | BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d) |
| Previous Message | PG Bug reporting form | 2026-10-18 11:59:47 | BUG #19769: int8range() raises an error for an inclusive upper bound at BIGINT_MAX |