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 1wDCZz-002ir1-18 for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2026 02:35:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wDCZy-003K4u-1I for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2026 02:35:42 +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 1wDCZy-003K4m-0L for pgsql-hackers@lists.postgresql.org; Thu, 16 Apr 2026 02:35:42 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wDCZv-00000001FE7-15kL for pgsql-hackers@postgresql.org; Thu, 16 Apr 2026 02:35:40 +0000 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-c76b994f7a8so2780111a12.3 for ; Wed, 15 Apr 2026 19:35:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776306939; x=1776911739; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0GnHyhcynv7MqvjkSi4dZKUCv19PYh7KVgcPNjkgf0Q=; b=l85VE0rc8/d5PfKmmBEny1jKt3HxpE2xP+wdAHfbs9PMWisUaTq8Gq6ADuX74s0byN 80rcLfw1cUmNZNl0upTQBI+sIhA6N+P/Lf4StJqrzu/jkFVnnKpP/FNHLFv0N285iV48 mBSqWQ8FaJCMsZetdB/q6g+ZuupjulKn3ID8mW7OHsS4jcVbbwfwz26qQqPI1xwiZq9i NQDDABb8iKyCIuiEP0eIXDv19NwQj0NLwb3y7C7DRyyu9lnmBI0/ue/JwnmOeF93k82E 6hxEV5fxT7iPMNsdAZZySTBuotEN26qDVoWlf3T+ppry7An7ml0eZTYG3zm2YnkEYWLY iGOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776306939; x=1776911739; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=0GnHyhcynv7MqvjkSi4dZKUCv19PYh7KVgcPNjkgf0Q=; b=XEw80JFBbBXigkq9CfrTZycHn6O/4BTQWbFSeyUpc/0yIKAChK7ykf14XFLmXmFx3v v6LStK5FadjOE9vmcXf6lL4TeEPyQFCNnmmUx4QrDeLXNDMjYfw9cvyjvZlryFKRiwkM tMbAYUd3UeTq1RLLoIn5IcEngG5F3FEV4GjzYnQqQY5l9jxC+9cw77QCLP0Yyo8egc04 G0/WhibprwGV9ZfejCMOj+e3YREPzrtZGIZGGO3U14iduwLRLzWSEBFxgQO8nAUHHGJ5 Jsvnim9PrQOKil3g2T3bIq2Tn8X2fDf2eiafO9AmF6ZMo+pJdOF7ee1m4pzUmMeZdWSg jbGA== X-Forwarded-Encrypted: i=1; AFNElJ970ZK6F/SEuFw+cBY2PfCgo0niEZJizd6C36GXthiKRisRSq5eiQqLXVeeWPHiS+PwC8DhAYnFt1zCsKMA@postgresql.org X-Gm-Message-State: AOJu0YwHI2TC1g3cs/xlbCWmcpEj96JPF8Rn8Eu33wJ1SY2exKwlw93C 1NnR9hr28RWlVTQtXj35KHKXdxA3bIIzg6A+UMCu/pOI1wA2FJP6kJPSBVe7sYnrLfo= X-Gm-Gg: AeBDiesskyopoGQihqaiEox4073Ae6dfF31RLco69ARY2Eu0TDiSPSCRLPbOmFI1z6h UAnwJMiE9AQgk41aJyvj4mGSrXAk4yShU+GC6EFW5RJaZUAId72ONOxF3dP3PwcQ1Bs0d4WKGVf 0px0IyecH2IlHYJ0z0k8ZfK5ZybZiOvrc1JmpSxyTFpg2LVo5PKdSWEWov99vOmVRaZ4XPvKoa+ NG19cSFZ6SqsItgLNyBPy9aAFHL6UqN0BJM1KZZZ1c2hZdjawHDXps3EBw/bmZnXnNJ+TAsE+gp WkXvPDE/CxkQ+P9jmr+lHow+On296pkjJZxASEArtvhYgmoN0vaDC5jZEuGoVz0vArR1e23hALT 6gmjvG2PUaaYs0KBfHdygWzNK8sjGrgUFRccWERznl7gcjXyFy0ut1BfplEBWJe/kcasa9I1lV8 mFqBpK0jJ0qkWICdUga/0JpEfNoHXachU= X-Received: by 2002:a17:902:f650:b0:2b2:4e70:6339 with SMTP id d9443c01a7336-2b2d5a58218mr250087335ad.25.1776306938609; Wed, 15 Apr 2026 19:35:38 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b478142565sm35084985ad.37.2026.04.15.19.35.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Apr 2026 19:35:38 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: GetCachedPlan() refactor: move execution lock acquisition out From: Chao Li In-Reply-To: Date: Thu, 16 Apr 2026 10:34:58 +0800 Cc: Kirill Reshke , PostgreSQL-development Content-Transfer-Encoding: quoted-printable Message-Id: <6B155E97-EDE7-4CBE-8196-E709F6A4B33D@gmail.com> References: To: Amit Langote X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Apr 15, 2026, at 21:46, Amit Langote = wrote: >=20 > On Wed, Apr 15, 2026 at 11:14=E2=80=AFAM Amit Langote = wrote: >> On Wed, Apr 15, 2026 at 1:08=E2=80=AFAM Kirill Reshke = wrote: >>>> +static bool >>>> +LockCachedPlan(CachedPlan *cplan) >>>> +{ >>>> + AcquireExecutorLocks(cplan->stmt_list, true); >>>> + if (!cplan->is_valid) >>>> + { >>>> + AcquireExecutorLocks(cplan->stmt_list, false); >>>> + return false; >>>> + } >>>> + return true; >>>> +} >>>=20 >>> simply `return cplan->is_valid ` would be more Postgres-y here, isnt = it? >>=20 >> Agreed, will fix too. >=20 > Thinking more about this bit, checking only cplan->is_valid after > locking is not really equivalent to what CheckCachedPlan() was doing > before. >=20 > CheckCachedPlan() can also reset plan->is_valid based on other state > (role, xmin), whereas in v1 the new check relied only on lock inval > callbacks to have set is_valid to false. That means the new check is > not enough. It may happen to be okay for the current callers in this > patch, but it is not something I think is safe to rely on once there > are future callers that do arbitrary work, such as ExecutorPrep(), > even if carefully coded, between the original GetCachedPlan() / > CheckCachedPlan() step and the later recheck after locking. >=20 > Separately, I also realized that v1 was introducing redundant lock > acquisition for freshly built plans, which is not really a no behavior > change refactoring either. >=20 > So in v2 I ended up reworking two parts more substantially: >=20 > * The patch now factors the reused-generic-plan validity logic into > RecheckCachedPlan(). >=20 > * It also adds an is_reused output argument to GetCachedPlan(), so > callers can distinguish the reused-plan case from a freshly built plan > and avoid the extra lock step in the latter case. >=20 > --=20 > Thanks, Amit Langote > V2 overall looks good, it preserves the current behavior. I just have a question. RecheckCachedPlan is only called by = LockCachedPlan and CheckCachedPlan, and both callers are static, why = RecheckCachedPlan is exported? Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/