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 1ujeJa-00AGNS-K7 for pgsql-general@arkaria.postgresql.org; Wed, 06 Aug 2025 13:36:22 +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 1ujeJZ-00FqGi-ID for pgsql-general@arkaria.postgresql.org; Wed, 06 Aug 2025 13:36:21 +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 1ujeJZ-00FqGZ-6h for pgsql-general@lists.postgresql.org; Wed, 06 Aug 2025 13:36:21 +0000 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujeJW-0012rr-2I for pgsql-general@lists.postgresql.org; Wed, 06 Aug 2025 13:36:20 +0000 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-3b7892609a5so4488121f8f.1 for ; Wed, 06 Aug 2025 06:36:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=connah.dev; s=google; t=1754487378; x=1755092178; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=anSRYyWi9wiHGn6Iyk4NMYlmQQ8P/zFDkc3ONqF09Gk=; b=UXudpLBfR0vNwUAZ/MOMi5sQpZfLCrpWtzCKFeIategwIWa6GfXcl4hRN/3dv5YLr3 CVGOwd/xVXkepf3RkSHYqKg8cYdobknTEIR8HrPRh7c3L2OBUsf8RtcJwIwA3SziH2vX NSs7YVpWLHeiS0TvaLQTxivKtg6duo2qUeLhIg0EKKC1ij/UwUEyn+esGX96sMHjm5+r G5yYzudEix05Z39HcmMO5hXXA3e2ODqOGTlqZ2lhL0WQK6W/eIJAVS7K84R3RGd2pB53 41sJoDaHGYtPiJVAGj+Z4AYkLcU3PsAqX3oAhl9GELL8Q+F+6F3bRvzSJIQNXC7hMoIX rH4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754487378; x=1755092178; h=content-transfer-encoding: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=anSRYyWi9wiHGn6Iyk4NMYlmQQ8P/zFDkc3ONqF09Gk=; b=HWBYOFRf1hAExsq0Zxk2fZLMv68ARKe4q80jZWHCBwDwUoitb239y9jxre8mMO+xLL GLlqFznzesfXUW9op3PkDH3+ZrqJbAj7OSshEha+YgTB57TxMhgizU4atJMeNOi5AUt3 RIQI3Z7LB0P+1zZ2E5zjYVv1OcVWpMne+NXxGikTs2gOWQ+W6cjtczqVKBemg7e9V5TZ sVr3gzntvarSEn4Qj8/fJoRCP/Vlc97znAMGxhlOdhrqNXyE6LqM2l4lNQl+DFhTGW1I 8Wuuqmjkno77Ky9B+3xgdrp1m1CCwdqNH5sXYvkpBAkTfSq01G6SZ0xwEdeUg4ckQM7V 1Nyg== X-Gm-Message-State: AOJu0YxZKjQrpptL3DwZimvvrw4OFxKE3ntRkjIDCQdOBIGW34O2RfYp P8FpDutYSKxP96p4u1oUNyxE9wwbL8i31/7CXBwG0QrnaguYMcp+U/f2s51w+AmJYrd0GiyPvDi 9N5dhF+ZaYpUN9FFnBOl/CA2nITAq8vmT99BCbL0JKg3s9Ss5XW61Mfc= X-Gm-Gg: ASbGnctA5QuyHySZX5Usou62dlS/EKluvYIfzVpdWs8Do0h1slbR3gx+JUxhpP4V9pb YFRUFwGdYx1wn/AepTS2ya0Pa7B4lGrQHS6VsTGOpBcx98kBI+WJpB1TJRwzi+EGlUv0ktjRpj8 HyyrkH/JgWlLHVaa8+GEsQEjKkHvB8E/8RGYraKm/j1dDo1PGXICJlPrtg7qsxlCiqGD3fEkpNz PU= X-Google-Smtp-Source: AGHT+IHqcWJQvCn/d60nN1/v7v7XXpiH+GVrIhY1YywtHA0xQZ2sTvH80XgcoAdb/SYgedBs2CZ2vlRvYk7Qt01rufM= X-Received: by 2002:a5d:5d10:0:b0:3b7:96e8:7598 with SMTP id ffacd0b85a97d-3b8f41f4c7bmr2048715f8f.27.1754487377999; Wed, 06 Aug 2025 06:36:17 -0700 (PDT) MIME-Version: 1.0 References: <12D4341E-023D-420B-8522-DD9B97EC2B90@americanefficient.com> In-Reply-To: <12D4341E-023D-420B-8522-DD9B97EC2B90@americanefficient.com> From: Simon Connah Date: Wed, 6 Aug 2025 14:36:06 +0100 X-Gm-Features: Ac12FXyxBKrM4svvyRds-sDwQHuSq5lbVSqolkYwVmfYZicjiUaBc6n3rvI7nB8 Message-ID: Subject: Re: Stored procedures or raw queries To: Philip Semanchuk Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thank you all for your help. It is much appreciated. I'll have a play around and see how things work out. Simon. On Wed, 6 Aug 2025 at 14:17, Philip Semanchuk wrote: > > > > > On Aug 6, 2025, at 8:03=E2=80=AFAM, Simon Connah wro= te: > > > > 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 b= e overoptimizing. It=E2=80=99s true that they cut down on overhead, but tha= t overhead might be meaningless in terms of overall performance. For instan= ce, if you have low latency between your web server and database server, an= d 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 wha= t seems simplest for you (which sounds like raw SQL from your description) = and then addressing performance problems as they arise. Stored procedures a= re one tool in the performance toolbox. > > good luck > Philip >