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 1nguT8-0005TR-OF for pgsql-general@arkaria.postgresql.org; Tue, 19 Apr 2022 20:29:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nguT6-0005Jf-Qe for pgsql-general@arkaria.postgresql.org; Tue, 19 Apr 2022 20:29:00 +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 1nguT6-0005Id-Cc for pgsql-general@lists.postgresql.org; Tue, 19 Apr 2022 20:29:00 +0000 Received: from relay8-d.mail.gandi.net ([2001:4b98:dc4:8::228]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nguT3-0000bk-Em for pgsql-general@postgresql.org; Tue, 19 Apr 2022 20:28:59 +0000 Received: from localhost (pc-227-216-214-201.cm.vtr.net [201.214.216.227]) (Authenticated sender: andrew@acooke.org) by mail.gandi.net (Postfix) with ESMTPA id 4AB191BF206; Tue, 19 Apr 2022 20:28:54 +0000 (UTC) Received: from localhost (localhost [IPv6:::1]) by localhost (Postfix) with ESMTP id 7C5883F295; Tue, 19 Apr 2022 16:28:51 -0400 (-04) Date: Tue, 19 Apr 2022 16:28:51 -0400 From: andrew cooke To: pgsql-general@postgresql.org Subject: Repeated, never-ending deadlock Message-ID: <20220419202851.5utfi4pangkyt4sd@acooke.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi All, OK, apologies in advance, I feel like this is a rather unintelligent question that I probably know the answer to, but I am banging my head against a wall trying to understand what is happening. I have an ETL program, written in Python, that uses mutiple processes to read and load a bunch of files into a database. Sometimes (especially early on, when the database is new) common information that occurs in several files, and which is normalized out into sub-tables, causes deadlocks. That's OK; the code should receive an exception, back off, try again, reading first, etc. And I see exceptions occuring in the Python code log as expected. However, today, I saw the same deadlock error repeated in the Postgres log every second for minutes. The same two processes, with the same data, and the same tables. I can't work out how this can happen without a crazy tight loop in the Python code that's just repeatedly trying to commit a value (which afaik doesn't exist!) A fragment of the Postgres log is below. The Python logs show nothing. So my question is - is there some circumstance where instead of raising an error to the calling code, Postgres instead goes round in circles? An answer of "nope, it must be a bug in your code" is fine / expected. It's just one of those days... This is Postgres 14 with a fairly default config (except that logical replication is enabled). Thanks, Andrew 2022-04-19 19:55:54.482 UTC [28560] ERROR: deadlock detected 2022-04-19 19:55:54.482 UTC [28560] DETAIL: Process 28560 waits for ShareLock on transaction 609318483; blocked by process 28559. Process 28559 waits for ShareLock on transaction 609318682; blocked by process 28560. Process 28560: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'KNW', 'HHZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher Process 28559: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'CRY', 'HNZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher 2022-04-19 19:55:54.482 UTC [28560] HINT: See server log for query details. 2022-04-19 19:55:54.482 UTC [28560] CONTEXT: while inserting index tuple (25,3) in relation "channel_nscl_nscl_idx" 2022-04-19 19:55:54.482 UTC [28560] STATEMENT: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'KNW', 'HHZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher 2022-04-19 19:55:55.488 UTC [28560] ERROR: deadlock detected 2022-04-19 19:55:55.488 UTC [28560] DETAIL: Process 28560 waits for ShareLock on transaction 609318483; blocked by process 28559. Process 28559 waits for ShareLock on transaction 609318682; blocked by process 28560. Process 28560: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'KNW', 'HHZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher Process 28559: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'CRY', 'HNZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher 2022-04-19 19:55:55.488 UTC [28560] HINT: See server log for query details. 2022-04-19 19:55:55.488 UTC [28560] CONTEXT: while inserting index tuple (25,4) in relation "channel_nscl_nscl_idx" 2022-04-19 19:55:55.488 UTC [28560] STATEMENT: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'KNW', 'HHZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher 2022-04-19 19:55:56.495 UTC [28560] ERROR: deadlock detected 2022-04-19 19:55:56.495 UTC [28560] DETAIL: Process 28560 waits for ShareLock on transaction 609318483; blocked by process 28559. Process 28559 waits for ShareLock on transaction 609318682; blocked by process 28560. Process 28560: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'KNW', 'HHZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher Process 28559: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'CRY', 'HNZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher 2022-04-19 19:55:56.495 UTC [28560] HINT: See server log for query details. 2022-04-19 19:55:56.495 UTC [28560] CONTEXT: while inserting index tuple (25,5) in relation "channel_nscl_nscl_idx" 2022-04-19 19:55:56.495 UTC [28560] STATEMENT: INSERT INTO channel_nscl (net, sta, chan, loc) VALUES ('AZ', 'KNW', 'HHZ', '--') RETURNING channel_nscl.rowid, channel_nscl.publisher