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.94.2) (envelope-from ) id 1tJTgB-003FyD-6P for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Dec 2024 08:27:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tJTg8-00AqMb-JU for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Dec 2024 08:27:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJTg8-00AqMS-54 for pgsql-hackers@lists.postgresql.org; Fri, 06 Dec 2024 08:27:13 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tJTg6-001KMl-DB for pgsql-hackers@postgresql.org; Fri, 06 Dec 2024 08:27:12 +0000 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-7fcf8406d8bso2377109a12.0 for ; Fri, 06 Dec 2024 00:27:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733473629; x=1734078429; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=CnkigpALEdHGAEwR1fdSbaJ8zl4fKXRQqiLm39/BYJI=; b=hRY5i6UmLCZfpJh8Sc4v8ve/b+bTVeY1Xhh6oY6gC95E7nI+zhbBw2W/+A9cWInhuP HoebZE7Irc7t/lZy+DeYF6SCUMI+yN6knjNcwl/3iMpZpYu/XsmuXK515DN3zN0MV8CH qY7dGTYnZgJ2ONaNykr/KkIxcgwsuWPs5VfNZOFGVF68YaIdRK+KKUc8IY1mdkbpJ9Pp kNA6m3Sfre7mXSBzsTaQIQDUgrtdHK7t82LQWS01b2njAZD2I0mXmW+Z8R+WH+HDn+dP XMXvMEM3BFXLjEFbT2J140X0lB2yVv35M386wN5RjGBk432Jh9BKNO7liyxuZNqjHtyA rC+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733473629; x=1734078429; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CnkigpALEdHGAEwR1fdSbaJ8zl4fKXRQqiLm39/BYJI=; b=r0gtcwNwNzmKymWaHSEqHd1j2cfnOH1RYcB/hCKOxttS0MHiHr6ddbcH4hqmAtsNwA ImgDMro1SXAbQsyalXMJ5MuHw9xTbiWOGsm+AMdeVdrJ/DeCk/ohcos06LuuahHrh44O 0Bhv+E34pl9qy6rqUyIAXp5IsqPbRn640rqDD+fI7ab+uOESnVVAeZo4RXR5QkFQVO26 Fe0eMNXZ0hclI5fdO/YAet4NZ5vh+GIdWRFSPYW2gyux/u4aEQTFccoKQN96BOTjNg/s xvD1sweIp/PunSUMEyZ2aTFVMpcJlb/fO7bcu2Cb3L+E8/XDXkpEps2BC9rPNcnKgpp8 m5bg== X-Forwarded-Encrypted: i=1; AJvYcCVSBTwiTg9b241nlzWaDl5zySuTpdR2DbRRMS6PI4Z8ALkqLw4nRgbFDOJzzJEzOXpgUOUJmhi2CFDXRzA9@postgresql.org X-Gm-Message-State: AOJu0YwkK15T3457LE6dTNbnNtF9XJKgzt5I65nPiK14Z+4Ca3u5UO9j O5MA0BPVnqvOgfJSTR9WyVv9MoeqXC8fu84K58/G3voZxt4mVZyo7pFVaIq7eD9IUprj8VGZhvi N8cZchsqA++/Uox/xvPMYITJz2UfpAg3+ X-Gm-Gg: ASbGncvk66cDSTUp7Avg5T6TYwok/Xy8llvRrP9cVBHzASGmvKQR8rjspQlouVIKST7 916dlmHINOWNPVrK9OsYRIME9MIlOj2Y4tfKPlzishO8lU7Qy8o7FKzInLHT4b3rM X-Google-Smtp-Source: AGHT+IHDdTSdT2WUraq615UJ5IUHv7I8tDdhdNYgMsLQRZ2aiIvLie2fZwkWHUecia7np8EaNdhP3+zLfSNlku1E7zY= X-Received: by 2002:a17:90a:c10f:b0:2ee:ee77:226d with SMTP id 98e67ed59e1d1-2ef68c88eebmr3974539a91.4.1733473628599; Fri, 06 Dec 2024 00:27:08 -0800 (PST) MIME-Version: 1.0 References: <54c35fb9-da3a-4754-ab8c-46ed0b612465@vondra.me> <684c70d7-180e-461d-9377-600c2db581ba@vondra.me> <52e3aa78-18d6-424c-b48b-f5018d103824@vondra.me> In-Reply-To: <52e3aa78-18d6-424c-b48b-f5018d103824@vondra.me> From: Amit Langote Date: Fri, 6 Dec 2024 17:26:49 +0900 Message-ID: Subject: Re: generic plans and "initial" pruning To: Tomas Vondra Cc: Robert Haas , Alvaro Herrera , Andres Freund , Daniel Gustafsson , David Rowley , PostgreSQL Hackers , Thom Brown , Tom Lane Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Dec 5, 2024 at 10:53=E2=80=AFPM Tomas Vondra wrot= e: > On 12/5/24 07:53, Amit Langote wrote: > > On Thu, Dec 5, 2024 at 2:20=E2=80=AFAM Tomas Vondra w= rote: > >> ... > >> > >>>> What if an > >>>> extension doesn't do that? What weirdness will happen? > >>> > >>> The QueryDesc.planstate won't contain a PlanState tree for starters > >>> and other state information that InitPlan() populates in EState based > >>> on the PlannedStmt. > >> > >> OK, and the consequence is that the query will fail, right? > > > > No, the core executor will retry the execution with a new updated > > plan. In the absence of the early return, the extension might even > > crash when accessing such incomplete QueryDesc. > > > > What the patch makes the ExecutorStart_hook do is similar to how > > InitPlan() will return early when locks taken on partitions that > > survive initial pruning invalidate the plan. > > Isn't that what I said? My question was what happens if the extension > does not add the new ExecPlanStillValid() call - sorry if that wasn't > clear. If it can crash, that's what I meant by "fail". Ok, I see. So, I suppose you meant to confirm if the invalid plan won't silently be executed returning wrong results. Yes, I don't think that would happen given the kinds of invalidations that are possible. The various checks in the ExecInitNode() path, such as the one that catches a missing index, will prevent the plan from running. I may not have searched exhaustively enough though. > >>>> Maybe it'd be > >>>> possible to at least check this in some other executor hook? Or at l= east > >>>> we could ensure the check was done in assert-enabled builds? Or > >>>> something to make extension authors aware of this? > >>> > >>> I've added a note in the commit message, but if that's not enough, on= e > >>> idea might be to change the return type of ExecutorStart_hook so that > >>> the extensions that implement it are forced to be adjusted. Say, from > >>> void to bool to indicate whether standard_ExecutorStart() succeeded > >>> and thus created a "valid" plan. I had that in the previous versions > >>> of the patch. Thoughts? > >> > >> Maybe. My concern is that this case (plan getting invalidated) is fair= ly > >> rare, so it's entirely plausible the extension will seem to work just > >> fine without the code update for a long time. > > > > You might see the errors like the one below when the core executor or > > a hook tries to initialize or process in some other way a known > > invalid plan, for example, because an unpruned partition's index got > > concurrently dropped before the executor got the lock: > > > > ERROR: could not open relation with OID xxx > > Yeah, but how likely is that? How often get plans invalidated in regular > application workload. People don't create or drop indexes very often, > for example ... Yeah, that's a valid point. Andres once mentioned that ANALYZE can invalidate plans and that can occur frequently in busy systems. > Again, I'm not saying requiring the call would be unacceptable, I'm sure > we made similar changes in the past. But if it wasn't needed without too > much contortion, that would be nice. I tend to agree. Another change introduced by the patch that extensions might need to mind (noted in the commit message of v58-0004) is the addition of the es_unpruned_relids field to EState. This field tracks the RT indexes of relations that are locked and therefore safe to access during execution. Importantly, it does not include the RT indexes of leaf partitions that are pruned during "initial" pruning and thus remain unlocked. This change means that executor extensions can no longer assume that all relations in the range table are locked and safe to access. Instead, extensions must account for the possibility that some relations, specifically pruned partitions, are not locked. Normally, executor code accesses relations using ExecGetRangeTableRelation(), which does not take a lock before returning the Relation pointer, assuming that locks are already managed upstream. --=20 Thanks, Amit Langote