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 1mfN7B-0004sb-Ij for pgsql-general@arkaria.postgresql.org; Tue, 26 Oct 2021 14:07:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mfN7A-0006Y0-H7 for pgsql-general@arkaria.postgresql.org; Tue, 26 Oct 2021 14:07:44 +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 1mfN7A-0006TY-6V for pgsql-general@lists.postgresql.org; Tue, 26 Oct 2021 14:07:44 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mfN77-0007Yl-1J for pgsql-general@postgresql.org; Tue, 26 Oct 2021 14:07:43 +0000 Received: by mail-ed1-x536.google.com with SMTP id w15so14157549edc.9 for ; Tue, 26 Oct 2021 07:07:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=L/lLgvpJMTIkIlMZCeygjMViQYOaSzEQjzctAmwflyY=; b=pLMRrPYmjWUu35NdNqioxhgBPI74Ugs4pU1mmFKDmS02Kmpv0HbwmiVBfiSp9JqRBY nkkgV0sCOF5sV2/z8a9NYZl1ThZ/g6CoEygzbHGxfAXAmteI9i9IBqO1DWd31wuO9Whu eJ2XR0wvKoigbAChqRzj1QLwa2qgbjclvTGJqvgyGs21Ebu2ta6O0ayivl+LFkSmfD95 ogEjpUg4AUwMdOxqfNB///oFdFva52qEFbSd4h5uCSfLRigL1n3YrVT8Olimx9KYIj99 Hn+n1C2dMlGWqDPog0gCWfi+uW2NVlNLQExs5z/DcZj10l01aBHj/pfWVuTHf9srzbUR TPmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=L/lLgvpJMTIkIlMZCeygjMViQYOaSzEQjzctAmwflyY=; b=NIxLOv61tNyhCprBixPw6MrH3v4j0/DmTKq0RoWwB/hqQyJXE0F8Qr0mlNeS5Hc+Uh 6C7SiHk8fQ4HC5OIxs24jyQaNqtaEk0Pe3gVdwR11WV+6Q0zZmh8Q4Uvk0WtcN7l1clY rBbpP2U42PrYRD4i63LlK81S+9eBMK7Yhsas1/AGg4JztIc3ApASVW6aP5pwAN24xFil sPm9TkdEe9zAKh5vA++rlpOsJ0oTczYUZsBtjq9e86ekYGUHXEDlxzRmG5tjsJL5IKCK mZz1oYinEi8jJRcZsyDbAL6beP7r3hUQ3O2z+C1yHyQoyGYPOdOB9RvWkBd8YDAf3jiG aqdA== X-Gm-Message-State: AOAM531/5y3eWZyUDM046RuZlSlTGsPLmouP1dg5jeHwoQSnSLXaEG6C wO62QUJUkcEzLDyOLrZWIaAAL2XenSU= X-Google-Smtp-Source: ABdhPJxFULyxN4OXzTPvMB5gVyCQdG/dpbnqJMk+/62AjfGln4uGnbcXm4RTJR0V5C1GK+NUbqPMBA== X-Received: by 2002:a05:6402:278a:: with SMTP id b10mr35961729ede.134.1635257143675; Tue, 26 Oct 2021 07:05:43 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id h10sm7630929edb.20.2021.10.26.07.05.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Oct 2021 07:05:43 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: Determining if a table really changed in a trigger From: Alban Hertroys In-Reply-To: Date: Tue, 26 Oct 2021 16:05:42 +0200 Cc: Postgres General Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Mitar X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 26 Oct 2021, at 9:05, Mitar wrote: >=20 > Hi! >=20 > I have a trigger like: >=20 > CREATE TRIGGER update_trigger AFTER UPDATE ON my_table REFERENCING NEW > TABLE AS new_table OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE > FUNCTION trigger_function; >=20 > I would like to test inside trigger_function if the table really > changed. I have tried to do: >=20 > PERFORM * FROM ((TABLE old_table EXCEPT TABLE new_table) UNION ALL > (TABLE new_table EXCEPT TABLE old_table)) AS differences LIMIT 1; > IF FOUND THEN > ... changed ... > END IF; >=20 > But this fails if the table contains a JSON field with the error: >=20 > could not identify an equality operator for type json Perhaps if you store an extra column containing a hash (for example MD5) = of the row contents (minus the hash column, obviously)? You can put an = index on the hash and match between OLD and NEW tables which ones = changed. When calculating the hash, you would have to specify the column names to = exclude the hash itself, so something like this: md5(row(col1, col2, col3)::text) The row-to-text conversion already takes care of converting JSONB(!) to = text. Don=E2=80=99t use this approach with JSON (as opposed to JSONB) type = fields though, a single extra space in the JSON structure would already = lead to a difference, as would other formatting differences. Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.