BUG #19753: PERIOD foreign key rejects an empty referencing range although the coverage test returns true

Started by PG Bug reporting formabout 2 months ago1 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19753
Logged by: N J
Email address: 1482694023@qq.com
PostgreSQL version: 18.6
Operating system: Windows 11 64-bit
Description:

PostgreSQL version: 18.6
Client: pgAdmin 4
Operating system: Windows 11 64-bit

Steps to reproduce:

CREATE EXTENSION IF NOT EXISTS btree_gist;

CREATE TEMP TABLE parent_period (
k integer NOT NULL,
p int4range NOT NULL,
PRIMARY KEY (k, p WITHOUT OVERLAPS)
);

INSERT INTO parent_period VALUES (1, '[0,10)'::int4range);

CREATE TEMP TABLE child_period (
k integer NOT NULL,
p int4range NOT NULL,
FOREIGN KEY (k, PERIOD p)
REFERENCES parent_period (k, PERIOD p)
);

SELECT range_agg(p) @> 'empty'::int4range AS covered
FROM parent_period
WHERE k = 1;

The SELECT returns true.

Then run:

INSERT INTO child_period VALUES (1, 'empty'::int4range);

Actual result:
The INSERT fails with SQLSTATE 23503 (foreign_key_violation):
Key (k, p)=(1, empty) is not present in table "parent_period".

Expected result:
I expected the INSERT to succeed because the referencing period is empty and
the coverage query returns true. This expectation follows the documented
PERIOD foreign-key coverage rule and the set-containment behavior of empty
ranges.

The behavior appears inconsistent between the coverage query and PERIOD
foreign-key enforcement. I have reproduced it on this installation, but have
not established the underlying cause.