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 1iyX7Q-0001U2-9t for pgsql-general@arkaria.postgresql.org; Mon, 03 Feb 2020 08:30:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iyX7N-00021r-T5 for pgsql-general@arkaria.postgresql.org; Mon, 03 Feb 2020 08:30:05 +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 1iyX7N-00021k-Ad for pgsql-general@lists.postgresql.org; Mon, 03 Feb 2020 08:30:05 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iyX7K-0001LZ-Ai for pgsql-general@lists.postgresql.org; Mon, 03 Feb 2020 08:30:04 +0000 Received: by mail-wr1-x432.google.com with SMTP id a6so16700776wrx.12 for ; Mon, 03 Feb 2020 00:30:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=n26.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=uR3eqC+vd9PXZ2tskR/G4nuk9NsWa0UEKIILvZlKKyM=; b=CNDceuDTUFMozNepC02JLtdx9x7/ZEPom/uce6NDSf8Ad6NLy4bczY0V3nLVtcdsMh CvOAbnuFcVLuXASj7mWqabYQWc/U8xNHzfKX4+pORKKMu4laS4tRQ9e7eeeJMQ8ebv1P wAFZwF4EpaopVpbV6mjl06GSUFIi5b+yfKgx22/lhpydsBUP6sBJ2m3CA+pFwLRcluVW KSMemvMts7RNrRPKl0AUHRy8Avzgy6FrUA3I29bGAydop1a+0QRGS7jMvN9NjkxuC8WA rpWyHPRb2ymBllgCpVk62TFA8moDBVbLJqUBa5fZbOCc3Rton2wyMl05OjgT7xfSzKHU gXjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=uR3eqC+vd9PXZ2tskR/G4nuk9NsWa0UEKIILvZlKKyM=; b=FAs9fSq1j+Gj9v/XQVx+FppIcXutvK0hAaI9MXnTr5wsE0PrPxylk5tc5RhXOBzZe4 K/ECuyrU5ux4UL+9b100/oRUD4weRGiKmVEhM4SxsY5xkXtb3D+ZqeC+Yrz/gVOgG3J7 UvnlPmpK+QY+b4UAYjM0D/Lh7mf068XFJRY0GPs3demSTBeG81USSzui6LR/9beTCvWX jY2VGkUgYa8Gxjxw3BiXeRbB37vzrZkQ5RzAskyxDjmZocV2aKvDOh72Q9Rkao6BHXol wtVen91yzfVxBI1VUfI0sJFkXCT2RF1osCNolZo96rOxh0KDuDVVTberv7doyEje1idX 7iiA== X-Gm-Message-State: APjAAAUxpw0tAn210lBNMBDMQyM4VFKej4TScKBb50UjuKpoXjnTD8Tm PRucupn0pR78Bo+jmD44znJyv2Veq5oRow== X-Google-Smtp-Source: APXvYqxT8p+YRfMAzIEgeVlk4lDH/xIoUpb2NBvbMWSdJrhSqsQCpBpvjTtxD0+aXSTylRd9/oJigw== X-Received: by 2002:a5d:4d8d:: with SMTP id b13mr14107960wru.6.1580718600076; Mon, 03 Feb 2020 00:30:00 -0800 (PST) Received: from [10.4.23.40] ([62.96.137.138]) by smtp.gmail.com with ESMTPSA id y20sm22404990wmi.23.2020.02.03.00.29.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Feb 2020 00:29:59 -0800 (PST) From: Andrei Zhidenkov Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_64DE3A9F-3AD7-40EB-8B19-F05966616BC8" Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3601.0.10\)) Subject: Re: How to avoid UPDATE on same data in table ? Date: Mon, 3 Feb 2020 09:29:58 +0100 In-Reply-To: <22cd5ff9ef7651d08628567eafad0de1@stz-bg.com> Cc: Adrian Klaver , pgsql-general@lists.postgresql.org To: Condor References: <1a25799e60c79c65575f6e6375c96a52@stz-bg.com> <22cd5ff9ef7651d08628567eafad0de1@stz-bg.com> X-Mailer: Apple Mail (2.3601.0.10) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_64DE3A9F-3AD7-40EB-8B19-F05966616BC8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Have you tried to use built-in suppress_redundant_updates_trigger[1] for = this? 1. https://www.postgresql.org/docs/12/functions-trigger.html = > On 3. Feb 2020, at 09:27, Condor wrote: >=20 > On 02-02-2020 23:06, Adrian Klaver wrote: >> On 2/2/20 1:24 AM, Condor wrote: >>> Hello, >>> I'm using PostgreSQL 12.1 and trying to avoid update on table when = data is the same. I read somewhere if UPDATE is with the same data SQL = server on system level does not do update on table but don't know if = that is true or not. If that is not true I do: >>> First I create a function that should update data: >>> CREATE OR REPLACE FUNCTION log_last_chaged() RETURNS TRIGGER >>> LANGUAGE plpgsql >>> AS $$ >>> BEGIN >>> UPDATE status_table SET status0 =3D NEW.status0, lastchage =3D = CURRENT_TIMESTAMP WHERE rowid =3D OLD.rowid; >>> RETURN NEW; >>> END >>> $$; >>> then create table: >>> CREATE TABLE status_table ( >>> rowid INTEGER, >>> status0 INTEGER, >>> lastchage TIMESTAMP(0) WITHOUT TIME ZONE >>> ); >>> attach trigger: >> Why the DROP TRIGGER on card_sync_tbl? >> More below. >>> DROP TRIGGER last_changes ON card_sync_tbl; >>> CREATE TRIGGER last_changes >>> BEFORE UPDATE ON status_table >>> FOR EACH ROW >>> WHEN (OLD.* IS DISTINCT FROM NEW.*) >>> EXECUTE FUNCTION log_last_chaged(); >>> insert first data: >>> INSERT INTO status_table (rowid, status0) VALUES (11, 1); >>> INSERT INTO status_table (rowid, status0) VALUES (12, 2); >>> 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; >>> I receive something on rowid 12 that probably is error: >>> SQL statement "UPDATE status_table SET status0 =3D NEW.status0, = lastchage =3D CURRENT_TIMESTAMP WHERE rowid =3D OLD.rowid" >>> PL/pgSQL function log_last_chaged() line 3 at SQL statement >>> After quick look on duckduckgo I change the function to this: >>> CREATE OR REPLACE FUNCTION log_last_chaged() RETURNS TRIGGER >>> LANGUAGE plpgsql >>> AS $$ >>> BEGIN >>> NEW.lastchage :=3D CURRENT_TIMESTAMP; >>> RETURN NEW; >>> END >>> $$; >>> and everything seems work now, but that break the idea update not to = hit table if data is the same. >> Some changes based on: >> 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. " >> = https://www.postgresql.org/docs/12/plpgsql-trigger.html#PLPGSQL-DML-TRIGGE= R >> "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)." >> 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$ >> CREATE TRIGGER last_changes >> BEFORE UPDATE ON status_table >> FOR EACH ROW >> EXECUTE FUNCTION log_last_chaged(); >> 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) >> 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) >> NOTE: UPDATE 0 and no change in ctid >> 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) >> NOTE: UPDATE 1 and ctid change. >>> Any body can help with some hint ? Also I want to know why my first = function does not work, probably loop is happened if trigger does not = stop update to be sent to table on rowid 12 or syntax error. >>> Regards, >>> HS >=20 > Thank you for detailed explanation. > Have a good day. >=20 > Regards, > HS >=20 >=20 --Apple-Mail=_64DE3A9F-3AD7-40EB-8B19-F05966616BC8 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii Have = you tried to use built-in suppress_redundant_updates_trigger[1] for = this?

1. https://www.postgresql.org/docs/12/functions-trigger.html

On 3. Feb 2020, at 09:27, Condor <condor@stz-bg.com> = wrote:

On 02-02-2020 23:06, Adrian Klaver wrote:
On 2/2/20 1:24 AM, = Condor wrote:
Hello,I'm using PostgreSQL 12.1 and trying to avoid update on = table when data is the same. I read somewhere if UPDATE is with the same = data SQL server on system level does not do update on table but don't = know if that is true or not. If that is not true I do:
First= I create a function that should update data:
CREATE OR = REPLACE FUNCTION log_last_chaged() RETURNS TRIGGER
LANGUAGE = plpgsql
AS $$
BEGIN
=     UPDATE status_table SET status0 =3D NEW.status0, = lastchage =3D CURRENT_TIMESTAMP WHERE rowid =3D OLD.rowid;
=     RETURN NEW;
END
$$;
then create table:
CREATE TABLE status_table = (
    rowid INTEGER,
=     status0 INTEGER,
    = lastchage TIMESTAMP(0) WITHOUT TIME ZONE
);
attach trigger:
Why the DROP = TRIGGER on card_sync_tbl?
More below.
DROP TRIGGER = last_changes ON card_sync_tbl;
CREATE TRIGGER = last_changes
  BEFORE UPDATE ON status_table
  FOR EACH ROW
  WHEN (OLD.* IS = DISTINCT FROM NEW.*)
  EXECUTE FUNCTION = log_last_chaged();
insert first data:
INSERT = INTO status_table (rowid, status0) VALUES (11, 1);
INSERT = INTO status_table (rowid, status0) VALUES (12, 2);
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;
I receive something on = rowid 12 that probably is error:
SQL statement "UPDATE = status_table SET status0 =3D NEW.status0, lastchage =3D = CURRENT_TIMESTAMP WHERE rowid =3D OLD.rowid"
PL/pgSQL = function log_last_chaged() line 3 at SQL statement
After = quick look on duckduckgo I change the function to this:
CREATE OR REPLACE FUNCTION log_last_chaged() RETURNS = TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
    NEW.lastchage :=3D = CURRENT_TIMESTAMP;
    RETURN NEW;
END
$$;
and everything seems work = now, but that break the idea update not to hit table if data is the = same.
Some changes based on:
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.= "
https://www.postgresql.org/docs/12/plpgsql-trigger.html#PLPGSQL= -DML-TRIGGER
"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)."
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$
CREATE TRIGGER last_changes
 BEFORE = UPDATE ON status_table
 FOR EACH ROW
=  EXECUTE FUNCTION log_last_chaged();
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)
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)
NOTE: UPDATE 0 and no change in = ctid
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)
NOTE: UPDATE 1 and ctid change.
Any body can help with = some hint ? Also I want to know why my first function does not work, = probably loop is happened if trigger does not stop update to be sent to = table on rowid 12 or syntax error.
Regards,
HS

Thank= you for detailed explanation.
Have a good day.

Regards,
HS



= --Apple-Mail=_64DE3A9F-3AD7-40EB-8B19-F05966616BC8--