diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-11-15 13:05:45 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-11-15 13:05:45 -0500 |
commit | 37ee4b75db8f979da6d67ba153d068b012394b46 (patch) | |
tree | b888127287d4f1b06d34cd57ec9dba8cf40a62b1 /src/include/utils/rangetypes.h | |
parent | ad50934eaadb626de682defe0ad270bbf31e92a2 (diff) | |
download | postgresql-37ee4b75db8f979da6d67ba153d068b012394b46.tar.gz postgresql-37ee4b75db8f979da6d67ba153d068b012394b46.zip |
Restructure function-internal caching in the range type code.
Move the responsibility for caching specialized information about range
types into the type cache, so that the catalog lookups only have to occur
once per session. Rearrange APIs a bit so that fn_extra caching is
actually effective in the GiST support code. (Use of OidFunctionCallN is
bad enough for performance in itself, but it also prevents the function
from exploiting fn_extra caching.)
The range I/O functions are still not very bright about caching repeated
lookups, but that seems like material for a separate patch.
Also, avoid unnecessary use of memcpy to fetch/store the range type OID and
flags, and don't use the full range_deserialize machinery when all we need
to see is the flags value.
Also fix API error in range_gist_penalty --- it was failing to set *penalty
for any case involving an empty range.
Diffstat (limited to 'src/include/utils/rangetypes.h')
-rw-r--r-- | src/include/utils/rangetypes.h | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/src/include/utils/rangetypes.h b/src/include/utils/rangetypes.h index e218a91d1a8..585d32134c0 100644 --- a/src/include/utils/rangetypes.h +++ b/src/include/utils/rangetypes.h @@ -14,37 +14,51 @@ #ifndef RANGETYPES_H #define RANGETYPES_H -#include "fmgr.h" +#include "utils/typcache.h" -/* All ranges are represented as varlena objects */ -typedef struct varlena RangeType; +/* + * Ranges are varlena objects, so must meet the varlena convention that + * the first int32 of the object contains the total object size in bytes. + * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though! + */ +typedef struct +{ + int32 vl_len_; /* varlena header (do not touch directly!) */ + Oid rangetypid; /* range type's own OID */ + /* Following the OID are zero to two bound values, then a flags byte */ +} RangeType; + +/* Use this macro in preference to fetching rangetypid field directly */ +#define RangeTypeGetOid(r) ((r)->rangetypid) + +/* A range's flags byte contains these bits: */ +#define RANGE_EMPTY 0x01 /* range is empty */ +#define RANGE_LB_INC 0x02 /* lower bound is inclusive (vs exclusive) */ +#define RANGE_LB_NULL 0x04 /* lower bound is null (NOT CURRENTLY USED) */ +#define RANGE_LB_INF 0x08 /* lower bound is +/- infinity */ +#define RANGE_UB_INC 0x10 /* upper bound is inclusive (vs exclusive) */ +#define RANGE_UB_NULL 0x20 /* upper bound is null (NOT CURRENTLY USED) */ +#define RANGE_UB_INF 0x40 /* upper bound is +/- infinity */ + +#define RANGE_HAS_LBOUND(flags) (!((flags) & (RANGE_EMPTY | \ + RANGE_LB_NULL | \ + RANGE_LB_INF))) + +#define RANGE_HAS_UBOUND(flags) (!((flags) & (RANGE_EMPTY | \ + RANGE_UB_NULL | \ + RANGE_UB_INF))) + /* Internal representation of either bound of a range (not what's on disk) */ typedef struct { Datum val; /* the bound value, if any */ - Oid rngtypid; /* OID of the range type itself */ bool infinite; /* bound is +/- infinity */ - bool lower; /* this is the lower (vs upper) bound */ bool inclusive; /* bound is inclusive (vs exclusive) */ + bool lower; /* this is the lower (vs upper) bound */ } RangeBound; -/* Standard runtime-cached data for a range type */ -typedef struct -{ - FmgrInfo canonicalFn; /* canonicalization function, if any */ - FmgrInfo cmpFn; /* element type's btree comparison function */ - FmgrInfo subdiffFn; /* element type difference function, if any */ - Oid rngtypid; /* OID of the range type itself */ - Oid subtype; /* OID of the element type */ - Oid collation; /* collation for comparisons, if any */ - int16 subtyplen; /* typlen of element type */ - char subtypalign; /* typalign of element type */ - char subtypstorage; /* typstorage of element type */ - bool subtypbyval; /* typbyval of element type */ -} RangeTypeInfo; - /* * fmgr macros for range type objects */ @@ -129,18 +143,19 @@ extern Datum tsrange_subdiff(PG_FUNCTION_ARGS); extern Datum tstzrange_subdiff(PG_FUNCTION_ARGS); /* assorted support functions */ -extern Datum range_serialize(FunctionCallInfo fcinfo, RangeBound *lower, +extern TypeCacheEntry *range_get_typcache(FunctionCallInfo fcinfo, + Oid rngtypid); +extern RangeType *range_serialize(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty); -extern void range_deserialize(FunctionCallInfo fcinfo, RangeType *range, +extern void range_deserialize(TypeCacheEntry *typcache, RangeType *range, RangeBound *lower, RangeBound *upper, bool *empty); -extern Datum make_range(FunctionCallInfo fcinfo, RangeBound *lower, +extern char range_get_flags(RangeType *range); +extern RangeType *make_range(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty); -extern int range_cmp_bounds(FunctionCallInfo fcinfo, RangeBound *b1, +extern int range_cmp_bounds(TypeCacheEntry *typcache, RangeBound *b1, RangeBound *b2); -extern RangeType *make_empty_range(FunctionCallInfo fcinfo, Oid rngtypid); -extern void range_gettypinfo(FunctionCallInfo fcinfo, Oid rngtypid, - RangeTypeInfo *rngtypinfo); +extern RangeType *make_empty_range(TypeCacheEntry *typcache); /* GiST support (in rangetypes_gist.c) */ extern Datum range_gist_consistent(PG_FUNCTION_ARGS); |