Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ewZo5-00075i-Ih for pgsql-general@arkaria.postgresql.org; Thu, 15 Mar 2018 20:49:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ewZo4-0006z7-4j for pgsql-general@arkaria.postgresql.org; Thu, 15 Mar 2018 20:49:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ewZo3-0006yy-Sw for pgsql-general@lists.postgresql.org; Thu, 15 Mar 2018 20:48:59 +0000 Received: from tamriel.snowman.net ([2001:470:e38f::11]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ewZnw-0004yh-J2 for pgsql-general@lists.postgresql.org; Thu, 15 Mar 2018 20:48:59 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 791EE5F79C; Thu, 15 Mar 2018 16:48:51 -0400 (EDT) Date: Thu, 15 Mar 2018 16:48:51 -0400 From: Stephen Frost To: Enrico Thierbach Cc: pgsql-general@lists.postgresql.org Subject: Re: SELECT .. FOR UPDATE: find out who locked a row Message-ID: <20180315204851.GU2416@tamriel.snowman.net> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="edeba2vA178fKPoS" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --edeba2vA178fKPoS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Enrico Thierbach (eno@open-lab.org) wrote: > I am using `SELECT * FROM queue ... FOR UPDATE SKIP LOCKED` to implement a > queueing system. >=20 > Now I wonder if it is possible, given the id of one of the locked rows in > the queue table, to find out which connection/which transaction owns the > lock. Sure, you can attempt to lock the record and then run pg_blocking_pids() (in another session) against the pid which is trying to acquire the lock. Session #1: Connect SELECT * FROM queue ... FOR UPDATE SKIP LOCKED; =2E.. gets back some id X =2E.. waits Session #2: Connect SELECT pg_backend_pid(); -- save this for the 3rd session SELECT * FROM queue WHERE id =3D X FOR UPDATE; =2E.. get blocked waiting for #1 =2E.. waits Session #3: SELECT pg_blocking_pids(SESSION_2_PID); -- returns PID of Session #1 Obviously there's race conditions and whatnot (what happens if session #1 releases the lock?), but that should work to figure out who is blocking who. If you're on a version of PG without pg_blocking_pids then you can look in the pg_locks view, though that's a bit more annoying to decipher. Thanks! Stephen --edeba2vA178fKPoS Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJaqtwzAAoJEO1sijiDR2RVuV0P/ihI/XczCLSFFz4IGz778fuJ Eplqv2En4s/CyY+bvIxuscPYRKtjQrKUVMsWIQR2E4Ql36hO7+uPL6mPE46VAiZM bq+xA+YbzaGzVM5lMheztUSCefFyqBZG4BjmkQ0IHSw7uf0xrAhZXZ05FjRq/1Pe NQoXvrwY6R11pqiHy1M6KjceRjMyjg2jET1MezQDChafcUvoabNRJc9Q7mbAOaJv 0dUP60goIrWi1zBBICdzFhuwi5gDBqjeayQU74UWAxJfZ2JULGGZArZXKn06A+on wpFrUd0S3qoeySqqNa47fPMj7FYaHncfkjJ+qlYo3clsgf++dcYWyT5uxxNFq3VL Oggru2t36z0HV8RYN5e1ZLQqJpNC7Q+kM4ewMMUZnjA6WraBI9G4+JwrQh2eOOLE blipeUgDipsXzX+ksKwLvGQvjm/Vqvs8gZiD58PsA+ePwjEvL91cEmyMKw3p2CAZ B8SuEr+OHwXKCIRyhz1UK3CGpp+OxprigVn758O87e5IcNzmwtABINUUCe0VYQxS uAnuuF8dKKwxYUgJoTe/2Pu5oIx7BTGK+tAyKsCFMYvnY7SnySSVkf+pbtOxdhmq bgSf28BXNK92azE3ne8M4hioYxBWlR7+8QsPsqym5dfjC8KtQj6t2virhnCdaNvJ ED7J5wHZc5FiXBTRSzg9 =TEyf -----END PGP SIGNATURE----- --edeba2vA178fKPoS--