v1-0002-nbtree-Limit-scope-of-variables-in-_bt_preprocess.patch.txt

text/plain

Filename: v1-0002-nbtree-Limit-scope-of-variables-in-_bt_preprocess.patch.txt
Type: text/plain
Part: 1
Message: Re: Optimizing nbtree ScalarArrayOp execution, allowing multi-column ordered scans, skip scan
From 1208e00a45076acc028c5435be7ad56e5e031051 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Tue, 19 Mar 2024 20:22:42 +0100
Subject: [PATCH v1 2/2] nbtree: Limit scope of variables in
 _bt_preprocess_keys

Many variables were used and rewritten across various loops in the code,
but we didn't actually depend on their values across loops. By limiting
their scope, we show this to the compiler, too, so that it can better
optimize this code.
---
 src/backend/access/nbtree/nbtutils.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c
index 8cd6270408..d7b30f8b9b 100644
--- a/src/backend/access/nbtree/nbtutils.c
+++ b/src/backend/access/nbtree/nbtutils.c
@@ -2511,10 +2511,6 @@ _bt_preprocess_keys(IndexScanDesc scan)
 	ScanKey		inkeys;
 	ScanKey		outkeys;
 	ScanKey		cur;
-	bool		test_result;
-	int			i,
-				j;
-	AttrNumber	attno;
 	ScanKey		arrayKeyData;
 	int		   *keyDataMap = NULL;
 	int			arrayidx = 0;
@@ -2619,11 +2615,12 @@ _bt_preprocess_keys(IndexScanDesc scan)
 	 * is NULL if we haven't yet found such a key for this attr.
 	 */
 
-	for (i = 0; i < numberOfKeys;)
+	for (int i = 0; i < numberOfKeys;)
 	{
 		BTScanKeyPreproc xform[BTMaxStrategyNumber];
 		int			priorNumberOfEqualCols = numberOfEqualCols;
 		bool		fast = true;
+		AttrNumber	attno;
 
 		/* initialize for this attno */
 		attno = cur->sk_attno;
@@ -2672,6 +2669,8 @@ _bt_preprocess_keys(IndexScanDesc scan)
 		 */
 		for (;i < numberOfKeys && cur->sk_attno == attno; cur++, i++)
 		{
+			bool		test_result;
+			int			j;
 			/* Apply indoption to scankey (might change sk_strategy!) */
 			if (!_bt_fix_scankey_strategy(cur, indoption))
 			{
@@ -2882,9 +2881,10 @@ _bt_preprocess_keys(IndexScanDesc scan)
 				Assert(OidIsValid(orderproc->fn_oid));
 			}
 
-			for (j = BTMaxStrategyNumber; --j >= 0;)
+			for (int j = BTMaxStrategyNumber; --j >= 0;)
 			{
 				ScanKey		chk = xform[j].skey;
+				bool		test_result;
 
 				if (!chk || j == (BTEqualStrategyNumber - 1))
 					continue;
@@ -2923,6 +2923,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
 		{
 			ScanKey		lt = xform[BTLessStrategyNumber - 1].skey;
 			ScanKey		le = xform[BTLessEqualStrategyNumber - 1].skey;
+			bool		test_result;
 
 			if (_bt_compare_scankey_args(scan, le, lt, le, NULL, NULL,
 										 &test_result))
@@ -2940,6 +2941,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
 		{
 			ScanKey		gt = xform[BTGreaterStrategyNumber - 1].skey;
 			ScanKey		ge = xform[BTGreaterEqualStrategyNumber - 1].skey;
+			bool		test_result;
 
 			if (_bt_compare_scankey_args(scan, ge, gt, ge, NULL, NULL,
 										 &test_result))
@@ -2956,7 +2958,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
 		 * mark them if they are required.  They are required (possibly
 		 * only in one direction) if all attrs before this one had "=".
 		 */
-		for (j = BTMaxStrategyNumber; --j >= 0;)
+		for (int j = BTMaxStrategyNumber; --j >= 0;)
 		{
 			if (xform[j].skey)
 			{
-- 
2.40.1