aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/nbtree/nbtutils.c
blob: 6d0a40ef132fbdbdfac92de0676cb6008d4ca501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-------------------------------------------------------------------------
 *
 * btutils.c--
 *    Utility code for Postgres btree implementation.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtutils.c,v 1.8 1997/03/18 18:38:46 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */

#include <postgres.h>

#include <access/genam.h>
#include <fmgr.h>
#include <storage/bufpage.h>
#include <access/nbtree.h>
#include <access/istrat.h>
#include <access/iqual.h>

#ifndef HAVE_MEMMOVE
# include <regex/utils.h>
#else
# include <string.h>
#endif

ScanKey 
_bt_mkscankey(Relation rel, IndexTuple itup)
{     
    ScanKey skey;
    TupleDesc itupdesc;
    int natts;
    int i;
    Datum arg;
    RegProcedure proc;
    bool null;
    
    natts = rel->rd_rel->relnatts;
    itupdesc = RelationGetTupleDescriptor(rel);
    
    skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
    
    for (i = 0; i < natts; i++) {
	arg = index_getattr(itup, i + 1, itupdesc, &null);
	proc = index_getprocid(rel, i + 1, BTORDER_PROC);
	ScanKeyEntryInitialize(&skey[i],
			       0x0, (AttrNumber) (i + 1), proc, arg);
    }
    
    return (skey);
}

void
_bt_freeskey(ScanKey skey)
{
    pfree(skey);
}

void
_bt_freestack(BTStack stack)
{
    BTStack ostack;
    
    while (stack != (BTStack) NULL) {
	ostack = stack;
	stack = stack->bts_parent;
	pfree(ostack->bts_btitem);
	pfree(ostack);
    }
}

/*
 *  _bt_orderkeys() -- Put keys in a sensible order for conjunctive quals.
 *
 *	The order of the keys in the qual match the ordering imposed by
 *	the index.  This routine only needs to be called if there are
 *	more than one qual clauses using this index.
 */
void
_bt_orderkeys(Relation relation, BTScanOpaque so)
{
    ScanKey xform;
    ScanKeyData *cur;
    StrategyMap map;
    int nbytes;
    long test;
    int i, j;
    int init[BTMaxStrategyNumber+1];
    ScanKey key;
    uint16 numberOfKeys, new_numberOfKeys = 0;
    AttrNumber attno = 1;
    
    numberOfKeys = so->numberOfKeys;
    key = so->keyData;
    
    if ( numberOfKeys <= 1 )
    	return;
    
    /* get space for the modified array of keys */
    nbytes = BTMaxStrategyNumber * sizeof(ScanKeyData);
    xform = (ScanKey) palloc(nbytes);
    
    cur = &key[0];
    if ( cur->sk_attno != 1 )
	elog (WARN, "_bt_orderkeys: key(s) for attribute 1 missed");

    memset(xform, 0, nbytes); 
    map = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(relation),
				      BTMaxStrategyNumber,
				      attno);
    for (j = 0; j <= BTMaxStrategyNumber; j++)
	init[j] = 0;
    
    /* check each key passed in */
    for (i = 0; ; )
    {
	if ( i < numberOfKeys )
	    cur = &key[i];
	if ( i == numberOfKeys || cur->sk_attno != attno )
	{
	    if ( cur->sk_attno != attno + 1 && i < numberOfKeys )
	    {
	    	elog (WARN, "_bt_orderkeys: key(s) for attribute %d missed", attno + 1);
	    }
	    /* 
	     * If = has been specified, no other key will be used.
	     * In case of key < 2 && key == 1 and so on 
     	     * we have to set qual_ok to 0
     	     */
	    if (init[BTEqualStrategyNumber - 1])
	    {
		ScanKeyData *eq, *chk;

		eq = &xform[BTEqualStrategyNumber - 1];
		for (j = BTMaxStrategyNumber; --j >= 0; )
		{
		    if ( j == (BTEqualStrategyNumber - 1) || init[j] == 0 )
			continue;
		    chk = &xform[j];
		    test = (long) fmgr(chk->sk_procedure, eq->sk_argument, chk->sk_argument);
		    if (!test)
			so->qual_ok = 0;
		}
		init[BTLessStrategyNumber - 1] = 0;
		init[BTLessEqualStrategyNumber - 1] = 0;
		init[BTGreaterEqualStrategyNumber - 1] = 0;
		init[BTGreaterStrategyNumber - 1] = 0;
	    }
    
	    /* only one of <, <= */
	    if (init[BTLessStrategyNumber - 1]
			&& init[BTLessEqualStrategyNumber - 1])
	    {
		ScanKeyData *lt, *le;
	
		lt = &xform[BTLessStrategyNumber - 1];
		le = &xform[BTLessEqualStrategyNumber - 1];
		/*
		 *  DO NOT use the cached function stuff here -- this is key
		 *  ordering, happens only when the user expresses a hokey
		 *  qualification, and gets executed only once, anyway.  The
		 *  transform maps are hard-coded, and can't be initialized
		 *  in the correct way.
		 */
		test = (long) fmgr(le->sk_procedure, lt->sk_argument, le->sk_argument);
		if (test)
	    	    init[BTLessEqualStrategyNumber - 1] = 0;
		else
		    init[BTLessStrategyNumber - 1] = 0;
    	    }
    
	    /* only one of >, >= */
	    if (init[BTGreaterStrategyNumber - 1]
			&& init[BTGreaterEqualStrategyNumber - 1])
	    {
	    	ScanKeyData *gt, *ge;
	
		gt = &xform[BTGreaterStrategyNumber - 1];
		ge = &xform[BTGreaterEqualStrategyNumber - 1];
	
		/* see note above on function cache */
		test = (long) fmgr(ge->sk_procedure, gt->sk_argument, ge->sk_argument);
		if (test)
	    	    init[BTGreaterEqualStrategyNumber - 1] = 0;
		else
	    	    init[BTGreaterStrategyNumber - 1] = 0;
    	    }
    
    	    /* okay, reorder and count */
    	    for (j = BTMaxStrategyNumber; --j >= 0; )
	    	if (init[j])
		    key[new_numberOfKeys++] = xform[j];
    
    	    if ( attno == 1 )
    		so->numberOfFirstKeys = new_numberOfKeys;
    	    
    	    if ( i == numberOfKeys )
    	    	break;

	    /* initialization for new attno */    	    
    	    attno = cur->sk_attno;
    	    memset(xform, 0, nbytes); 
	    map = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(relation),
				      BTMaxStrategyNumber,
				      attno);
	    /* haven't looked at any strategies yet */
	    for (j = 0; j <= BTMaxStrategyNumber; j++)
		init[j] = 0;
	}

	for (j = BTMaxStrategyNumber; --j >= 0; )
	{
	    if (cur->sk_procedure == map->entry[j].sk_procedure)
	    	break;
	}
	
	/* have we seen one of these before? */
	if (init[j])
	{
	    /* yup, use the appropriate value */
	    test =
		(long) FMGR_PTR2(cur->sk_func, cur->sk_procedure,
				 cur->sk_argument, xform[j].sk_argument);
	    if (test)
		xform[j].sk_argument = cur->sk_argument;
	    else if ( j == (BTEqualStrategyNumber - 1) )
	    	so->qual_ok = 0;	/* key == a && key == b, but a != b */
	} else
	{
	    /* nope, use this value */
	    memmove(&xform[j], cur, sizeof(*cur));
	    init[j] = 1;
	}
	
	i++;
    }
    
    so->numberOfKeys = new_numberOfKeys;
    
    pfree(xform);
}

bool
_bt_checkqual(IndexScanDesc scan, IndexTuple itup)
{
    BTScanOpaque so;
    
    so = (BTScanOpaque) scan->opaque;
    if (so->numberOfKeys > 0)
	return (index_keytest(itup, RelationGetTupleDescriptor(scan->relation),
			      so->numberOfKeys, so->keyData));
    else
	return (true);
}

bool
_bt_checkforkeys(IndexScanDesc scan, IndexTuple itup, Size keysz)
{
    BTScanOpaque so;
    
    so = (BTScanOpaque) scan->opaque;
    if ( keysz > 0 && so->numberOfKeys >= keysz )
	return (index_keytest(itup, RelationGetTupleDescriptor(scan->relation),
			      keysz, so->keyData));
    else
	return (true);
}

BTItem
_bt_formitem(IndexTuple itup)
{
    int nbytes_btitem;
    BTItem btitem;
    Size tuplen;
    extern Oid newoid();
    
    /* disallow nulls in btree keys */
    if (itup->t_info & INDEX_NULL_MASK)
	elog(WARN, "btree indices cannot include null keys");
    
    /* make a copy of the index tuple with room for the sequence number */
    tuplen = IndexTupleSize(itup);
    nbytes_btitem = tuplen +
	(sizeof(BTItemData) - sizeof(IndexTupleData));
    
    btitem = (BTItem) palloc(nbytes_btitem);
    memmove((char *) &(btitem->bti_itup), (char *) itup, tuplen);
    
    btitem->bti_oid = newoid();
    return (btitem);
}