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

Commit6974924

Browse files
committed
Specialize tuplesort routines for different kinds of abbreviated keys
Previously, the specialized tuplesort routine inlined handling forreverse-sort and NULLs-ordering but called the datum comparator via apointer in the SortSupport struct parameter. Testing has showed that wecan get a useful performance gain by specializing datum comparison forthe different representations of abbreviated keys -- signed and unsigned64-bit integers and signed 32-bit integers. Almost all abbreviatable datatypes will benefit -- the only exception for now is numeric, since thedatum comparison is more complex. The performance gain depends on datatype and input distribution, but often falls in the range of 10-20% faster.Thomas MunroReviewed by Peter Geoghegan, review and performance testing by meDiscussion:https://www.postgresql.org/message-id/CA%2BhUKGKKYttZZk-JMRQSVak%3DCXSJ5fiwtirFf%3Dn%3DPAbumvn1Ww%40mail.gmail.com
1 parentdb086de commit6974924

File tree

10 files changed

+308
-132
lines changed

10 files changed

+308
-132
lines changed

‎src/backend/access/gist/gistproc.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static uint64 part_bits32_by2(uint32 x);
3737
staticuint32ieee_float32_to_uint32(floatf);
3838
staticintgist_bbox_zorder_cmp(Datuma,Datumb,SortSupportssup);
3939
staticDatumgist_bbox_zorder_abbrev_convert(Datumoriginal,SortSupportssup);
40-
staticintgist_bbox_zorder_cmp_abbrev(Datumz1,Datumz2,SortSupportssup);
4140
staticboolgist_bbox_zorder_abbrev_abort(intmemtupcount,SortSupportssup);
4241

4342

@@ -1726,21 +1725,6 @@ gist_bbox_zorder_abbrev_convert(Datum original, SortSupport ssup)
17261725
#endif
17271726
}
17281727

1729-
staticint
1730-
gist_bbox_zorder_cmp_abbrev(Datumz1,Datumz2,SortSupportssup)
1731-
{
1732-
/*
1733-
* Compare the pre-computed Z-orders as unsigned integers. Datum is a
1734-
* typedef for 'uintptr_t', so no casting is required.
1735-
*/
1736-
if (z1>z2)
1737-
return1;
1738-
elseif (z1<z2)
1739-
return-1;
1740-
else
1741-
return0;
1742-
}
1743-
17441728
/*
17451729
* We never consider aborting the abbreviation.
17461730
*
@@ -1764,7 +1748,7 @@ gist_point_sortsupport(PG_FUNCTION_ARGS)
17641748

17651749
if (ssup->abbreviate)
17661750
{
1767-
ssup->comparator=gist_bbox_zorder_cmp_abbrev;
1751+
ssup->comparator=ssup_datum_unsigned_cmp;
17681752
ssup->abbrev_converter=gist_bbox_zorder_abbrev_convert;
17691753
ssup->abbrev_abort=gist_bbox_zorder_abbrev_abort;
17701754
ssup->abbrev_full_comparator=gist_bbox_zorder_cmp;

‎src/backend/access/nbtree/nbtcompare.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,12 @@ btint4cmp(PG_FUNCTION_ARGS)
119119
PG_RETURN_INT32(A_LESS_THAN_B);
120120
}
121121

122-
staticint
123-
btint4fastcmp(Datumx,Datumy,SortSupportssup)
124-
{
125-
int32a=DatumGetInt32(x);
126-
int32b=DatumGetInt32(y);
127-
128-
if (a>b)
129-
returnA_GREATER_THAN_B;
130-
elseif (a==b)
131-
return0;
132-
else
133-
returnA_LESS_THAN_B;
134-
}
135-
136122
Datum
137123
btint4sortsupport(PG_FUNCTION_ARGS)
138124
{
139125
SortSupportssup= (SortSupport)PG_GETARG_POINTER(0);
140126

141-
ssup->comparator=btint4fastcmp;
127+
ssup->comparator=ssup_datum_int32_cmp;
142128
PG_RETURN_VOID();
143129
}
144130

@@ -156,6 +142,7 @@ btint8cmp(PG_FUNCTION_ARGS)
156142
PG_RETURN_INT32(A_LESS_THAN_B);
157143
}
158144

145+
#ifndefUSE_FLOAT8_BYVAL
159146
staticint
160147
btint8fastcmp(Datumx,Datumy,SortSupportssup)
161148
{
@@ -169,13 +156,18 @@ btint8fastcmp(Datum x, Datum y, SortSupport ssup)
169156
else
170157
returnA_LESS_THAN_B;
171158
}
159+
#endif
172160

173161
Datum
174162
btint8sortsupport(PG_FUNCTION_ARGS)
175163
{
176164
SortSupportssup= (SortSupport)PG_GETARG_POINTER(0);
177165

166+
#ifdefUSE_FLOAT8_BYVAL
167+
ssup->comparator=ssup_datum_signed_cmp;
168+
#else
178169
ssup->comparator=btint8fastcmp;
170+
#endif
179171
PG_RETURN_VOID();
180172
}
181173

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,25 +439,12 @@ date_cmp(PG_FUNCTION_ARGS)
439439
PG_RETURN_INT32(0);
440440
}
441441

442-
staticint
443-
date_fastcmp(Datumx,Datumy,SortSupportssup)
444-
{
445-
DateADTa=DatumGetDateADT(x);
446-
DateADTb=DatumGetDateADT(y);
447-
448-
if (a<b)
449-
return-1;
450-
elseif (a>b)
451-
return1;
452-
return0;
453-
}
454-
455442
Datum
456443
date_sortsupport(PG_FUNCTION_ARGS)
457444
{
458445
SortSupportssup= (SortSupport)PG_GETARG_POINTER(0);
459446

460-
ssup->comparator=date_fastcmp;
447+
ssup->comparator=ssup_datum_int32_cmp;
461448
PG_RETURN_VOID();
462449
}
463450

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ typedef struct
4444

4545
staticintmacaddr_cmp_internal(macaddr*a1,macaddr*a2);
4646
staticintmacaddr_fast_cmp(Datumx,Datumy,SortSupportssup);
47-
staticintmacaddr_cmp_abbrev(Datumx,Datumy,SortSupportssup);
4847
staticboolmacaddr_abbrev_abort(intmemtupcount,SortSupportssup);
4948
staticDatummacaddr_abbrev_convert(Datumoriginal,SortSupportssup);
5049

@@ -381,7 +380,7 @@ macaddr_sortsupport(PG_FUNCTION_ARGS)
381380

382381
ssup->ssup_extra=uss;
383382

384-
ssup->comparator=macaddr_cmp_abbrev;
383+
ssup->comparator=ssup_datum_unsigned_cmp;
385384
ssup->abbrev_converter=macaddr_abbrev_convert;
386385
ssup->abbrev_abort=macaddr_abbrev_abort;
387386
ssup->abbrev_full_comparator=macaddr_fast_cmp;
@@ -405,22 +404,6 @@ macaddr_fast_cmp(Datum x, Datum y, SortSupport ssup)
405404
returnmacaddr_cmp_internal(arg1,arg2);
406405
}
407406

408-
/*
409-
* SortSupport abbreviated key comparison function. Compares two MAC addresses
410-
* quickly by treating them like integers, and without having to go to the
411-
* heap.
412-
*/
413-
staticint
414-
macaddr_cmp_abbrev(Datumx,Datumy,SortSupportssup)
415-
{
416-
if (x>y)
417-
return1;
418-
elseif (x==y)
419-
return0;
420-
else
421-
return-1;
422-
}
423-
424407
/*
425408
* Callback for estimating effectiveness of abbreviated key optimization.
426409
*
@@ -537,8 +520,8 @@ macaddr_abbrev_convert(Datum original, SortSupport ssup)
537520
/*
538521
* Byteswap on little-endian machines.
539522
*
540-
* This is needed so thatmacaddr_cmp_abbrev() (an unsigned integer 3-way
541-
* comparator) works correctly on all platforms. Without this, the
523+
* This is needed so thatssup_datum_unsigned_cmp() (an unsigned integer
524+
*3-waycomparator) works correctly on all platforms. Without this, the
542525
* comparator would have to call memcmp() with a pair of pointers to the
543526
* first byte of each abbreviated key, which is slower.
544527
*/

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ typedef struct
5353

5454
staticint32network_cmp_internal(inet*a1,inet*a2);
5555
staticintnetwork_fast_cmp(Datumx,Datumy,SortSupportssup);
56-
staticintnetwork_cmp_abbrev(Datumx,Datumy,SortSupportssup);
5756
staticboolnetwork_abbrev_abort(intmemtupcount,SortSupportssup);
5857
staticDatumnetwork_abbrev_convert(Datumoriginal,SortSupportssup);
5958
staticList*match_network_function(Node*leftop,
@@ -456,7 +455,7 @@ network_sortsupport(PG_FUNCTION_ARGS)
456455

457456
ssup->ssup_extra=uss;
458457

459-
ssup->comparator=network_cmp_abbrev;
458+
ssup->comparator=ssup_datum_unsigned_cmp;
460459
ssup->abbrev_converter=network_abbrev_convert;
461460
ssup->abbrev_abort=network_abbrev_abort;
462461
ssup->abbrev_full_comparator=network_fast_cmp;
@@ -479,20 +478,6 @@ network_fast_cmp(Datum x, Datum y, SortSupport ssup)
479478
returnnetwork_cmp_internal(arg1,arg2);
480479
}
481480

482-
/*
483-
* Abbreviated key comparison func
484-
*/
485-
staticint
486-
network_cmp_abbrev(Datumx,Datumy,SortSupportssup)
487-
{
488-
if (x>y)
489-
return1;
490-
elseif (x==y)
491-
return0;
492-
else
493-
return-1;
494-
}
495-
496481
/*
497482
* Callback for estimating effectiveness of abbreviated key optimization.
498483
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include"utils/datetime.h"
3838
#include"utils/float.h"
3939
#include"utils/numeric.h"
40+
#include"utils/sortsupport.h"
4041

4142
/*
4243
* gcc's -ffast-math switch breaks routines that expect exact results from
@@ -2155,6 +2156,7 @@ timestamp_cmp(PG_FUNCTION_ARGS)
21552156
PG_RETURN_INT32(timestamp_cmp_internal(dt1,dt2));
21562157
}
21572158

2159+
#ifndefUSE_FLOAT8_BYVAL
21582160
/* note: this is used for timestamptz also */
21592161
staticint
21602162
timestamp_fastcmp(Datumx,Datumy,SortSupportssup)
@@ -2164,13 +2166,22 @@ timestamp_fastcmp(Datum x, Datum y, SortSupport ssup)
21642166

21652167
returntimestamp_cmp_internal(a,b);
21662168
}
2169+
#endif
21672170

21682171
Datum
21692172
timestamp_sortsupport(PG_FUNCTION_ARGS)
21702173
{
21712174
SortSupportssup= (SortSupport)PG_GETARG_POINTER(0);
21722175

2176+
#ifdefUSE_FLOAT8_BYVAL
2177+
/*
2178+
* If this build has pass-by-value timestamps, then we can use a standard
2179+
* comparator function.
2180+
*/
2181+
ssup->comparator=ssup_datum_signed_cmp;
2182+
#else
21732183
ssup->comparator=timestamp_fastcmp;
2184+
#endif
21742185
PG_RETURN_VOID();
21752186
}
21762187

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ typedef struct
3434
staticvoidstring_to_uuid(constchar*source,pg_uuid_t*uuid);
3535
staticintuuid_internal_cmp(constpg_uuid_t*arg1,constpg_uuid_t*arg2);
3636
staticintuuid_fast_cmp(Datumx,Datumy,SortSupportssup);
37-
staticintuuid_cmp_abbrev(Datumx,Datumy,SortSupportssup);
3837
staticbooluuid_abbrev_abort(intmemtupcount,SortSupportssup);
3938
staticDatumuuid_abbrev_convert(Datumoriginal,SortSupportssup);
4039

@@ -255,7 +254,7 @@ uuid_sortsupport(PG_FUNCTION_ARGS)
255254

256255
ssup->ssup_extra=uss;
257256

258-
ssup->comparator=uuid_cmp_abbrev;
257+
ssup->comparator=ssup_datum_unsigned_cmp;
259258
ssup->abbrev_converter=uuid_abbrev_convert;
260259
ssup->abbrev_abort=uuid_abbrev_abort;
261260
ssup->abbrev_full_comparator=uuid_fast_cmp;
@@ -278,20 +277,6 @@ uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
278277
returnuuid_internal_cmp(arg1,arg2);
279278
}
280279

281-
/*
282-
* Abbreviated key comparison func
283-
*/
284-
staticint
285-
uuid_cmp_abbrev(Datumx,Datumy,SortSupportssup)
286-
{
287-
if (x>y)
288-
return1;
289-
elseif (x==y)
290-
return0;
291-
else
292-
return-1;
293-
}
294-
295280
/*
296281
* Callback for estimating effectiveness of abbreviated key optimization.
297282
*
@@ -390,10 +375,10 @@ uuid_abbrev_convert(Datum original, SortSupport ssup)
390375
/*
391376
* Byteswap on little-endian machines.
392377
*
393-
* This is needed so thatuuid_cmp_abbrev() (an unsigned integer 3-way
394-
* comparator) works correctly on all platforms. If we didn't do this,
395-
* the comparator would have to call memcmp() with a pair of pointers to
396-
* the first byte of each abbreviated key, which is slower.
378+
* This is needed so thatssup_datum_unsigned_cmp() (an unsigned integer
379+
*3-waycomparator) works correctly on all platforms. If we didn't do
380+
*this,the comparator would have to call memcmp() with a pair of pointers
381+
*tothe first byte of each abbreviated key, which is slower.
397382
*/
398383
res=DatumBigEndianToNative(res);
399384

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

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ static intnamefastcmp_c(Datum x, Datum y, SortSupport ssup);
126126
staticintvarlenafastcmp_locale(Datumx,Datumy,SortSupportssup);
127127
staticintnamefastcmp_locale(Datumx,Datumy,SortSupportssup);
128128
staticintvarstrfastcmp_locale(char*a1p,intlen1,char*a2p,intlen2,SortSupportssup);
129-
staticintvarstrcmp_abbrev(Datumx,Datumy,SortSupportssup);
130129
staticDatumvarstr_abbrev_convert(Datumoriginal,SortSupportssup);
131130
staticboolvarstr_abbrev_abort(intmemtupcount,SortSupportssup);
132131
staticint32text_length(Datumstr);
@@ -2159,7 +2158,7 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
21592158
initHyperLogLog(&sss->abbr_card,10);
21602159
initHyperLogLog(&sss->full_card,10);
21612160
ssup->abbrev_full_comparator=ssup->comparator;
2162-
ssup->comparator=varstrcmp_abbrev;
2161+
ssup->comparator=ssup_datum_unsigned_cmp;
21632162
ssup->abbrev_converter=varstr_abbrev_convert;
21642163
ssup->abbrev_abort=varstr_abbrev_abort;
21652164
}
@@ -2445,27 +2444,6 @@ varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
24452444
returnresult;
24462445
}
24472446

2448-
/*
2449-
* Abbreviated key comparison func
2450-
*/
2451-
staticint
2452-
varstrcmp_abbrev(Datumx,Datumy,SortSupportssup)
2453-
{
2454-
/*
2455-
* When 0 is returned, the core system will call varstrfastcmp_c()
2456-
* (bpcharfastcmp_c() in BpChar case) or varlenafastcmp_locale(). Even a
2457-
* strcmp() on two non-truncated strxfrm() blobs cannot indicate *equality*
2458-
* authoritatively, for the same reason that there is a strcoll()
2459-
* tie-breaker call to strcmp() in varstr_cmp().
2460-
*/
2461-
if (x>y)
2462-
return1;
2463-
elseif (x==y)
2464-
return0;
2465-
else
2466-
return-1;
2467-
}
2468-
24692447
/*
24702448
* Conversion routine for sortsupport. Converts original to abbreviated key
24712449
* representation. Our encoding strategy is simple -- pack the first 8 bytes
@@ -2504,7 +2482,7 @@ varstr_abbrev_convert(Datum original, SortSupport ssup)
25042482
* strings may contain NUL bytes. Besides, this should be faster, too.
25052483
*
25062484
* More generally, it's okay that bytea callers can have NUL bytes in
2507-
* strings becausevarstrcmp_abbrev() need not make a distinction between
2485+
* strings becauseabbreviated cmp need not make a distinction between
25082486
* terminating NUL bytes, and NUL bytes representing actual NULs in the
25092487
* authoritative representation. Hopefully a comparison at or past one
25102488
* abbreviated key's terminating NUL byte will resolve the comparison
@@ -2694,10 +2672,10 @@ varstr_abbrev_convert(Datum original, SortSupport ssup)
26942672
/*
26952673
* Byteswap on little-endian machines.
26962674
*
2697-
* This is needed so thatvarstrcmp_abbrev() (an unsigned integer 3-way
2698-
* comparator) works correctly on all platforms. If we didn't do this,
2699-
* the comparator would have to call memcmp() with a pair of pointers to
2700-
* the first byte of each abbreviated key, which is slower.
2675+
* This is needed so thatssup_datum_unsigned_cmp() (an unsigned integer
2676+
*3-waycomparator) works correctly on all platforms. If we didn't do
2677+
*this,the comparator would have to call memcmp() with a pair of pointers
2678+
*tothe first byte of each abbreviated key, which is slower.
27012679
*/
27022680
res=DatumBigEndianToNative(res);
27032681

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp