pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Simon Connah <simon.n.connah@protonmail.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Prepared statements versus stored procedures
Date: Mon, 20 Nov 2023 11:07:23 +0100
Message-ID: <10fa77d60703bae073a2f301443db84fe903b139.camel@cybertec.at> (raw)
In-Reply-To: <Kqgk_qyGD98su8jLVl1pTnkOJSD8qcyv3FhqFokRUCLKKZA8TF2oL0WQCVgm7DVGPTPHcjVxgPAfPP9QSgPWUXpT8swlSYKGMNDjP8Hzmbo=@protonmail.com>
References: <Kqgk_qyGD98su8jLVl1pTnkOJSD8qcyv3FhqFokRUCLKKZA8TF2oL0WQCVgm7DVGPTPHcjVxgPAfPP9QSgPWUXpT8swlSYKGMNDjP8Hzmbo=@protonmail.com>

On Sun, 2023-11-19 at 17:30 +0000, Simon Connah wrote:
> I was reading about prepared statements and how they allow the server to
> plan the query in advance so that if you execute that query multiple times
> it gets sped up as the database has already done the planning work.
> 
> My question is this. If I make a stored procedure doesn't the database
> already pre-plan and optimise the query because it has access to the whole
> query? Or could I create a stored procedure and then turn it into a prepared
> statement for more speed? I was also thinking a stored procedure would help
> as it requires less network round trips as the query is already on the server.

Statements in functions and procedures don't get planned until the function
or procedure is called for the first time.  These plans don't get cached unless
the procedural language you are using has special support for that.

Currently, only functions and procedures written in PL/pgSQL cache execution
plans of static SQL statements.  And you are right, that is usually a good thing.

Yours,
Laurenz Albe





view thread (7+ messages)  latest in thread

Message-ID: <10fa77d60703bae073a2f301443db84fe903b139.camel@cybertec.at>
Permalink:  ../10fa77d60703bae073a2f301443db84fe903b139.camel@cybertec.at/
Also on:    postgresql.org/message-id/10fa77d60703bae073a2f301443db84fe903b139.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, simon.n.connah@protonmail.com
  Subject: Re: Prepared statements versus stored procedures
  In-Reply-To: <10fa77d60703bae073a2f301443db84fe903b139.camel@cybertec.at>
  References: <Kqgk_qyGD98su8jLVl1pTnkOJSD8qcyv3FhqFokRUCLKKZA8TF2oL0WQCVgm7DVGPTPHcjVxgPAfPP9QSgPWUXpT8swlSYKGMNDjP8Hzmbo=@protonmail.com> <10fa77d60703bae073a2f301443db84fe903b139.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox