Thread: BUG #18803: ERROR: wrong varnullingrels (b) (expected (b 4)) for Var 2/1

BUG #18803: ERROR: wrong varnullingrels (b) (expected (b 4)) for Var 2/1

From
PG Bug reporting form
Date:
The following bug has been logged on the website:

Bug reference:      18803
Logged by:          Marko Tiikkaja
Email address:      marko@joh.to
PostgreSQL version: 16.6
Operating system:   Linux
Description:

Given this schema:

BEGIN;
CREATE TABLE Users();
CREATE TABLE Orders(MessageID text);
CREATE TABLE Transfers (TransferID bigint);
COMMIT;


Running this query:

DO $$
BEGIN

EXECUTE $SQL$
    SELECT
        CASE WHEN $1 < 1
            THEN 1
            ELSE sum(1) OVER ()
        END AS Total,
        MessageID
    FROM (
        SELECT
            coalesce(
                ECR.MessageID,
                Orders.MessageID
            ) AS MessageID
        FROM Orders
        LEFT JOIN LATERAL (
            SELECT
                Transfers.TransferID || Orders.MessageID AS MessageID
            FROM Transfers
        ) ECR ON TRUE
    ) Txn
$SQL$
    USING 1;

END
$$;

produces the following error:

ERROR:  wrong varnullingrels (b) (expected (b 4)) for Var 2/1

(where the "1" seems to correspond to the pg_attribute.attnum of
Orders.MessageID)


PG Bug reporting form <noreply@postgresql.org> writes:
> Given this schema:
> ...
> Running this query:
> ...
> produces the following error:
> ERROR:  wrong varnullingrels (b) (expected (b 4)) for Var 2/1

Thanks for the clear report!  We've seen a few variants of this,
and apparently yours is among the cases that are recently fixed.
Trying this example on a fresh 16-tip build (more or less 16.7)
just emits

DO

            regards, tom lane



Hi Tom,

On Tue, Feb 11, 2025 at 5:33 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Thanks for the clear report!  We've seen a few variants of this,
> and apparently yours is among the cases that are recently fixed.
> Trying this example on a fresh 16-tip build (more or less 16.7)
> just emits

That would be my bad.  I git grep'd the 16 branch but I thought it was
unrelated.  Thanks for the confirmation!


.m