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 1w8YmK-000ejF-2o for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Apr 2026 07:17:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w8YmI-00A9Ee-0H for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Apr 2026 07:17:14 +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 1w8YmH-00A9EW-2f for pgsql-bugs@lists.postgresql.org; Fri, 03 Apr 2026 07:17:14 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w8YmG-00000000JgC-2Cod for pgsql-bugs@lists.postgresql.org; Fri, 03 Apr 2026 07:17:13 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-43cfa33a983so1018024f8f.1 for ; Fri, 03 Apr 2026 00:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775200631; x=1775805431; darn=lists.postgresql.org; h=content-transfer-encoding:subject:from:content-language:to :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=cqcy8GK0VQePP8fu0RQOYotgYFfNKmBcF4d5YuWlqV4=; b=R0ckL+5bQDWSCduZWE7cab/emh3xKDh2ux31sCkHBLcOg+T3hs8obGRMqyL7ebyP6X t22EGBiEKjN9f7JGfdRu0kfMimSSdSm/Fxeemxy4enbTpLdr51YyUPaWgXEQ4ALt6zMk nXNwrvU1bX/NkbctFqdCUwVZjgg36rWtyfcPfMzM7pBH6UUK9ypBPHGQbTRXKAYFBdWw G3IFDGXTpOpQ3PTAAUP+JtQZ+XOgRhW10Lq9djWCe0kgZE0J1vDIEtBsdBVSTLC4pEr0 uKDhj23KwvqEr8yDmJVD0G0AYK64DfCzbKMfe81RfQRIsC3Edrb0oSvLXBEB9lsBF+hp Q26Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775200631; x=1775805431; h=content-transfer-encoding:subject:from:content-language:to :user-agent:mime-version:date:message-id:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cqcy8GK0VQePP8fu0RQOYotgYFfNKmBcF4d5YuWlqV4=; b=KHi3bBOHb1AO0N7J0r7lU9gt6P3C5ewBZcJ/y1FrZ7lU/N+trkwkUhk6uDl95cl/6j ZshVW2p4ca1ZsbVeoOSSWb9TxY1SXJHj37WAWzckCgpthq4mcRtxI/1iLZZZxUH9E9Kl LadYx7MpGW2XGUtSXLnuuRJVbC5swSqIMkYW1VyY13i1d299vEAUl0lqnDyr+Th48Xxi Ed/+V/c4PLwWOqgXggdlBFvLKaVGxht2JS/QoL/e2ngXpSj8TA2MawDb17rhaKOig2bk 3gVnJ9fhIsE81iXYSCqtICIxbdqXCoggSelDZJhNPQKdSeQ1Eu2TsxJ/TksHw2fJohq3 5jlA== X-Gm-Message-State: AOJu0Yz2I2bz3Q7yrc7FGVByV9mqkR5fml3KZ+fPWBxlezECUou0V/ws JatJ1HeuBbmRitim/ZDXPJyTlBsUpkRQx70GGgnAmdE5pKYL05x7s8jBL0dZP1je X-Gm-Gg: AeBDieuN67f4pBKG43YHq+yCpsiib3QUHgx/wFtguiDPsQRuMztUEpkKEcb2V6EY+BN T0ImIeEWEeRlXM8Gg+7qj2AdP3+IW1/Gpj4I22nyh9khGudbP7CC46npBdH/ANtzaAf4TlA1b4q sBBp5TAg5W3Plt//K7YK9cq3Q3I3IvkczyglaHEBGSDn4lfLcRDWz4354GoxSY2aDyWLpRPGDOO FYadJ0sGsF7lberfOWeVm5aGYyMeetWCUHpw+AtEXjeiTvkmz4WvYt8buZbrdc7AaYN2hNblRP8 LjBLXwVQ2VO/1zllEF2gPr+X9UbnchIkBXEBv+52dw/XysuhDuIXu6i5pg7shHgquV3zLFdgmWg xXLuaoCZEWWy4xM5wiRvA5OalQ3SE1J0b4SB1uMRrOf602tS8s0ayPcK9KKDuvimgKwxbKLWYjD rgoxg6f7MkRMOWVObIInGrR8gnjDvU/XL+Vv83zA== X-Received: by 2002:a5d:588c:0:b0:43d:29a:e42a with SMTP id ffacd0b85a97d-43d292e1583mr3027374f8f.29.1775200631097; Fri, 03 Apr 2026 00:17:11 -0700 (PDT) Received: from [192.168.15.128] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d1e2c637asm13406575f8f.14.2026.04.03.00.17.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 Apr 2026 00:17:09 -0700 (PDT) Message-ID: <78dd9572-7569-4025-984d-e07d7f381b6e@gmail.com> Date: Fri, 3 Apr 2026 09:17:08 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: pgsql-bugs , Robert Haas Content-Language: en-US From: Andrei Lepikhov Subject: pg_plan_advice fails when NestLoop outer side is Sort over FunctionScan Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, While testing the optimiser extension that extends planner's scope by pre-sorted outer paths I found that current master hit a crash in the test_plan_advice TAP test (src/test/modules/test_plan_advice/t/001_replan_regress.pl): ERROR: plan node has no RTIs: 380 The error originates in pgpa_scan.c: if (within_join_problem) elog(ERROR, "plan node has no RTIs: %d", (int) nodeTag(plan)); It is triggered by the pg_lsn regression test query: SELECT DISTINCT (i || '/' || j)::pg_lsn f FROM generate_series(1, 10) i, generate_series(1, 10) j, generate_series(1, 5) k WHERE i <= 10 AND j > 0 AND j <= 10 ORDER BY f; I have the following query plan: Unique -> Nested Loop -> Sort -> Nested Loop -> Function Scan on generate_series j Filter: ((j > 0) AND (j <= 10)) -> Function Scan on generate_series i Filter: (i <= 10) -> Function Scan on generate_series k The assumption baked into pg_plan_advice is that when walking a join subtree (within_join_problem = true), every leaf node will be a base-relation scan with RTIs. Before now, that assumption was always valid: no PostgreSQL core code placed a Sort node between a NestLoop and a FunctionScan. But extensions might want to employ more sorted paths to find better plan - it might happen in complex analytics tasks. My case is presorted outer side of a LEFT JOIN in case of ORDER-BY .. LIMIT present on the outer table only. I'm not aware about how this module is designed, but I think it should not unconditionally error. A Sort injected between a join and a non-relation scan leaf is a legitimate plan node that the walker should handle gracefully in case it is loaded with other extensions. Just for the reproduction, see the branch [1] over fresh PostgreSQL master. [1] https://github.com/danolivo/pgdev/tree/bounded-left-join-outer -- regards, Andrei Lepikhov, pgEdge