From 9a0cb8be6e3146d9cde39c4d9291bf3990604823 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sun, 14 Mar 2021 10:00:25 +1300
Subject: [PATCH 2/9] Supply sort/search specializations for common scalar
 types.

---
 src/backend/utils/sort/Makefile     |  1 +
 src/backend/utils/sort/sortscalar.c | 11 +++++++++++
 src/include/utils/sortscalar.h      | 26 ++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
 create mode 100644 src/backend/utils/sort/sortscalar.c
 create mode 100644 src/include/utils/sortscalar.h

diff --git a/src/backend/utils/sort/Makefile b/src/backend/utils/sort/Makefile
index 26f65fcaf7..b4dce08614 100644
--- a/src/backend/utils/sort/Makefile
+++ b/src/backend/utils/sort/Makefile
@@ -17,6 +17,7 @@ override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS)
 OBJS = \
 	logtape.o \
 	sharedtuplestore.o \
+	sortscalar.o \
 	sortsupport.o \
 	tuplesort.o \
 	tuplestore.o
diff --git a/src/backend/utils/sort/sortscalar.c b/src/backend/utils/sort/sortscalar.c
new file mode 100644
index 0000000000..026e472a1d
--- /dev/null
+++ b/src/backend/utils/sort/sortscalar.c
@@ -0,0 +1,11 @@
+#include "postgres.h"
+
+#include "utils/sortscalar.h"
+
+#define ST_SORT qsort_uint32
+#define ST_ELEMENT_TYPE uint32
+#define ST_COMPARE(a, b) (((int64) *(a)) - ((int64) *(b)))
+#define ST_COMPARE_TYPE int64
+#define ST_SCOPE
+#define ST_DEFINE
+#include "lib/sort_template.h"
diff --git a/src/include/utils/sortscalar.h b/src/include/utils/sortscalar.h
new file mode 100644
index 0000000000..932fe29d42
--- /dev/null
+++ b/src/include/utils/sortscalar.h
@@ -0,0 +1,26 @@
+#ifndef SORTSCALAR_H
+#define SORTSCALAR_H
+
+/* We'll define the sort functions in sortscalar.c (they'd better match). */
+extern void qsort_uint32(uint32 *array, size_t n);
+
+/* The unique and search functions are defined here so they can be inlined. */
+#define ST_UNIQUE unique_uint32
+#define ST_SEARCH bsearch_uint32
+#define ST_ELEMENT_TYPE uint32
+#define ST_COMPARE(a, b) (((int64) *(a)) - ((int64) *(b)))
+#define ST_COMPARE_TYPE int64
+#define ST_SCOPE static inline
+#define ST_DEFINE
+#include "lib/sort_template.h"
+
+/* Provide names for other common types that are currently uint32. */
+#define qsort_oid			qsort_uint32
+#define unique_oid			unique_uint32
+#define bsearch_oid			bsearch_uint32
+
+#define qsort_blocknum		qsort_uint32
+#define unique_blocknum		unique_uint32
+#define bsearch_blocknum	bsearch_uint32
+
+#endif
-- 
2.30.1

