Start background writer during archive recovery. Background writer now performs
[PostgreSQL.git] / contrib / btree_gist / btree_numeric.c
blob866dd08f6203b4aaa3eeae03fe0e7d58dd84b2e4
1 /*
2 * $PostgreSQL$
3 */
4 #include "btree_gist.h"
6 #include <math.h>
7 #include <float.h>
9 #include "btree_utils_var.h"
10 #include "utils/builtins.h"
11 #include "utils/numeric.h"
12 #include "utils/rel.h"
15 ** Bytea ops
17 PG_FUNCTION_INFO_V1(gbt_numeric_compress);
18 PG_FUNCTION_INFO_V1(gbt_numeric_union);
19 PG_FUNCTION_INFO_V1(gbt_numeric_picksplit);
20 PG_FUNCTION_INFO_V1(gbt_numeric_consistent);
21 PG_FUNCTION_INFO_V1(gbt_numeric_penalty);
22 PG_FUNCTION_INFO_V1(gbt_numeric_same);
24 Datum gbt_numeric_compress(PG_FUNCTION_ARGS);
25 Datum gbt_numeric_union(PG_FUNCTION_ARGS);
26 Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS);
27 Datum gbt_numeric_consistent(PG_FUNCTION_ARGS);
28 Datum gbt_numeric_penalty(PG_FUNCTION_ARGS);
29 Datum gbt_numeric_same(PG_FUNCTION_ARGS);
32 /* define for comparison */
34 static bool
35 gbt_numeric_gt(const void *a, const void *b)
37 return (DatumGetBool(DirectFunctionCall2(numeric_gt, PointerGetDatum(a), PointerGetDatum(b))));
40 static bool
41 gbt_numeric_ge(const void *a, const void *b)
43 return (DatumGetBool(DirectFunctionCall2(numeric_ge, PointerGetDatum(a), PointerGetDatum(b))));
46 static bool
47 gbt_numeric_eq(const void *a, const void *b)
49 return (DatumGetBool(DirectFunctionCall2(numeric_eq, PointerGetDatum(a), PointerGetDatum(b))));
52 static bool
53 gbt_numeric_le(const void *a, const void *b)
55 return (DatumGetBool(DirectFunctionCall2(numeric_le, PointerGetDatum(a), PointerGetDatum(b))));
58 static bool
59 gbt_numeric_lt(const void *a, const void *b)
61 return (DatumGetBool(DirectFunctionCall2(numeric_lt, PointerGetDatum(a), PointerGetDatum(b))));
65 static int32
66 gbt_numeric_cmp(const bytea *a, const bytea *b)
68 return
69 (DatumGetInt32(DirectFunctionCall2(numeric_cmp, PointerGetDatum(a), PointerGetDatum(b))));
73 static const gbtree_vinfo tinfo =
75 gbt_t_numeric,
77 FALSE,
78 gbt_numeric_gt,
79 gbt_numeric_ge,
80 gbt_numeric_eq,
81 gbt_numeric_le,
82 gbt_numeric_lt,
83 gbt_numeric_cmp,
84 NULL
88 /**************************************************
89 * Text ops
90 **************************************************/
93 Datum
94 gbt_numeric_compress(PG_FUNCTION_ARGS)
96 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
98 PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
103 Datum
104 gbt_numeric_consistent(PG_FUNCTION_ARGS)
106 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
107 void *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
108 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
109 /* Oid subtype = PG_GETARG_OID(3); */
110 bool *recheck = (bool *) PG_GETARG_POINTER(4);
111 bool retval;
112 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
113 GBT_VARKEY_R r = gbt_var_key_readable(key);
115 /* All cases served by this function are exact */
116 *recheck = false;
118 retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
119 PG_RETURN_BOOL(retval);
124 Datum
125 gbt_numeric_union(PG_FUNCTION_ARGS)
127 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
128 int32 *size = (int *) PG_GETARG_POINTER(1);
130 PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
134 Datum
135 gbt_numeric_same(PG_FUNCTION_ARGS)
137 Datum d1 = PG_GETARG_DATUM(0);
138 Datum d2 = PG_GETARG_DATUM(1);
139 bool *result = (bool *) PG_GETARG_POINTER(2);
141 PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
145 Datum
146 gbt_numeric_penalty(PG_FUNCTION_ARGS)
148 GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
149 GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
150 float *result = (float *) PG_GETARG_POINTER(2);
152 Numeric us,
156 GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
157 GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
158 Datum uni;
159 GBT_VARKEY_R rk,
163 rk = gbt_var_key_readable(org);
164 uni = PointerGetDatum(gbt_var_key_copy(&rk, TRUE));
165 gbt_var_bin_union(&uni, newe, &tinfo);
166 ok = gbt_var_key_readable(org);
167 uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni));
169 us = DatumGetNumeric(DirectFunctionCall2(
170 numeric_sub,
171 PointerGetDatum(uk.upper),
172 PointerGetDatum(uk.lower)
175 os = DatumGetNumeric(DirectFunctionCall2(
176 numeric_sub,
177 PointerGetDatum(ok.upper),
178 PointerGetDatum(ok.lower)
181 ds = DatumGetNumeric(DirectFunctionCall2(
182 numeric_sub,
183 NumericGetDatum(us),
184 NumericGetDatum(os)
187 if (NUMERIC_IS_NAN(us))
189 if (NUMERIC_IS_NAN(os))
190 *result = 0.0;
191 else
192 *result = 1.0;
194 else
196 Numeric nul = DatumGetNumeric(DirectFunctionCall1(int4_numeric, Int32GetDatum(0)));
198 *result = 0.0;
200 if (DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul)))
202 *result += FLT_MIN;
203 os = DatumGetNumeric(DirectFunctionCall2(
204 numeric_div,
205 NumericGetDatum(ds),
206 NumericGetDatum(us)
208 *result += (float4) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, NumericGetDatum(os)));
212 if (*result > 0)
213 *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
215 PG_RETURN_POINTER(result);
220 Datum
221 gbt_numeric_picksplit(PG_FUNCTION_ARGS)
223 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
224 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
226 gbt_var_picksplit(entryvec, v, &tinfo);
227 PG_RETURN_POINTER(v);