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 1m8XeC-0000Uu-Bt for pgsql-novice@arkaria.postgresql.org; Wed, 28 Jul 2021 00:42:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m8Xe9-00010J-6C for pgsql-novice@arkaria.postgresql.org; Wed, 28 Jul 2021 00:42:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m8Xe8-0000zF-U4 for pgsql-novice@lists.postgresql.org; Wed, 28 Jul 2021 00:42:04 +0000 Received: from mout02.posteo.de ([185.67.36.66]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m8Xe6-0000iN-0f for pgsql-novice@lists.postgresql.org; Wed, 28 Jul 2021 00:42:03 +0000 Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id BF68D240101 for ; Wed, 28 Jul 2021 02:41:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1627432917; bh=gxHdCMF5YEYvIXDFvvqO/d0/tWldYuMqinQdQGVmeqY=; h=From:To:Cc:Subject:Date:From; b=CLMtelnFTiKS4UdRz1Ff256M/zC167biHALWOQRTI1d0MrYcZxIK6ctTwsUucH4zA oiAd+5/rV3raVcmrh0v8ajy5WoRN5/nNuyy1/a3Tyoo4fSdP1B/PMw+RU56HGcSjkU swu3FKpv8+kMhBgO5fDSNG1DVIwEMW7P+3A7jJyTzK70VelVGnfJ4BgBE9+A3JhVEN C/owaZ8HMudTgpqYwzTHKylCRfk0Dz5Ft9xKWd5pXGmYq6bUGnsMJ/i3SFo4YnzZMa q739w3nL3HZEd6Wcgso6RAX9uA8s+QZiC+XRmO/GEKTa7ehBJJA74c9W5VIdU/5TIC jHerjhiliav+Q== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GZFGr49Vjz9rxF; Wed, 28 Jul 2021 02:41:56 +0200 (CEST) From: "Nicolas Mitchell" To: "David G. Johnston" Cc: "hubert depesz lubaczewski" , pgsql-novice Subject: Re: Trigger function Date: Wed, 28 Jul 2021 00:41:55 +0000 Message-ID: <09E00165-C826-44D5-9CD2-E767DFFEC099@posteo.net> In-Reply-To: References: <30B8A3D0-8A87-40E2-BE40-31A17BA4B944@posteo.net> <20210727063805.GA28983@depesz.com> <2D1DF30A-25BC-48CA-B502-D8B3147B9BEB@posteo.net> <53180757-855C-4FFF-B5CC-572BAEFD9397@posteo.net> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 28 Jul 2021, at 0:19, David G. Johnston wrote: > On Tue, Jul 27, 2021 at 4:04 PM Nicolas Mitchell > > wrote: > >> >>> When you write a trigger for a table you should be executing >>> commands >>> against the same table. >> >> I believe this implies that the code creating a new object (INSERT >> INTO >> public.object…) row should reside elsewhere. >> >> I worked out a solution to my question (as stated, but with a different table): create function func__new_user_object() returns trigger language plpgsql as $$ begin if NEW.object is NULL then -- RAISE NOTICE 'No user.object value, generating a new user object'; WITH object_id AS ( INSERT INTO public.object (type) VALUES ( ( SELECT obtype.id FROM public.obtype WHERE obtype.name LIKE 'user' )) RETURNING id) SELECT * FROM object_id INTO NEW.object; end if; RETURN NEW; end $$; CREATE TRIGGER trig__new_user_object BEFORE INSERT ON public."user" FOR EACH ROW EXECUTE PROCEDURE public.func__new_user_object(); > > Personally, I'm generally against placing this kind of data > construction > logic inside triggers. I have been working with a mind to keep as much logic as I can inside the database/PostgreSQL. I can’t tell from your comment whether you prefer other mechanisms available within PG to achieve the same, or prefer to manage these operations in an application. If within PG, then I’d view that as something I should explore. Otherwise, I’m not keen, at present, to push things into an application when they can be achieved within PG - this being my own (fairly uneducated) preference. As you may have gathered, this is a new area for me and I am interested to hear opinions - just a few pointers - now I have what seems to be a working solution to my question. Nic