Expand virtual generated columns in the planner Commit 83ea6c540 added support for virtual generated columns that are computed on read. All Var nodes in the query that reference virtual generated columns must be replaced with the corresponding generation expressions. Currently, this replacement occurs in the rewriter. However, this approach has several issues. If a Var referencing a virtual generated column has any varnullingrels, those varnullingrels need to be propagated into the generation expression. Failing to do so can lead to "wrong varnullingrels" errors and improper outer-join removal.
Additionally, if such a Var comes from the nullable side of an outer join, we may need to wrap the generation expression in a PlaceHolderVar to ensure that it is evaluated at the right place and hence is forced to null when the outer join should do so. In certain cases, such as when the query uses grouping sets, we also need a PlaceHolderVar for anything that is not a simple Var to isolate subexpressions. Failure to do so can result in incorrect results. To fix these issues, this patch expands the virtual generated columns in the planner rather than in the rewriter, and leverages the pullup_replace_vars architecture to avoid code duplication. The generation expressions will be correctly marked with nullingrel bits and wrapped in PlaceHolderVars when needed by the pullup_replace_vars callback function. This requires handling the OLD/NEW RETURNING list Vars in pullup_replace_vars_callback, as it may now deal with Vars referencing the result relation instead of a subquery. The "wrong varnullingrels" error was reported by Alexander Lakhin. The incorrect result issue and the improper outer-join removal issue were reported by Richard Guo. Author: Richard Guo <guofengli...@gmail.com> Author: Dean Rasheed <dean.a.rash...@gmail.com> Reviewed-by: Jian He <jian.universal...@gmail.com> Discussion: https://postgr.es/m/75eb1a6f-d59f-42e6-8a78-124ee808c...@gmail.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/1e4351af329f2949c679a215f63c51d663ecd715 Modified Files -------------- src/backend/optimizer/plan/planner.c | 8 + src/backend/optimizer/prep/prepjointree.c | 196 ++++++++++++++++++++++++ src/backend/rewrite/rewriteHandler.c | 91 +++++------ src/backend/rewrite/rewriteManip.c | 2 +- src/include/nodes/primnodes.h | 2 +- src/include/optimizer/prep.h | 1 + src/include/rewrite/rewriteHandler.h | 1 + src/include/rewrite/rewriteManip.h | 3 + src/test/regress/expected/generated_virtual.out | 130 ++++++++++++++++ src/test/regress/sql/generated_virtual.sql | 57 +++++++ 10 files changed, 445 insertions(+), 46 deletions(-)