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 1iP5Hg-0001aQ-Si for pgsql-performance@arkaria.postgresql.org; Mon, 28 Oct 2019 13:42:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iP5Hf-0000WT-KR for pgsql-performance@arkaria.postgresql.org; Mon, 28 Oct 2019 13:42:11 +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 1iP5Hf-0000WM-5f for pgsql-performance@lists.postgresql.org; Mon, 28 Oct 2019 13:42:11 +0000 Received: from mail.online.net ([62.210.16.11]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iP5Hb-0002RK-Lo for pgsql-performance@lists.postgresql.org; Mon, 28 Oct 2019 13:42:09 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.online.net (Postfix) with ESMTP id 8D65CF2B87E4 for ; Mon, 28 Oct 2019 14:42:05 +0100 (CET) Received: from mail.online.net ([127.0.0.1]) by localhost (mail.online.net [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id QPWRUe2PfgNC for ; Mon, 28 Oct 2019 14:42:05 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail.online.net (Postfix) with ESMTP id 67859F2B8886 for ; Mon, 28 Oct 2019 14:42:05 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.online.net 67859F2B8886 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=scaleway.com; s=CFA38AAE-D493-11E9-9041-6F7FC87095BF; t=1572270125; bh=VxppXO56ImXfoHdUXPqy0bdhBbSYG9NZkwVVZanmiRs=; h=To:From:Message-ID:Date:MIME-Version; b=c8yaTPIB/19Qn9CuWFLakNUu+x+DQRfUNDyXcatbtW5+328KBjchUanBpNbz9yWbb AYIsR6QQL7BNjH99C6XMKfuiO3Pu/uO5apRX+kv79VEkIGertIbd10/qlf6k8C2cbg K8BX0AT3+A3UZIMax/+cC6NV6lJVS0IehpKGcX9RaK1Z/NonrtLXPywDxxro0ef0P5 eoBOQU8TebXY8Y7T5sNbpkj2r9jI9Gfcp4bHvWPa6VtLOoRE28dUbYKdOamHJxnxkH vdgT2VslwzXj6dNTVlfmLjtAgopK1fkZcdzspsj6e2eOxef7BuDckDm0jaMI0DpXjO R/Z1i7RvSsTDA== X-Virus-Scanned: amavisd-new at mail.online.net Received: from mail.online.net ([127.0.0.1]) by localhost (mail.online.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id MttyIrmA17MS for ; Mon, 28 Oct 2019 14:42:05 +0100 (CET) Received: from MacBook-Pro-de-Greg.local (unknown [195.154.229.35]) by mail.online.net (Postfix) with ESMTPSA id 4BBD5F2B87E4 for ; Mon, 28 Oct 2019 14:42:05 +0100 (CET) To: pgsql-performance@lists.postgresql.org From: =?UTF-8?Q?Gr=c3=a9goire_de_Turckheim?= Subject: Notifications within triggers seem to compromise performance Message-ID: <2b617759-5c59-de3c-1de0-bed20d9c0db1@scaleway.com> Date: Mon, 28 Oct 2019 14:42:03 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------CBB63DDE92E1FFB9036EB03C" Content-Language: fr List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------CBB63DDE92E1FFB9036EB03C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Hi there! I guess we stumbled upon a performance issue with notifications sent with= in triggers (using PostgreSQL version 11.5) and I'd like your opinion about this. We want our app to maintain a data cache, so each instance of the app lis= tens to some channels (one per table). There are update triggers set up on the tables so each update yelds a not= ification to the appropriate channel. It works fine and we love the feature, but it seems to come with a perfor= mance cost. Since we set them up, we get query timeouts in our app (set to 200ms in t= he app). To try and understand this, we set deadlock_timeout to 100ms and enabled = log_lock_waits to get the following warnings in the log: process XXXXX st= ill waiting for AccessExclusiveLock on object 0 of class 1262 of database= 0 after YYY.YYY ms A row update transaction on table A is waiting for another row update tra= nsaction on table B. Tables are only tied by an FK, the updated fields ar= e not the ID or FK fields. A quick google + source code search showed thePreCommit_Notify = function is trying to acquire this lock. My educated guess of what happens during a COMMIT is the following : - pre-commit actions are taken, the "notification lock" is taken - commit actions are performed (can take some time) - post-commit actions are taken, the notification is enqueued and "notifi= cation lock" is released Am I correct ? Other transactions involving a notification are stuck waiting for previou= s transactions to finish, this can be a performance issue. I understand the need for lock to be taken pre-commit to ensure notificat= ion order matches transaction order, but it in my case I don't really car= e about the order and the performance penalty is high. We could think of several options there : - different locks for different channels (implies different notification = queues I guess) - an argument to NOTIFY query not to guarantee notifications order (and t= hus take and release the lock in post-commit actions) I believe the notify-in-trigger should be a pretty common usage pattern a= nd so this finding may impact quite a few systems. What do you think about this ? Regards, --=20 Gr=C3=A9goire de Turckheim --------------CBB63DDE92E1FFB9036EB03C Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hi there!

I guess we stumbled upon a performance issue with notifications sent with=
in triggers (using PostgreSQL version 11.5)
and I'd like your opinion about this.

We want our app to maintain a data cache, so each instance of the app lis=
tens to some channels (one per table).
There are update triggers set up on the tables so each update yelds a not=
ification to the appropriate channel.

It works fine and we love the feature, but it seems to come with a perfor=
mance cost.
Since we set them up, we get query timeouts in our app (set to 200ms in t=
he app).

To try and understand this, we set deadlock_timeout to 100ms and enabled =
log_lock_waits to get the following warnings in the log: process XXXXX st=
ill waiting for AccessExclusiveLock on object 0 of class 1262 of database=
 0 after YYY.YYY ms
A row update transaction on table A is waiting for another row update tra=
nsaction on table B. Tables are only tied by an FK, the updated fields ar=
e not the ID or FK fields.

A quick google + source code search showed the PreCommit_Notify function is trying to acquire this lock.
My educated guess of what happens during a COMMIT is the following :
- pre-commit actions are taken, the "notification lock" is taken
- commit actions are performed (can take some time)
- post-commit actions are taken, the notification is enqueued and "notifi=
cation lock" is released

Am I correct ?

Other transactions involving a notification are stuck waiting for previou=
s transactions to finish, this can be a performance issue.

I understand the need for lock to be taken pre-commit to ensure notificat=
ion order matches transaction order, but it in my case I don't really car=
e about the order and the performance penalty is high.

We could think of several options there :
- different locks for different channels (implies different notification =
queues I guess)
- an argument to NOTIFY query not to guarantee notifications order (and t=
hus take and release the lock in post-commit actions)

I believe the notify-in-trigger should be a pretty common usage pattern a=
nd so this finding may impact quite a few systems.

What do you think about this ?

Regards,
--=20
Gr=C3=A9goire de Turckheim
--------------CBB63DDE92E1FFB9036EB03C--