Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite9e7b66

Browse files
committed
Add GiST and btree sortsupport routines for range types
For GiST, having a sortsupport function allows building the indexusing the "sorted build" method, which is much faster.For b-tree, the sortsupport routine doesn't give any newfunctionality, but speeds up sorting a tiny bit. The difference is notvery significant, about 2% in cursory testing on my laptop, becausethe range type comparison function has quite a lot of overhead fromdetoasting. In any case, since we have the function for GiST anyway,we might as well register it for the btree opfamily too.Author: Bernd Helmle <mailings@oopsware.de>Discussion:https://www.postgresql.org/message-id/64d324ce2a6d535d3f0f3baeeea7b25beff82ce4.camel@oopsware.de
1 parentea3f9b6 commite9e7b66

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

‎src/backend/utils/adt/rangetypes.c‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include"utils/date.h"
4444
#include"utils/lsyscache.h"
4545
#include"utils/rangetypes.h"
46+
#include"utils/sortsupport.h"
4647
#include"utils/timestamp.h"
4748

4849

@@ -57,6 +58,7 @@ typedef struct RangeIOData
5758

5859
staticRangeIOData*get_range_io_data(FunctionCallInfofcinfo,Oidrngtypid,
5960
IOFuncSelectorfunc);
61+
staticintrange_fast_cmp(Datuma,Datumb,SortSupportssup);
6062
staticcharrange_parse_flags(constchar*flags_str);
6163
staticboolrange_parse(constchar*string,char*flags,char**lbound_str,
6264
char**ubound_str,Node*escontext);
@@ -1290,6 +1292,68 @@ range_cmp(PG_FUNCTION_ARGS)
12901292
PG_RETURN_INT32(cmp);
12911293
}
12921294

1295+
/* Sort support strategy routine */
1296+
Datum
1297+
range_sortsupport(PG_FUNCTION_ARGS)
1298+
{
1299+
SortSupportssup= (SortSupport)PG_GETARG_POINTER(0);
1300+
1301+
ssup->comparator=range_fast_cmp;
1302+
ssup->ssup_extra=NULL;
1303+
1304+
PG_RETURN_VOID();
1305+
}
1306+
1307+
/* like range_cmp, but uses the new sortsupport interface */
1308+
staticint
1309+
range_fast_cmp(Datuma,Datumb,SortSupportssup)
1310+
{
1311+
RangeType*range_a=DatumGetRangeTypeP(a);
1312+
RangeType*range_b=DatumGetRangeTypeP(b);
1313+
TypeCacheEntry*typcache;
1314+
RangeBoundlower1,
1315+
lower2;
1316+
RangeBoundupper1,
1317+
upper2;
1318+
boolempty1,
1319+
empty2;
1320+
intcmp;
1321+
1322+
/* cache the range info between calls */
1323+
if (ssup->ssup_extra==NULL)
1324+
{
1325+
Assert(RangeTypeGetOid(range_a)==RangeTypeGetOid(range_b));
1326+
ssup->ssup_extra=
1327+
lookup_type_cache(RangeTypeGetOid(range_a),TYPECACHE_RANGE_INFO);
1328+
}
1329+
typcache=ssup->ssup_extra;
1330+
1331+
range_deserialize(typcache,range_a,&lower1,&upper1,&empty1);
1332+
range_deserialize(typcache,range_b,&lower2,&upper2,&empty2);
1333+
1334+
/* For b-tree use, empty ranges sort before all else */
1335+
if (empty1&&empty2)
1336+
cmp=0;
1337+
elseif (empty1)
1338+
cmp=-1;
1339+
elseif (empty2)
1340+
cmp=1;
1341+
else
1342+
{
1343+
cmp=range_cmp_bounds(typcache,&lower1,&lower2);
1344+
if (cmp==0)
1345+
cmp=range_cmp_bounds(typcache,&upper1,&upper2);
1346+
}
1347+
1348+
if ((Datum)range_a!=a)
1349+
pfree(range_a);
1350+
if ((Datum)range_b!=b)
1351+
pfree(range_b);
1352+
1353+
returncmp;
1354+
}
1355+
1356+
12931357
/* inequality operators using the range_cmp function */
12941358
Datum
12951359
range_lt(PG_FUNCTION_ARGS)

‎src/include/catalog/catversion.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/*yyyymmddN */
60-
#defineCATALOG_VERSION_NO202504012
60+
#defineCATALOG_VERSION_NO202504021
6161

6262
#endif

‎src/include/catalog/pg_amproc.dat‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@
283283
amprocrighttype => 'tsquery', amprocnum => '1', amproc => 'tsquery_cmp' },
284284
{ amprocfamily => 'btree/range_ops', amproclefttype => 'anyrange',
285285
amprocrighttype => 'anyrange', amprocnum => '1', amproc => 'range_cmp' },
286+
{ amprocfamily => 'btree/range_ops', amproclefttype => 'anyrange',
287+
amprocrighttype => 'anyrange', amprocnum => '2',
288+
amproc => 'range_sortsupport' },
286289
{ amprocfamily => 'btree/multirange_ops', amproclefttype => 'anymultirange',
287290
amprocrighttype => 'anymultirange', amprocnum => '1',
288291
amproc => 'multirange_cmp' },
@@ -606,6 +609,9 @@
606609
{ amprocfamily => 'gist/range_ops', amproclefttype => 'anyrange',
607610
amprocrighttype => 'anyrange', amprocnum => '7',
608611
amproc => 'range_gist_same' },
612+
{ amprocfamily => 'gist/range_ops', amproclefttype => 'anyrange',
613+
amprocrighttype => 'anyrange', amprocnum => '11',
614+
amproc => 'range_sortsupport' },
609615
{ amprocfamily => 'gist/range_ops', amproclefttype => 'any',
610616
amprocrighttype => 'any', amprocnum => '12',
611617
amproc => 'gist_stratnum_common' },

‎src/include/catalog/pg_proc.dat‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10855,6 +10855,9 @@
1085510855
{ oid => '3870', descr => 'less-equal-greater',
1085610856
proname => 'range_cmp', prorettype => 'int4',
1085710857
proargtypes => 'anyrange anyrange', prosrc => 'range_cmp' },
10858+
{ oid => '8849', descr => 'sort support',
10859+
proname => 'range_sortsupport', prorettype => 'void',
10860+
proargtypes => 'internal', prosrc => 'range_sortsupport' },
1085810861
{ oid => '3871',
1085910862
proname => 'range_lt', prorettype => 'bool',
1086010863
proargtypes => 'anyrange anyrange', prosrc => 'range_lt' },

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp