Re: BUG #17061: Impossible to query the fields of the tuple created by SEARCH BREADTH FIRST BY .. SET ..

From: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
To: blo(dot)talkto(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17061: Impossible to query the fields of the tuple created by SEARCH BREADTH FIRST BY .. SET ..
Date: 2021-07-06 17:56:10
Message-ID: 4a068167-37ed-3d6c-5ec5-c9b03cae84e6@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 17.06.21 13:44, PG Bug reporting form wrote:
> -- So I try to get the "*DEPTH*" column but it fails with (I get the same
> error with (breadth).id) :
> --
> -- ERROR: CTE m does not have attribute 3
>
> WITH RECURSIVE mtree(id, name) AS (
> SELECT id, name
> FROM tree
> WHERE id = 1
> UNION ALL
> SELECT t.id, t.name
> FROM tree AS t
> INNER JOIN mtree AS m ON t.parent_id = m.id
> ) SEARCH BREADTH FIRST BY id SET breadth
> SELECT (breadth)."*DEPTH*"
> FROM mtree m;

We could put a workaround for this into expandRecordVariable() to handle
the extra columns similar to what we have in markTargetListOrigin(), but
it's still not going to work, because then you'll get

ERROR: record type has not been registered

This is similar to what you get now if you write something like

...
) SEARCH DEPTH FIRST BY id SET seq
SELECT (seq[1]).*
FROM mtree m;

It's not really meant to be used that way. I'm not sure whether it's
worth spending extra effort on.

> -- The following works but feels a little hacky
>
> WITH RECURSIVE mtree(id, name) AS (
> SELECT id, name
> FROM tree
> WHERE id = 1
> UNION ALL
> SELECT t.id, t.name
> FROM tree AS t
> INNER JOIN mtree AS m ON t.parent_id = m.id
> ) SEARCH BREADTH FIRST BY id SET breadth
> SELECT row_to_json(breadth) -> '*DEPTH*'
> FROM mtree m;

This works because it does not require type information for the fields
in the row you're digging into (which is what the "registering" of the
record type would accomplish).

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2021-07-06 21:03:37 BUG #17085: Should be able to create an index without referential checking when ON DELETE NO ACTION
Previous Message Tom Lane 2021-07-06 15:25:30 Re: BUG #17084: Wrong results of distinct node.