Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pSBar-0000Vz-93 for pgsql-bugs@arkaria.postgresql.org; Wed, 15 Feb 2023 06:48:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pSBap-0005oU-2g for pgsql-bugs@arkaria.postgresql.org; Wed, 15 Feb 2023 06:48:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pSAYC-0001nR-HP for pgsql-bugs@lists.postgresql.org; Wed, 15 Feb 2023 05:41:52 +0000 Received: from mail.indevlab.com ([116.202.37.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pSAY8-0008Iu-CB for pgsql-bugs@lists.postgresql.org; Wed, 15 Feb 2023 05:41:52 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.indevlab.com (Postfix) with ESMTP id 67F5D84D10B8; Wed, 15 Feb 2023 03:41:47 -0200 (-02) Received: from mail.indevlab.com ([127.0.0.1]) by localhost (mail.indevlab.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id W5w8UlivouZT; Wed, 15 Feb 2023 03:41:47 -0200 (-02) Received: from localhost (localhost [127.0.0.1]) by mail.indevlab.com (Postfix) with ESMTP id 07AB484D10B6; Wed, 15 Feb 2023 03:41:47 -0200 (-02) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.indevlab.com 07AB484D10B6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=indevlab.com; s=2BB9513E-7140-11E7-B94A-EFF73A7DAAA4; t=1676439707; bh=p8xV1YO95abeWFEFkmR5NOOPRkCg9t2e3jFd0De7qRE=; h=Date:From:To:Message-ID:MIME-Version; b=hC5qt6/XKjCpD91DXZRbWSbiw7cW1bJQlP84u9JzC1jzqGffVIijGjJkLOGZ8lnrL 2tjYsOd2CdYaBl5zNcpyfxj6s4TJZQYhxs7GQkR8Dt9ieOc/t214ko25KcZpZ8WDbd WTO3UwWtqN0oOJSV4nvt9qBNDdYl300BC4wyzbSGGTkffAWbjmocVX/021sXdzivQ4 JqD+wQl4fnxjZIY3LM25sPCB9SyFqAEBCE0WHbFEN6/E31NfSu2Yc2//Z48yu5C/xm 0QF/AlwnzOrHyDAZmvthpNll5WTOMI4B1SvhIbqbkXZvEZw3T/wyzMP52OdqQ6SZjK 0NUE4YzK9Avjw== X-Virus-Scanned: amavisd-new at indevlab.com Received: from mail.indevlab.com ([127.0.0.1]) by localhost (mail.indevlab.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id qr4XV0qkeai8; Wed, 15 Feb 2023 03:41:46 -0200 (-02) Received: from mail.indevlab.com (mail.indevlab.com [116.202.37.26]) by mail.indevlab.com (Postfix) with ESMTP id DE1E784D10B7; Wed, 15 Feb 2023 03:41:46 -0200 (-02) Date: Wed, 15 Feb 2023 03:41:46 -0200 (GMT-02:00) From: Alexander Bluce To: Tom Lane Cc: Dmytro Hanzhelo , pgsql-bugs Message-ID: <342574819.782194.1676439706839.JavaMail.zimbra@indevlab.com> In-Reply-To: <1703359.1676392011@sss.pgh.pa.us> References: <17782-23a2f2a4c868a72e@postgresql.org> <8fceb0e9-de0b-42b9-b12b-e3d6b32332ba@Spark> <1125463649.611902.1676379093613.JavaMail.zimbra@indevlab.com> <1703359.1676392011@sss.pgh.pa.us> Subject: Re: BUG #17782: ERROR: variable not found in subplan target lists MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 8.8.9_GA_3798 (ZimbraWebClient - FF102 (Linux)/8.8.9_GA_3744) Thread-Topic: BUG #17782: ERROR: variable not found in subplan target lists Thread-Index: +Xx93AdHDa+WgaDia0ue2EU/kCOGvw== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tom, thanks for the info, passed it to the client. Alexander Bluce, Head of IT Infrastructure department at InDevLab LLC. https://indevlab.com/ Kyiv, Ukraine ----- Original Message ----- From: "Tom Lane" To: "Alexander Bluce" Cc: "Dmytro Hanzhelo" , "pgsql-bugs" Sent: Tuesday, February 14, 2023 6:26:51 PM Subject: Re: BUG #17782: ERROR: variable not found in subplan target lists Alexander Bluce writes: > The client has agreed to publish an empty dump. The dump in attachment. Thanks for the info! I can reduce this to CREATE TABLE system_module ( system_name text NOT NULL, module_name text NOT NULL, state boolean NOT NULL, system_day date, next_system_day date ); CREATE TABLE sub_system_day_settings ( system_name text PRIMARY KEY, current_day date NOT NULL, next_day date NOT NULL ); CREATE RULE on_update_also_update_sub_system_day_settings AS ON UPDATE TO system_module WHERE (new.module_name = 'b2'::text) DO ALSO INSERT INTO sub_system_day_settings (system_name, current_day, next_day) VALUES (new.system_name, new.system_day, new.next_system_day) ON CONFLICT(system_name) DO UPDATE SET current_day = new.system_day, next_day = new.next_system_day; UPDATE system_module SET state=true WHERE module_name='cfront_exchangerate'; ERROR: variable not found in subplan target lists However ... it only fails like that in v13 and earlier. Since v14 (commit 6c0373ab7), the RULE is rejected with ERROR: invalid reference to FROM-clause entry for table "new" LINE 7: UPDATE SET current_day = new.system_day, next_day = ne... ^ HINT: There is an entry for table "new", but it cannot be referenced from this part of the query. I think that's actually correct: the only table references you are supposed to write in ON CONFLICT are to the target table and EXCLUDED. So the rule should be written like ... ON CONFLICT(system_name) DO UPDATE SET current_day = excluded.current_day, next_day = excluded.next_day; That syntax works for me in v13 and later versions too. Interestingly, I tried to test the rule and found that this case does work in v13: insert into system_module values('sys', 'b2', false); update system_module set system_day = current_date, next_system_day = current_date+1; table sub_system_day_settings; I haven't dug into it in detail, but I suspect that the NEW reference works as long as the referenced column is an update target in the original statement; it's only if we would have to pull values from the original statement's target table that it gets confused. Anyway, given the current situation I would say that making NEW references work in this context is a possible future feature. But it's not something that's likely to be a high priority for anyone given that rules are semi-deprecated. (You'd likely be better advised to implement this behavior with a trigger.) What I *am* thinking about is back-patching 6c0373ab7. At the time it seemed to be adding a feature, but now we can see that it's blocking access to strange misbehaviors. On the other hand, since there are some cases that do work, perhaps that would just result in breaking applications that were not broken before. On the whole I'm inclined to leave things alone. regards, tom lane