pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses 7+ messages / 1 participants [nested] [flat]
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 ------ master Details ------- https://git.postgresql.org/pg/commitdiff/852fd5b86e1a2ac1139c716eb4c96c09c03a632e Modified Files -------------- src/backend/optimizer/plan/planner.c | 16 +++++++++++----- src/test/regress/expected/tablesample.out | 32 +++++++++++++++++++++++++++++++ src/test/regress/sql/tablesample.sql | 12 ++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/651893c2270fbb963c1a9cbb5fe9880dd84c4aef Modified Files -------------- src/backend/optimizer/plan/planner.c | 16 +++++++++++----- src/test/regress/expected/tablesample.out | 32 +++++++++++++++++++++++++++++++ src/test/regress/sql/tablesample.sql | 12 ++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/30baaacdd1c039e4aced3f4970f24c425a5e0145 Modified Files -------------- src/backend/optimizer/plan/planner.c | 16 +++++++++++----- src/test/regress/expected/tablesample.out | 32 +++++++++++++++++++++++++++++++ src/test/regress/sql/tablesample.sql | 12 ++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/70cf3250610adea0ff5d15d89c65026ebfbdb80a 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(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_16_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/e2a7f784ccc9017968c3bab2c7debf7fe34ed7e4 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(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses @ 2026-10-04 07:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-10-04 07:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_14_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/df1196369080a8ec42e5820d7653039a01061ec8 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(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-10-04 07:10 UTC | newest] Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org> 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org> 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org> 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org> 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org> 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org> 2026-10-04 07:10 pgsql: Fix join alias expansion in lateral TABLESAMPLE clauses Richard Guo <rguo@postgresql.org>
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