From 65cf8ac1bb314e9f787f1e0fbf939e3cce561fe1 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sat, 15 Oct 2022 10:51:34 -0500
Subject: [PATCH 4/4] f!brinsort

//-os-only: linux-meson
---
 src/backend/executor/meson.build              |  1 +
 src/backend/executor/nodeBrinSort.c           | 11 ++--
 src/backend/utils/misc/guc_tables.c           |  2 +-
 src/backend/utils/misc/postgresql.conf.sample |  1 +
 src/include/access/brin_internal.h            |  2 +-
 src/include/catalog/pg_amproc.dat             | 52 +++++++++----------
 src/include/catalog/pg_proc.dat               |  2 +-
 src/include/executor/nodeBrinSort.h           |  6 +--
 src/test/regress/expected/sysviews.out        |  3 +-
 9 files changed, 41 insertions(+), 39 deletions(-)

diff --git a/src/backend/executor/meson.build b/src/backend/executor/meson.build
index 518674cfa28..c1fc50120d1 100644
--- a/src/backend/executor/meson.build
+++ b/src/backend/executor/meson.build
@@ -24,6 +24,7 @@ backend_sources += files(
   'nodeBitmapHeapscan.c',
   'nodeBitmapIndexscan.c',
   'nodeBitmapOr.c',
+  'nodeBrinSort.c',
   'nodeCtescan.c',
   'nodeCustom.c',
   'nodeForeignscan.c',
diff --git a/src/backend/executor/nodeBrinSort.c b/src/backend/executor/nodeBrinSort.c
index ca72c1ed22d..fcb0de71b9b 100644
--- a/src/backend/executor/nodeBrinSort.c
+++ b/src/backend/executor/nodeBrinSort.c
@@ -459,7 +459,7 @@ brinsort_load_tuples(BrinSortState *node, bool check_watermark, bool null_proces
 	scan = node->ss.ss_currentScanDesc;
 
 	/*
-	 * Read tuples, evaluate the filer (so that we don't keep tuples only to
+	 * Read tuples, evaluate the filter (so that we don't keep tuples only to
 	 * discard them later), and decide if it goes into the current range
 	 * (tuplesort) or overflow (tuplestore).
 	 */
@@ -501,7 +501,7 @@ brinsort_load_tuples(BrinSortState *node, bool check_watermark, bool null_proces
 		 *
 		 * XXX However, maybe we could also leverage other bitmap indexes,
 		 * particularly for BRIN indexes because that makes it simpler to
-		 * eliminage the ranges incrementally - we know which ranges to
+		 * eliminate the ranges incrementally - we know which ranges to
 		 * load from the index, while for other indexes (e.g. btree) we
 		 * have to read the whole index and build a bitmap in order to have
 		 * a bitmap for any range. Although, if the condition is very
@@ -896,9 +896,9 @@ IndexNext(BrinSortState *node)
 
 							tuplesort_get_stats(node->bs_tuplesortstate, &stats);
 
-							elog(DEBUG1, "method: %s  space: %ld kB (%s)",
+							elog(DEBUG1, "method: %s  space: %lld kB (%s)",
 								 tuplesort_method_name(stats.sortMethod),
-								 stats.spaceUsed,
+								 (long long)stats.spaceUsed,
 								 tuplesort_space_type_name(stats.spaceType));
 						}
 #endif
@@ -1015,7 +1015,6 @@ IndexNext(BrinSortState *node)
 				/* read tuples from the tuplesort range, and output them */
 				if (node->bs_tuplestore != NULL)
 				{
-
 					while (tuplestore_gettupleslot(node->bs_tuplestore, true, true, slot))
 						return slot;
 
@@ -1287,7 +1286,7 @@ ExecInitBrinSortRanges(BrinSort *node, BrinSortState *planstate)
 	 * Should not get here without a proc, thanks to the check before
 	 * building the BrinSort path.
 	 */
-	Assert(rangeproc != NULL);
+	Assert(OidIsValid(rangeproc->fn_oid));
 
 	memset(&planstate->bs_sortsupport, 0, sizeof(SortSupportData));
 	PrepareSortSupportFromOrderingOp(node->sortOperators[0], &planstate->bs_sortsupport);
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index a5ca3bd0cc4..27fb720d842 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -984,7 +984,7 @@ struct config_bool ConfigureNamesBool[] =
 			GUC_EXPLAIN
 		},
 		&enable_brinsort,
-		false,
+		true,
 		NULL, NULL, NULL
 	},
 	{
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index 8c5d442ff45..3f44d1229f4 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -370,6 +370,7 @@
 
 #enable_async_append = on
 #enable_bitmapscan = on
+#enable_brinsort = off
 #enable_gathermerge = on
 #enable_hashagg = on
 #enable_hashjoin = on
diff --git a/src/include/access/brin_internal.h b/src/include/access/brin_internal.h
index 06a36f769c5..355dddcc225 100644
--- a/src/include/access/brin_internal.h
+++ b/src/include/access/brin_internal.h
@@ -73,10 +73,10 @@ typedef struct BrinDesc
 #define BRIN_PROCNUM_UNION			4
 #define BRIN_MANDATORY_NPROCS		4
 #define BRIN_PROCNUM_OPTIONS 		5	/* optional */
-#define BRIN_PROCNUM_RANGES 		7	/* optional */
 /* procedure numbers up to 10 are reserved for BRIN future expansion */
 #define BRIN_FIRST_OPTIONAL_PROCNUM 11
 #define BRIN_PROCNUM_STATISTICS		11	/* optional */
+#define BRIN_PROCNUM_RANGES 		12	/* optional */
 #define BRIN_LAST_OPTIONAL_PROCNUM	15
 
 #undef BRIN_DEBUG
diff --git a/src/include/catalog/pg_amproc.dat b/src/include/catalog/pg_amproc.dat
index 7a22eaef33c..0d192ce40ee 100644
--- a/src/include/catalog/pg_amproc.dat
+++ b/src/include/catalog/pg_amproc.dat
@@ -807,7 +807,7 @@
 { amprocfamily => 'brin/bytea_minmax_ops', amproclefttype => 'bytea',
   amprocrighttype => 'bytea', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/bytea_minmax_ops', amproclefttype => 'bytea',
-  amprocrighttype => 'bytea', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'bytea', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # bloom bytea
 { amprocfamily => 'brin/bytea_bloom_ops', amproclefttype => 'bytea',
@@ -842,7 +842,7 @@
 { amprocfamily => 'brin/char_minmax_ops', amproclefttype => 'char',
   amprocrighttype => 'char', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/char_minmax_ops', amproclefttype => 'char',
-  amprocrighttype => 'char', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'char', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # bloom "char"
 { amprocfamily => 'brin/char_bloom_ops', amproclefttype => 'char',
@@ -875,7 +875,7 @@
 { amprocfamily => 'brin/name_minmax_ops', amproclefttype => 'name',
   amprocrighttype => 'name', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/name_minmax_ops', amproclefttype => 'name',
-  amprocrighttype => 'name', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'name', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # bloom name
 { amprocfamily => 'brin/name_bloom_ops', amproclefttype => 'name',
@@ -908,7 +908,7 @@
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int8',
   amprocrighttype => 'int8', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int8',
-  amprocrighttype => 'int8', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'int8', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int2',
   amprocrighttype => 'int2', amprocnum => '1',
@@ -924,7 +924,7 @@
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int2',
   amprocrighttype => 'int2', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int2',
-  amprocrighttype => 'int2', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'int2', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int4',
   amprocrighttype => 'int4', amprocnum => '1',
@@ -940,7 +940,7 @@
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int4',
   amprocrighttype => 'int4', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/integer_minmax_ops', amproclefttype => 'int4',
-  amprocrighttype => 'int4', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'int4', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax multi integer: int2, int4, int8
 { amprocfamily => 'brin/integer_minmax_multi_ops', amproclefttype => 'int2',
@@ -1061,7 +1061,7 @@
 { amprocfamily => 'brin/text_minmax_ops', amproclefttype => 'text',
   amprocrighttype => 'text', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/text_minmax_ops', amproclefttype => 'text',
-  amprocrighttype => 'text', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'text', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # bloom text
 { amprocfamily => 'brin/text_bloom_ops', amproclefttype => 'text',
@@ -1093,7 +1093,7 @@
 { amprocfamily => 'brin/oid_minmax_ops', amproclefttype => 'oid',
   amprocrighttype => 'oid', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/oid_minmax_ops', amproclefttype => 'oid',
-  amprocrighttype => 'oid', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'oid', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax multi oid
 { amprocfamily => 'brin/oid_minmax_multi_ops', amproclefttype => 'oid',
@@ -1145,7 +1145,7 @@
 { amprocfamily => 'brin/tid_minmax_ops', amproclefttype => 'tid',
   amprocrighttype => 'tid', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/tid_minmax_ops', amproclefttype => 'tid',
-  amprocrighttype => 'tid', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'tid', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # bloom tid
 { amprocfamily => 'brin/tid_bloom_ops', amproclefttype => 'tid',
@@ -1200,7 +1200,7 @@
   amprocrighttype => 'float4', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/float_minmax_ops', amproclefttype => 'float4',
-  amprocrighttype => 'float4', amprocnum => '7',
+  amprocrighttype => 'float4', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 { amprocfamily => 'brin/float_minmax_ops', amproclefttype => 'float8',
@@ -1219,7 +1219,7 @@
   amprocrighttype => 'float8', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/float_minmax_ops', amproclefttype => 'float8',
-  amprocrighttype => 'float8', amprocnum => '7',
+  amprocrighttype => 'float8', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi float
@@ -1313,7 +1313,7 @@
   amprocrighttype => 'macaddr', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/macaddr_minmax_ops', amproclefttype => 'macaddr',
-  amprocrighttype => 'macaddr', amprocnum => '7',
+  amprocrighttype => 'macaddr', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi macaddr
@@ -1372,7 +1372,7 @@
   amprocrighttype => 'macaddr8', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/macaddr8_minmax_ops', amproclefttype => 'macaddr8',
-  amprocrighttype => 'macaddr8', amprocnum => '7',
+  amprocrighttype => 'macaddr8', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi macaddr8
@@ -1429,7 +1429,7 @@
 { amprocfamily => 'brin/network_minmax_ops', amproclefttype => 'inet',
   amprocrighttype => 'inet', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/network_minmax_ops', amproclefttype => 'inet',
-  amprocrighttype => 'inet', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'inet', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax multi inet
 { amprocfamily => 'brin/network_minmax_multi_ops', amproclefttype => 'inet',
@@ -1504,7 +1504,7 @@
   amprocrighttype => 'bpchar', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/bpchar_minmax_ops', amproclefttype => 'bpchar',
-  amprocrighttype => 'bpchar', amprocnum => '7',
+  amprocrighttype => 'bpchar', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # bloom character
@@ -1540,7 +1540,7 @@
 { amprocfamily => 'brin/time_minmax_ops', amproclefttype => 'time',
   amprocrighttype => 'time', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/time_minmax_ops', amproclefttype => 'time',
-  amprocrighttype => 'time', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'time', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax multi time without time zone
 { amprocfamily => 'brin/time_minmax_multi_ops', amproclefttype => 'time',
@@ -1595,7 +1595,7 @@
   amprocrighttype => 'timestamp', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/datetime_minmax_ops', amproclefttype => 'timestamp',
-  amprocrighttype => 'timestamp', amprocnum => '7',
+  amprocrighttype => 'timestamp', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 { amprocfamily => 'brin/datetime_minmax_ops', amproclefttype => 'timestamptz',
@@ -1614,7 +1614,7 @@
   amprocrighttype => 'timestamptz', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/datetime_minmax_ops', amproclefttype => 'timestamptz',
-  amprocrighttype => 'timestamptz', amprocnum => '7',
+  amprocrighttype => 'timestamptz', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 { amprocfamily => 'brin/datetime_minmax_ops', amproclefttype => 'date',
@@ -1631,7 +1631,7 @@
 { amprocfamily => 'brin/datetime_minmax_ops', amproclefttype => 'date',
   amprocrighttype => 'date', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/datetime_minmax_ops', amproclefttype => 'date',
-  amprocrighttype => 'date', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'date', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax multi datetime (date, timestamp, timestamptz)
 { amprocfamily => 'brin/datetime_minmax_multi_ops',
@@ -1762,7 +1762,7 @@
   amprocrighttype => 'interval', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/interval_minmax_ops', amproclefttype => 'interval',
-  amprocrighttype => 'interval', amprocnum => '7',
+  amprocrighttype => 'interval', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi interval
@@ -1821,7 +1821,7 @@
   amprocrighttype => 'timetz', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/timetz_minmax_ops', amproclefttype => 'timetz',
-  amprocrighttype => 'timetz', amprocnum => '7',
+  amprocrighttype => 'timetz', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi time with time zone
@@ -1876,7 +1876,7 @@
 { amprocfamily => 'brin/bit_minmax_ops', amproclefttype => 'bit',
   amprocrighttype => 'bit', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/bit_minmax_ops', amproclefttype => 'bit',
-  amprocrighttype => 'bit', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'bit', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax bit varying
 { amprocfamily => 'brin/varbit_minmax_ops', amproclefttype => 'varbit',
@@ -1895,7 +1895,7 @@
   amprocrighttype => 'varbit', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/varbit_minmax_ops', amproclefttype => 'varbit',
-  amprocrighttype => 'varbit', amprocnum => '7',
+  amprocrighttype => 'varbit', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax numeric
@@ -1915,7 +1915,7 @@
   amprocrighttype => 'numeric', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/numeric_minmax_ops', amproclefttype => 'numeric',
-  amprocrighttype => 'numeric', amprocnum => '7',
+  amprocrighttype => 'numeric', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi numeric
@@ -1972,7 +1972,7 @@
 { amprocfamily => 'brin/uuid_minmax_ops', amproclefttype => 'uuid',
   amprocrighttype => 'uuid', amprocnum => '11', amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/uuid_minmax_ops', amproclefttype => 'uuid',
-  amprocrighttype => 'uuid', amprocnum => '7', amproc => 'brin_minmax_ranges' },
+  amprocrighttype => 'uuid', amprocnum => '12', amproc => 'brin_minmax_ranges' },
 
 # minmax multi uuid
 { amprocfamily => 'brin/uuid_minmax_multi_ops', amproclefttype => 'uuid',
@@ -2050,7 +2050,7 @@
   amprocrighttype => 'pg_lsn', amprocnum => '11',
   amproc => 'brin_minmax_stats' },
 { amprocfamily => 'brin/pg_lsn_minmax_ops', amproclefttype => 'pg_lsn',
-  amprocrighttype => 'pg_lsn', amprocnum => '7',
+  amprocrighttype => 'pg_lsn', amprocnum => '12',
   amproc => 'brin_minmax_ranges' },
 
 # minmax multi pg_lsn
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 18e0824a08e..2bd034e7616 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -5252,7 +5252,7 @@
   proname => 'pg_stat_get_numscans', provolatile => 's', proparallel => 'r',
   prorettype => 'int8', proargtypes => 'oid',
   prosrc => 'pg_stat_get_numscans' },
-{ oid => '9976', descr => 'statistics: time of the last scan for table/index',
+{ oid => '8912', descr => 'statistics: time of the last scan for table/index',
   proname => 'pg_stat_get_lastscan', provolatile => 's', proparallel => 'r',
   prorettype => 'timestamptz', proargtypes => 'oid',
   prosrc => 'pg_stat_get_lastscan' },
diff --git a/src/include/executor/nodeBrinSort.h b/src/include/executor/nodeBrinSort.h
index 2c860d926ea..3cac599d811 100644
--- a/src/include/executor/nodeBrinSort.h
+++ b/src/include/executor/nodeBrinSort.h
@@ -11,8 +11,8 @@
  *
  *-------------------------------------------------------------------------
  */
-#ifndef NODEBrinSort_H
-#define NODEBrinSort_H
+#ifndef NODEBRIN_SORT_H
+#define NODEBRIN_SORT_H
 
 #include "access/genam.h"
 #include "access/parallel.h"
@@ -44,4 +44,4 @@ extern bool ExecIndexEvalArrayKeys(ExprContext *econtext,
 								   IndexArrayKeyInfo *arrayKeys, int numArrayKeys);
 extern bool ExecIndexAdvanceArrayKeys(IndexArrayKeyInfo *arrayKeys, int numArrayKeys);
 
-#endif							/* NODEBrinSort_H */
+#endif							/* NODEBRIN_SORT_H */
diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out
index b19dae255e9..7b697941cad 100644
--- a/src/test/regress/expected/sysviews.out
+++ b/src/test/regress/expected/sysviews.out
@@ -113,6 +113,7 @@ select name, setting from pg_settings where name like 'enable%';
 --------------------------------+---------
  enable_async_append            | on
  enable_bitmapscan              | on
+ enable_brinsort                | on
  enable_gathermerge             | on
  enable_hashagg                 | on
  enable_hashjoin                | on
@@ -132,7 +133,7 @@ select name, setting from pg_settings where name like 'enable%';
  enable_seqscan                 | on
  enable_sort                    | on
  enable_tidscan                 | on
-(21 rows)
+(22 rows)
 
 -- Test that the pg_timezone_names and pg_timezone_abbrevs views are
 -- more-or-less working.  We can't test their contents in any great detail
-- 
2.25.1

