pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: anncalla@163.com
Subject: BUG #19770: CREATE TABLE (LIKE ...) PARTITION BY copies a NO INHERIT not-null constraint; NULLs then go into par
Date: Sun, 18 Oct 2026 13:58:03 +0000
Message-ID: <19770-7c6e75fc30df7e06@postgresql.org> (raw)

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








view thread (2+ messages)  latest in thread

Message-ID: <19770-7c6e75fc30df7e06@postgresql.org>
Permalink:  ../19770-7c6e75fc30df7e06@postgresql.org/
Also on:    postgresql.org/message-id/19770-7c6e75fc30df7e06@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, anncalla@163.com
  Subject: Re: BUG #19770: CREATE TABLE (LIKE ...) PARTITION BY copies a NO INHERIT not-null constraint; NULLs then go into par
  In-Reply-To: <19770-7c6e75fc30df7e06@postgresql.org>
  References: <19770-7c6e75fc30df7e06@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox