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 1ns0km-0005cq-Pc for pgsql-novice@arkaria.postgresql.org; Fri, 20 May 2022 11:25: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 1ns0kk-0002gp-AA for pgsql-novice@arkaria.postgresql.org; Fri, 20 May 2022 11:25:06 +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 1ns0kk-0002gg-0I for pgsql-novice@lists.postgresql.org; Fri, 20 May 2022 11:25:06 +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 1ns0kf-0004KP-Nw for pgsql-novice@lists.postgresql.org; Fri, 20 May 2022 11:25:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: 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=XYRh5TMyrLKKX6GZBfY8e/2tqJdZfYfMOnv2PwnbA2Q=; b=u6tEPF7XQZndQUTC1a5C8m8AbY XLfq147K287Xw17QiD+pS5uuzJo/aYFe8g7tNRQ7TiwtRsLqTu37uBcbpjNG11qT9vISUPnyjvZS8 OpbCtKaU3ZBskwibXiKuvivU1LrBWZGApWYtffX4Qo71nvDTdGE3eCqq6z7tuaZCUxjo=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1ns0kd-0001Yx-RN; Fri, 20 May 2022 13:24:59 +0200 Date: Fri, 20 May 2022 13:24:59 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Chris Tsongas Cc: pgsql-novice@lists.postgresql.org Subject: Re: Writing my first trigger Message-ID: <20220520112459.GA20589@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 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 Thu, May 19, 2022 at 11:18:48AM -0700, Chris Tsongas wrote: > CREATE OR REPLACE FUNCTION update_employee() RETURNS TRIGGER AS $$ > BEGIN > IF (OLD."preferredFirstName" IS NOT NULL) THEN > NEW."fullName" = OLD."preferredFirstName" || ' ' || OLD."lastName"; > ELSE > NEW."fullName" = OLD."firstName" || ' ' || OLD."lastName"; > END IF; > NEW."updatedAt" = now(); > RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > CREATE TRIGGER fullName > INSTEAD OF INSERT OR UPDATE ON employee > FOR EACH ROW EXECUTE FUNCTION update_employee(); I'm not sure instead of trigger will do what you want. The tasks looks much more like "before" trigger. The whole if is not really needed. You can use coalesce: NEW."fullName" := coalesce( NEW.preferredFirstName, NEW.firstName ) || ' ' || NEW.lastName; RETURN NEW; and really, really, really, read: https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_upper_case_table_or_column_names depesz