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: Fix join alias expansion in lateral TABLESAMPLE clauses
Date: Sun, 04 Oct 2026 07:10:23 +0000
Message-ID: <E1xDGMZ-00000000FKE-2k82@gemulon.postgresql.org> (raw)

Fix join alias expansion in lateral TABLESAMPLE clauses

preprocess_expression skips flatten_join_alias_vars for TABLESAMPLE
clauses, on the assumption that they cannot contain Vars of the
current query level.  That's not true once a LATERAL subquery
containing a sampled relation has been pulled up: the relation's
TABLESAMPLE clause then becomes a lateral reference of this level, and
if it referred to a join alias Var, that Var is left unexpanded.
extract_lateral_references then pulls the join alias Var out as a
lateral reference and fails with "no relation entry for relid N".

To fix, treat such clauses like lateral function and VALUES RTEs:
introduce EXPRKIND_TABLESAMPLE_LATERAL, used for the TABLESAMPLE
clause of a lateral relation RTE, and keep skipping the alias
expansion only for the non-lateral kind.

Back-patch to all supported branches, as this dates back to the
introduction of TABLESAMPLE.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ewan Young <kdbase.hack@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4834TwOtyyycyvja3MuOBkfpXy3dFAL0zt_vcM1KtqV-Q@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/011dd5095752acda0397cef3ae0f3aa86e44c7ef

Modified Files
--------------
src/backend/optimizer/plan/planner.c      | 14 ++++++++++----
src/test/regress/expected/tablesample.out | 32 +++++++++++++++++++++++++++++++
src/test/regress/sql/tablesample.sql      | 12 ++++++++++++
3 files changed, 54 insertions(+), 4 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xDGMZ-00000000FKE-2k82@gemulon.postgresql.org>
Permalink:  ../E1xDGMZ-00000000FKE-2k82@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xDGMZ-00000000FKE-2k82@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org
  Subject: Re: pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses
  In-Reply-To: <E1xDGMZ-00000000FKE-2k82@gemulon.postgresql.org>
  References: <E1xDGMZ-00000000FKE-2k82@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