Thread: ERROR: could not devise a query plan for the given query (UNIONS and LATERAL)

ERROR: could not devise a query plan for the given query (UNIONS and LATERAL)

From
Elvis Pranskevichus
Date:
Hi,

The following (admittedly bizzare) query causes the error in the subject 
on all recent versions of Postgres:

SELECT
        "q2"."v"
    FROM
        LATERAL
        (SELECT
                "q11"."v"
            FROM
                (
                    (SELECT 2::bigint AS "v") UNION ALL
                    (SELECT 3::bigint AS "v")
                ) AS "q11"
        ) AS "q1"
        CROSS JOIN LATERAL
        (
            (SELECT
                "q3"."v"
             FROM
                (
                    (SELECT 4::bigint AS "v") UNION ALL
                    (SELECT 5::bigint AS "v")
                ) AS "q3"
            )
            UNION ALL
            (SELECT "q1"."v")
        ) AS "q2"
    ORDER BY
        "q2"."v"; 


Interestingly, removing UNION from either "q3" or "q11", as well as 
removing the ::bigint casts makes it work.


                            Elvis




Elvis Pranskevichus <elprans@gmail.com> writes:
> The following (admittedly bizzare) query causes the error in the subject 
> on all recent versions of Postgres:

Interesting.  What this shows is that reparameterize_path has to be able
to work on an AppendPath, because if you've got nested appendrels that's
the only sort of path there will be for that particular child of the
outer appendrel.

I'm a little worried about whether this doesn't mean that
reparameterize_path needs to be able to work on any kind of path.
However, given that it's been there quite some time without previous
reports of trouble, jumping to that conclusion may be premature.

I'm also wondering where reparameterize_path_by_child came from and
why it's so enormously more ambitious than reparameterize_path
in terms of its path-type coverage.

            regards, tom lane