Re: Stored procedures or raw queries

From: Philip Semanchuk <philip(at)americanefficient(dot)com>
To: Simon Connah <simon(at)connah(dot)dev>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Stored procedures or raw queries
Date: 2025-08-06 13:16:50
Message-ID: 12D4341E-023D-420B-8522-DD9B97EC2B90@americanefficient.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On Aug 6, 2025, at 8:03 AM, Simon Connah <simon(at)connah(dot)dev> wrote:
>
> Hi,
>
> I'm pretty new to PostgreSQL and am building a simple website with it.
>
> My main question is whether I should use stored procedures / functions
> or whether I should embed raw SQL queries in my backend? I understand
> that procedures are faster as it cuts down on the round trip speed and
> the database can optimise it better.
>
> On the other hand raw SQL is much easier to manage as you just change
> the query in your bankend code without having to apply changes to the
> database at deployment time of your backend.
>
> What is considered the best approach? My backend is written in Go if
> that makes a difference.
>
> If you need any additional information then please let me know.

If your only concern is performance, then using stored procedures might be overoptimizing. It’s true that they cut down on overhead, but that overhead might be meaningless in terms of overall performance. For instance, if you have low latency between your web server and database server, and your typical query takes 5-6 seconds to execute, then most of your users’ wait time is due to execution time, not to communication between servers. Stored procedures won’t help you much.

If you’re building a simple web site, I suggest starting with what seems simplest for you (which sounds like raw SQL from your description) and then addressing performance problems as they arise. Stored procedures are one tool in the performance toolbox.

good luck
Philip

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message suporte02@unitsistemas.com.br 2025-08-06 13:17:56 Libpq.dll
Previous Message Charles Qi 2025-08-06 12:37:49 When UPDATE a row in a table with BEFORE ROW UPDATE trigger, the XMAX of new tuple is set to current XID