Skip to content

Commit c90c165

Browse files
committed
Fix some incorrect preprocessor tests in tuplesort specializations
6974924 added 3 new quicksort specialization functions for common datatypes. That commit was not very consistent in how it would determine if we're compiling for 32-bit or 64-bit machines. It would sometimes use USE_FLOAT8_BYVAL and at other times check if SIZEOF_DATUM == 8. This could cause theoretical problems due to the way USE_FLOAT8_BYVAL is now defined based on SIZEOF_VOID_P >= 8. If pointers for some reason were ever larger than 8-bytes then we'd end up doing 32-bit comparisons mistakenly. Let's just always check SIZEOF_DATUM >= 8. It also seems that ssup_datum_signed_cmp is just never used on 32-bit builds, so let's just ifdef that out to make sure we never accidentally use that comparison function on such machines. This also allows us to ifdef out 1 of the 3 new specialization quicksort functions in 32-bit builds which seems to shrink down the binary by over 4KB on my machine. In passing, also add the missing DatumGetInt32() / DatumGetInt64() macros in the comparison functions. Discussion: https://postgr.es/m/CAApHDvqcQExRhtRa9hJrJB_5egs3SUfOcutP3m+3HO8A+fZTPA@mail.gmail.com Reviewed-by: John Naylor
1 parent aff45c8 commit c90c165

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

src/backend/access/nbtree/nbtcompare.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ btint8cmp(PG_FUNCTION_ARGS)
142142
PG_RETURN_INT32(A_LESS_THAN_B);
143143
}
144144

145-
#ifndef USE_FLOAT8_BYVAL
145+
#if SIZEOF_DATUM < 8
146146
static int
147147
btint8fastcmp(Datum x, Datum y, SortSupport ssup)
148148
{
@@ -163,7 +163,7 @@ btint8sortsupport(PG_FUNCTION_ARGS)
163163
{
164164
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
165165

166-
#ifdef USE_FLOAT8_BYVAL
166+
#if SIZEOF_DATUM >= 8
167167
ssup->comparator = ssup_datum_signed_cmp;
168168
#else
169169
ssup->comparator = btint8fastcmp;

src/backend/utils/adt/timestamp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ timestamp_cmp(PG_FUNCTION_ARGS)
21762176
PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
21772177
}
21782178

2179-
#ifndef USE_FLOAT8_BYVAL
2179+
#if SIZEOF_DATUM < 8
21802180
/* note: this is used for timestamptz also */
21812181
static int
21822182
timestamp_fastcmp(Datum x, Datum y, SortSupport ssup)
@@ -2193,7 +2193,7 @@ timestamp_sortsupport(PG_FUNCTION_ARGS)
21932193
{
21942194
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
21952195

2196-
#ifdef USE_FLOAT8_BYVAL
2196+
#if SIZEOF_DATUM >= 8
21972197
/*
21982198
* If this build has pass-by-value timestamps, then we can use a standard
21992199
* comparator function.

src/backend/utils/sort/tuplesort.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ qsort_tuple_unsigned_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
715715
return state->comparetup(a, b, state);
716716
}
717717

718+
#if SIZEOF_DATUM >= 8
718719
/* Used if first key's comparator is ssup_datum_signed_compare */
719720
static pg_attribute_always_inline int
720721
qsort_tuple_signed_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
@@ -737,6 +738,7 @@ qsort_tuple_signed_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
737738

738739
return state->comparetup(a, b, state);
739740
}
741+
#endif
740742

741743
/* Used if first key's comparator is ssup_datum_int32_compare */
742744
static pg_attribute_always_inline int
@@ -779,6 +781,7 @@ qsort_tuple_int32_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
779781
#define ST_DEFINE
780782
#include "lib/sort_template.h"
781783

784+
#if SIZEOF_DATUM >= 8
782785
#define ST_SORT qsort_tuple_signed
783786
#define ST_ELEMENT_TYPE SortTuple
784787
#define ST_COMPARE(a, b, state) qsort_tuple_signed_compare(a, b, state)
@@ -787,6 +790,7 @@ qsort_tuple_int32_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
787790
#define ST_SCOPE static
788791
#define ST_DEFINE
789792
#include "lib/sort_template.h"
793+
#endif
790794

791795
#define ST_SORT qsort_tuple_int32
792796
#define ST_ELEMENT_TYPE SortTuple
@@ -3666,6 +3670,7 @@ tuplesort_sort_memtuples(Tuplesortstate *state)
36663670
state);
36673671
return;
36683672
}
3673+
#if SIZEOF_DATUM >= 8
36693674
else if (state->sortKeys[0].comparator == ssup_datum_signed_cmp)
36703675
{
36713676
elog(DEBUG1, "qsort_tuple_signed");
@@ -3674,6 +3679,7 @@ tuplesort_sort_memtuples(Tuplesortstate *state)
36743679
state);
36753680
return;
36763681
}
3682+
#endif
36773683
else if (state->sortKeys[0].comparator == ssup_datum_int32_cmp)
36783684
{
36793685
elog(DEBUG1, "qsort_tuple_int32");
@@ -4905,16 +4911,12 @@ ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
49054911
return 0;
49064912
}
49074913

4914+
#if SIZEOF_DATUM >= 8
49084915
int
49094916
ssup_datum_signed_cmp(Datum x, Datum y, SortSupport ssup)
49104917
{
4911-
#if SIZEOF_DATUM == 8
4912-
int64 xx = (int64) x;
4913-
int64 yy = (int64) y;
4914-
#else
4915-
int32 xx = (int32) x;
4916-
int32 yy = (int32) y;
4917-
#endif
4918+
int64 xx = DatumGetInt64(x);
4919+
int64 yy = DatumGetInt64(y);
49184920

49194921
if (xx < yy)
49204922
return -1;
@@ -4923,12 +4925,13 @@ ssup_datum_signed_cmp(Datum x, Datum y, SortSupport ssup)
49234925
else
49244926
return 0;
49254927
}
4928+
#endif
49264929

49274930
int
49284931
ssup_datum_int32_cmp(Datum x, Datum y, SortSupport ssup)
49294932
{
4930-
int32 xx = (int32) x;
4931-
int32 yy = (int32) y;
4933+
int32 xx = DatumGetInt32(x);
4934+
int32 yy = DatumGetInt32(y);
49324935

49334936
if (xx < yy)
49344937
return -1;

src/include/utils/sortsupport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ ApplySortAbbrevFullComparator(Datum datum1, bool isNull1,
379379
* are eligible for faster sorting.
380380
*/
381381
extern int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup);
382+
#if SIZEOF_DATUM >= 8
382383
extern int ssup_datum_signed_cmp(Datum x, Datum y, SortSupport ssup);
384+
#endif
383385
extern int ssup_datum_int32_cmp(Datum x, Datum y, SortSupport ssup);
384386

385387
/* Other functions in utils/sort/sortsupport.c */

0 commit comments

Comments
 (0)