From 20a6e4eb0c0223feaebd996371a006698bfa1d01 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Mon, 10 Jun 2019 18:49:52 +0200
Subject: [PATCH 05/10] Add opclass parameters to BRIN

---
 src/backend/access/brin/brin.c          | 10 ++++++++++
 src/backend/access/brin/brin_validate.c | 16 ++++++++++------
 src/include/access/brin.h               |  2 +-
 src/include/access/brin_internal.h      |  3 +++
 src/include/catalog/pg_proc.dat         | 12 ++++++------
 5 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index ae7b729edd..63f2d7990c 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -102,6 +102,7 @@ brinhandler(PG_FUNCTION_ARGS)
 	amroutine->amcanparallel = false;
 	amroutine->amcaninclude = false;
 	amroutine->amkeytype = InvalidOid;
+	amroutine->amopclassoptions = brinopclassoptions;
 
 	amroutine->ambuild = brinbuild;
 	amroutine->ambuildempty = brinbuildempty;
@@ -518,6 +519,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
 											PointerGetDatum(bval),
 											PointerGetDatum(key));
 					addrange = DatumGetBool(add);
+
 					if (!addrange)
 						break;
 				}
@@ -845,6 +847,14 @@ brinoptions(Datum reloptions, bool validate)
 	return (bytea *) rdopts;
 }
 
+bytea *
+brinopclassoptions(Relation index, AttrNumber attnum, Datum attoptions,
+				   bool validate)
+{
+	return index_opclass_options_generic(index, attnum, BRIN_OPCLASSOPT_PROC,
+										 attoptions, validate);
+}
+
 /*
  * SQL-callable function to scan through an index and summarize all ranges
  * that are not currently summarized.
diff --git a/src/backend/access/brin/brin_validate.c b/src/backend/access/brin/brin_validate.c
index 012933833b..4222a1781f 100644
--- a/src/backend/access/brin/brin_validate.c
+++ b/src/backend/access/brin/brin_validate.c
@@ -93,18 +93,22 @@ brinvalidate(Oid opclassoid)
 				break;
 			case BRIN_PROCNUM_ADDVALUE:
 				ok = check_amproc_signature(procform->amproc, BOOLOID, true,
-											4, 4, INTERNALOID, INTERNALOID,
-											INTERNALOID, INTERNALOID);
+											4, 5, INTERNALOID, INTERNALOID,
+											INTERNALOID, INTERNALOID, INTERNALOID);
 				break;
 			case BRIN_PROCNUM_CONSISTENT:
 				ok = check_amproc_signature(procform->amproc, BOOLOID, true,
-											3, 3, INTERNALOID, INTERNALOID,
-											INTERNALOID);
+											3, 4, INTERNALOID, INTERNALOID,
+											INTERNALOID, INTERNALOID);
 				break;
 			case BRIN_PROCNUM_UNION:
 				ok = check_amproc_signature(procform->amproc, BOOLOID, true,
-											3, 3, INTERNALOID, INTERNALOID,
-											INTERNALOID);
+											3, 4, INTERNALOID, INTERNALOID,
+											INTERNALOID, INTERNALOID);
+				break;
+			case BRIN_OPCLASSOPT_PROC:
+				ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
+											2, 2, INTERNALOID, BOOLOID);
 				break;
 			default:
 				/* Complain if it's not a valid optional proc number */
diff --git a/src/include/access/brin.h b/src/include/access/brin.h
index 612721baf3..8271404332 100644
--- a/src/include/access/brin.h
+++ b/src/include/access/brin.h
@@ -37,6 +37,7 @@ typedef struct BrinStatsData
 
 
 #define BRIN_DEFAULT_PAGES_PER_RANGE	128
+
 #define BrinGetPagesPerRange(relation) \
 	((relation)->rd_options ? \
 	 ((BrinOptions *) (relation)->rd_options)->pagesPerRange : \
@@ -46,7 +47,6 @@ typedef struct BrinStatsData
 	 ((BrinOptions *) (relation)->rd_options)->autosummarize : \
 	  false)
 
-
 extern void brinGetStats(Relation index, BrinStatsData *stats);
 
 #endif							/* BRIN_H */
diff --git a/src/include/access/brin_internal.h b/src/include/access/brin_internal.h
index b1c9199946..0532094e28 100644
--- a/src/include/access/brin_internal.h
+++ b/src/include/access/brin_internal.h
@@ -68,6 +68,7 @@ typedef struct BrinDesc
 #define BRIN_PROCNUM_ADDVALUE		2
 #define BRIN_PROCNUM_CONSISTENT		3
 #define BRIN_PROCNUM_UNION			4
+#define BRIN_OPCLASSOPT_PROC		5	/* optional */
 #define BRIN_MANDATORY_NPROCS		4
 /* procedure numbers up to 10 are reserved for BRIN future expansion */
 #define BRIN_FIRST_OPTIONAL_PROCNUM 11
@@ -103,6 +104,8 @@ extern IndexBulkDeleteResult *brinbulkdelete(IndexVacuumInfo *info,
 extern IndexBulkDeleteResult *brinvacuumcleanup(IndexVacuumInfo *info,
 												IndexBulkDeleteResult *stats);
 extern bytea *brinoptions(Datum reloptions, bool validate);
+extern bytea *brinopclassoptions(Relation index, AttrNumber colno,
+								 Datum attoptions, bool validate);
 
 /* brin_validate.c */
 extern bool brinvalidate(Oid opclassoid);
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 82b51fc1bb..bc3d08caec 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -7887,15 +7887,15 @@
   proargtypes => 'internal', prosrc => 'brin_minmax_opcinfo' },
 { oid => '3384', descr => 'BRIN minmax support',
   proname => 'brin_minmax_add_value', prorettype => 'bool',
-  proargtypes => 'internal internal internal internal',
+  proargtypes => 'internal internal internal internal internal',
   prosrc => 'brin_minmax_add_value' },
 { oid => '3385', descr => 'BRIN minmax support',
   proname => 'brin_minmax_consistent', prorettype => 'bool',
-  proargtypes => 'internal internal internal',
+  proargtypes => 'internal internal internal internal',
   prosrc => 'brin_minmax_consistent' },
 { oid => '3386', descr => 'BRIN minmax support',
   proname => 'brin_minmax_union', prorettype => 'bool',
-  proargtypes => 'internal internal internal', prosrc => 'brin_minmax_union' },
+  proargtypes => 'internal internal internal internal', prosrc => 'brin_minmax_union' },
 
 # BRIN inclusion
 { oid => '4105', descr => 'BRIN inclusion support',
@@ -7903,15 +7903,15 @@
   proargtypes => 'internal', prosrc => 'brin_inclusion_opcinfo' },
 { oid => '4106', descr => 'BRIN inclusion support',
   proname => 'brin_inclusion_add_value', prorettype => 'bool',
-  proargtypes => 'internal internal internal internal',
+  proargtypes => 'internal internal internal internal internal',
   prosrc => 'brin_inclusion_add_value' },
 { oid => '4107', descr => 'BRIN inclusion support',
   proname => 'brin_inclusion_consistent', prorettype => 'bool',
-  proargtypes => 'internal internal internal',
+  proargtypes => 'internal internal internal internal',
   prosrc => 'brin_inclusion_consistent' },
 { oid => '4108', descr => 'BRIN inclusion support',
   proname => 'brin_inclusion_union', prorettype => 'bool',
-  proargtypes => 'internal internal internal',
+  proargtypes => 'internal internal internal internal',
   prosrc => 'brin_inclusion_union' },
 
 # userlock replacements
-- 
2.20.1

