citus/src/backend/distributed/deparser
Colm McHugh 5f06ba26a8 PG18: Add support for virtual generated columns.
Generated columns can be virtual (not stored) and this is the default.
This PG18 feature requires tweaking citus_ruleutils and deparse table
to support in Citus. Relevant PG commit: 83ea6c540.

Also ensure that:
1) Cannot distribute on a GENERATED .. VIRTUAL column
2) undistribute_table() and alter_*_table UDFs handle
   GENERATED .. VIRTUAL in addition to STORED.
3) Citus COPY implementation takes GENERATED .. VIRTUAL
   columns into account.

Utility function `IsDroppedOrGenerated()` is used by the above to
detect generated columns; its agnostic to whether or not the column
is stored or virtual so does not need to be PG18-specific. Function
`DistributionColumnIsGeneratedCheck()` however has PG18-specific code
mainly because the error message thrown when a Citus client invokes
`create_distributed_table()` on a generated column reports whether
the column is stored or virtual.

The commit alson includes a fix for an 'unrecognized relation id'
error in GROUP BY on table with virtual column, which cropped up
in testing. The query:
```
   SELECT count(1), device_id FROM v_reading GROUP BY device_id
```
errored out with 'Unrecognized relid 2' when v_reading had a virtual
column, but ran fine if the column was stored. It turns out to be
because of Postgres commit 1e4351a "Expand virtual generated columns
in the planner", which fixed an issue with virtual cols (83ea6c5).
The fix involved constructing a new Query object and applying preprocessing
to that, with the consequence that changes made by the Postgres planner
to the Query are not available to the caller. One such change is
expanding of references to the GROUP BY expressions; they are not
reflected back when the table has at least one virtual column.

The broader implication for Citus is that after the distributed_planner()
hook has called Postgres' standard_planner(), it may not be aware of
modifications made to the Query. Citus tracks both the query passed
into its planner hook and the query given to the Postgres planner; the latter
may undergo transformations that Citus needs to be aware of in subsequent
distributed planning, for example expanding of GROUP BY expressions.
To resolve this, we enable Citus's planner hooks to access the distributed
planning context through the restriction context, and change the query field
if it no longer refers to the same query tree being used by the Postgres
planner. This is implanted as follows:

* Planner restriction context has a new field that refers to its distributed
  planner context
* Citus `distributed_planner()` hook initializes this when pushing a new
  restriction context
* Citus `multi_relation_restriction_hook()` checks if the distributed context
  query is no longer being used by the Postgres planner; this is only done at
  the outermost query level to stay in sync with Citus `distributed_planner()`
* Citus `distributed_planner()` hook clears the distributed planner context
  reference immediately after calling `standard_planner()`, to ensure that any
  Postgres planner calls made by Citus distributed planning do not get confused
  and incorrectly swap out the query tree
2025-12-04 19:31:22 +00:00
..
citus_deparseutils.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
citus_grantutils.c Fixes granted by cascade/restrict statements for revoke (#7517) 2024-02-19 15:44:21 +03:00
citus_ruleutils.c PG18: Add support for virtual generated columns. 2025-12-04 19:31:22 +00:00
citus_setutils.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
deparse.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_attribute_stmts.c Introduce GetDistributeObjectOps to organize dispatch of logic dependent on node/object type 2020-01-09 18:24:29 +00:00
deparse_collation_stmts.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
deparse_comment_stmts.c Adds comment on database and role propagation (#7388) 2024-01-18 20:58:44 +03:00
deparse_database_stmts.c Some cleanup from dropping pg14 2025-03-13 15:13:56 +03:00
deparse_domain_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_extension_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_foreign_data_wrapper_stmts.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
deparse_foreign_server_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_function_stmts.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
deparse_owned_stmts.c Adds REASSIGN OWNED BY propagation (#7319) 2023-12-28 15:15:58 +03:00
deparse_publication_stmts.c Drops PG14 support (#7753) 2025-03-12 12:43:01 +03:00
deparse_role_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_schema_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_seclabel_stmts.c Propagate SECURITY LABEL on tables and columns. (#7956) 2025-04-30 18:03:52 +01:00
deparse_sequence_stmts.c Drops PG14 support (#7753) 2025-03-12 12:43:01 +03:00
deparse_statistics_stmts.c PG17 compatibility: Resolve compilation issues (#7699) 2025-03-12 11:01:49 +03:00
deparse_table_stmts.c PG18: Add support for virtual generated columns. 2025-12-04 19:31:22 +00:00
deparse_text_search.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
deparse_type_stmts.c Drop PG13 Support Phase 2 - Remove PG13 specific paths/tests (#7007) 2023-06-21 14:18:23 +03:00
deparse_view_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
format_collate.c Remove unused functions that cppcheck found 2020-10-19 13:50:52 +03:00
objectaddress.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
qualify.c Introduce GetDistributeObjectOps to organize dispatch of logic dependent on node/object type 2020-01-09 18:24:29 +00:00
qualify_aggregate_stmts.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
qualify_collation_stmt.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
qualify_domain.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
qualify_function_stmt.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
qualify_publication_stmt.c Drops PG14 support (#7753) 2025-03-12 12:43:01 +03:00
qualify_role_stmt.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
qualify_sequence_stmt.c Drops PG14 support (#7753) 2025-03-12 12:43:01 +03:00
qualify_statistics_stmt.c Fix crash on create statistics with non-RangeVar type pt2 (#8227) 2025-10-07 11:56:20 +03:00
qualify_table_stmt.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
qualify_text_search_stmts.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
qualify_type_stmt.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
qualify_view_stmt.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
ruleutils_15.c Remove incorrect assertion from Postgres ruleutils. (#8136) 2025-08-19 15:52:13 +01:00
ruleutils_16.c Remove incorrect assertion from Postgres ruleutils. (#8136) 2025-08-19 15:52:13 +01:00
ruleutils_17.c Remove incorrect assertion from Postgres ruleutils. (#8136) 2025-08-19 15:52:13 +01:00
ruleutils_18.c PG18 deparser: map Vars through JOIN aliases (fixes whole-row join column names) (#8300) 2025-11-05 11:08:58 +00:00