Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wF64X-004k6Y-0j for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Apr 2026 08:03:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wF64W-007Rfd-0i for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Apr 2026 08:03:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wF64V-007RfV-2X for pgsql-hackers@lists.postgresql.org; Tue, 21 Apr 2026 08:03:03 +0000 Received: from mail-ua1-x92a.google.com ([2607:f8b0:4864:20::92a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wF64O-000000023M9-3hBr for pgsql-hackers@lists.postgresql.org; Tue, 21 Apr 2026 08:03:02 +0000 Received: by mail-ua1-x92a.google.com with SMTP id a1e0cc1a2514c-95697b46831so2364024241.0 for ; Tue, 21 Apr 2026 01:02:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776758576; cv=none; d=google.com; s=arc-20240605; b=COw7yiZwYXqzcGJzvBFAktFa/BvfRGRIYs/8TnXY4wWZhfBydkZEZmIwtFChPNVU1/ scBRwjqzPxE+A2UBCrH7V390ypi9FUUvjW+WWUfVzmOh4s+BoiXsircqNvGEHNqDkPmz QbCmNCfXJdTctdAR4kFVpSpf5r/JDUOayKuqDdFtPFF5jWQK2j1JBnL1bQp1FGgeijcq /aHoQrc7xwkbOdUZ5ZZFtkVPXXYgyXeG3tAiTOmYjjLhNlBOe5xRJtBn1NZUkMMttnIx JmKf9P7depFsuFu+OepfV4SscWMzgXVk70TcjXXChKV02FhvPjxAC3V6oN7FkFLcfZk3 TGrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=uyz9Bj+dAUiNf1cj5vRBMLVWlGxmuXLXwKWiFq6rhL4=; fh=aLFlIwLRhjJ62Oi7jfh17CC0bv2ps6dnSrUZCg2hAeg=; b=IxX2rbV5ajns/6nKwYg7LzlclOO0o4DqLAKaInIlz/pNU6T2fTO0+glxAXdGofiXXk 6svWKNpD5774ByWogMFsu5V8zjNvl+y6HZhSuugIdHpecppthq/K27MdMgmlJj0zApNe huexxsAx8Z78UkjN3Mw/qPLUJv8iEkKZjsVR5B6vNue2dxdPQHLIQks7SrK+vl44fKwi JHOfepkCbe8AbMDyGjQT2jcO568tegKCHHQ0icMBseuxueUh94eqaxcl7UiLLDO+zTsm 0Rk/yjObuAGP6jy4nhM086D33ZFh+OtDXmpXaLs45fnvbv0/LsQhZDb+OcZHxZE0WGv5 qOUQ==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776758576; x=1777363376; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=uyz9Bj+dAUiNf1cj5vRBMLVWlGxmuXLXwKWiFq6rhL4=; b=oJ8FhTf7xXq3RewAYruJDwjRK4b/NiVmd/SsYybNhMMOjYbB5UZsMX+SrJTQo+dMGh Ttu6FRMxtZlJUidNtUkhJUbb2W56y9n8RQ/eQ++GkqPrlDYgBZ/OK2wUg5iaMPeCmkOH 3cU9fCJIUUKZV9PTahFo5cMBGS5PIn/1N2PfQv+DCA6V2n84aGiDZmXtrxBbtr8aiKzn jskyTi/NHgcM12EWenJrLVGdWT3W7QuQvJlzPAGogv3vMmGmoeBCxWt/Ry0SOCP8Kj5F Idd/teC6cYZRiK2cbd7QF04Wo4cj3Cp8hA/VCSTvRmDut597rzIkPnk7On6zDbFjNiHa lf7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776758576; x=1777363376; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=uyz9Bj+dAUiNf1cj5vRBMLVWlGxmuXLXwKWiFq6rhL4=; b=KuVlVGcfIHb5NS2EflwAknVw4OLMYKdJhy66V6FlKFAD6au13nNtO6AyOFG2yHArtZ +6hCCSGxH0vXI5Q3B2JaQ0haYIflqTNoqQV2XUfuZ6N/9l5uzeIKgMG2wdmrn0pTOfd/ OwtxPkqUHNfXbZecNValy5JYBwbaqiFcJ6NFoXFz9r3XCQ/X7wbXRNFXTUqJBMp67eCd 0shOh8sxcldjBYeW0ma0M32GxiQU+Nc2JIoOUn1y35hbmB4h9svSchHzmjVbOxPex9UF /XIjuw51adq5txwB7a5Q6sqhXtIvebeGjiIr/Wn0JZCP8Cth51oLyXJpAfZR9ceFWbA+ f8aw== X-Gm-Message-State: AOJu0Yz7AYr8JacGCk4zImCbpz68dZ6FlppREAlMG2hq4S1miUQkIpIh ZJRhShk9siAKeWkIIx50yRAEtolz4nHOQOCJtTVux645g4zBKUNBfV58MCJUyTgsgL2trpWHofp 0cmO9e/SUz+eIzWLBwd6++XyTsH6IZgI= X-Gm-Gg: AeBDietgNMrcHm9PhYMX7xsn1VTzl1zCEZN1hfTM3wGQi7+f+L3+yY4Nw5EwIrT2b3H MMhT7MyiSmaiwlOy68OtHcU/TuuhYs7+3PPMzfqZ3ze3XDFqUE3XwSmSccO0zXzemGqjYnL+xR4 s/RmY0F1Crd/FZj8W09+19i0n5kheyvQ5Ufs5bQlXmgbiu+e7htVa43Gow+CD4BEGF74fbZgR74 6vS6qNZFcif++qo6Uz5w7gxIn1KUyy4MVwsYlcMR2eT2/r+kOAB2Ihk60lpuxlDo71Z1ZF27MAp c8rFO9+oRDf/Q5UVpQ== X-Received: by 2002:a05:6102:4426:b0:605:2a07:65e5 with SMTP id ada2fe7eead31-616f67c7eb0mr8038391137.18.1776758576485; Tue, 21 Apr 2026 01:02:56 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: SATYANARAYANA NARLAPURAM Date: Tue, 21 Apr 2026 01:02:43 -0700 X-Gm-Features: AQROBzCIVUV-ZnXCyE1PaF7RbqC5NsrOJHbmT8eYPP1XNorh9r2YEQGyEcl8We4 Message-ID: Subject: Re: Bug: pg_get_viewdef() fails on GRAPH_TABLE views with lateral column references To: Ashutosh Bapat Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000cd987c064ff3d76a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000cd987c064ff3d76a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Ashutosh, On Mon, Apr 20, 2026 at 11:52=E2=80=AFPM Ashutosh Bapat < ashutosh.bapat.oss@gmail.com> wrote: > On Sat, Apr 18, 2026 at 1:26=E2=80=AFPM SATYANARAYANA NARLAPURAM > wrote: > > > > Hi hackers, > > > > pg_get_viewdef() fails with ERROR: bogus varlevelsup: 0 offset 0 for an= y > > view containing a GRAPH_TABLE whose COLUMNS clause references an outer > (lateral) > > table. This also breaks pg_dump and \d+ for any database containing suc= h > a > > view. > > > > Repro: > > > > CREATE TABLE vtab (id int PRIMARY KEY, name text); > > CREATE TABLE etab (eid int PRIMARY KEY, > > src int REFERENCES vtab(id), dst int REFERENCES vtab(id)); > > CREATE PROPERTY GRAPH g1 > > VERTEX TABLES (vtab) > > EDGE TABLES (etab KEY (eid) > > SOURCE KEY (src) REFERENCES vtab(id) > > DESTINATION KEY (dst) REFERENCES vtab(id)); > > CREATE TABLE outer_t (val int); > > > > CREATE VIEW v AS > > SELECT * FROM outer_t, > > GRAPH_TABLE (g1 MATCH (a IS vtab) > > COLUMNS (a.name AS src_name, outer_t.val AS oval)); > > > > pg_dump -d foo -p 5433 > > pg_dump: error: query failed: ERROR: bogus varlevelsup: 0 offset 0 > > pg_dump: detail: Query was: SELECT > pg_catalog.pg_get_viewdef('173849'::pg_catalog.oid) AS viewdef > > > > Problem: > > deparse_context context variable declared in the case RTE_GRAPH_TABLE > shadows the function's > > deparse_context *context parameter. The zeroed struct has namespaces = =3D > NIL, so when get_rule_expr() > > reaches a Var node, get_variable() sees list_length(context->namespaces= ) > =3D=3D 0 and raises the error. Property > > references are fine because GraphPropertyRef deparsing never touches > namespaces. > > > > Fix: > > Remove the shadowing local variable and pass the outer context pointer > to get_rule_expr(). Attached a patch > > with a fix, additionally added a test. > > The code doesn't explain why it adds the dummy context but it seemed > intentional. But it's not used at other places like deparsing WHERE > clause in element patterns or that in the graph_table itself. Since a > lateral reference is allowed in COLUMNS clause as well, it doesn't > make sense not to pass a context with lateral namespaces. Also there > is no comment explaining the dummy context. So your fix looks good to > me. I adjusted the surrounding code a bit. > > I adjusted an existing view for the testing instead of adding a new > one with all the additional objects. Since that view definition was > getting more complex, I formatted the DDL to be more readable. > > I also think that we should use prettyFlags to deparse all GRAPH_TABLE > components in a human readable form. But that's out of the scope for > this patch. > > PFA updated patch. > Thank you for updating the patch. It applies cleanly and the related tests are passing. Thanks, Satya --000000000000cd987c064ff3d76a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Ashutosh,

On Mon,= Apr 20, 2026 at 11:52=E2=80=AFPM Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> wrote:
On Sat, Apr 18, 2026 = at 1:26=E2=80=AFPM SATYANARAYANA NARLAPURAM
<satyanar= lapuram@gmail.com> wrote:
>
> Hi hackers,
>
> pg_get_viewdef() fails with ERROR: bogus varlevelsup: 0 offset 0 for a= ny
> view containing a GRAPH_TABLE whose COLUMNS clause references an outer= (lateral)
> table. This also breaks pg_dump and \d+ for any database containing su= ch a
> view.
>
> Repro:
>
> CREATE TABLE vtab (id int PRIMARY KEY, name text);
> CREATE TABLE etab (eid int PRIMARY KEY,
>=C2=A0 =C2=A0 =C2=A0src int REFERENCES vtab(id), dst int REFERENCES vta= b(id));
> CREATE PROPERTY GRAPH g1
>=C2=A0 =C2=A0 =C2=A0VERTEX TABLES (vtab)
>=C2=A0 =C2=A0 =C2=A0EDGE TABLES (etab KEY (eid)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0SOURCE KEY (src) REFERENCES vtab(id)<= br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DESTINATION KEY (dst) REFERENCES vtab= (id));
> CREATE TABLE outer_t (val int);
>
> CREATE VIEW v AS
>=C2=A0 =C2=A0SELECT * FROM outer_t,
>=C2=A0 =C2=A0 =C2=A0GRAPH_TABLE (g1 MATCH (a IS vtab)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0COLUMNS (a.name AS src_name, outer_t.val AS oval));<= br> >
> pg_dump -d foo -p 5433
> pg_dump: error: query failed: ERROR:=C2=A0 bogus varlevelsup: 0 offset= 0
> pg_dump: detail: Query was: SELECT pg_catalog.pg_get_viewdef('1738= 49'::pg_catalog.oid) AS viewdef
>
> Problem:
> deparse_context context variable declared in the case RTE_GRAPH_TABLE = shadows the function's
> deparse_context *context parameter. The zeroed struct has namespaces = =3D NIL, so when get_rule_expr()
> reaches a Var node, get_variable() sees list_length(context->namesp= aces) =3D=3D 0 and raises the error. Property
> references are fine because GraphPropertyRef deparsing never touches n= amespaces.
>
> Fix:
> Remove the shadowing local variable and pass the outer context pointer= to get_rule_expr(). Attached a patch
> with a fix, additionally added a test.

The code doesn't explain why it adds the dummy context but it seemed intentional. But it's not used at other places like deparsing WHERE
clause in element patterns or that in the graph_table itself. Since a
lateral reference is allowed in COLUMNS clause as well, it doesn't
make sense not to pass a context with lateral namespaces. Also there
is no comment explaining the dummy context. So your fix looks good to
me. I adjusted the surrounding code a bit.

I adjusted an existing view for the testing instead of adding a new
one with all the additional objects. Since that view definition was
getting more complex, I formatted the DDL to be more readable.

I also think that we should use prettyFlags to deparse all GRAPH_TABLE
components in a human readable form. But that's out of the scope for this patch.

PFA updated patch.

Thank you for updati= ng the patch. It applies cleanly and the related tests are passing.=C2=A0


Thanks,
Satya=C2=A0
<= /div>
--000000000000cd987c064ff3d76a--