0006-Specialize-the-HeapTuple-sort-routine-for-ANALYZE.patch
text/x-patch
Filename: 0006-Specialize-the-HeapTuple-sort-routine-for-ANALYZE.patch
Type: text/x-patch
Part: 5
Message:
Re: A qsort template
Patch
Format: format-patch
Series: patch 0006
Subject: Specialize the HeapTuple sort routine for ANALYZE.
| File | + | − |
|---|---|---|
| src/backend/commands/analyze.c | 9 | 27 |
From 50bb6dfb24fb68559c87ce4e2785c9a65be9a674 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 13 Mar 2021 17:30:56 +1300
Subject: [PATCH 6/9] Specialize the HeapTuple sort routine for ANALYZE.
Instead of a branching comparator, use "encoded" format, based on 48 bit
integers that can be compared by subtraction.
---
src/backend/commands/analyze.c | 36 +++++++++-------------------------
1 file changed, 9 insertions(+), 27 deletions(-)
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 3a9f358dd4..e760821ef5 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -98,7 +98,6 @@ static VacAttrStats *examine_attribute(Relation onerel, int attnum,
static int acquire_sample_rows(Relation onerel, int elevel,
HeapTuple *rows, int targrows,
double *totalrows, double *totaldeadrows);
-static int compare_rows(const void *a, const void *b);
static int acquire_inherited_sample_rows(Relation onerel, int elevel,
HeapTuple *rows, int targrows,
double *totalrows, double *totaldeadrows);
@@ -998,6 +997,14 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
return stats;
}
+#define ST_SORT sort_heaptuples_by_tid
+#define ST_ELEMENT_TYPE HeapTuple
+#define ST_COMPARE(a, b) (itemptr_encode(&((*a)->t_self)) - itemptr_encode(&((*b)->t_self)))
+#define ST_COMPARE_TYPE int64
+#define ST_SCOPE static
+#define ST_DEFINE
+#include "lib/sort_template.h"
+
/*
* acquire_sample_rows -- acquire a random sample of rows from the table
*
@@ -1141,7 +1148,7 @@ acquire_sample_rows(Relation onerel, int elevel,
* tuples are already sorted.
*/
if (numrows == targrows)
- qsort((void *) rows, numrows, sizeof(HeapTuple), compare_rows);
+ sort_heaptuples_by_tid(rows, numrows);
/*
* Estimate total numbers of live and dead rows in relation, extrapolating
@@ -1176,31 +1183,6 @@ acquire_sample_rows(Relation onerel, int elevel,
return numrows;
}
-/*
- * qsort comparator for sorting rows[] array
- */
-static int
-compare_rows(const void *a, const void *b)
-{
- HeapTuple ha = *(const HeapTuple *) a;
- HeapTuple hb = *(const HeapTuple *) b;
- BlockNumber ba = ItemPointerGetBlockNumber(&ha->t_self);
- OffsetNumber oa = ItemPointerGetOffsetNumber(&ha->t_self);
- BlockNumber bb = ItemPointerGetBlockNumber(&hb->t_self);
- OffsetNumber ob = ItemPointerGetOffsetNumber(&hb->t_self);
-
- if (ba < bb)
- return -1;
- if (ba > bb)
- return 1;
- if (oa < ob)
- return -1;
- if (oa > ob)
- return 1;
- return 0;
-}
-
-
/*
* acquire_inherited_sample_rows -- acquire sample rows from inheritance tree
*
--
2.30.1