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 1m8GjJ-0004LK-C1 for pgsql-novice@arkaria.postgresql.org; Tue, 27 Jul 2021 06:38:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m8GjH-0001g1-42 for pgsql-novice@arkaria.postgresql.org; Tue, 27 Jul 2021 06:38:15 +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 1m8GjG-0001fs-Rb for pgsql-novice@lists.postgresql.org; Tue, 27 Jul 2021 06:38:14 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m8GjA-00022S-35 for pgsql-novice@lists.postgresql.org; Tue, 27 Jul 2021 06:38:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=o1GaDcS/q/pORPAolNu/zh3ABGWb147IpmqWOc3rm90=; b=U8LBB/5sMRUVjvJC2fKCgzI5SW oz5ALzY7M9xVKH+XXXxYHCZR9O6IQgddSSd8wonRnXY6Pm9tTkP9AeLV2V7Bd5xNXcZujik6uHk4q OZVbXNu+VtyBuUqSbxqbpVIDQCjDvfYSUhZJIrld/frh6Qaizjln4PmSRJp/AAU13KT4=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1m8Gj7-0008Mo-V5; Tue, 27 Jul 2021 08:38:05 +0200 Date: Tue, 27 Jul 2021 08:38:05 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Nicolas Mitchell Cc: pgsql-novice@lists.postgresql.org Subject: Re: Trigger function Message-ID: <20210727063805.GA28983@depesz.com> Reply-To: depesz@depesz.com References: <30B8A3D0-8A87-40E2-BE40-31A17BA4B944@posteo.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <30B8A3D0-8A87-40E2-BE40-31A17BA4B944@posteo.net> 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 Mon, Jul 26, 2021 at 03:17:12PM +0000, Nicolas Mitchell wrote: > I am looking at whether functions can help me automate creating a new object > when a new host is added. > > I can do this manually with the following code: > > WITH object_id AS > (INSERT INTO public.object (type) > VALUES ( > ( SELECT obtype.id > FROM public.obtype > WHERE obtype.name LIKE 'host' > ) > ) > RETURNING id) > INSERT INTO host (name, domain, object) > VALUES ('gary', 1000001, (SELECT * FROM object_id)); > > I have a number of questions but I would like to begin by asking whether > this a candidate for a trigger function on table ‘host’, triggered before an > insert? *after* insert, not *before*. But other than that yes. If I might suggest: https://www.depesz.com/2012/11/14/how-i-learned-to-stop-worrying-and-love-the-triggers/ depesz