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 1ujcwB-009z9s-BJ for pgsql-general@arkaria.postgresql.org; Wed, 06 Aug 2025 12:08:07 +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 1ujcwA-00FVKi-9x for pgsql-general@arkaria.postgresql.org; Wed, 06 Aug 2025 12:08:06 +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 1ujcw9-00FVKZ-Ty for pgsql-general@lists.postgresql.org; Wed, 06 Aug 2025 12:08:05 +0000 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujcw7-0013gE-1e for pgsql-general@lists.postgresql.org; Wed, 06 Aug 2025 12:08:05 +0000 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-31ece02ad92so5375533a91.2 for ; Wed, 06 Aug 2025 05:08:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754482081; x=1755086881; 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=4M01eSqBsnMQDzcAgBOuhVzN0nowrqbACHzOO6ON5GE=; b=DwMOsABfMpen11TC2PY07oeHR3KiDomC6a41fBL8o60f9ZaKO5iZObOdMOGIOa/wcZ xWhopXdoEAjzQ0zME0ps4/EkSa8ACa1UqP2ZCyCLmuLpiAUJnd90+i8kwrpJMf6VspNR /yYUmIA4BJ/BEREhfa05wFqmxX1hn2sbtPnhMdspQXfxIhpSQCaaiObbDTuVh5Yalbl/ C/4e3YjKvcdXmKh9G0myYAN6RxIH9qD9V0+5Ss3HhL9y5DTHUwv4k5T+HxqU6+YZ/k5f D4GH+Xgdp67XStMJ6ustwgtwjfzdRmfOkIOamPf1tfIPJwF/68UkbbCtieNuN+lJZZHj e43Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754482081; x=1755086881; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4M01eSqBsnMQDzcAgBOuhVzN0nowrqbACHzOO6ON5GE=; b=qu6wu+tVvP7vUK+3y/THZw1NsaJCgNErgn0K0Sbelodj+TtvsqYj4cSnZ37q3JFNfR ApSe3RVC2J8RhMF/hBQ2oT6RCQS0EBXKuvrPM1fp0DvBY+P6Xy0M7/chtS9hqjEuczlD 1B25TqWLNr+EChKHy7DGh+wHVQXB9wICpWQXqvB7N51lJARQEoVQNm3cT1miE5TVhxXb crn0HIv26C2/sSk/LiWAWS6PEKy3LT4i+VXVxEo89ygzY4zuNtjpA0ZfJ1uIZLlTBifc FwfO9tgJZntPTrMKO7eL48lIgQ86XsOpMaL89KlGt0vNg49cj4As9E3Lzmp9Gs41uU2T ozsQ== X-Gm-Message-State: AOJu0YzpoW8XlfNdlz38mu7eLZMQ8Kq/Lt4EN82D+MoMLUKyL4lEOKZ9 JsybK1m3pr/3xBt7PRZk8mjbuv073OtY+y6Ps6e9FfdKNp1ExxmIVyifcboAHsLX6EjX2e7teTU A+ZH1CBFvgyricJfcsHZzPMlruf/j+Q3QAA== X-Gm-Gg: ASbGncvjs80AqsYucRsPwEluvuiIv8eayyyghoV11OjgWAYeabRvAwB0wxp5UGyl1Ed 3FRSM9EjYECqOOKzOgLNnXZZMwDwJHtf6DwZJqintWeDt9zt4HKfd+4WlU5+m3ef5woxQxZsTwC 8wTABS8YLj/I92HWheJs76COcSQVzVdYdqWdjwqrxdcZD3IAR+vYS3woT/ZOY5h2ZmNMxG0pRDF 143Y+U= X-Google-Smtp-Source: AGHT+IFOxHTbcb2D/M/H54k/37I6JUU+jAmNiaK0VwQMDgTpvgf5T4U//fhWgz7Efeno3KtSsk1eEgWDQkuojDFZosg= X-Received: by 2002:a17:90b:5284:b0:320:ff84:ceb5 with SMTP id 98e67ed59e1d1-32166ca7c61mr3978905a91.16.1754482081165; Wed, 06 Aug 2025 05:08:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Justin Swanhart Date: Wed, 6 Aug 2025 08:07:49 -0400 X-Gm-Features: Ac12FXxTvSLbEg30BQNL1yB9s4XDFot5JL1_-rdQl1gg2hlCcl4W1q4FRluet3E Message-ID: Subject: Re: Stored procedures or raw queries To: Simon Connah Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000003676c5063bb131cd" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003676c5063bb131cd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Generally you should use stored procedures when it will reduce the number of round trips to the database. Wrapping simple SELECT statements in a stored proc just adds friction for updating the application especially as the number of developers grows. On Wed, Aug 6, 2025, 8:04=E2=80=AFAM Simon Connah 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. > > Simon. > > > --0000000000003676c5063bb131cd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Generally you should use stored procedures when it will r= educe the number of round trips to the database.=C2=A0 Wrapping simple SELE= CT statements in a stored proc just adds friction for updating the applicat= ion especially as the number of developers grows.

On W= ed, Aug 6, 2025, 8:04=E2=80=AFAM Simon Connah <simon@connah.dev> wrote:
Hi,

I'm pretty new to PostgreSQL and am building a simple website with it.<= br>
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.

Simon.


--0000000000003676c5063bb131cd--