diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-12-07 00:18:38 -0500 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-12-07 00:19:39 -0500 |
| commit | c6e3ac11b60ac4a8942ab964252d51c1c0bd8845 (patch) | |
| tree | fa9ffffed5b31d01a007f447368fd9479bba3aef /src/backend/utils/adt | |
| parent | d2a662182eac1069ff3874a1db499508a13c6bca (diff) | |
| download | postgresql-c6e3ac11b60ac4a8942ab964252d51c1c0bd8845.tar.gz | |
Create a "sort support" interface API for faster sorting.
This patch creates an API whereby a btree index opclass can optionally
provide non-SQL-callable support functions for sorting. In the initial
patch, we only use this to provide a directly-callable comparator function,
which can be invoked with a bit less overhead than the traditional
SQL-callable comparator. While that should be of value in itself, the real
reason for doing this is to provide a datatype-extensible framework for
more aggressive optimizations, as in Peter Geoghegan's recent work.
Robert Haas and Tom Lane
Diffstat (limited to 'src/backend/utils/adt')
| -rw-r--r-- | src/backend/utils/adt/date.c | 23 | ||||
| -rw-r--r-- | src/backend/utils/adt/float.c | 37 | ||||
| -rw-r--r-- | src/backend/utils/adt/timestamp.c | 19 |
3 files changed, 79 insertions, 0 deletions
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 271b57a830..ff4e3044f0 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -29,6 +29,7 @@ #include "utils/date.h" #include "utils/datetime.h" #include "utils/nabstime.h" +#include "utils/sortsupport.h" /* * gcc's -ffast-math switch breaks routines that expect exact results from @@ -320,6 +321,28 @@ date_cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(0); } +static int +date_fastcmp(Datum x, Datum y, SortSupport ssup) +{ + DateADT a = DatumGetDateADT(x); + DateADT b = DatumGetDateADT(y); + + if (a < b) + return -1; + else if (a > b) + return 1; + return 0; +} + +Datum +date_sortsupport(PG_FUNCTION_ARGS) +{ + SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); + + ssup->comparator = date_fastcmp; + PG_RETURN_VOID(); +} + Datum date_finite(PG_FUNCTION_ARGS) { diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index e3c84fd6c5..63b09a4eca 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -23,6 +23,7 @@ #include "libpq/pqformat.h" #include "utils/array.h" #include "utils/builtins.h" +#include "utils/sortsupport.h" #ifndef M_PI @@ -936,6 +937,24 @@ btfloat4cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(float4_cmp_internal(arg1, arg2)); } +static int +btfloat4fastcmp(Datum x, Datum y, SortSupport ssup) +{ + float4 arg1 = DatumGetFloat4(x); + float4 arg2 = DatumGetFloat4(y); + + return float4_cmp_internal(arg1, arg2); +} + +Datum +btfloat4sortsupport(PG_FUNCTION_ARGS) +{ + SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); + + ssup->comparator = btfloat4fastcmp; + PG_RETURN_VOID(); +} + /* * float8{eq,ne,lt,le,gt,ge} - float8/float8 comparison operations */ @@ -1032,6 +1051,24 @@ btfloat8cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(float8_cmp_internal(arg1, arg2)); } +static int +btfloat8fastcmp(Datum x, Datum y, SortSupport ssup) +{ + float8 arg1 = DatumGetFloat8(x); + float8 arg2 = DatumGetFloat8(y); + + return float8_cmp_internal(arg1, arg2); +} + +Datum +btfloat8sortsupport(PG_FUNCTION_ARGS) +{ + SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); + + ssup->comparator = btfloat8fastcmp; + PG_RETURN_VOID(); +} + Datum btfloat48cmp(PG_FUNCTION_ARGS) { diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 45e70029e5..450dcd4727 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -1830,6 +1830,25 @@ timestamp_cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2)); } +/* note: this is used for timestamptz also */ +static int +timestamp_fastcmp(Datum x, Datum y, SortSupport ssup) +{ + Timestamp a = DatumGetTimestamp(x); + Timestamp b = DatumGetTimestamp(y); + + return timestamp_cmp_internal(a, b); +} + +Datum +timestamp_sortsupport(PG_FUNCTION_ARGS) +{ + SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); + + ssup->comparator = timestamp_fastcmp; + PG_RETURN_VOID(); +} + Datum timestamp_hash(PG_FUNCTION_ARGS) { |
