pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter@eisentraut.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Add range_minus_multi and multirange_minus_multi functions
Date: Sat, 22 Nov 2025 08:48:00 +0000
Message-ID: <E1vMjHj-000mYP-1s@gemulon.postgresql.org> (raw)

Add range_minus_multi and multirange_minus_multi functions

The existing range_minus function raises an exception when the range is
"split", because then the result can't be represented by a single range.
For example '[0,10)'::int4range - '[4,5)' would be '[0,4)' and '[5,10)'.

This commit adds new set-returning functions so that callers can get
results even in the case of splits. There is no risk of an exception for
multiranges, but a set-returning function lets us handle them the same
way we handle ranges.

Both functions return zero results if the subtraction would give an
empty range/multirange.

The main use-case for these functions is to implement UPDATE/DELETE FOR
PORTION OF, which must compute the application-time of "temporal
leftovers": the part of history in an updated/deleted row that was not
changed. To preserve the untouched history, we will implicitly insert
one record for each result returned by range/multirange_minus_multi.
Using a set-returning function will also let us support user-defined
types for application-time update/delete in the future.

Author: Paul A. Jungwirth <pj@illuminatedcomputing.com>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/ec498c3d-5f2b-48ec-b989-5561c8aa2024%40illuminatedcomputi...

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/5eed8ce50ce9df1067b95593dde9f4fc526dfc72

Modified Files
--------------
doc/src/sgml/func/func-range.sgml             |  42 +++++++
src/backend/utils/adt/multirangetypes.c       |  71 +++++++++++
src/backend/utils/adt/rangetypes.c            | 167 ++++++++++++++++++++++++++
src/include/catalog/catversion.h              |   2 +-
src/include/catalog/pg_proc.dat               |   8 ++
src/include/utils/rangetypes.h                |   2 +
src/test/regress/expected/multirangetypes.out | 116 ++++++++++++++++++
src/test/regress/expected/rangetypes.out      |  54 +++++++++
src/test/regress/sql/multirangetypes.sql      |  22 ++++
src/test/regress/sql/rangetypes.sql           |  10 ++
10 files changed, 493 insertions(+), 1 deletion(-)



Message-ID: <E1vMjHj-000mYP-1s@gemulon.postgresql.org>
Permalink:  ../E1vMjHj-000mYP-1s@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1vMjHj-000mYP-1s@gemulon.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 using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: peter@eisentraut.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Add range_minus_multi and multirange_minus_multi functions
  In-Reply-To: <E1vMjHj-000mYP-1s@gemulon.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