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 1nrlUh-0002L3-GF for pgsql-novice@arkaria.postgresql.org; Thu, 19 May 2022 19:07:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nrlUe-0007WZ-Mp for pgsql-novice@arkaria.postgresql.org; Thu, 19 May 2022 19:07:28 +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 1nrlUe-0007WP-Dr for pgsql-novice@lists.postgresql.org; Thu, 19 May 2022 19:07:28 +0000 Received: from mout.gmx.net ([212.227.17.21]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nrlUX-0002kQ-JP for pgsql-novice@lists.postgresql.org; Thu, 19 May 2022 19:07:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1652987239; bh=209vsYxcTCXFvX0stUg7c1W1CeEKifMddzTtLy8RNCM=; h=X-UI-Sender-Class:Date:From:To:Subject:In-Reply-To:References; b=BLN/BotQUNYDSMvv51gjiQd1BvIPm53pDREAQvuAbc7DpAolTD3gFLc3i/UD0uUYc w5UH9qXxZtjbx4VY8+KxAhIbqqUkukA1QCn2LOFaAGrHzTEcOeupiol/mso/wI4RVT DeP0wVzQuWI+lUVyux+HkBzxim4pMEllAOTA17GQ= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mail.defcon1.lan ([93.15.31.113]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1N95iR-1nlw4v40nv-0163wU for ; Thu, 19 May 2022 21:07:19 +0200 Date: Thu, 19 May 2022 21:07:16 +0200 From: Bzzzz To: pgsql-novice@lists.postgresql.org Subject: Re: Writing my first trigger Message-ID: <20220519210716.20a02035@msi.defcon1.lan> In-Reply-To: References: Organization: Anyone, anywhere but in banana demokratik republik of france or UERSS. Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:IN9YHgMQhxzUrjNX9BDDdFeFlU0iyhPeO5Kx7sX/IJBmnGHiEDT KgDUuYZkN6RGF0FkdDNMoZTlOjq2O8KmjiaZpxFIXKGvpT9kDpu4miE5NlWEAxaeuXEvV3V PgiVgdQVkWei5ef69P4v2RsBYevhmyAcEeLVri1HNByYEfaZv3wwjxCvqYN1j+rJezbAw8z Q98cHLqeN7LmdSnxRt1FA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:0GCERzW58KY=:4bHKWrX2gO/TykLeu4gT8i NqIvoyZSUtWXNTRUqZ0nhlYFe4RmgEmQTuj/Pmfr3fvK4IMqZmgdTfcQ1JbUOYvz48M0OBP/3 CGgg8/Gi8smoDYXWauivyGYVVM2n1/EbxeSNZpaZtgS6rPbXCV5s3DAJhG8vjsuFkOo3l8tn2 sMUJBQqoaHrVYmfMpe78JknKPxDFVGfSXK9iVRxJx4D8te3ujAcvYcg6RDc7M0rX1w7FLTEqx RyYFFK2m0/fSPXumTm925W5bR3usA6pVRtijZv2UhTc3JC5xoAEEJ281mmIJ873XYw6U+9wUJ cRHIdn9X87DqpE1rEkpevlw0WL7uj9t6EwTr22sz9edPBLauwODvWlS/wPeDPsV7DiudVE7K6 dDQvByY81zpwP3700AhEhKTLVhsQXrRsxSehfBSIBm5q596+64ZOsK3fp73JowGkO3SXc1Fvu Odu2+7n7wm14Let7eELqib25ja0SOBgNdtQCw6LZklU7fV34dTtOBdtZ/b4zfKb1zBpczGTbX SrO3X5xzrbfnUqItWbEPn6aSv+bpX8eePJs2wtvlzBmnvkyhxCQLGPNBexQGAwfRFuovU7jsd XnvLqzsuUV4X6TXiXil4msFzV9sq+2C4MMCZ6TqlTWaWcfCV0RXHfb8f4uSlqVOGo8H9eRFCR U7WrA96tfhS8+8//v5yzkBBSmAU6ktOKUfobu/q9DDUmd1eal+MdmQsCiKzmbfdvL2cCeZN/Z zElme57r5npt53/q62MFO3zErEmZuJA1GImGS5fgUf4xD8WfZCkAovWj7xauU2cJ2e9kf60GM q/hyj9qjIUdGTDyBm8eMQAbQn/Kw8MQCadyNPCHlgXyxBOuDk/aI37Zd7NaYWK7LqXpkitelO gxOdGpQYnACBNPKLUGLrZcwQSvpOJL7HUrekgSyJhAEnwpa5ukoELBuK0Rp3mTgasSr+P1lub V8nKQG0lfDYK5IXtR2aQe2o0TYLumHo+KcOaLN2zHjdCFGGKCg6OP1Lbndr6siLy/O/AR/dnz HR2cHHP1xvOD7yOOHMObNyDR5bah0RQciGZlc8x+aH6r5n7++c8DoSVtWa2Z0dsiZkWX460aP A7/wSge4/bIZ5qZbxLkivThIDCXfyp9/bkHp1UibiBO8/VEKs5olulZYw== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 19 May 2022 11:24:41 -0700 Chris Tsongas wrote: > Upon looking at my own code, I realized there's no reason for me to be > looking at the OLD values, I only care about the NEW: > > CREATE OR REPLACE FUNCTION update_employee() RETURNS TRIGGER AS $$ > BEGIN > IF (NEW."preferredFirstName" IS NOT NULL) THEN > NEW."fullName" =3D NEW."preferredFirstName" || ' ' || > NEW."lastName"; ELSE > NEW."fullName" =3D NEW."firstName" || ' ' || NEW."lastName"; > END IF; > NEW."updatedAt" =3D now(); > RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > Any other feedback appreciated! > > On Thu, May 19, 2022 at 11:18 AM Chris Tsongas > wrote: > > > > Working on my first trigger to create a fullName value from > > firstName, optional preferredFirstName, and lastName fields, where > > the full name uses the optional preferred first name if it exists, > > otherwise it uses the first name and of course the required last > > name. > > > > Would be great to get feedback on the following code before I try > > running it (note I already have an employee table, just including > > the CREATE TABLE statement for clarity): > > > > CREATE TABLE employee ( > > firstName text NOT NULL, > > preferredFirstName text, > > lastName text NOT NULL, > > fullName text, > > ); > > > > CREATE OR REPLACE FUNCTION update_employee() RETURNS TRIGGER AS $$ > > BEGIN > > IF (OLD."preferredFirstName" IS NOT NULL) THEN > > NEW."fullName" =3D OLD."preferredFirstName" || ' ' || > > OLD."lastName"; ELSE > > NEW."fullName" =3D OLD."firstName" || ' ' || OLD."lastName"; > > END IF; > > NEW."updatedAt" =3D now(); You may want to switch to : clock_timestamp() that record the time of writing (now() stays stuck on the calling time) > > RETURN NEW; > > END; > > $$ LANGUAGE plpgsql; > > > > CREATE TRIGGER fullName > > INSTEAD OF INSERT OR UPDATE ON employee > > FOR EACH ROW EXECUTE FUNCTION update_employee(); May be something closer to : CREATE TRIGGER triggername BEFORE UPDATE ON myschema.mytable FOR EACH ROW EXECUTE PROCEDURE mytriggerproc Jean-Yves