0001-Cache-the-result-of-RelationHasUnloggedIndex.patch

application/octet-stream

Filename: 0001-Cache-the-result-of-RelationHasUnloggedIndex.patch
Type: application/octet-stream
Part: 0
Message: Re: old_snapshot_threshold vs indexes

Patch

Format: format-patch
Series: patch 0001
Subject: Cache the result of RelationHasUnloggedIndex().
File+
src/backend/utils/cache/relcache.c 8 0
src/include/utils/rel.h 4 0
From 82ce2f9620097759261f9039befbeacf6ea3d295 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 25 Jun 2019 11:55:39 +1200
Subject: [PATCH] Cache the result of RelationHasUnloggedIndex().

When old_snapshot_threshold is configured, we frequently call
RelationHasUnloggedIndex() from heap_page_prune_opt(), and it
loops over all indexes in the table.

Let's cache its result in the Relation object so we don't slow
down scans of tables that happen to have lots of indexes, when
using the snapshot-too-old feature.

Author: Thomas Munro
Discussion: https://postgr.es/m/CA%2BhUKGKT8oTkp5jw_U4p0S-7UG9zsvtw_M47Y285bER6a2gD%2Bg%40mail.gmail.com
---
 src/backend/utils/cache/relcache.c | 8 ++++++++
 src/include/utils/rel.h            | 4 ++++
 2 files changed, 12 insertions(+)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 2b992d7832..e57fd8260a 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -5600,6 +5600,8 @@ load_relcache_init_file(bool shared)
 			rel->rd_refcnt = 0;
 		rel->rd_indexvalid = false;
 		rel->rd_indexlist = NIL;
+		rel->rd_hasunloggedindexvalid = false;
+		rel->rd_hasunloggedindex = false;
 		rel->rd_pkindex = InvalidOid;
 		rel->rd_replidindex = InvalidOid;
 		rel->rd_indexattr = NULL;
@@ -5949,6 +5951,9 @@ RelationHasUnloggedIndex(Relation rel)
 	ListCell   *indexoidscan;
 	bool		result = false;
 
+	if (rel->rd_hasunloggedindexvalid)
+		return rel->rd_hasunloggedindex;
+
 	indexoidlist = RelationGetIndexList(rel);
 
 	foreach(indexoidscan, indexoidlist)
@@ -5973,6 +5978,9 @@ RelationHasUnloggedIndex(Relation rel)
 
 	list_free(indexoidlist);
 
+	rel->rd_hasunloggedindex = result;
+	rel->rd_hasunloggedindexvalid = true;
+
 	return result;
 }
 
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index d7f33abce3..2803543d4d 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -108,6 +108,10 @@ typedef struct RelationData
 	Oid			rd_pkindex;		/* OID of primary key, if any */
 	Oid			rd_replidindex; /* OID of replica identity index, if any */
 
+	/* data managed by RelationHasUnloggedIndex */
+	bool		rd_hasunloggedindex;
+	bool		rd_hasunloggedindexvalid;
+
 	/* data managed by RelationGetStatExtList: */
 	List	   *rd_statlist;	/* list of OIDs of extended stats */
 
-- 
2.21.0