~ubuntu-branches/ubuntu/natty/postgresql-8.4/natty-security

« back to all changes in this revision

Viewing changes to src/backend/access/gin/ginutil.c

Tags: upstream-8.4.0
ImportĀ upstreamĀ versionĀ 8.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
9
 *
10
10
 * IDENTIFICATION
11
 
 *                      $PostgreSQL: pgsql/src/backend/access/gin/ginutil.c,v 1.21 2009/03/24 20:17:11 tgl Exp $
 
11
 *                      $PostgreSQL: pgsql/src/backend/access/gin/ginutil.c,v 1.22 2009/06/11 14:48:53 momjian Exp $
12
12
 *-------------------------------------------------------------------------
13
13
 */
14
14
 
16
16
#include "access/genam.h"
17
17
#include "access/gin.h"
18
18
#include "access/reloptions.h"
19
 
#include "catalog/pg_type.h" 
 
19
#include "catalog/pg_type.h"
20
20
#include "storage/bufmgr.h"
21
21
#include "storage/freespace.h"
22
22
#include "storage/indexfsm.h"
25
25
void
26
26
initGinState(GinState *state, Relation index)
27
27
{
28
 
        int i;
 
28
        int                     i;
29
29
 
30
30
        state->origTupdesc = index->rd_att;
31
31
 
32
32
        state->oneCol = (index->rd_att->natts == 1) ? true : false;
33
33
 
34
 
        for(i=0;i<index->rd_att->natts;i++)
 
34
        for (i = 0; i < index->rd_att->natts; i++)
35
35
        {
36
 
                state->tupdesc[i] = CreateTemplateTupleDesc(2,false);
 
36
                state->tupdesc[i] = CreateTemplateTupleDesc(2, false);
37
37
 
38
 
                TupleDescInitEntry( state->tupdesc[i], (AttrNumber) 1, NULL,
39
 
                                                        INT2OID, -1, 0);
40
 
                TupleDescInitEntry( state->tupdesc[i], (AttrNumber) 2, NULL,
41
 
                                                        index->rd_att->attrs[i]->atttypid,
42
 
                                                        index->rd_att->attrs[i]->atttypmod,
43
 
                                                        index->rd_att->attrs[i]->attndims
44
 
                                                        );
 
38
                TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 1, NULL,
 
39
                                                   INT2OID, -1, 0);
 
40
                TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 2, NULL,
 
41
                                                   index->rd_att->attrs[i]->atttypid,
 
42
                                                   index->rd_att->attrs[i]->atttypmod,
 
43
                                                   index->rd_att->attrs[i]->attndims
 
44
                        );
45
45
 
46
46
                fmgr_info_copy(&(state->compareFn[i]),
47
 
                                                index_getprocinfo(index, i+1, GIN_COMPARE_PROC),
48
 
                                                CurrentMemoryContext);
 
47
                                           index_getprocinfo(index, i + 1, GIN_COMPARE_PROC),
 
48
                                           CurrentMemoryContext);
49
49
                fmgr_info_copy(&(state->extractValueFn[i]),
50
 
                                                index_getprocinfo(index, i+1, GIN_EXTRACTVALUE_PROC),
51
 
                                                CurrentMemoryContext);
 
50
                                           index_getprocinfo(index, i + 1, GIN_EXTRACTVALUE_PROC),
 
51
                                           CurrentMemoryContext);
52
52
                fmgr_info_copy(&(state->extractQueryFn[i]),
53
 
                                                index_getprocinfo(index, i+1, GIN_EXTRACTQUERY_PROC),
54
 
                                                CurrentMemoryContext);
 
53
                                           index_getprocinfo(index, i + 1, GIN_EXTRACTQUERY_PROC),
 
54
                                           CurrentMemoryContext);
55
55
                fmgr_info_copy(&(state->consistentFn[i]),
56
 
                                                index_getprocinfo(index, i+1, GIN_CONSISTENT_PROC),
57
 
                                                CurrentMemoryContext);
 
56
                                           index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC),
 
57
                                           CurrentMemoryContext);
58
58
 
59
59
                /*
60
60
                 * Check opclass capability to do partial match.
61
61
                 */
62
 
                if ( index_getprocid(index, i+1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid )
 
62
                if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid)
63
63
                {
64
64
                        fmgr_info_copy(&(state->comparePartialFn[i]),
65
 
                                                   index_getprocinfo(index, i+1, GIN_COMPARE_PARTIAL_PROC),
 
65
                                   index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
66
66
                                                   CurrentMemoryContext);
67
67
 
68
68
                        state->canPartialMatch[i] = true;
82
82
{
83
83
        OffsetNumber colN = FirstOffsetNumber;
84
84
 
85
 
        if ( !ginstate->oneCol )
 
85
        if (!ginstate->oneCol)
86
86
        {
87
 
                Datum   res;
88
 
                bool    isnull;
 
87
                Datum           res;
 
88
                bool            isnull;
89
89
 
90
90
                /*
91
 
                 * First attribute is always int16, so we can safely use any
92
 
                 * tuple descriptor to obtain first attribute of tuple
 
91
                 * First attribute is always int16, so we can safely use any tuple
 
92
                 * descriptor to obtain first attribute of tuple
93
93
                 */
94
94
                res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
95
95
                                                        &isnull);
96
96
                Assert(!isnull);
97
97
 
98
98
                colN = DatumGetUInt16(res);
99
 
                Assert( colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts );
 
99
                Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
100
100
        }
101
101
 
102
102
        return colN;
108
108
Datum
109
109
gin_index_getattr(GinState *ginstate, IndexTuple tuple)
110
110
{
111
 
        bool    isnull;
112
 
        Datum   res;
 
111
        bool            isnull;
 
112
        Datum           res;
113
113
 
114
 
        if ( ginstate->oneCol )
 
114
        if (ginstate->oneCol)
115
115
        {
116
116
                /*
117
117
                 * Single column index doesn't store attribute numbers in tuples
122
122
        else
123
123
        {
124
124
                /*
125
 
                 * Since the datum type depends on which index column it's from,
126
 
                 * we must be careful to use the right tuple descriptor here.
 
125
                 * Since the datum type depends on which index column it's from, we
 
126
                 * must be careful to use the right tuple descriptor here.
127
127
                 */
128
128
                OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
129
129
 
216
216
void
217
217
GinInitMetabuffer(Buffer b)
218
218
{
219
 
        GinMetaPageData *metadata;
220
 
        Page                     page = BufferGetPage(b);
 
219
        GinMetaPageData *metadata;
 
220
        Page            page = BufferGetPage(b);
221
221
 
222
222
        GinInitPage(page, GIN_META, BufferGetPageSize(b));
223
223
 
234
234
{
235
235
        return DatumGetInt32(
236
236
                                                 FunctionCall2(
237
 
                                                                           &ginstate->compareFn[attnum-1],
 
237
                                                                           &ginstate->compareFn[attnum - 1],
238
238
                                                                           a, b
239
239
                                                                           )
240
240
                );
242
242
 
243
243
int
244
244
compareAttEntries(GinState *ginstate, OffsetNumber attnum_a, Datum a,
245
 
                                                                          OffsetNumber attnum_b, Datum b)
 
245
                                  OffsetNumber attnum_b, Datum b)
246
246
{
247
 
        if ( attnum_a == attnum_b )
248
 
                return compareEntries( ginstate, attnum_a, a, b);
 
247
        if (attnum_a == attnum_b)
 
248
                return compareEntries(ginstate, attnum_a, a, b);
249
249
 
250
 
        return ( attnum_a < attnum_b ) ? -1 : 1;
 
250
        return (attnum_a < attnum_b) ? -1 : 1;
251
251
}
252
252
 
253
253
typedef struct
275
275
        Datum      *entries;
276
276
 
277
277
        entries = (Datum *) DatumGetPointer(FunctionCall2(
278
 
                                                                                                   &ginstate->extractValueFn[attnum-1],
 
278
                                                                           &ginstate->extractValueFn[attnum - 1],
279
279
                                                                                                          value,
280
280
                                                                                                        PointerGetDatum(nentries)
281
281
                                                                                                          ));
288
288
        {
289
289
                cmpEntriesData arg;
290
290
 
291
 
                arg.cmpDatumFunc = &ginstate->compareFn[attnum-1];
 
291
                arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
292
292
                arg.needUnique = needUnique;
293
293
                qsort_arg(entries, *nentries, sizeof(Datum),
294
294
                                  (qsort_arg_comparator) cmpEntries, (void *) &arg);
348
348
        rdopts = allocateReloptStruct(sizeof(GinOptions), options, numoptions);
349
349
 
350
350
        fillRelOptions((void *) rdopts, sizeof(GinOptions), options, numoptions,
351
 
                                        validate, tab, lengthof(tab));
 
351
                                   validate, tab, lengthof(tab));
352
352
 
353
353
        pfree(options);
354
354