Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 05dbba7

Browse files
hlinnakaCommitfest Bot
authored and
Commitfest Bot
committed
Add support for sorted gist index builds to btree_gist
This enables sortsupport in the btree_gist extension for faster builds of gist indexes. Sorted gist index build strategy is the new default now. Regression tests are unchanged (except for one small change in the 'enum' test to add coverage for enum values added later) and are using the sorted build strategy instead. One version of this was committed a long time ago already, in commit 9f984ba, but it was reverted because of buildfarm failures. The failures were presumably caused by some small bugs, but we never got around to debug and commit it again. This patch was written from scratch, implementing the same idea, with some fragments copied from the original patch. Author: Bernd Helmle <mailings@oopsware.de> Author: Andrey Borodin <x4mmm@yandex-team.ru> Discussion: https://www.postgresql.org/message-id/64d324ce2a6d535d3f0f3baeeea7b25beff82ce4.camel@oopsware.de
1 parent a7187c3 commit 05dbba7

29 files changed

+870
-8
lines changed

contrib/btree_gist/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ DATA = btree_gist--1.0--1.1.sql \
3434
btree_gist--1.1--1.2.sql btree_gist--1.2.sql btree_gist--1.2--1.3.sql \
3535
btree_gist--1.3--1.4.sql btree_gist--1.4--1.5.sql \
3636
btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql \
37-
btree_gist--1.7--1.8.sql
37+
btree_gist--1.7--1.8.sql btree_gist--1.8--1.9.sql
3838
PGFILEDESC = "btree_gist - B-tree equivalent GiST operator classes"
3939

4040
REGRESS = init int2 int4 int8 float4 float8 cash oid timestamp timestamptz \

contrib/btree_gist/btree_bit.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_var.h"
88
#include "utils/fmgrprotos.h"
9+
#include "utils/sortsupport.h"
910
#include "utils/varbit.h"
1011

1112

@@ -18,10 +19,33 @@ PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
1819
PG_FUNCTION_INFO_V1(gbt_bit_consistent);
1920
PG_FUNCTION_INFO_V1(gbt_bit_penalty);
2021
PG_FUNCTION_INFO_V1(gbt_bit_same);
22+
PG_FUNCTION_INFO_V1(gbt_bit_sortsupport);
23+
PG_FUNCTION_INFO_V1(gbt_varbit_sortsupport);
2124

2225

2326
/* define for comparison */
2427

28+
static int
29+
gbt_bit_ssup_cmp(Datum x, Datum y, SortSupport ssup)
30+
{
31+
GBT_VARKEY *key1 = PG_DETOAST_DATUM(x);
32+
GBT_VARKEY *key2 = PG_DETOAST_DATUM(y);
33+
34+
GBT_VARKEY_R arg1 = gbt_var_key_readable(key1);
35+
GBT_VARKEY_R arg2 = gbt_var_key_readable(key2);
36+
Datum result;
37+
38+
/* for leaf items we expect lower == upper, so only compare lower */
39+
result = DirectFunctionCall2(byteacmp,
40+
PointerGetDatum(arg1.lower),
41+
PointerGetDatum(arg2.lower));
42+
43+
GBT_FREE_IF_COPY(key1, x);
44+
GBT_FREE_IF_COPY(key2, y);
45+
46+
return DatumGetInt32(result);
47+
}
48+
2549
static bool
2650
gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
2751
{
@@ -207,3 +231,25 @@ gbt_bit_penalty(PG_FUNCTION_ARGS)
207231
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
208232
&tinfo, fcinfo->flinfo));
209233
}
234+
235+
Datum
236+
gbt_bit_sortsupport(PG_FUNCTION_ARGS)
237+
{
238+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
239+
240+
ssup->comparator = gbt_bit_ssup_cmp;
241+
ssup->ssup_extra = NULL;
242+
243+
PG_RETURN_VOID();
244+
}
245+
246+
Datum
247+
gbt_varbit_sortsupport(PG_FUNCTION_ARGS)
248+
{
249+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
250+
251+
ssup->comparator = gbt_bit_ssup_cmp;
252+
ssup->ssup_extra = NULL;
253+
254+
PG_RETURN_VOID();
255+
}

contrib/btree_gist/btree_bool.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
8+
#include "utils/sortsupport.h"
89

910
typedef struct boolkey
1011
{
@@ -22,6 +23,17 @@ PG_FUNCTION_INFO_V1(gbt_bool_picksplit);
2223
PG_FUNCTION_INFO_V1(gbt_bool_consistent);
2324
PG_FUNCTION_INFO_V1(gbt_bool_penalty);
2425
PG_FUNCTION_INFO_V1(gbt_bool_same);
26+
PG_FUNCTION_INFO_V1(gbt_bool_sortsupport);
27+
28+
static int
29+
gbt_bool_ssup_cmp(Datum x, Datum y, SortSupport ssup)
30+
{
31+
boolKEY *arg1 = (boolKEY *) DatumGetPointer(x);
32+
boolKEY *arg2 = (boolKEY *) DatumGetPointer(y);
33+
34+
/* for leaf items we expect lower == upper, so only compare lower */
35+
return (int32) arg1->lower - (int32) arg2->lower;
36+
}
2537

2638
static bool
2739
gbt_boolgt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -166,3 +178,14 @@ gbt_bool_same(PG_FUNCTION_ARGS)
166178
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
167179
PG_RETURN_POINTER(result);
168180
}
181+
182+
Datum
183+
gbt_bool_sortsupport(PG_FUNCTION_ARGS)
184+
{
185+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
186+
187+
ssup->comparator = gbt_bool_ssup_cmp;
188+
ssup->ssup_extra = NULL;
189+
190+
PG_RETURN_VOID();
191+
}

contrib/btree_gist/btree_bytea.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_var.h"
88
#include "utils/fmgrprotos.h"
9+
#include "utils/sortsupport.h"
910

1011

1112
/*
@@ -17,7 +18,41 @@ PG_FUNCTION_INFO_V1(gbt_bytea_picksplit);
1718
PG_FUNCTION_INFO_V1(gbt_bytea_consistent);
1819
PG_FUNCTION_INFO_V1(gbt_bytea_penalty);
1920
PG_FUNCTION_INFO_V1(gbt_bytea_same);
21+
PG_FUNCTION_INFO_V1(gbt_bytea_sortsupport);
2022

23+
/* sortsupport support */
24+
25+
static int
26+
gbt_bytea_ssup_cmp(Datum x, Datum y, SortSupport ssup)
27+
{
28+
GBT_VARKEY *key1 = PG_DETOAST_DATUM(x);
29+
GBT_VARKEY *key2 = PG_DETOAST_DATUM(y);
30+
31+
GBT_VARKEY_R xkey = gbt_var_key_readable(key1);
32+
GBT_VARKEY_R ykey = gbt_var_key_readable(key2);
33+
Datum result;
34+
35+
/* for leaf items we expect lower == upper, so only compare lower */
36+
result = DirectFunctionCall2(byteacmp,
37+
PointerGetDatum(xkey.lower),
38+
PointerGetDatum(ykey.lower));
39+
40+
GBT_FREE_IF_COPY(key1, x);
41+
GBT_FREE_IF_COPY(key2, y);
42+
43+
return DatumGetInt32(result);
44+
}
45+
46+
Datum
47+
gbt_bytea_sortsupport(PG_FUNCTION_ARGS)
48+
{
49+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
50+
51+
ssup->comparator = gbt_bytea_ssup_cmp;
52+
ssup->ssup_extra = NULL;
53+
54+
PG_RETURN_VOID();
55+
}
2156

2257
/* define for comparison */
2358

contrib/btree_gist/btree_cash.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "btree_utils_num.h"
88
#include "common/int.h"
99
#include "utils/cash.h"
10+
#include "utils/sortsupport.h"
1011

1112
typedef struct
1213
{
@@ -25,6 +26,24 @@ PG_FUNCTION_INFO_V1(gbt_cash_consistent);
2526
PG_FUNCTION_INFO_V1(gbt_cash_distance);
2627
PG_FUNCTION_INFO_V1(gbt_cash_penalty);
2728
PG_FUNCTION_INFO_V1(gbt_cash_same);
29+
PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
30+
31+
extern Datum cash_cmp(PG_FUNCTION_ARGS);
32+
33+
static int
34+
gbt_cash_ssup_cmp(Datum x, Datum y, SortSupport ssup)
35+
{
36+
cashKEY *arg1 = (cashKEY *) DatumGetPointer(x);
37+
cashKEY *arg2 = (cashKEY *) DatumGetPointer(y);
38+
39+
/* for leaf items we expect lower == upper, so only compare lower */
40+
if (arg1->lower > arg2->lower)
41+
return 1;
42+
else if (arg1->lower < arg2->lower)
43+
return -1;
44+
else
45+
return 0;
46+
}
2847

2948
static bool
3049
gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -215,3 +234,14 @@ gbt_cash_same(PG_FUNCTION_ARGS)
215234
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
216235
PG_RETURN_POINTER(result);
217236
}
237+
238+
Datum
239+
gbt_cash_sortsupport(PG_FUNCTION_ARGS)
240+
{
241+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
242+
243+
ssup->comparator = gbt_cash_ssup_cmp;
244+
ssup->ssup_extra = NULL;
245+
246+
PG_RETURN_VOID();
247+
}

contrib/btree_gist/btree_date.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "btree_utils_num.h"
88
#include "utils/fmgrprotos.h"
99
#include "utils/date.h"
10+
#include "utils/sortsupport.h"
1011

1112
typedef struct
1213
{
@@ -25,6 +26,32 @@ PG_FUNCTION_INFO_V1(gbt_date_consistent);
2526
PG_FUNCTION_INFO_V1(gbt_date_distance);
2627
PG_FUNCTION_INFO_V1(gbt_date_penalty);
2728
PG_FUNCTION_INFO_V1(gbt_date_same);
29+
PG_FUNCTION_INFO_V1(gbt_date_sortsupport);
30+
31+
/* sortsupport functions */
32+
33+
static int
34+
gbt_date_ssup_cmp(Datum x, Datum y, SortSupport ssup)
35+
{
36+
dateKEY *akey = (dateKEY *) DatumGetPointer(x);
37+
dateKEY *bkey = (dateKEY *) DatumGetPointer(y);
38+
39+
/* for leaf items we expect lower == upper, so only compare lower */
40+
return DatumGetInt32(DirectFunctionCall2(date_cmp,
41+
DateADTGetDatum(akey->lower),
42+
DateADTGetDatum(bkey->lower)));
43+
}
44+
45+
Datum
46+
gbt_date_sortsupport(PG_FUNCTION_ARGS)
47+
{
48+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
49+
50+
ssup->comparator = gbt_date_ssup_cmp;
51+
ssup->ssup_extra = NULL;
52+
53+
PG_RETURN_VOID();
54+
}
2855

2956
static bool
3057
gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)

contrib/btree_gist/btree_enum.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "btree_utils_num.h"
88
#include "fmgr.h"
99
#include "utils/fmgrprotos.h"
10+
#include "utils/fmgroids.h"
11+
#include "utils/sortsupport.h"
1012

1113
/* enums are really Oids, so we just use the same structure */
1214

@@ -26,8 +28,23 @@ PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
2628
PG_FUNCTION_INFO_V1(gbt_enum_consistent);
2729
PG_FUNCTION_INFO_V1(gbt_enum_penalty);
2830
PG_FUNCTION_INFO_V1(gbt_enum_same);
31+
PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
2932

3033

34+
static int
35+
gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
36+
{
37+
oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
38+
oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
39+
40+
/* for leaf items we expect lower == upper, so only compare lower */
41+
return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
42+
ssup->ssup_extra,
43+
InvalidOid,
44+
arg1->lower,
45+
arg2->lower));
46+
}
47+
3148
static bool
3249
gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
3350
{
@@ -183,3 +200,25 @@ gbt_enum_same(PG_FUNCTION_ARGS)
183200
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
184201
PG_RETURN_POINTER(result);
185202
}
203+
204+
Datum
205+
gbt_enum_sortsupport(PG_FUNCTION_ARGS)
206+
{
207+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
208+
FmgrInfo *flinfo;
209+
210+
ssup->comparator = gbt_enum_ssup_cmp;
211+
212+
/*
213+
* Since gbt_enum_ssup_cmp() uses enum_cmp() like the rest of the
214+
* comparison functions, it also needs to pass flinfo when calling it. The
215+
* caller to a SortSupport comparison function doesn't provide an FmgrInfo
216+
* struct, so look it up now, save it in ssup_extra and use it in
217+
* gbt_enum_ssup_cmp() later.
218+
*/
219+
flinfo = MemoryContextAlloc(ssup->ssup_cxt, sizeof(FmgrInfo));
220+
fmgr_info_cxt(F_ENUM_CMP, flinfo, ssup->ssup_cxt);
221+
ssup->ssup_extra = flinfo;
222+
223+
PG_RETURN_VOID();
224+
}

contrib/btree_gist/btree_float4.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
8+
#include "utils/sortsupport.h"
89
#include "utils/float.h"
910

1011
typedef struct float4key
@@ -24,6 +25,31 @@ PG_FUNCTION_INFO_V1(gbt_float4_consistent);
2425
PG_FUNCTION_INFO_V1(gbt_float4_distance);
2526
PG_FUNCTION_INFO_V1(gbt_float4_penalty);
2627
PG_FUNCTION_INFO_V1(gbt_float4_same);
28+
PG_FUNCTION_INFO_V1(gbt_float4_sortsupport);
29+
30+
extern Datum btfloat4cmp(PG_FUNCTION_ARGS);
31+
32+
/* sortsupport functions */
33+
static int
34+
gbt_float4_ssup_cmp(Datum x, Datum y, SortSupport ssup)
35+
{
36+
float4KEY *arg1 = (float4KEY *) DatumGetPointer(x);
37+
float4KEY *arg2 = (float4KEY *) DatumGetPointer(y);
38+
39+
/* for leaf items we expect lower == upper, so only compare lower */
40+
return float4_cmp_internal(arg1->lower, arg2->lower);
41+
}
42+
43+
Datum
44+
gbt_float4_sortsupport(PG_FUNCTION_ARGS)
45+
{
46+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
47+
48+
ssup->comparator = gbt_float4_ssup_cmp;
49+
ssup->ssup_extra = NULL;
50+
51+
PG_RETURN_VOID();
52+
}
2753

2854
static bool
2955
gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)

contrib/btree_gist/btree_float8.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
8+
#include "utils/sortsupport.h"
89
#include "utils/float.h"
910

1011
typedef struct float8key
@@ -24,7 +25,31 @@ PG_FUNCTION_INFO_V1(gbt_float8_consistent);
2425
PG_FUNCTION_INFO_V1(gbt_float8_distance);
2526
PG_FUNCTION_INFO_V1(gbt_float8_penalty);
2627
PG_FUNCTION_INFO_V1(gbt_float8_same);
28+
PG_FUNCTION_INFO_V1(gbt_float8_sortsupport);
2729

30+
extern Datum btfloat8cmp(PG_FUNCTION_ARGS);
31+
32+
/* sortsupport functions */
33+
static int
34+
gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
35+
{
36+
float8KEY *arg1 = (float8KEY *) DatumGetPointer(x);
37+
float8KEY *arg2 = (float8KEY *) DatumGetPointer(y);
38+
39+
/* for leaf items we expect lower == upper, so only compare lower */
40+
return float8_cmp_internal(arg1->lower, arg2->lower);
41+
}
42+
43+
Datum
44+
gbt_float8_sortsupport(PG_FUNCTION_ARGS)
45+
{
46+
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
47+
48+
ssup->comparator = gbt_float8_ssup_cmp;
49+
ssup->ssup_extra = NULL;
50+
51+
PG_RETURN_VOID();
52+
}
2853

2954
static bool
3055
gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)

0 commit comments

Comments
 (0)