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 1qaJ9p-008p0e-0L for pgsql-novice@arkaria.postgresql.org; Sun, 27 Aug 2023 17:02:37 +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 1qaJ9n-001LhA-MV for pgsql-novice@arkaria.postgresql.org; Sun, 27 Aug 2023 17:02:35 +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.94.2) (envelope-from ) id 1qaJ9n-001Lh2-Bk for pgsql-novice@lists.postgresql.org; Sun, 27 Aug 2023 17:02:35 +0000 Received: from mail-4322.protonmail.ch ([185.70.43.22]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qaJ9j-001Muv-ID for pgsql-novice@lists.postgresql.org; Sun, 27 Aug 2023 17:02:34 +0000 Date: Sun, 27 Aug 2023 17:02:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1693155749; x=1693414949; bh=OE2ikQspdBkPMcxMlrVCHwbg5vRU2wA4e9yWOE0bTSs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=l7iH39Ko5nys/imdAdqhLcz0ewLevyMMvkrkOIDkWyjLc8w9RS/LKTnZIvp3PdI5I JE+ci0fjO40WDLnQD/dO/EdHdq7Y0raj9c2zFVdAfZ7XEbXF4A/ZMP2seQ9NlAkz8Q txYYdGHNvFkm1LS3sFCR9BwKD93XspYGQMPhfhslIYZie7JFdvF0114A0AQUu1glLj L940gA3KgFJ75+rTfMcC3wVhU1CSjUJyUdPtSpjA0uBwOC4mBYaDjTG92gCnHY0WCq aK8ywHBuh9YRXz9pYB0FD1u2fl6sWstXp2VuTDa5HT8l7nxaP0gGJ7e8nUuIdWwW4l 2TxBScpAcBIQQ== To: Gurjeet Singh From: Simon Connah Cc: pgsql-novice Subject: Re: Stored procedures Message-ID: In-Reply-To: References: Feedback-ID: 24074989:user:proton MIME-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha512; boundary="------18f81a42ab13a8d37d438cc5794a67e56521206939d993202456dabb4efdb862"; charset=utf-8 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------18f81a42ab13a8d37d438cc5794a67e56521206939d993202456dabb4efdb862 Content-Type: multipart/mixed;boundary=---------------------fc3079d5ec59b50203c7a831ca7ec949 -----------------------fc3079d5ec59b50203c7a831ca7ec949 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain;charset=utf-8 ------- Original Message ------- On Sunday, August 27th, 2023 at 16:55, Gurjeet Singh wr= ote: > = > = > On Sun, Aug 27, 2023 at 4:49=E2=80=AFAM Simon Connah > simon.n.connah@protonmail.com wrote: > = > > I'm building a website in Python using the Django framework and am usi= ng the ORM at the moment but I can see a few situations where I might need= to write raw SQL. My question is do stored procedures execute faster when= compared to queries you generate using Python and then send to PostgreSQL= ? I would assume they would as there is less communication between the two= sides and also because PostgreSQL knows the stored procedure ahead of tim= e it should be able to better optimise it. > > = > > Is that true? Does PostgreSQL use a JIT for stored procedures which ma= ke them faster than one-off queries? > = > = > If your application logic involves many queries for one operation, > queries that produce the result that's then massaged and used in > subsequent queries, then yes, your application will benefit from > bundling that logic in a function/stored procedure and execute purely > on database server. This is because, as you noted, it reduces network > round-trips. Hence it will lower your latency for that one operation. > But also note that since the database is now doing most of the work, > and because it's limited by the number of CPUs on the database server, > this can lower your application's throughput; hence this is not > advisable if you have many more, and always querying client > connections than the number of CPUs on the server. > = > If you desire high throughput, and still wish to use functions to keep > the single-operation latency low, and if your application's workload > has a very high read:write ratio, you can split your read-only > workload to use streaming replicas/standby servers, which you can have > as many as you want (within reason :-). > = > Instead of, or in addition to functions, you may use CTE (aka WITH > clause) to bundle many SQL commands into one. > = > PL/pgSQL is an interpreted language, not a compiled one. It caches and > reuses the query plans of the SQL commands in the function, but that's > about it, in terms of optimizations. Unlike a compiled language, it's > not optimized to eliminate unnecessary operations, etc. As David > notes, plpgsql has some overhead, as well, so, for example, wrapping > single SQL statements in a plpgsql function will generally make the > operation slower than executing that SQL directly. > = > Postgres does have JIT optimizations for expression evaluation, but > that benefits all SQL commands, irrespective of whether they are > directly coming from the client, or wrapped in a function. > = > Best regards, > Gurjeet > http://Gurje.et Thank you both for your replies. That was really handy. -----------------------fc3079d5ec59b50203c7a831ca7ec949-- --------18f81a42ab13a8d37d438cc5794a67e56521206939d993202456dabb4efdb862 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: ProtonMail wnUEARYKACcFgmTrgZEJkFrvKC74ta6lFiEEXOYF9uqFRn4815bYWu8oLvi1 rqUAAO9UAQCyvlDzyfVR3Ior49haIRD6XVUdJBgTV5xwXVlyDAnHfwD9FkK9 5khhb5L3O7zTYBzyTGEzJLnVv9B1MQkq7/PfbAA= =nJcg -----END PGP SIGNATURE----- --------18f81a42ab13a8d37d438cc5794a67e56521206939d993202456dabb4efdb862--