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 1wOG3g-001RZO-0X for pgsql-general@arkaria.postgresql.org; Sat, 16 May 2026 14:32:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wOG3e-003APm-2t for pgsql-general@arkaria.postgresql.org; Sat, 16 May 2026 14:32:02 +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 1wOG3e-003APe-1q for pgsql-general@lists.postgresql.org; Sat, 16 May 2026 14:32:02 +0000 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wOG3c-00000000wCH-2XII for pgsql-general@lists.postgresql.org; Sat, 16 May 2026 14:32:02 +0000 Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-bd124546379so169158666b.3 for ; Sat, 16 May 2026 07:32:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778941920; cv=none; d=google.com; s=arc-20240605; b=HMBe2sMSefiwkgNmRW16yMWbVuQj+QIlTjQv+Ak7XKHqzlhmAK3Vx6cInp+QETwLUd mOMOIaz7hCrvak+8+gR4Wa9uVfuh3UtwlhKxhiK1YmrRwR9X9ylkSQNc4s3T2sbjUxh2 JSOYqnuKlujpJ/cM0nbKVXdvSCL+rOCxjIMcJOXXWB7ojvXAGhC5JJNo/zXT4R4vg2ij i9g4lakgaj75+Lg9A3PULZj+hwp8G7G/WEgsWsu7AqXnIqC6f/PDYIOqGmrVhR5dBMR3 uUosM8+pjQF5EsXnCrA97NojSXyPvVy6jgjQH6O4oqKb/earWBY4zkijl3KS4yarWkhG /o4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=CBvnerHzfKn55k3Xmk/cviZDkPydCDmf4KqaI+5O1dM=; fh=ucrVzzhuycqoxNVm0ejRGNULldv0vmIiGgdoh8aqH38=; b=WngQX2riidUDHvmVA21Pm0yh5kZzUxSGv7kGOYPIjn5Tkr/ku8GWWObdb6fDVoPktk YTvJktGmBYBHEsnIXgMjufyAy6IUZKVWkHQRQZZ0zzD7DHxq+4W0oRShFHuCiUxWTZHN xxiqKekRACtnh6DIpYFKFQBp9p4qJZf6Oc7ftaY9P2g97ehciDwSDFYXt4LdRN1j3sGJ PtUlhQEq1sq89mqGasq5Y82ZMJ4X2NPO92HkEZN9/bU6dUYYF8PhOeIK7G+WIPkzrxL4 A58TDOJiYd2SD87hobx72tcoexLUGOVu7gzrMGikQg0mrut2dcNSFSjCeEq4SYat2UUl n13A==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778941920; x=1779546720; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=CBvnerHzfKn55k3Xmk/cviZDkPydCDmf4KqaI+5O1dM=; b=lyvu8zW3OzBdAxfyfwdNSmLv2NWcGF2TsJg9GyrfKNEDFg8526/il26dZMZ5EvL9qx sKsWSKyueDXkZlxlNvgP3SPvO0VCum5/k6DSrwDXIvjvUwlHO0WvhzF93ydk7UUXA1WY 5wDDKwmNEOwSOThFxgccxrE3yQ+AmdCyrFI0AK4TcDDqC6L5ZZz9a+YKRn6w74jiElcd +AJ8o604DBzD9KN+EEHPQAaUvxr7HoXC4QenxwMSApdiPQDyEYryKHurxpG4yR1w0wN8 BLrHz9Qk5AHtrZXnU4westiGrWtFB4yYAotr7wpEeOztp1JzsfqogRnd1kjkAAuMQtlO ElNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778941920; x=1779546720; h=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=CBvnerHzfKn55k3Xmk/cviZDkPydCDmf4KqaI+5O1dM=; b=YJYOnCsx8/sw6y/7Ba98wn/lK4nFj4HwH7X/kDcT5Y/gAbgXWc0xiwJuxh2J9tZ3A/ PaMxtKxhRN9ZPwH5ZTANXYL4sDTJHK+a0VelitFpuftn/pJcDzCyDAjmBSeX2UtVpkQf fvEaBvmXd575iPTc8peel8NEobhH3vEGL0MxIsoqDlsnYC9Tz68gwPsZ862n0u656WRK SRIHM4ju4VZllwT+RNMz99UG1vVwNGDv7EsFLWLIZoQ75ilwNSEEB41O0Z+wnH9EX1k6 uUdvSqIv7EOed6W4FmcWG0aDoNH06fx+0ITbavmplSjJiTB7mqo9YQ8/el9Px/2PrJI1 9bdA== X-Forwarded-Encrypted: i=1; AFNElJ9B7exZxa5sXPQpLipk/LOYJAiVzzFlwAUlVv72exX4DkpM3nNAG1L0a0Jk0WBCbhnJfB3Mhe+gpmmH5qkb@lists.postgresql.org X-Gm-Message-State: AOJu0YyV7OJyqSNEw3sQI1xClQ0lrkA7/3uOvc4U26DvJ4/ATfMZ50yT m89xpN3xNbqsBsaPBik1uQjUvUAB8hfYhxmy7P77gg0TTS0cf6e3OnseJMfJ70bq9JXWwEdYZUb q9DNPVcym8CQBBFxbCWsCjgqL8zeH8x0= X-Gm-Gg: Acq92OGEqHRd7r2yUaYsaRCb9h7i0nqtSJjUkrMAnlr8aDX2/fpBN2RbJCUvqu2Hqzc FcWfK7qNRr1WnhqOYWW7QZhNqkrEBI1CxtAYQJPu1iKwz4oGKODUkSGJPRVWsOkywG8AhDXGAo+ Iq7ttfuFHUFlkRgh+ylNGhsk+Mc1N/3ceoy8PYX2Y4/49up6EuF6ixIllDg2YFCjEHapWoL0LeR 3rMYYQLTqK05XwW/IahS9SIJ2oH4SCJcWsuBxIzWb023DMBlNK6yigcwIoeDef1U/yoAnoWHJac J62Mr2twXNU0LsH12CQ= X-Received: by 2002:a17:907:8b8e:b0:bd5:7c2:7622 with SMTP id a640c23a62f3a-bd517aa9716mr455823466b.49.1778941919460; Sat, 16 May 2026 07:31:59 -0700 (PDT) MIME-Version: 1.0 References: <1495292.1778939187@sss.pgh.pa.us> <02D2D7C5-7932-4030-B65A-4BECB645E9F0@gmail.com> In-Reply-To: <02D2D7C5-7932-4030-B65A-4BECB645E9F0@gmail.com> From: Vladimir Sitnikov Date: Sat, 16 May 2026 17:31:49 +0300 X-Gm-Features: AVHnY4LOS3aH7JFLITH0B8ay2ol4reJvAA8cWtlqqCnqrZDIg9ZkwLLyXYS3v6o Message-ID: Subject: Re: Double prepare To: Igor Korot Cc: Tom Lane , "pgsql-generallists.postgresql.org" , Rob Sargent Content-Type: multipart/alternative; boundary="0000000000002f82760651f031b9" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000002f82760651f031b9 Content-Type: text/plain; charset="UTF-8" >Might there be value in calling PQprepare as late as possible against most up-to-dare data? > Or is it better to run PQprepare for all known parameterized queries > in the very beginning of the program and just execute them when needed? In pgjdbc we maintain a client-side cache, so we don't have to prepare everything in advance. Frankly, I find it was a very helpful tool from the library perspective, as it automatically optimized applications without requiring application rebuild. The added prepared statement cache cut the app server's response times from 1s to 0.5s (think of a generic enterprise webpage). Note that sql text is not a sufficient caching key: the same sql text might have completely different execution plans depending on the parameter types. Note that statements prepare per-connection, so if you prepare everything in advance, there might be noticeable overhead (cpu and memory) if the specific connection uses only a few queries. Vladimir --0000000000002f82760651f031b9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
>Might there be value in calling PQprepare as late= as possible against most up-to-dare data?
> Or is it better to = run PQprepare for all known parameterized queries
> in the very begin= ning of the program and just execute them when needed?

In pgjdbc we = maintain a client-side cache, so we don't have to prepare everything in= advance.
Frankly, I find it was=C2=A0a very helpful tool from the libra= ry perspective, as it automatically optimized applications without
requi= ring application rebuild. The added prepared statement cache cut the app se= rver's response times from 1s to 0.5s (think of a generic enterprise we= bpage).

Note that sql text is not a sufficient caching key: the same= sql text might have completely different
execution plans dependi= ng on the parameter types.

Note that statements prepare per-connecti= on, so if you prepare everything in advance, there might be noticeable over= head (cpu and memory)
if the specific connection uses only a few = queries.

Vladimi= r

--0000000000002f82760651f031b9--