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 1vNiIj-00BkgR-2c for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Nov 2025 01:57:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vNiIi-005s7s-0o for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Nov 2025 01:57:04 +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.96) (envelope-from ) id 1vNiIh-005s7k-2z for pgsql-hackers@lists.postgresql.org; Tue, 25 Nov 2025 01:57:04 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vNiIg-001Kmy-0e for pgsql-hackers@postgresql.org; Tue, 25 Nov 2025 01:57:03 +0000 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-343ee44d89aso7269624a91.2 for ; Mon, 24 Nov 2025 17:57:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764035819; x=1764640619; 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=1NmiWsjZSCXS1JNPKX9GoT1MHDDCnyM/RzNAgvyIkKg=; b=HqqX91LrowP7UTSRZvghx5+iuNnY5axEGX7b3b9IjY79NxBBnIpQb25Ag+QkKBkjdm NykKsfQE0lVAyXt1CZKycMcVlsFqX+mRVI0utOzIdJWKjTb/FsAxQzuPUsDU8m6F6QJP F0ALXPMiV/RaZPU9tt3XAkRXzmVAjXA7AsSHFJS24dqksj+9kHJ4RT4Wk7bMd6Y1IeFK Z0Do7WlUEcSr9sALqOKLFsSAzYlCBHIWw9E96+4rCpfuK+DcMimFJYkhwO5QM+uHU/io BAyxC8jw6ZeCMLcPrj69BwCnzYtraoMzxYNytKZo5YZkAphyIqsfGVnANhQNdRrKmEzj c4Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764035819; x=1764640619; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=1NmiWsjZSCXS1JNPKX9GoT1MHDDCnyM/RzNAgvyIkKg=; b=hZ+Trbg0SmWStZQ/kkwdZTNXgtpTB+ErlQMYccxTH2KR682mn3L55sd9QAOdupqFqc jhoQpQJHcPhC5zk2qI/6GsPJkBwZHMpHr6kjNPzmp5A1y8W3wDVm+98b5+TrpdYuhlZM Kn3XqPGQqZdJMUgQD3xD8hCBUWwvWFD3HYvcCO1aTvzdMh2PE2H0PceyV0qUY25r4kdQ INilOWGF1DkSoGjT5pv9I36dJUFFHM9FCfC6zVj9G/pBaICUjyGp7mrd6WeP5F+Pah+X UPCqIAJNK+oaYeedMZLBou/8S6qm5mtl6D2MZwzyXbRWr4GdXjDfZBjBZHgsy68pNlk2 qI7Q== X-Forwarded-Encrypted: i=1; AJvYcCWu73AugK2xkFSIdkR1Lb7U8NS8LQaZhl1MpPN7zb7Eguwf7cqBtXCh5gwJ/gnLRxiqvqfEUsxdTpZNFwHn@postgresql.org X-Gm-Message-State: AOJu0YwOx1Ongx1sQfkpbB3Jbb1kfdgw9ZDSDtRitHOITPFUPE56TyP5 vy9/6FDFLppTro8VdXIvlJ5g/mL8mt5mO0gTOqp04rnHA+LM3hs5brV9Y+vDHOaiC9b9L4Hfj2M L8DbTD+bXbw+33Le6Ysr3R3XFjmJgmBw= X-Gm-Gg: ASbGncsZV4hA9tfgaktfVcRMIg9thZaX06IL162D01ytjiWX50+cyNov/++PeJ/a5NH NYOrkgwr46rjel+hHSV9dXp0nvaPbaPS8aF6YnjEInl3gv5RiotkUbPqBbooyxsCsbA4GWpPdlB 0ocFr5NHqPUNfmrkSxtbwjSKHHbGl2pMY+3rNqWDv+P6OxYNu100AJgouTNGkKp5bHTf3m8Frrf VZHshpwybOIEQcWNMAJTQrAQY7M++u3/2ZDp7bPFRjEOhBVp1Pja9UQcBQ5v8R82RgfSmzY X-Google-Smtp-Source: AGHT+IGLf2VtQRhJavxYnIrD945rAa1XiOHACIYWCUF27auPiZt+2UBqoF+7IQRCnufj7ZNuDM+VukSFSbJQEN2+CNM= X-Received: by 2002:a17:90b:2750:b0:33b:cfae:3621 with SMTP id 98e67ed59e1d1-34733f36447mr13489405a91.32.1764035819455; Mon, 24 Nov 2025 17:56:59 -0800 (PST) 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, 25 Nov 2025 10:56:43 +0900 X-Gm-Features: AWmQ_bkmn6V9dgiJCNXPCLqLfHiVOyn6RmrLjvburLIk0jgJi1_OxYdTNqhM5r8 Message-ID: Subject: Re: generic plans and "initial" pruning To: Tender Wang Cc: Tom Lane , 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 Sun, Nov 23, 2025 at 9:17=E2=80=AFPM Tender Wang wr= ote: > Amit Langote =E4=BA=8E2025=E5=B9=B411=E6=9C=882= 0=E6=97=A5=E5=91=A8=E5=9B=9B 15:30=E5=86=99=E9=81=93=EF=BC=9A >> >> On Mon, Nov 17, 2025 at 9:50=E2=80=AFPM Amit Langote wrote: >> > On Wed, Nov 12, 2025 at 11:17=E2=80=AFPM Amit Langote wrote: >> > > * Enable pruning-aware locking in cached / generic plan reuse (0004)= : >> > > extends GetCachedPlan() and CheckCachedPlan() to call ExecutorPrep() >> > > on each PlannedStmt in the CachedPlan, locking only surviving >> > > partitions. Adds CachedPlanPrepData to pass this through plan cache >> > > APIs and down to execution via QueryDesc. Also reinstates the >> > > firstResultRel locking rule added in 28317de72 but later lost due to >> > > revert of the earlier pruning patch, to ensure correctness when all >> > > target partitions are pruned. >> > >> > Looking at the changes to executor/function.c, I also noticed that I >> > had mistakenly allocated the ExecutorPrep state in >> > SQLFunctionCache.fcontext whereas the correct context for execution >> > related state is SQLFunctionCache.subcontext. In the updated patch, >> > I've made postquel_start() reparent the prep EState's es_query_cxt to >> > subcontext from fcontext. I also did not have a test case that >> > exercised cached plan reuse for SQL functions, so I added one. I split >> > the function.c's GetCachedPlan() + CachedPlanPrepData plumbing into a >> > new patch 0005 so it can be reviewed separately, since it is the only >> > non-mechanical call-site change. >> >> I also noticed a bug in the prep cleanup logic that runs when a cached >> plan becomes invalid during the prep phase. Patch 0005 fixes that and >> adds a regression test that exercises the invalidation path. This will >> be folded into 0004 later. > > I spent time looking at these patches. > > I search all places that call GetCachedPlan(), and we always pass &cprep(= CachedPlanPrepData) to GetCachedPlan(). > In PrepAndCheckCachedPlan(), if the plan_cache_mode is force_generic_plan= , the LockPolicy is always LOCK_UNPRUNED. Because *cprep has never been NUL= L. > It seems that the LockPolicy has no chance to be LOCK_ALL. Do I miss some= thing here? Yes, eventually LockPolicy may end up redundant and we might not need AcquireExecutorLocksPolicy() at all, with a single locking path covering both cases. My goal initially was to stage the changes across call sites: keep a LOCK_ALL path for callers that still use the old lock everything up front behaviour, and gradually convert other callers to pass a non-NULL CachedPlanPrepData and handle the prep_list it may return, so that GetCachedPlan() can perform LOCK_UNPRUNED locking internally. That is why GetCachedPlan() accepts a possibly NULL cprep and why LockPolicy exists as a separate knob. For example, I decided to split out function.c refactoring of plan cache usage into its own patch. That made me realise that new users of GetCachedPlan() may appear that first adopt the simpler LOCK_ALL behaviour and only later switch to UNPRUNED when pruning aware locking becomes useful for them. Keeping the two paths preserves that incremental route and avoids forcing every new user to adopt CachedPlanPrepData and UNPRUNED locking up front. I am undecided yet if that two path structure is a good idea, but I am inclined to keep it for now. I would be happy to hear opinions on this. --=20 Thanks, Amit Langote