v2-0004-Update-some-code-that-handled-systable_beginscan-.patch

text/plain

Filename: v2-0004-Update-some-code-that-handled-systable_beginscan-.patch
Type: text/plain
Part: 3
Message: Re: Don't overwrite scan key in systable_beginscan()

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v2-0004
Subject: Update some code that handled systable_beginscan() overwriting scan key
File+
src/backend/utils/cache/catcache.c 20 22
src/backend/utils/cache/relfilenumbermap.c 3 4
From c14d4860eea754573eb28e86ee108e1a7e176c52 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Sat, 10 Aug 2024 16:56:45 +0200
Subject: [PATCH v2 4/5] Update some code that handled systable_beginscan()
 overwriting scan key

Some code made extra copies of a scan key before systable_beginscan()
in a loop because it used to modify the scan key.  This is no longer
the case, so that code can be simplified and some comments updated.
---
 src/backend/utils/cache/catcache.c         | 42 +++++++++++-----------
 src/backend/utils/cache/relfilenumbermap.c |  7 ++--
 2 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 111d8a280a0..348ebf81d37 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -1468,19 +1468,18 @@ SearchCatCacheMiss(CatCache *cache,
 	 */
 	relation = table_open(cache->cc_reloid, AccessShareLock);
 
+	/*
+	 * Ok, need to make a lookup in the relation, copy the scankey and fill
+	 * out any per-call fields.
+	 */
+	memcpy(cur_skey, cache->cc_skey, sizeof(ScanKeyData) * nkeys);
+	cur_skey[0].sk_argument = v1;
+	cur_skey[1].sk_argument = v2;
+	cur_skey[2].sk_argument = v3;
+	cur_skey[3].sk_argument = v4;
+
 	do
 	{
-		/*
-		 * Ok, need to make a lookup in the relation, copy the scankey and
-		 * fill out any per-call fields.  (We must re-do this when retrying,
-		 * because systable_beginscan scribbles on the scankey.)
-		 */
-		memcpy(cur_skey, cache->cc_skey, sizeof(ScanKeyData) * nkeys);
-		cur_skey[0].sk_argument = v1;
-		cur_skey[1].sk_argument = v2;
-		cur_skey[2].sk_argument = v3;
-		cur_skey[3].sk_argument = v4;
-
 		scandesc = systable_beginscan(relation,
 									  cache->cc_indexoid,
 									  IndexScanOK(cache, cur_skey),
@@ -1788,19 +1787,18 @@ SearchCatCacheList(CatCache *cache,
 
 		relation = table_open(cache->cc_reloid, AccessShareLock);
 
+		/*
+		 * Ok, need to make a lookup in the relation, copy the scankey and
+		 * fill out any per-call fields.
+		 */
+		memcpy(cur_skey, cache->cc_skey, sizeof(ScanKeyData) * cache->cc_nkeys);
+		cur_skey[0].sk_argument = v1;
+		cur_skey[1].sk_argument = v2;
+		cur_skey[2].sk_argument = v3;
+		cur_skey[3].sk_argument = v4;
+
 		do
 		{
-			/*
-			 * Ok, need to make a lookup in the relation, copy the scankey and
-			 * fill out any per-call fields.  (We must re-do this when
-			 * retrying, because systable_beginscan scribbles on the scankey.)
-			 */
-			memcpy(cur_skey, cache->cc_skey, sizeof(ScanKeyData) * cache->cc_nkeys);
-			cur_skey[0].sk_argument = v1;
-			cur_skey[1].sk_argument = v2;
-			cur_skey[2].sk_argument = v3;
-			cur_skey[3].sk_argument = v4;
-
 			scandesc = systable_beginscan(relation,
 										  cache->cc_indexoid,
 										  IndexScanOK(cache, cur_skey),
diff --git a/src/backend/utils/cache/relfilenumbermap.c b/src/backend/utils/cache/relfilenumbermap.c
index 9e76f745297..8dbccdb551e 100644
--- a/src/backend/utils/cache/relfilenumbermap.c
+++ b/src/backend/utils/cache/relfilenumbermap.c
@@ -141,7 +141,6 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 	SysScanDesc scandesc;
 	Relation	relation;
 	HeapTuple	ntp;
-	ScanKeyData skey[2];
 	Oid			relid;
 
 	if (RelfilenumberMapHash == NULL)
@@ -181,6 +180,8 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 	}
 	else
 	{
+		ScanKeyData skey[2];
+
 		/*
 		 * Not a shared table, could either be a plain relation or a
 		 * non-shared, nailed one, like e.g. pg_class.
@@ -189,10 +190,8 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 		/* check for plain relations by looking in pg_class */
 		relation = table_open(RelationRelationId, AccessShareLock);
 
-		/* copy scankey to local copy, it will be modified during the scan */
+		/* copy scankey to local copy and set scan arguments */
 		memcpy(skey, relfilenumber_skey, sizeof(skey));
-
-		/* set scan arguments */
 		skey[0].sk_argument = ObjectIdGetDatum(reltablespace);
 		skey[1].sk_argument = ObjectIdGetDatum(relfilenumber);
 
-- 
2.46.0