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
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/17/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/17/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.