Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNIjP-0005ah-J4 for pgsql-novice@arkaria.postgresql.org; Fri, 19 Mar 2021 17:16:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNIjO-0008Lu-Hg for pgsql-novice@arkaria.postgresql.org; Fri, 19 Mar 2021 17:16:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNIjO-0008Ln-Bu for pgsql-novice@lists.postgresql.org; Fri, 19 Mar 2021 17:16:14 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNIjH-0006Wf-RP for pgsql-novice@lists.postgresql.org; Fri, 19 Mar 2021 17:16:13 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1lNIjE-00033y-GY; Fri, 19 Mar 2021 13:16:04 -0400 Date: Fri, 19 Mar 2021 13:16:04 -0400 From: Bruce Momjian To: Simon Connah Cc: "pgsql-novice@lists.postgresql.org" Subject: Re: Is it better keep most logic in the database or the Node.js application? Message-ID: <20210319171604.GB3566@momjian.us> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Mar 19, 2021 at 08:37:48AM +0000, Simon Connah wrote: > Hi, > > I've been wondering this for some time but is it better to keep the vast majority of the logic in the database using functions, stored procedures, triggers and everything else or should I keep the SQL in the Node.js web application and use that to directly query the database object? I would have thought using database functions would be faster as the database will know what the query is ahead of time and can perform some optimisations on it but this is just a guess on my part. > > What should I do? Keep everything in the Node.js application or do as much as possible in PostgreSQL? I have a blog entry about it: https://momjian.us/main/blogs/pgblog/2017.html#June_14_2017 -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.