Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WcuQT-0006Pp-Oi for pgsql-performance@arkaria.postgresql.org; Wed, 23 Apr 2014 10:29:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WcuQS-0006Gt-Tz for pgsql-performance@arkaria.postgresql.org; Wed, 23 Apr 2014 10:29:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WcuQQ-0006Gk-Lt for pgsql-performance@postgresql.org; Wed, 23 Apr 2014 10:29:10 +0000 Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WcuQI-0006no-5x for pgsql-performance@postgresql.org; Wed, 23 Apr 2014 10:29:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20140207; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date; bh=0SzJyC9WiLmzP9+q0XC3ditKzanHfSqEmiSaPSIE4Bw=; b=ABtg0upXLp4I6WzNsq8ypeplDMs3p1hHIQsfGOTuxmaGwyn+kLK1XJHEqlyk7WsyR8IjMdILgaP0QwUnYpPp/ok4lElyi+C53e4UYvKZUok9JqHDll1SKJw09+mCziWt; Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by depesz.com with esmtpa (Exim 4.82) (envelope-from ) id 1WcuQF-0005Oh-WB; Wed, 23 Apr 2014 12:29:00 +0200 Date: Wed, 23 Apr 2014 12:28:59 +0200 From: hubert depesz lubaczewski To: "Verghese, Riya" Cc: "pgsql-performance@postgresql.org" Subject: Re: Best practices for update timestamp with/without triggers Message-ID: <20140423102859.GE4362@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="wTWi5aaYRw9ix9vO" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org --wTWi5aaYRw9ix9vO Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Apr 22, 2014 at 01:16:15AM +0000, Verghese, Riya wrote: > I am going to add a new column to a table for modify_date that needs > to be updated every time the table is updated. Is it better to just > update application code to set the modify_date to current_time, or > create a Before-Update trigger on the table that will update the > modify_date column to current_timestamp when the table is updated? > I also have slony in place, so the trigger will need to be on master > and slave. Slony will take care of suppressing it on the slave and > enabling in the event of a switchover, but it is additional overhead > and validation to make sure nothing failed on switchover. > So considering that we have slony, is it better to use application > code to update the modify_date or use a trigger?Is a trigger > essentially 2 updates to the table? Are there any other risks in using > the trigger? It's better (in my opinion) to use trigger. And it's not two updates. Just make your trigger function like: create function sample_trigger() returns trigger as $$ BEGIN NEW.modify_date :=3D clock_timestamp(); RETURN NEW; END; $$ language plpgsql; and that's all. Best regards, depesz --=20 The best thing about modern society is how easy it is to avoid contact with= it. http://depesz.= com/ --wTWi5aaYRw9ix9vO Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- iQIcBAEBCgAGBQJTV5XjAAoJEH8YKMd1okYDxXYQAMY+AxWO8c1Q9eF9y3n97BAL XSBqxnqM5rdfniMz4U/Y95tPJlSUYeNWLxbASrWVICHOPx51vIlJbmAwIuS0xR7F quBRaVLT3cjSfKhV4iJZvCpvo8MfOa2iQd/U9JDkhPKB9mKEjfjR3Np9KduyNpdD 1thH1vUSBtaBCW9tfJcIorr9ZygaRpCEtiOTnNxkCjXnwY8qa7r3rNb2l47HigUW 87N8322aJ4JHTlsJTQQ13M0FfEw1t7LJ6mMmYKCWKVY06rt9fn9asmirpeIfE7sU hjwpwvg6flKE97AmqhttdK7A6sEvOKHwvrT/ELiqxA8/4OAjE7ZdB5Ibpcl1PZu6 LFf5XzUpuHM1c4vOWKgSi0mr6TilpHG7RMKJMBpAReTZGf4mV/2F04P0uS7kiVAA zv7gEBopwCJdr6RC0NIy8Jbhyn1jKbtHAIAVyMpCJvjJSgdqIbe+fUdbJXNDOwrO MFQ9zkMgBEUarowexc+M2GOEPde3pD3Awwwdm82eNv+gT7ke45yNYdFQvn2EDFhS XVVjRW60T2KsmCyxx0hSTumaHrWO4xu5tZQ2NCssbzKuW5noqfJhaJYEq2qrC1vW 0tR6tkK9L6o470jFCLAUTjShizIAwemagPnDXLSuKINAPozj5Bp38kKt01bbyplw SkSLTRsMqa66Yv3QJfoe =rXhS -----END PGP SIGNATURE----- --wTWi5aaYRw9ix9vO--