Add UPDATE/DELETE FOR PORTION OF

Peter Eisentraut <peter@eisentraut.org>

Commit: 8e72d914c52876525a90b28444453de8085c866f
Author: Peter Eisentraut <peter@eisentraut.org>
Date: 2026-04-01T17:06:03Z
Add UPDATE/DELETE FOR PORTION OF

This is an extension of the UPDATE and DELETE commands to do a
"temporal update/delete" based on a range or multirange column.  The
user can say UPDATE t FOR PORTION OF valid_at FROM '2001-01-01' TO
'2002-01-01' SET ... (or likewise with DELETE) where valid_at is a
range or multirange column.

The command is automatically limited to rows overlapping the targeted
portion, and only history within those bounds is changed.  If a row
represents history partly inside and partly outside the bounds, then
the command truncates the row's application time to fit within the
targeted portion, then it inserts one or more "temporal leftovers":
new rows containing all the original values, except with the
application-time column changed to only represent the untouched part
of history.

To compute the temporal leftovers that are required, we use the *_minus_multi
set-returning functions defined in 5eed8ce50c.

- Added bison support for FOR PORTION OF syntax.  The bounds must be
  constant, so we forbid column references, subqueries, etc. We do
  accept functions like NOW().
- Added logic to executor to insert new rows for the "temporal
  leftover" part of a record touched by a FOR PORTION OF query.
- Documented FOR PORTION OF.
- Added tests.

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

Files

PathChange+/−
contrib/postgres_fdw/expected/postgres_fdw.out modified +44 −1
contrib/postgres_fdw/sql/postgres_fdw.sql modified +34 −0
contrib/test_decoding/expected/ddl.out modified +52 −0
contrib/test_decoding/sql/ddl.sql modified +30 −0
doc/src/sgml/dml.sgml modified +135 −0
doc/src/sgml/glossary.sgml modified +15 −0
doc/src/sgml/images/Makefile modified +3 −1
doc/src/sgml/images/meson.build modified +2 −0
doc/src/sgml/images/temporal-delete.svg added +41 −0
doc/src/sgml/images/temporal-delete.txt added +10 −0
doc/src/sgml/images/temporal-update.svg added +45 −0
doc/src/sgml/images/temporal-update.txt added +10 −0
doc/src/sgml/ref/create_publication.sgml modified +6 −0
doc/src/sgml/ref/delete.sgml modified +106 −2
doc/src/sgml/ref/update.sgml modified +110 −2
doc/src/sgml/trigger.sgml modified +12 −0
src/backend/executor/execMain.c modified +1 −0
src/backend/executor/nodeModifyTable.c modified +347 −3
src/backend/nodes/nodeFuncs.c modified +33 −0
src/backend/optimizer/plan/createplan.c modified +4 −2
src/backend/optimizer/plan/planner.c modified +1 −0
src/backend/optimizer/util/pathnode.c modified +2 −1
src/backend/parser/analyze.c modified +354 −3
src/backend/parser/gram.y modified +105 −6
src/backend/parser/parse_agg.c modified +10 −0
src/backend/parser/parse_collate.c modified +1 −0
src/backend/parser/parse_expr.c modified +8 −0
src/backend/parser/parse_func.c modified +3 −0
src/backend/parser/parse_merge.c modified +1 −1
src/backend/rewrite/rewriteHandler.c modified +74 −1
src/backend/utils/adt/ruleutils.c modified +41 −0
src/backend/utils/cache/lsyscache.c modified +25 −0
src/include/nodes/execnodes.h modified +22 −0
src/include/nodes/parsenodes.h modified +21 −0
src/include/nodes/pathnodes.h modified +1 −0
src/include/nodes/plannodes.h modified +2 −0
src/include/nodes/primnodes.h modified +35 −0
src/include/optimizer/pathnode.h modified +1 −1
src/include/parser/analyze.h modified +2 −1
src/include/parser/kwlist.h modified +1 −0
src/include/parser/parse_node.h modified +1 −0
src/include/utils/lsyscache.h modified +1 −0
src/test/regress/expected/for_portion_of.out added +2100 −0
src/test/regress/expected/privileges.out modified +28 −0
src/test/regress/expected/updatable_views.out modified +32 −0
src/test/regress/expected/without_overlaps.out modified +202 −43
src/test/regress/parallel_schedule modified +1 −1
src/test/regress/sql/for_portion_of.sql added +1368 −0
src/test/regress/sql/privileges.sql modified +27 −0
src/test/regress/sql/updatable_views.sql modified +14 −0
src/test/regress/sql/without_overlaps.sql modified +107 −13
src/test/subscription/t/034_temporal.pl modified +94 −6
src/tools/pgindent/typedefs.list modified +3 −0

Discussion