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 1mfdtM-0003jm-9X for pgsql-general@arkaria.postgresql.org; Wed, 27 Oct 2021 08:02:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mfdtL-0003b3-4S for pgsql-general@arkaria.postgresql.org; Wed, 27 Oct 2021 08:02:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfdtK-0003as-QC for pgsql-general@lists.postgresql.org; Wed, 27 Oct 2021 08:02:34 +0000 Received: from mout.gmx.net ([212.227.17.20]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfdtI-0003gK-75 for pgsql-general@lists.postgresql.org; Wed, 27 Oct 2021 08:02:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1635321749; bh=Z+vr1DeAHdXOK69jtThT6P8z0Ves78GihlUS150kI+o=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=L6lstmzjc4gn9I0iZt32YqmUZEsKG+WDQ1rWba327qyXHoM2Yc5Yp01RV9a1Bt20w T3V4fq8udQaNRIAUREDnWpiINUtUiNmsSG4VbxTQwQkkQ2zF4vB+6pVoBvroFOSAax KnVysWG0j7wWgQ8+0jU0JVcLbwTM6X4VXzwwZ2Ps= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [10.1.35.190] ([185.40.248.10]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Mo6qp-1mzuUJ2cLT-00pco1 for ; Wed, 27 Oct 2021 10:02:28 +0200 Subject: Re: Determining if a table really changed in a trigger To: pgsql-general@lists.postgresql.org References: From: Thomas Kellerer Message-ID: <021f82ab-110a-a663-1de2-687ef3645780@gmx.net> Date: Wed, 27 Oct 2021 10:02:27 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:b9JXkrvFc5JdrAipYcr5JhqfkN6h4oI9s7rDUWZo6aGkpTWpbnr kHZPzPdyPNAAeqkBOopieh+aEGFiQjqcVgw5X7ZO7UV6nYsHraILs3XYOh6A3StaZiM9DfE Ln+yi9h+Hfi6OHha+Gxby+uFwVnxBm6rYVMatnY4eoRXx4E7t62VHS2Vkbp9kgjhFx+5FBp N9sLyL094QObrs2Uk98Yg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:21H14jcw5ZY=:IfsyavjnD289ZJinYPEnPV ZPf3OMnFjqKMxlKgOUZE1qvxlmpgnCCuQjDfYGKtunthAMYXJdM37Y0+9vB+1KjaJfM/WgHfn rFaGvWnjEik+5TQgg5STJzZE9hB3mc7uHukt8UkfB9Jvn+gJSB1HtrkNrQu9f9txg8o6d4Keb yxp+JFeARpEIgL1ix+xZGsRlzbFZvw2VQXNH7YRd80TVrCF3EmtR4wQxTKDh375rtBZW1IFZw BEYFt05dbQrR3UdO7nx0b7qmpyHwAitos6UsVeNcqKoJaXuQfrK11GeuGUBw7I0aTK6SnERJT 18iAUsXca/puTeIx00Msi00qFCvNwhY5wD7A1OzUv3qr9+DDuv3JTToGplB6uJClIoQTqKDPi AKqlkjf5E50t+vjrfllYZbur2N6dmlXa8f1ghooie6v+48i//xz78hiNzEj02SBOuvMXhV95f ls/jzmpE95PavQ/3UWrWTBPPibvWhjrlTBZu9nxS7Ber6W5o4BIt1+bwb6GBTff2wQGSntqXn K9RYonUu6W6jou8sJslQ6WxMgKdPIcTIcgdjfsE81s+/kD6qwm4AbxrMUogiudaVFATaqSo9N IOrqeIjcxZsDS7ArVFO7PKjuqG6XDcBnLNI4wF0POfOc810/fGeWSsys2nAcNZ1aqZE/q8uXT Ess/Fux1iHhdqLCb1XUIdwLZZJNfJX3gSf0vk9b19WCmuCIv8FoYcaXfxO3fbZr1VnycBTsSA g7aCbUUpYZc4xmucBHRJQwZ3jcXB423KeKwxmP3GKTr6P3Njib8kzfhrtvtnzhIbnC5cUwZHU GZvAVYE7bIvoYL6FWHzd6WHFHPqm3TslmerRj/Bk6OHivaRZls= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Mitar schrieb am 26.10.2021 um 09:05: > I would like to test inside trigger_function if the table really > changed. I have tried to do: > > 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; > > But this fails if the table contains a JSON field with the error: > > could not identify an equality operator for type json > If you use jsonb (which is recommended over json anyway), then this would work. I would probably use a row level trigger instead of a statement level trig= ger Then compare the two records using IS DISTINCT FROM if new is distinct from old then .... end if; > I want to skip trivial updates (those which have not changed anything). It seems you might want to use the pre-defined function suppress_redundant= _updates_trigger() https://www.postgresql.org/docs/current/functions-trigger.html Might be faster than a self written trigger. Regards Thomas