pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Expand virtual generated columns in the planner
Date: Tue, 25 Feb 2025 07:12:35 +0000
Message-ID: <E1tmp7K-000ZsN-2f@gemulon.postgresql.org> (raw)

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 <guofenglinux@gmail.com>
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Discussion: https://postgr.es/m/75eb1a6f-d59f-42e6-8a78-124ee808cda7@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(-)



view thread (3+ messages)  latest in thread

Message-ID: <E1tmp7K-000ZsN-2f@gemulon.postgresql.org>
Permalink:  ../E1tmp7K-000ZsN-2f@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1tmp7K-000ZsN-2f@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: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Expand virtual generated columns in the planner
  In-Reply-To: <E1tmp7K-000ZsN-2f@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