Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iyX52-0001Pa-0V for pgsql-general@arkaria.postgresql.org; Mon, 03 Feb 2020 08:27:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iyX50-0006up-Iw for pgsql-general@arkaria.postgresql.org; Mon, 03 Feb 2020 08:27:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iyX50-0006uh-2L for pgsql-general@lists.postgresql.org; Mon, 03 Feb 2020 08:27:38 +0000 Received: from stz-bg.com ([46.40.123.212] helo=mail.stzbg.com) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1iyX4r-0001KY-Ue for pgsql-general@lists.postgresql.org; Mon, 03 Feb 2020 08:27:36 +0000 Received: from www.stz-bg.com (stz-bg.com [IPv6:2a02:6800:0:99::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: condor@stz-bg.com) by mail.stzbg.com (Postfix) with ESMTPSA id 26D042E199F; Mon, 3 Feb 2020 10:27:27 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stz-bg.com; s=mail; t=1580718447; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9OW9aFNb1WRlZM/ClrqQX/AHRT2cpcGX8s2rDaCAxlM=; b=ZvnpYjdJE//quIyIy9+znxz/OMWTxx/AJQuFsgDwK7U5FcjRXOAqEPMTml0qykyPfwz3RF f398x6Tn1KA1CN7OUj+aUlEMiBJsdA5E8TnTa79k/3thsIRNc2x/q37UG2OL+F8s2/7ADI weq6m+opaCy9lrduwRc+zzwsBKwgUzI= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Date: Mon, 03 Feb 2020 10:27:25 +0200 From: Condor To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: How to avoid UPDATE on same data in table ? In-Reply-To: References: <1a25799e60c79c65575f6e6375c96a52@stz-bg.com> User-Agent: Roundcube Webmail/1.4.2 Message-ID: <22cd5ff9ef7651d08628567eafad0de1@stz-bg.com> X-Sender: condor@stz-bg.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02-02-2020 23:06, Adrian Klaver wrote: > On 2/2/20 1:24 AM, Condor wrote: >>=20 >> Hello, >>=20 >> I'm using PostgreSQL 12.1 and trying to avoid update on table when=20 >> data is the same. I read somewhere if UPDATE is with the same data SQL= =20 >> server on system level does not do update on table but don't know if=20 >> that is true or not. If that is not true I do: >>=20 >> First I create a function that should update data: >> CREATE OR REPLACE FUNCTION log_last_chaged() RETURNS TRIGGER >> LANGUAGE plpgsql >> AS $$ >> BEGIN >> =C2=A0=C2=A0=C2=A0 UPDATE status_table SET status0 =3D NEW.status0, las= tchage =3D=20 >> CURRENT_TIMESTAMP WHERE rowid =3D OLD.rowid; >> =C2=A0=C2=A0=C2=A0 RETURN NEW; >> END >> $$; >>=20 >> then create table: >> CREATE TABLE status_table ( >> =C2=A0=C2=A0=C2=A0 rowid INTEGER, >> =C2=A0=C2=A0=C2=A0 status0 INTEGER, >> =C2=A0=C2=A0=C2=A0 lastchage TIMESTAMP(0) WITHOUT TIME ZONE >> ); >>=20 >> attach trigger: >=20 > Why the DROP TRIGGER on card_sync_tbl? >=20 > More below. >=20 >> DROP TRIGGER last_changes ON card_sync_tbl; >> CREATE TRIGGER last_changes >> =C2=A0 BEFORE UPDATE ON status_table >> =C2=A0 FOR EACH ROW >> =C2=A0 WHEN (OLD.* IS DISTINCT FROM NEW.*) >> =C2=A0 EXECUTE FUNCTION log_last_chaged(); >>=20 >> insert first data: >> INSERT INTO status_table (rowid, status0) VALUES (11, 1); >> INSERT INTO status_table (rowid, status0) VALUES (12, 2); >>=20 >> and check do everything work fine: >> UPDATE status_table SET status0 =3D 1 WHERE rowid =3D 11; >> UPDATE status_table SET status0 =3D 4 WHERE rowid =3D 12; >>=20 >> I receive something on rowid 12 that probably is error: >> SQL statement "UPDATE status_table SET status0 =3D NEW.status0,=20 >> lastchage =3D CURRENT_TIMESTAMP WHERE rowid =3D OLD.rowid" >> PL/pgSQL function log_last_chaged() line 3 at SQL statement >>=20 >> After quick look on duckduckgo I change the function to this: >> CREATE OR REPLACE FUNCTION log_last_chaged() RETURNS TRIGGER >> LANGUAGE plpgsql >> AS $$ >> BEGIN >> =C2=A0=C2=A0=C2=A0 NEW.lastchage :=3D CURRENT_TIMESTAMP; >> =C2=A0=C2=A0=C2=A0 RETURN NEW; >> END >> $$; >>=20 >> and everything seems work now, but that break the idea update not to=20 >> hit table if data is the same. >=20 > Some changes based on: >=20 > https://www.postgresql.org/docs/12/sql-createtrigger.html > "In a BEFORE trigger, the WHEN condition is evaluated just before the > function is or would be executed, so using WHEN is not materially > different from testing the same condition at the beginning of the > trigger function. Note in particular that the NEW row seen by the > condition is the current value, as possibly modified by earlier > triggers. " >=20 > https://www.postgresql.org/docs/12/plpgsql-trigger.html#PLPGSQL-DML-TRIGG= ER >=20 > "Row-level triggers fired BEFORE can return null to signal the trigger > manager to skip the rest of the operation for this row (i.e., > subsequent triggers are not fired, and the INSERT/UPDATE/DELETE does > not occur for this row)." >=20 > CREATE OR REPLACE FUNCTION public.log_last_chaged() > RETURNS trigger > LANGUAGE plpgsql > AS $function$ > BEGIN > IF OLD.* IS DISTINCT FROM NEW.* THEN > RAISE NOTICE 'UPDATE'; > NEW.lastchage :=3D CURRENT_TIMESTAMP; > RETURN NEW; > ELSE > RETURN NULL; > END IF; > END > $function$ >=20 >=20 > CREATE TRIGGER last_changes > BEFORE UPDATE ON status_table > FOR EACH ROW > EXECUTE FUNCTION log_last_chaged(); >=20 > test=3D> INSERT INTO status_table (rowid, status0) VALUES (11, 1); > INSERT 0 1 > test=3D> INSERT INTO status_table (rowid, status0) VALUES (12, 2); > INSERT 0 1 > test=3D> select ctid, * from status_table ; > ctid | rowid | status0 | lastchage > -------+-------+---------+----------- > (0,1) | 11 | 1 | > (0,2) | 12 | 2 | > (2 rows) >=20 > test=3D> UPDATE status_table SET status0 =3D 1 WHERE rowid =3D 11; > UPDATE 0 > test=3D> select ctid, * from status_table ; > ctid | rowid | status0 | lastchage > -------+-------+---------+----------- > (0,1) | 11 | 1 | > (0,2) | 12 | 2 | > (2 rows) >=20 > NOTE: UPDATE 0 and no change in ctid >=20 > test=3D> UPDATE status_table SET status0 =3D 4 WHERE rowid =3D 12; > NOTICE: UPDATE > UPDATE 1 > test=3D> select ctid, * from status_table ; > ctid | rowid | status0 | lastchage > -------+-------+---------+--------------------- > (0,1) | 11 | 1 | > (0,3) | 12 | 4 | 02/02/2020 13:03:21 > (2 rows) >=20 > NOTE: UPDATE 1 and ctid change. >=20 >> Any body can help with some hint ? Also I want to know why my first=20 >> function does not work, probably loop is happened if trigger does not=20 >> stop update to be sent to table on rowid 12 or syntax error. >>=20 >> Regards, >> HS >>=20 >>=20 Thank you for detailed explanation. Have a good day. Regards, HS