postgresql/src/backend/nodes
Peter Eisentraut 8e72d914c5 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
2026-04-01 19:06:03 +02:00
..
.gitignore Automatically generate node support functions 2022-07-09 08:53:59 +02:00
bitmapset.c Make use of pg_popcount() in more places. 2026-02-23 09:26:00 -06:00
copyfuncs.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
equalfuncs.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
extensible.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
gen_node_support.pl Remove bits* typedefs. 2026-03-30 16:12:08 -05:00
list.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
Makefile Move CompareType to separate header file 2025-02-02 08:11:57 +01:00
makefuncs.c Allow IS JSON predicate to work with domain types 2026-03-17 15:20:22 -04:00
meson.build Update copyright for 2026 2026-01-01 13:24:10 -05:00
multibitmapset.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
nodeFuncs.c Add UPDATE/DELETE FOR PORTION OF 2026-04-01 19:06:03 +02:00
outfuncs.c SQL Property Graph Queries (SQL/PGQ) 2026-03-16 10:14:18 +01:00
params.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
print.c SQL Property Graph Queries (SQL/PGQ) 2026-03-16 10:14:18 +01:00
queryjumblefuncs.c Fix typos and inconsistencies in code and comments 2026-01-05 09:19:15 +09:00
read.c Update copyright for 2026 2026-01-01 13:24:10 -05:00
readfuncs.c SQL Property Graph Queries (SQL/PGQ) 2026-03-16 10:14:18 +01:00
README Convert node test compile-time settings into run-time parameters 2024-08-01 10:09:18 +02:00
tidbitmap.c Fix accidentally cast away qualifiers 2026-01-26 16:02:31 +01:00
value.c Update copyright for 2026 2026-01-01 13:24:10 -05:00

src/backend/nodes/README

Node Structures
===============

Introduction
------------

Postgres uses "node" types to organize parse trees, plan trees, and
executor state trees.  All objects that can appear in such trees must
be declared as node types.  In addition, a few object types that aren't
part of parse/plan/execute node trees receive NodeTags anyway for
identification purposes, usually because they are involved in APIs
where we want to pass multiple object types through the same pointer.

The node structures are plain old C structures with the first field
being of type NodeTag.  "Inheritance" is achieved by convention:
the first field can alternatively be of another node type.

Node types typically have support for being copied by copyObject(),
compared by equal(), serialized by outNode(), and deserialized by
nodeRead().  For some classes of Nodes, not all of these support
functions are required; for example, executor state nodes don't
presently need any of them.  So far as the system is concerned,
output and read functions are only needed for node types that can
appear in parse trees stored in the catalogs, and for plan tree
nodes because those are serialized to be passed to parallel workers.
However, we provide output functions for some other node types as well,
because they are very handy for debugging.  Currently, such coverage
exists for raw parsetrees and most planner data structures.  However,
output coverage of raw parsetrees is incomplete: in particular, utility
statements are almost entirely unsupported.

Relevant Files
--------------

Utility functions for manipulating node structures reside in this
directory.  Some support functions are automatically generated by the
gen_node_support.pl script, other functions are maintained manually.
To control the automatic generation of support functions, node types
and node fields can be annotated with pg_node_attr() specifications;
see further documentation in src/include/nodes/nodes.h.


FILES IN THIS DIRECTORY (src/backend/nodes/)

    General-purpose node manipulation functions:
	copyfuncs.c	- copy a node tree (*)
	equalfuncs.c	- compare two node trees (*)
	outfuncs.c	- convert a node tree to text representation (*)
	readfuncs.c	- convert text representation back to a node tree (*)
	makefuncs.c	- creator functions for some common node types
	nodeFuncs.c	- some other general-purpose manipulation functions
	queryjumblefuncs.c - compute a node tree for query jumbling (*)

    (*) - Most functions in these files are generated by
    gen_node_support.pl and #include'd there.

    Specialized manipulation functions:
	bitmapset.c	- Bitmapset support
	list.c		- generic list support
	multibitmapset.c - List-of-Bitmapset support
	params.c	- Param support
	tidbitmap.c	- TIDBitmap support
	value.c		- support for value nodes

FILES IN src/include/nodes/

    Node definitions primarily appear in:
	nodes.h		- define node tags (NodeTag) (*)
	primnodes.h	- primitive nodes
	parsenodes.h	- parse tree nodes
	pathnodes.h	- path tree nodes and planner internal structures
	plannodes.h	- plan tree nodes
	execnodes.h	- executor nodes
	memnodes.h	- memory nodes
	pg_list.h	- generic list

    (*) - Also #include's files generated by gen_node_support.pl.


Steps to Add a Node
-------------------

Suppose you want to define a node Foo:

1. Add the structure definition to the appropriate include/nodes/???.h file.
   If you intend to inherit from, say a Plan node, put Plan as the first field
   of your struct definition.  (The T_Foo tag is created automatically.)
2. Check that the generated support functions in copyfuncs.funcs.c,
   equalfuncs.funcs.c, outfuncs.funcs.c, queryjumblefuncs.funcs.c and
   readfuncs.funcs.c look correct.  Add attributes as necessary to control the
   outcome.  (For some classes of node types, you don't need all the support
   functions.  Use node attributes similar to those of related node types.)
3. Add cases to the functions in nodeFuncs.c as needed.  There are many
   other places you'll probably also need to teach about your new node
   type.  Best bet is to grep for references to one or two similar existing
   node types to find all the places to touch.
   (Except for frequently-created nodes, don't bother writing a creator
   function in makefuncs.c.)
4. Consider testing your new code with debug_copy_parse_plan_trees,
   debug_write_read_parse_plan_trees, and
   debug_raw_expression_coverage_test to ensure support has been added
   everywhere that it's necessary (e.g., run the tests with
   PG_TEST_INITDB_EXTRA_OPTS='-c debug_...=on').

Adding a new node type moves the numbers associated with existing
tags, so you'll need to recompile the whole tree after doing this.
(--enable-depend usually helps.)  It doesn't force initdb though,
because the numbers never go to disk.  But altering or removing a node
type should usually be accompanied by an initdb-forcing catalog
version change, since the interpretation of serialized node trees
stored in system catalogs is affected by that.  (If the node type
never appears in stored parse trees, as for example Plan nodes do not,
then a catversion change is not needed to change it.)