Re: ERROR: no relation entry for relid 3

From: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: ERROR: no relation entry for relid 3
Date: 2026-09-16 10:53:51
Message-ID: CAJTYsWWgia0x+qMfZ7yhkyGmLuQfm1k7UEUr4+soPH-Gp2z0HA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Wed, 16 Sept 2026 at 11:54, Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
>
> While fuzzing around the join alias expansion code, I ran into this:
>
> create table t (a int);
>
> select count(*)
> from (t t1 full join t t2 using (a)) j,
> lateral (select * from t tablesample system (50) repeatable (j.a)) ss;
> ERROR: no relation entry for relid 3
>
> This fails all the way back to v14 at least, dates back to the
> introduction of TABLESAMPLE.
>
> What happens is that the lateral subquery gets pulled up, and the
> sampled relation becomes a lateral RTE of the outer level, with the
> join alias Var j.a now in its TABLESAMPLE clause. But
> preprocess_expression skips flatten_join_alias_vars for
> EXPRKIND_TABLESAMPLE, on the assumption that such clauses can't
> contain Vars of the current query level. The alias Var thus never
> gets expanded, and results in that error at last.
>
> Apparently that assumption does not always hold. I think we can fix
> it the same way lateral function and VALUES RTEs are handled, by
> adding an EXPRKIND_TABLESAMPLE_LATERAL and using it for the
> TABLESAMPLE clause of a lateral relation RTE, so that only the
> non-lateral kind skips the alias expansion. Please see attached.

Thanks for the patch! It looks good to me.

Though is the EXPLAIN test added necessary? Since the execution
query already fails without the fix, maybe we can get rid of the EXPLAIN
portion?

Regards,
Ayush

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alena Rybakina 2026-09-16 10:57:49 Re: Vacuum statistics
Previous Message Alexander Korotkov 2026-09-16 10:51:43 Re: Two issues leading to discrepancies in FSM data on the standby server