Hello,

> Parallel version is not supported, but I think it should be possible.

@Tomas are you working on this ? If not, I would like to give it a try.

> static void
> AssertCheckRanges(BrinSortState *node)
> {
> #ifdef USE_ASSERT_CHECKING
>
> #endif
> }

I guess it should not be empty at the ongoing development stage.

Attached a small modification of the patch with a draft of the docs.

Regards,
Sergey Dudoladov
From d4050be4bfd0a518eba0ff0a7b561f0420be9861 Mon Sep 17 00:00:00 2001
From: Sergey Dudoladov <sergey.dudola...@gmail.com>
Date: Wed, 2 Aug 2023 16:47:35 +0200
Subject: [PATCH] BRIN sort: add docs / fix typos

---
 doc/src/sgml/brin.sgml              | 48 +++++++++++++++++++++++++++++
 src/backend/executor/nodeBrinSort.c |  9 +++---
 2 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/brin.sgml b/doc/src/sgml/brin.sgml
index 9c5ffcddf8..a76f26e032 100644
--- a/doc/src/sgml/brin.sgml
+++ b/doc/src/sgml/brin.sgml
@@ -810,6 +810,54 @@ LOG:  request for BRIN range summarization for index "brin_wi_idx" page 128 was
 
 </sect1>
 
+<sect1 id="brin-sort">
+ <title>BRIN Sort</title>
+
+ <para>
+  This section provides an overview of <acronym>BRIN</acronym> sort that may be
+  useful to advanced users. See <filename>src/backend/executor/nodeBrinSort.c</filename>
+  in the source distribution for the implementation.
+ </para>
+
+ <sect2 id="brin-sort-overview">
+  <title>Overview</title>
+  <para>
+   The information about ranges in a <acronym>BRIN</acronym> index can be used
+   to split a table into parts and sort each part separately. A <acronym>BRIN</acronym>
+   sort is an index scan that uses the idea to incrementally sort the data. On
+   large tables <acronym>BRIN</acronym> index scan bridges the performance gap 
+   between having a large B-tree index that supports ordered retrieval, and having
+   no index at all.
+  </para>
+ </sect2>
+
+ <sect2 id="brin-sort-implementation">
+  <title>Implementation</title>
+  <para>
+
+  <acronym>BRIN</acronym> sort fetches a list of page ranges from the <acronym>BRIN</acronym>
+  index and sorts them by their summary info depending on the operator class and
+  sort direction. It then determines the watermark: the next summary value from
+  the list. Tuples with summary values less than the watermark are sorted and
+  outputted directly; tuples with values greater than the watermark are spilled
+  into the next iteration. The process continues until either all ranges are
+  processed or the desired number of tuples has been retrieved.
+  </para>
+ </sect2>
+
+ <sect2 id="brin-sort-limitations">
+  <title>Limitations</title>
+  <para>
+   The performance of the <acronym>BRIN</acronym> sort depends on the degree of
+   correlation between the values of the indexed column and their physical location
+   on disk: in the case of high correlation, there will be few overlapping ranges,
+   and performance will be best. Another limitation is that <acronym>BRIN</acronym>
+   sort only works for indexes built on a single column.
+  </para>
+ </sect2>
+
+</sect1>
+
 <sect1 id="brin-extensibility">
  <title>Extensibility</title>
 
diff --git a/src/backend/executor/nodeBrinSort.c b/src/backend/executor/nodeBrinSort.c
index d1b6b4e1ed..c640aea2d5 100644
--- a/src/backend/executor/nodeBrinSort.c
+++ b/src/backend/executor/nodeBrinSort.c
@@ -731,8 +731,9 @@ brinsort_load_spill_tuples(BrinSortState *node, bool check_watermark)
 static bool
 brinsort_next_range(BrinSortState *node, bool asc)
 {
-	/* FIXME free the current bs_range, if any */
-	node->bs_range = NULL;
+	/* free the current bs_range, if any */
+	if (node->bs_range != NULL)
+		pfree(node->bs_range);
 
 	/*
 	 * Mark the position, so that we can restore it in case we reach the
@@ -1227,7 +1228,7 @@ IndexNext(BrinSortState *node)
 
 			case BRINSORT_PROCESS_RANGE:
 
-				elog(DEBUG1, "phase BRINSORT_PROCESS_RANGE");
+				elog(DEBUG1, "phase = BRINSORT_PROCESS_RANGE");
 
 				slot = node->ss.ps.ps_ResultTupleSlot;
 
@@ -1263,7 +1264,7 @@ IndexNext(BrinSortState *node)
 				}
 				else
 				{
-					/* updte the watermark and try reading more ranges */
+					/* update the watermark and try reading more ranges */
 					node->bs_phase = BRINSORT_LOAD_RANGE;
 					brinsort_update_watermark(node, false, asc);
 				}
-- 
2.34.1

Reply via email to