BUG #19777: PERIOD foreign key rejects an empty referencing range even though the referenced period covers it

Started by PG Bug reporting formabout 1 month ago2 messagesbugs

Thread notes

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

@global #bug #not-a-bug

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

The following bug has been logged on the website:

Bug reference: 19777
Logged by: N J
Email address: 1482694023@qq.com
PostgreSQL version: 18.6
Operating system: Windows 11 x64
Description:

Tested with PostgreSQL 18.6 on Windows 11 x64. No extensions or non-default
configuration are required.

On a fresh connection, run the setup and check:

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

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

CREATE TEMP TABLE period_child_minimal (
k int4range NOT NULL,
p int4range NOT NULL,
FOREIGN KEY (k, PERIOD p)
REFERENCES period_parent_minimal (k, PERIOD p)
);

SELECT range_agg(p) @> 'empty'::int4range AS parent_covers_empty
FROM period_parent_minimal
WHERE k = '[1,2)'::int4range;

The SELECT returns true. Then run:

INSERT INTO period_child_minimal
VALUES ('[1,2)'::int4range, 'empty'::int4range);

Actual result: rejected with SQLSTATE 23503 (foreign key violation).

Expected result: the insert should succeed if an empty referencing period is
considered covered by the referenced period, consistent with range
containment and the documented PERIOD foreign-key coverage rule. Please
confirm whether empty referencing periods are intended to satisfy PERIOD
foreign keys.

#2Takeshi Ideriha
iderihatakeshi@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19777: PERIOD foreign key rejects an empty referencing range even though the referenced period covers it

Hi,

On 2026/10/20 18:14, PG Bug reporting form wrote:

Expected result: the insert should succeed if an empty referencing period is
considered covered by the referenced period, consistent with range
containment and the documented PERIOD foreign-key coverage rule. Please
confirm whether empty referencing periods are intended to satisfy PERIOD
foreign keys.

In my opinion, this result is expected because empty is treated
differently by range type and by WITHOUT OVERLAPS constraint.

SELECT range_agg(p) @> 'empty'::int4range AS parent_covers_empty
FROM period_parent_minimal
WHERE k = '[1,2)'::int4range;

The SELECT returns true. Then run:

This is considered to be the same as "select '[0,10)'::int4range @>
'empty';", which returns true since 'empty' range is contained in every
range [1]https://www.postgresql.org/docs/18/functions-range.html -- Every range contains the empty range. --.

[1]: https://www.postgresql.org/docs/18/functions-range.html -- Every range contains the empty range. --
--
Every range contains the empty range.
--

INSERT INTO period_child_minimal
VALUES ('[1,2)'::int4range, 'empty'::int4range);

Actual result: rejected with SQLSTATE 23503 (foreign key violation).

However, the INSERT fails. The point is a WITHOUT OVERLAPS column does
not permit empty range [2]https://www.postgresql.org/docs/18/sql-createtable.html -- The `WITHOUT OVERLAPS` column must have a range or multirange type. Empty ranges/multiranges are not permitted. --. The referenced table does not permit empty
range so referencing table also cannot accept it.

[2]: https://www.postgresql.org/docs/18/sql-createtable.html -- The `WITHOUT OVERLAPS` column must have a range or multirange type. Empty ranges/multiranges are not permitted. --
--
The `WITHOUT OVERLAPS` column must have a range or multirange type.
Empty ranges/multiranges are not permitted.
--

Regards,
Takeshi Ideriha