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 1ue6j5-0057QB-4h for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 06:43:48 +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 1ue6j3-005RE9-Bv for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 06:43:45 +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.94.2) (envelope-from ) id 1ue6j2-005RE0-TB for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 06:43:45 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ue6j1-0009kQ-1H for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 06:43:44 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-3122a63201bso3751328a91.0 for ; Mon, 21 Jul 2025 23:43:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753166622; x=1753771422; 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=Ew2nRhdopZ2YWSU6RuTYLP3rMY9M0bL80hFm8rntOaM=; b=WOpKYU48XdjeFHYPEYYKNkAepe3H7Og0LxSxOLlUhgLtLP3d67AfALcErdOpAjljsr zCXWHXGGGBdwVZYPK1khlqnq+Urq9ZKe7pO0miS0H31AMX2Ud7khXAt8uhe21JOzeJ/y pT/ZRXhHzIGbL3oZFmiQQqStwl2L0jKWwuQAl1ZEbOpMEbNJVCmKajheuCpCJsN0eoP2 Uhl6wAWzQvrYbfwqRbwB1iQcSncXAeaV4gfDolWqCAP++bOHVN2UILW2CZEM5Plw45Dt Oyqvgjq6JPAVo6KrR62LOSB+AD0C6BqNyw5MlzI+4A6wrdQSMJOtIQPxYvUe4vxEkjrw qqUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753166622; x=1753771422; 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=Ew2nRhdopZ2YWSU6RuTYLP3rMY9M0bL80hFm8rntOaM=; b=JqVmbbMMMsemV4R+wsv61ujlvvGbTKd3XjwMzsPq1oVqred2Ou0oMDEYeJQyt7NOXQ PpLwrU9QaGWDudhIp+twCaVsozFQ9P1dfebB6OSuXBJwcqUl2fEJoLcEoEx2YV9YRfR0 gi0tbkVejod7H0TJzfS8xSYcsg2ACVkvXzDtWCysBBg2quu9EDg+0uA8hbZQAnbmjh/V OkZ7Bu8QnYh4z2WysyCEEjKJB+ALQjfJ8DAj+XpMvcfFRkKAzyB+E8rCHNyuPFSJyktQ WegkR3vvJjQEw7D1FIkKItdJBVzE8FratqOKU+HLasvn/79U6oXghwIn0jw6fckqP2ml kb1w== X-Forwarded-Encrypted: i=1; AJvYcCU38K0IkSjMgW09vFmQi6PmDDpaHshVTR0Y9jO6Ay7agja3nEK/ZhchUap4WiwcT4AR7cSdxfoPFN+/52A6@postgresql.org X-Gm-Message-State: AOJu0YxScsl+IDa3e8VNsXcmMngIUplllLOSlAZOvQLaNHZT5yD43mB/ 2BS1kAbZhP2FxAoNhjfdU/+5tm3f4+S1QBoSeuDf01UHu4sKa7fl3NrCLI8sFzN+k3HWv5ijX32 28U0pK+5hl73Iw5TC0t4qIY9UDc6pK8Q= X-Gm-Gg: ASbGncsL++PnjrLFmg/r1vp8cIqMmwOoc2BUfFQewx95R/MVgEWwV8qbUJcLGDC3VsN gDUvNbjCQetCn2cJZlM3B6eH9PtnSTYxZKYLZX2Hr/v0gQxTqTenUWhAyPZ756b68r7wcsrABBz 86QxfVPwuiZGifOoPSBCk0YdAqyjD4qpLvxZz1JjGE6v3WP3W8Baj1w+B5gz7BTIVb2mWtACas9 HOxNmED X-Google-Smtp-Source: AGHT+IEz/RIQeBsZ+AzaYct0LdVfGiRdaFjVlrWoNfDNu3TIGtGLfjwxQZx4hGCNLMQR0dGyrgr6h/TkgU2637XNu1A= X-Received: by 2002:a17:90b:270b:b0:311:d670:a10d with SMTP id 98e67ed59e1d1-31c9f4d0057mr29696543a91.26.1753166622339; Mon, 21 Jul 2025 23:43:42 -0700 (PDT) MIME-Version: 1.0 References: <54c35fb9-da3a-4754-ab8c-46ed0b612465@vondra.me> <684c70d7-180e-461d-9377-600c2db581ba@vondra.me> <605328.1747710381@sss.pgh.pa.us> <691261.1747755511@sss.pgh.pa.us> In-Reply-To: From: Amit Langote Date: Tue, 22 Jul 2025 15:43:26 +0900 X-Gm-Features: Ac12FXyvY4An1x_1kIbWVqpuNzZAzttWuvSLUWHRD1qNZ1jLSJbfz94UWhBhGLQ Message-ID: Subject: Re: generic plans and "initial" pruning To: Tom Lane Cc: Tender Wang , Alexander Lakhin , Tomas Vondra , Robert Haas , Alvaro Herrera , Andres Freund , Daniel Gustafsson , David Rowley , PostgreSQL Hackers , Thom Brown 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, Jul 17, 2025 at 9:11=E2=80=AFPM Amit Langote wrote: > The refinements I described in my email above might help mitigate some > of those executor-related issues. However, I'm starting to wonder if > it's worth reconsidering our decision to handle pruning, locking, and > validation entirely at executor startup, which was the approach taken > in the reverted patch. > > The alternative approach, doing initial pruning and locking within > plancache.c itself (which I floated a while ago), might be worth > revisiting. It avoids the complications we've discussed around the > executor API and preserves the clear separation of concerns that > plancache.c provides, though it does introduce some new layering > concerns, which I describe further below. > > To support this, we'd need a mechanism to pass pruning results to the > executor alongside each PlannedStmt. For each PartitionPruneInfo in > the plan, that would include the corresponding PartitionPruneState and > the bitmapset of surviving relids determined by initial pruning. Given > that a CachedPlan can contain multiple PlannedStmts, this would > effectively be a list of pruning results, one per statement. One > reasonable way to handle that might be to define a parallel data > structure, separate from PlannedStmt, constructed by plancache.c and > carried via QueryDesc. The memory and lifetime management would mirror > how ParamListInfo is handled today, leaving the executor API unchanged > and avoiding intrusive changes to PlannedStmt. > > However, one potentially problematic aspect of this design is managing > the lifecycle of the relations referenced by PartitionPruneState. > Currently, partitioned table relations are opened by the executor > after entering ExecutorStart() and closed automatically by > ExecEndPlan(), allowing cleanup of pruning states implicitly. If we > perform initial pruning earlier, we'd need to keep these relations > open longer, necessitating explicit cleanup calls (e.g., a new > FinishPartitionPruneState()) invoked by the caller of the executor, > such as from ExecutorEnd() or even higher-level callers. This > introduces some questionable layering by shifting responsibility for > relation management tasks, which ideally belong within the executor, > into its callers. > > My sense is that the complexity involved in carrying pruning results > via this parallel data structure was one of the concerns Tom raised > previously, alongside the significant pruning code refactoring that > the earlier patch required. The latter, at least, should no longer be > necessary given recent code improvements. One point I forgot to mention about this approach is that we'd also need to ensure permissions on parent relations are checked before performing initial pruning in plancache.c, since pruning may involve evaluating user-provided expressions. So in effect, we'd need to invoke not just ExecDoInitialPruning(), but also ExecCheckPermissions(), or some variant of it, prior to executor startup. While manageable, it does add slightly to the complexity. --=20 Thanks, Amit Langote