PostgreSQL Source Code git master
Loading...
Searching...
No Matches
tsquery_gist.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * tsquery_gist.c
4 * GiST index support for tsquery
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 *
8 *
9 * IDENTIFICATION
10 * src/backend/utils/adt/tsquery_gist.c
11 *
12 *-------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "access/gist.h"
18#include "access/stratnum.h"
19#include "common/int.h"
20#include "tsearch/ts_utils.h"
21#include "utils/fmgrprotos.h"
22
23#define GETENTRY(vec,pos) DatumGetTSQuerySign((vec)->vector[pos].key)
24
25
28{
30 GISTENTRY *retval = entry;
31
32 if (entry->leafkey)
33 {
35
36 retval = palloc_object(GISTENTRY);
38
40 entry->rel, entry->page,
41 entry->offset, false);
42 }
43
44 PG_RETURN_POINTER(retval);
45}
46
47/*
48 * We do not need a decompress function, because the other gtsquery
49 * support functions work with the compressed representation.
50 */
51
54{
56 TSQuery query = PG_GETARG_TSQUERY(1);
58#ifdef NOT_USED
59 Oid subtype = PG_GETARG_OID(3);
60#endif
61 bool *recheck = (bool *) PG_GETARG_POINTER(4);
64 bool retval;
65
66 /* All cases served by this function are inexact */
67 *recheck = true;
68
69 switch (strategy)
70 {
72 if (GIST_LEAF(entry))
73 retval = (key & sq) == sq;
74 else
75 retval = (key & sq) != 0;
76 break;
78 if (GIST_LEAF(entry))
79 retval = (key & sq) == key;
80 else
81 retval = (key & sq) != 0;
82 break;
83 default:
84 retval = false;
85 }
86 PG_RETURN_BOOL(retval);
87}
88
91{
93 int *size = (int *) PG_GETARG_POINTER(1);
95 int i;
96
97 sign = 0;
98
99 for (i = 0; i < entryvec->n; i++)
100 sign |= GETENTRY(entryvec, i);
101
102 *size = sizeof(TSQuerySign);
103
105}
106
107Datum
109{
112 bool *result = (bool *) PG_GETARG_POINTER(2);
113
114 *result = (a == b);
115
116 PG_RETURN_POINTER(result);
117}
118
119static int
121{
122 int size = 0,
123 i;
124
125 for (i = 0; i < TSQS_SIGLEN; i++)
126 size += 0x01 & (sign >> i);
127
128 return size;
129}
130
131static int
133{
134 TSQuerySign res = a ^ b;
135
136 return sizebitvec(res);
137}
138
139Datum
141{
144 float *penalty = (float *) PG_GETARG_POINTER(2);
145
146 *penalty = hemdist(origval, newval);
147
148 PG_RETURN_POINTER(penalty);
149}
150
151
152typedef struct
153{
154 OffsetNumber pos;
155 int32 cost;
156} SPLITCOST;
157
158static int
159comparecost(const void *a, const void *b)
160{
161 return pg_cmp_s32(((const SPLITCOST *) a)->cost,
162 ((const SPLITCOST *) b)->cost);
163}
164
165#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
166
167Datum
169{
172 OffsetNumber maxoff = entryvec->n - 2;
173 OffsetNumber k,
174 j;
176 datum_r;
178 size_beta;
180 waste = -1;
181 int32 nbytes;
183 seed_2 = 0;
184 OffsetNumber *left,
185 *right;
186
188
189 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
190 left = v->spl_left = (OffsetNumber *) palloc(nbytes);
191 right = v->spl_right = (OffsetNumber *) palloc(nbytes);
192 v->spl_nleft = v->spl_nright = 0;
193
194 for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
195 for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
196 {
198 if (size_waste > waste)
199 {
200 waste = size_waste;
201 seed_1 = k;
202 seed_2 = j;
203 }
204 }
205
206
207 if (seed_1 == 0 || seed_2 == 0)
208 {
209 seed_1 = 1;
210 seed_2 = 2;
211 }
212
215
216 maxoff = OffsetNumberNext(maxoff);
218 for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
219 {
220 costvector[j - 1].pos = j;
223 costvector[j - 1].cost = abs(size_alpha - size_beta);
224 }
225 qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
226
227 for (k = 0; k < maxoff; k++)
228 {
229 j = costvector[k].pos;
230 if (j == seed_1)
231 {
232 *left++ = j;
233 v->spl_nleft++;
234 continue;
235 }
236 else if (j == seed_2)
237 {
238 *right++ = j;
239 v->spl_nright++;
240 continue;
241 }
244
245 if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.05))
246 {
248 *left++ = j;
249 v->spl_nleft++;
250 }
251 else
252 {
254 *right++ = j;
255 v->spl_nright++;
256 }
257 }
258
259 *right = *left = FirstOffsetNumber;
262
264}
265
266/*
267 * Formerly, gtsquery_consistent was declared in pg_proc.h with arguments
268 * that did not match the documented conventions for GiST support functions.
269 * We fixed that, but we still need a pg_proc entry with the old signature
270 * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
271 * This compatibility function should go away eventually.
272 */
273Datum
int32_t int32
Definition c.h:542
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define GIST_LEAF(entry)
Definition gist.h:171
#define gistentryinit(e, k, r, pg, o, l)
Definition gist.h:245
#define newval
char sign
Definition informix.c:693
static int pg_cmp_s32(int32 a, int32 b)
Definition int.h:713
int b
Definition isn.c:74
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
void * palloc(Size size)
Definition mcxt.c:1387
#define OffsetNumberNext(offsetNumber)
Definition off.h:52
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
#define qsort(a, b, c, d)
Definition port.h:495
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
uint16 StrategyNumber
Definition stratnum.h:22
#define RTContainsStrategyNumber
Definition stratnum.h:57
#define RTContainedByStrategyNumber
Definition stratnum.h:58
OffsetNumber offset
Definition gist.h:164
Datum key
Definition gist.h:161
Page page
Definition gist.h:163
Relation rel
Definition gist.h:162
bool leafkey
Definition gist.h:165
int spl_nleft
Definition gist.h:144
OffsetNumber * spl_right
Definition gist.h:148
Datum spl_ldatum
Definition gist.h:145
Datum spl_rdatum
Definition gist.h:150
int spl_nright
Definition gist.h:149
OffsetNumber * spl_left
Definition gist.h:143
static TSQuery DatumGetTSQuery(Datum X)
Definition ts_type.h:249
#define PG_GETARG_TSQUERY(n)
Definition ts_type.h:266
#define TSQS_SIGLEN
Definition ts_utils.h:251
static Datum TSQuerySignGetDatum(TSQuerySign X)
Definition ts_utils.h:254
uint64 TSQuerySign
Definition ts_utils.h:249
static TSQuerySign DatumGetTSQuerySign(Datum X)
Definition ts_utils.h:260
#define PG_GETARG_TSQUERYSIGN(n)
Definition ts_utils.h:266
#define PG_RETURN_TSQUERYSIGN(X)
Definition ts_utils.h:265
#define WISH_F(a, b, c)
#define GETENTRY(vec, pos)
Datum gtsquery_consistent(PG_FUNCTION_ARGS)
Datum gtsquery_consistent_oldsig(PG_FUNCTION_ARGS)
Datum gtsquery_union(PG_FUNCTION_ARGS)
Datum gtsquery_compress(PG_FUNCTION_ARGS)
static int sizebitvec(TSQuerySign sign)
Datum gtsquery_penalty(PG_FUNCTION_ARGS)
Datum gtsquery_same(PG_FUNCTION_ARGS)
static int hemdist(TSQuerySign a, TSQuerySign b)
Datum gtsquery_picksplit(PG_FUNCTION_ARGS)
static int comparecost(const void *a, const void *b)
TSQuerySign makeTSQuerySign(TSQuery a)
Definition tsquery_op.c:250