From 835d94dc1e37473a7bc93cee095aa3c123c8e614 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 30 Mar 2019 22:40:10 +1300
Subject: [PATCH] Fix deadlock in heap_compute_xid_horizon_for_tuples().

We can't call code that uses syscache while we hold buffer locks
on a catalog.  If called for a catalog, just fall back to the
effective_io_concurrency GUC rather than trying to look up the
tablespace's IO concurrency setting.

Diagnosed-by: Peter Geoghegan
Discussion: https://postgr.es/m/CA%2BhUKGLCwPF0S4Mk7S8qw%2BDK0Bq65LueN9rofAA3HHSYikW-Zw%40mail.gmail.com
---
 src/backend/access/heap/heapam.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index de5bb9194e..8fefd2a3fe 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -6976,8 +6976,15 @@ heap_compute_xid_horizon_for_tuples(Relation rel,
 	 * more prefetching in this case, too. It may be that this formula is too
 	 * simplistic, but at the moment there is no evidence of that or any idea
 	 * about what would work better.
+	 *
+	 * Since the caller holds a buffer lock somewhere in rel, we'd better make
+	 * sure that isn't a catalog before we call code that does syscache
+	 * lookups, or it could deadlock.
 	 */
-	io_concurrency = get_tablespace_io_concurrency(rel->rd_rel->reltablespace);
+	if (IsCatalogRelation(rel))
+		io_concurrency = effective_io_concurrency;
+	else
+		io_concurrency = get_tablespace_io_concurrency(rel->rd_rel->reltablespace);
 	prefetch_distance = Min((io_concurrency) + 10, MAX_IO_CONCURRENCY);
 
 	/* Start prefetching. */
-- 
2.21.0

