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 1nhMpD-0007UB-RG for pgsql-general@arkaria.postgresql.org; Thu, 21 Apr 2022 02:45:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nhMpB-0003yO-Q6 for pgsql-general@arkaria.postgresql.org; Thu, 21 Apr 2022 02:45:41 +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 1nhMpB-0003yF-BV for pgsql-general@lists.postgresql.org; Thu, 21 Apr 2022 02:45:41 +0000 Received: from mail-qt1-x82e.google.com ([2607:f8b0:4864:20::82e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nhMp8-0008Id-DH for pgsql-general@lists.postgresql.org; Thu, 21 Apr 2022 02:45:40 +0000 Received: by mail-qt1-x82e.google.com with SMTP id x24so2380594qtq.11 for ; Wed, 20 Apr 2022 19:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to; bh=x7GSe0F/Yt/FS8xcuv4Jr7YE1uqSw6Ep4sA/7nPJ+H4=; b=HiQU5J4WV6tps8meiE1avB5y4R86p3jsenvCby7DsBCjR18XEqtnbU76rGffiEq7Y3 muW/949J6aFePSe/fX/R8ACAGblNGFSnj7Ybw37s1LRb8RHg3FJ8d5PplMpuKiskAsXR TgANyXbNpi719+/fEsTdB0OsGTbdfjSFF4K9NlHSrmbpLxPNzcfnIrDimXPpyVT2nWA+ bIYpFqvj4cjxDJCvqBdJXv1e/VUjaznFycJX7rYGbZGOeOg8yWfKkFoZzLiko2nZ4gjP XKFkE+PjUBd6mBRImI+8fEPZS1TDS2eG/VPaGm9JiFxcvIeeF+A29HgFRATZpsfQFsYi 4IWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=x7GSe0F/Yt/FS8xcuv4Jr7YE1uqSw6Ep4sA/7nPJ+H4=; b=kaQ5AeFXUV0NmP2nPoCnc96Am8UaUTGFLiyGOGVn8Ekxu62JsZsPKuqUMkmM5cU8HC KHbAWRdTyukcBhwbqaZ6XsuI/jNQ7BKe4F8g/IYkjP3JpkM525emiYJFLBq9musFB7Tb 3VWwWtkLrULQps3bhYJ75tLTY4354wlPoKhq8oaBOEI4ow4sutEDvbWjILCiRx779Fzd 6F4E8iO3A8Wpjv1N7irGtdfgz2yXwNWf8By4L/Hm3WpKu/FKDWczI+jCURkB2XYNE3CZ Zk2eQe06lH+3q+XTX49P4oUaEU3ZSLhb39BYFtUQPEgCt0SNyFqczSG8Ejfu0+TYhRt8 S4Lg== X-Gm-Message-State: AOAM53240NHboVgeYzgex1cwoci/q8c2HP/cBqWE9S4k2vPqrJOwgNH1 4rPcUyyA/szfWUFLYhqNMW+qHkiO0XE= X-Google-Smtp-Source: ABdhPJwjCxT0ydCQNSdU7V7T/qnc6j8itdzLrXaeNHUgpq/xzil2hI33f6u/mlD/VBomi6FaFkKHyg== X-Received: by 2002:a05:622a:1211:b0:2f2:167:55dc with SMTP id y17-20020a05622a121100b002f2016755dcmr10999620qtx.105.1650509135709; Wed, 20 Apr 2022 19:45:35 -0700 (PDT) Received: from [192.168.2.100] (pool-72-79-19-93.nwrknj.east.verizon.net. [72.79.19.93]) by smtp.gmail.com with ESMTPSA id s195-20020a37a9cc000000b0069ca29ab6f4sm2500565qke.26.2022.04.20.19.45.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Apr 2022 19:45:35 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------NBtDTuVyaFtQYNmxkYzhyui4" Message-ID: Date: Wed, 20 Apr 2022 22:45:34 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: Are stored procedures/triggers common in your industry Content-Language: en-US To: pgsql-general@lists.postgresql.org References: <1D9B083B-8C41-4BB1-9A30-5ED2799AE963@gmail.com> From: Mladen Gogala In-Reply-To: <1D9B083B-8C41-4BB1-9A30-5ED2799AE963@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------NBtDTuVyaFtQYNmxkYzhyui4 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 4/20/22 15:18, Guyren Howe wrote: > I’ve really only ever worked in web development. 90+% of web > developers regard doing anything at all clever in the database with > suspicion. > > I’m considering working on a book about implementing business logic in > Postgres, and I’m curious about how common that actually is. Well, there are 2 schools of thought: 1. Put the business logic into the application 2. Put the business logic into the database Putting the business logic into the application can give you more flexibility around enforcing them. On the other hand, you also increase chances of inconsistency. There will likely be more than one application using reference tables like ADDRESS, ZIP_CODE, STATE, COUNTRY, QUARTER, ACCOUNT, CUSTOMER and similar. If there is a rule that a country must exist before you add an address in that country into the table, that can be enforced by a foreign key. Enforcing it within the application does 2 things: 1. Move the rule code to the application server which is traditionally weaker than a database server. In other words, you are more likely to run out of CPU juice and memory on an application server than you are likely to run out of resources on the DB server. 2. There is a possibility for inconsistency. Different applications can use different business rules for the same set of tables. That means that data entered by one application may make the table internally inconsistent for another application. I am a big proponent of using foreign keys, check constraints and triggers to enforce business rules. I am also a big proponent of avoiding NULL values wherever possible. Database design is an art. CAD software used to be popular once upon a time, in a galaxy far, far away.  Properly enforcing the business rules in the database itself makes the application more clear and easier to write. Regards -- Mladen Gogala Database Consultant Tel: (347) 321-1217 https://dbwhisperer.wordpress.com --------------NBtDTuVyaFtQYNmxkYzhyui4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 4/20/22 15:18, Guyren Howe wrote:
I=E2=80=99ve really only ever worked in web development. 90+% of we= b developers regard doing anything at all clever in the database with suspicion.

I=E2=80=99m considering working on a book about imp= lementing business logic in Postgres, and I=E2=80=99m curious about how com= mon that actually is.
3D""=

Well, there are 2 schools of thought:

  1. Put the business logic into the application
  2. Put the business logic into the database

Putting the business logic into the application can give you more flexibility around enforcing them. On the other hand, you also increase chances of inconsistency. There will likely be more than one application using reference tables like ADDRESS, ZIP_CODE, STATE, COUNTRY, QUARTER, ACCOUNT, CUSTOMER and similar. If there is a rule that a country must exist before you add an address in that country into the table, that can be enforced by a foreign key. Enforcing it within the application does 2 things:

  1. Move the rule code to the application server which is traditionally weaker than a database server. In other words, you are more likely to run out of CPU juice and memory on an application server than you are likely to run out of resources on the DB server.
  2. There is a possibility for inconsistency. Different applications can use different business rules for the same set of tables. That means that data entered by one application may make the table internally inconsistent for another application.

I am a big proponent of using foreign keys, check constraints and triggers to enforce business rules. I am also a big proponent of avoiding NULL values wherever possible. Database design is an art. CAD software used to be popular once upon a time, in a galaxy far, far away.=C2=A0 Properly enforcing the business rules in the databa= se itself makes the application more clear and easier to write.

Regards

--=20
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
--------------NBtDTuVyaFtQYNmxkYzhyui4--