ERROR: no relation entry for relid 3

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: ERROR: no relation entry for relid 3
Date: 2026-09-16 06:23:43
Message-ID: CAMbWs4834TwOtyyycyvja3MuOBkfpXy3dFAL0zt_vcM1KtqV-Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

- Richard

Attachment Content-Type Size
v1-0001-Fix-join-alias-expansion-in-lateral-TABLESAMPLE-c.patch application/octet-stream 5.0 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nisha Moond 2026-09-16 06:30:24 Re: Distinguish publication exclusions in object addresses
Previous Message Chao Li 2026-09-16 05:54:58 Re: [PATCH] Release replication slot on error in SQL-callable slot functions