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 1uje0y-00ACRI-IP for pgsql-general@arkaria.postgresql.org; Wed, 06 Aug 2025 13:17:09 +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 1uje0w-00FkXy-5X for pgsql-general@arkaria.postgresql.org; Wed, 06 Aug 2025 13:17:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uje0v-00FkXm-MS for pgsql-general@lists.postgresql.org; Wed, 06 Aug 2025 13:17:05 +0000 Received: from mail-qk1-x72c.google.com ([2607:f8b0:4864:20::72c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uje0s-0012jL-2C for pgsql-general@lists.postgresql.org; Wed, 06 Aug 2025 13:17:04 +0000 Received: by mail-qk1-x72c.google.com with SMTP id af79cd13be357-7e7f940a386so213121885a.3 for ; Wed, 06 Aug 2025 06:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=americanefficient-com.20230601.gappssmtp.com; s=20230601; t=1754486223; x=1755091023; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=uy5QRUFA9dQ7PyKjr+SbtO8VAN07qtlRBjuI1Gcuh/E=; b=JZrLyhESsdbdX6nbVqTxO6mLDPl1Ab6zgteT7U7IrjeWSj7dzTQ3Ju/F1j9x8t9pB1 oGgNCqHh/n4L3e+uBxbRXS6nix3sWebUPxShcRiD8LT6x5atli0ZlY9hhsQdQs7JErss ffoiVnhJ1Y0lrn7ebbJbDokh0XfwjQkmv27nMwbBwKebXOm5A0ThV1+8pFSThVXMMcS1 q4wwS1xma9GaKXhF/goEFmyvYAQcoQNqs3SCToX39SBw07uOF65geiRTxRZ26Bmbk7J2 Ig+bcRGCAm0r28sA77JmV11jMZNSFe3gF/Mn4rF19CZKrFptrzdlQIMnWsn5+l/UP1Ea yAmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754486223; x=1755091023; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uy5QRUFA9dQ7PyKjr+SbtO8VAN07qtlRBjuI1Gcuh/E=; b=MuBjDUvyDjLjUz/T5bPvDY3QKuROAB36scIWQIEWtRFqBcqYRd+Oz9/XcB4jfGkGGq qH8rS3ufjOFGPLtsYwfmDroDkdBVLmUfcl8+IA8oIINsI/aLX2C+12qUsA5MUD+hNerp 4L2U+z+UQJJbw9STuNJ31ZDsqTW4YTZk2ge6XnJv1oS4qblJOhlG88syP+OpG63egjm/ puruUUPSW2P1ig34lXESbXb+rj91gi41jlDZAnmV+tfcGFeFMomhfRLdZ6E77ncrHfMt KD6PYPyMxMgKpxmLfwm3I2QUhs3OGcoLgrwFhVaeXj842XkHFWa9CzKw0suPb+s9jLQK Msmw== X-Gm-Message-State: AOJu0YyTba1mWahoGsKetRmTOYEDxiCNFp0P+n9aPz9uPnoWy2zbQksQ SDIegDSOTYMF2yNrQCEpsnKE3Y7qEOYb8TrbnpIH3d2UJdnGd168+YYHCWEXHQ/QRSCdUoIeF7g nQO4i X-Gm-Gg: ASbGnctjNwEiKcflr6jKXC0FHS++Qd30bl0KaGthTQ8VZS39knV1V3dNTotBgun8xlJ vxGAJkvJZt67QVmo0TsQuuufSkeYWFt8XfF7mWXk8DJ3CM54JYWtVDKOEealeIxCZThdyDROCBW T1Ieu+uvq2CyMbPh47Y9dWOg9lpCK0l3sYy5ZAC8i0LOEVRT6tKTb1UXNhPGPBMrtLkOxo5aGpJ i5cGLHkBHbaRC3yEQ4DUCQd+9Qll7diklamEhd3kzUX/iAsG88e3QaitMst420hBwTOT1m+m64l L5e3MXXUaQ+RJC3F4zenk15YXGPa1bXUnwsCiWNHRDA0zpapsGpLghrTwrSMuXbQotuz+CM1LZX oS4VkptVvoxarUqW+XPCQMlt7tV10mtwe8b5YgjXL8QdKsbPr0EMPQH28Wvvc9WU0M8LflSi2LM OjEWnxyYKR00S+3dBF7Zrfxoo= X-Google-Smtp-Source: AGHT+IEzV2csw7Lr4tVhdTgr/qGntUu8uJkqqb4Z4Jyq+USl/QoEjcEQIOwlyHopCP1O+6MWX0YAww== X-Received: by 2002:a05:620a:7004:b0:7e3:57ea:c6d7 with SMTP id af79cd13be357-7e8166067ecmr296623785a.15.1754486222482; Wed, 06 Aug 2025 06:17:02 -0700 (PDT) Received: from smtpclient.apple (syn-098-026-024-127.res.spectrum.com. [98.26.24.127]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7e67f5cd509sm820638185a.40.2025.08.06.06.17.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Aug 2025 06:17:01 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: Stored procedures or raw queries From: Philip Semanchuk In-Reply-To: Date: Wed, 6 Aug 2025 09:16:50 -0400 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <12D4341E-023D-420B-8522-DD9B97EC2B90@americanefficient.com> References: To: Simon Connah X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Aug 6, 2025, at 8:03=E2=80=AFAM, Simon Connah = wrote: >=20 > Hi, >=20 > I'm pretty new to PostgreSQL and am building a simple website with it. >=20 > 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. >=20 > 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. >=20 > What is considered the best approach? My backend is written in Go if > that makes a difference. >=20 > 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=E2=80=99s 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=E2=80=99 wait time is due to execution time, not to = communication between servers. Stored procedures won=E2=80=99t help you = much. If you=E2=80=99re 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.=20 good luck Philip=