aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/defind.c
blob: da797e23cbbe6327bb87f7646cd68608cacbf066 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*-------------------------------------------------------------------------
 *
 * defind.c--
 *    POSTGRES define, extend and remove index code.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/commands/Attic/defind.c,v 1.1.1.1 1996/07/09 06:21:20 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/attnum.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup.h"
#include "access/funcindex.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
#include "catalog/index.h"
#include "catalog/pg_index.h"
#include "catalog/pg_proc.h"
#include "nodes/pg_list.h"
#include "nodes/plannodes.h"
#include "nodes/primnodes.h"
#include "nodes/relation.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/relcache.h"
#include "utils/lsyscache.h"

#include "commands/defrem.h"
#include "parser/parsetree.h"		/* for getrelid() */

#include "optimizer/prep.h"
#include "optimizer/clauses.h"
#include "storage/lmgr.h"

#define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->args!=NULL)

/* non-export function prototypes */
static void CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid);
static void CheckPredExpr(Node *predicate, List *rangeTable,
			  Oid baseRelOid);
static void
CheckPredClause(Expr *predicate, List *rangeTable, Oid baseRelOid);
static void FuncIndexArgs(IndexElem *funcIndex, AttrNumber *attNumP,
			  Oid *argTypes, Oid *opOidP, Oid relId);
static void NormIndexAttrs(List *attList, AttrNumber *attNumP,
			   Oid *opOidP, Oid relId);

/*
 * DefineIndex --
 *	Creates a new index.
 *
 * 'attributeList' is a list of IndexElem specifying either a functional
 *	index or a list of attributes to index on.
 * 'parameterList' is a list of ParamString specified in the with clause.
 * 'predicate' is the qual specified in the where clause.
 * 'rangetable' is for the predicate
 *
 * Exceptions:
 *	XXX
 */
void
DefineIndex(char *heapRelationName,
	    char *indexRelationName,
	    char *accessMethodName,
	    List *attributeList,
	    List *parameterList,
	    Expr *predicate,
	    List *rangetable)
{
    Oid	*classObjectId;
    Oid	accessMethodId;
    Oid	relationId;
    int		numberOfAttributes;
    AttrNumber	*attributeNumberA;
    HeapTuple	tuple;
    uint16	parameterCount = 0;
    Datum	*parameterA = NULL;
    FuncIndexInfo fInfo;
    List 	*cnfPred = NULL;
    
    
    /*
     * Handle attributes
     */
    numberOfAttributes = length(attributeList);
    if (numberOfAttributes <= 0) {
	elog(WARN, "DefineIndex: must specify at least one attribute");
    }
    
    /*
     * compute heap relation id
     */
    tuple = SearchSysCacheTuple(RELNAME, 
				PointerGetDatum(heapRelationName),
				0,0,0);
    if (!HeapTupleIsValid(tuple)) {
	elog(WARN, "DefineIndex: %s relation not found",
	     heapRelationName);
    }
    relationId = tuple->t_oid;
    
    /*
     * compute access method id
     */
    tuple = SearchSysCacheTuple(AMNAME, PointerGetDatum(accessMethodName),
				0,0,0);
    if (!HeapTupleIsValid(tuple)) {
	elog(WARN, "DefineIndex: %s access method not found",
	     accessMethodName);
    }
    accessMethodId = tuple->t_oid;
    
    
    /*
     * Handle parameters
     * [param list is now different (NOT USED, really) - ay 10/94]
     */

    
    /*
     * Convert the partial-index predicate from parsetree form to plan
     * form, so it can be readily evaluated during index creation.
     * Note: "predicate" comes in as a list containing (1) the predicate
     * itself (a where_clause), and (2) a corresponding range table.
     *
     * [(1) is 'predicate' and (2) is 'rangetable' now. - ay 10/94]
     */
    if (predicate != NULL && rangetable != NIL) {
	cnfPred = cnfify((Expr*)copyObject(predicate), true);
	fix_opids(cnfPred);
	CheckPredicate(cnfPred, rangetable, relationId);
    }
    
    if (IsFuncIndex(attributeList)) {
	IndexElem *funcIndex= lfirst(attributeList);
	int nargs;
	
	nargs = length(funcIndex->args);
	if (nargs > INDEX_MAX_KEYS) {
	    elog(WARN, 
		 "Too many args to function, limit of %d",
		 INDEX_MAX_KEYS);
	}
	
	FIsetnArgs(&fInfo,nargs);

	strcpy(FIgetname(&fInfo), funcIndex->name);

	attributeNumberA =
	    (AttrNumber *)palloc(nargs * sizeof attributeNumberA[0]);
		     
	classObjectId = (Oid *)palloc(sizeof classObjectId[0]);
				 
	
	FuncIndexArgs(funcIndex, attributeNumberA, 
		      &(FIgetArg(&fInfo, 0)),
		      classObjectId, relationId);
	
	index_create(heapRelationName, 
		     indexRelationName,
		     &fInfo, accessMethodId, 
		     numberOfAttributes, attributeNumberA,
		     classObjectId, parameterCount, parameterA, (Node*)cnfPred);
    }else {
	attributeNumberA =
	    (AttrNumber *)palloc(numberOfAttributes *
				      sizeof attributeNumberA[0]);
		     
	classObjectId =
	    (Oid *)palloc(numberOfAttributes * sizeof classObjectId[0]);
	
	NormIndexAttrs(attributeList, attributeNumberA, 
		       classObjectId, relationId);
	
	index_create(heapRelationName, indexRelationName, NULL,
		     accessMethodId, numberOfAttributes, attributeNumberA,
		     classObjectId, parameterCount, parameterA, (Node*)cnfPred);
    }
}


/*
 * ExtendIndex --
 *	Extends a partial index.
 *
 * Exceptions:
 *	XXX
 */
void
ExtendIndex(char *indexRelationName, Expr *predicate, List *rangetable)
{
    Oid	*classObjectId;
    Oid	accessMethodId;
    Oid	indexId, relationId;
    Oid	indproc;
    int		numberOfAttributes;
    AttrNumber	*attributeNumberA;
    HeapTuple	tuple;
    FuncIndexInfo	fInfo;
    FuncIndexInfo	*funcInfo = NULL;
    IndexTupleForm	index;
    Node 	*oldPred = NULL;
    List	*cnfPred = NULL;
    PredInfo 	*predInfo;
    Relation	heapRelation;
    Relation	indexRelation;
    int		i;
    
    /*
     * compute index relation id and access method id
     */
    tuple = SearchSysCacheTuple(RELNAME, PointerGetDatum(indexRelationName),
				0,0,0);
    if (!HeapTupleIsValid(tuple)) {
	elog(WARN, "ExtendIndex: %s index not found",
	     indexRelationName);
    }
    indexId = tuple->t_oid;
    accessMethodId = ((Form_pg_class) GETSTRUCT(tuple))->relam;
    
    /*
     * find pg_index tuple
     */
    tuple = SearchSysCacheTuple(INDEXRELID, 
				ObjectIdGetDatum(indexId),
				0,0,0);
    if (!HeapTupleIsValid(tuple)) {
	elog(WARN, "ExtendIndex: %s is not an index",
	     indexRelationName);
    }
    
    /*
     * Extract info from the pg_index tuple
     */
    index = (IndexTupleForm)GETSTRUCT(tuple);
    Assert(index->indexrelid == indexId);
    relationId = index->indrelid;
    indproc = index->indproc;
    
    for (i=0; i<INDEX_MAX_KEYS; i++)
	if (index->indkey[i] == 0) break;
    numberOfAttributes = i;
    
    if (VARSIZE(&index->indpred) != 0) {
	char *predString;

	predString = fmgr(F_TEXTOUT, &index->indpred);
	oldPred = stringToNode(predString);
	pfree(predString);
    }
    if (oldPred == NULL)
	elog(WARN, "ExtendIndex: %s is not a partial index",
	     indexRelationName);
    
    /*
     * Convert the extension predicate from parsetree form to plan
     * form, so it can be readily evaluated during index creation.
     * Note: "predicate" comes in as a list containing (1) the predicate
     * itself (a where_clause), and (2) a corresponding range table.
     */
    if (rangetable != NIL) {
	cnfPred = cnfify((Expr*)copyObject(predicate), true);
	fix_opids(cnfPred);
	CheckPredicate(cnfPred, rangetable, relationId);
    }
    
    /* make predInfo list to pass to index_build */
    predInfo = (PredInfo*)palloc(sizeof(PredInfo));
    predInfo->pred = (Node*)cnfPred;
    predInfo->oldPred = oldPred;
    
    attributeNumberA =
	(AttrNumber *)palloc(numberOfAttributes*
				  sizeof attributeNumberA[0]);
    classObjectId =
	(Oid *)palloc(numberOfAttributes * sizeof classObjectId[0]);
		 
    
    for (i=0; i<numberOfAttributes; i++) {
	attributeNumberA[i] = index->indkey[i];
	classObjectId[i] = index->indclass[i];
    }
    
    if (indproc != InvalidOid) {
	funcInfo = &fInfo;
/*	FIgetnArgs(funcInfo) = numberOfAttributes; */
	FIsetnArgs(funcInfo,numberOfAttributes);
	
	tuple = SearchSysCacheTuple(PROOID,
				    ObjectIdGetDatum(indproc),
				    0,0,0);
	if (!HeapTupleIsValid(tuple))
	    elog(WARN, "ExtendIndex: index procedure not found");

	namecpy(&(funcInfo->funcName),
		&(((Form_pg_proc) GETSTRUCT(tuple))->proname));

	FIsetProcOid(funcInfo,tuple->t_oid);
    }
    
    heapRelation = heap_open(relationId);
    indexRelation = index_open(indexId);
    
    RelationSetLockForWrite(heapRelation);
    
    InitIndexStrategy(numberOfAttributes, indexRelation, accessMethodId);
    
    index_build(heapRelation, indexRelation, numberOfAttributes,
		attributeNumberA, 0, NULL, funcInfo, predInfo);
}


/*
 * CheckPredicate
 *	Checks that the given list of partial-index predicates refer
 *	(via the given range table) only to the given base relation oid,
 *	and that they're in a form the planner can handle, i.e.,
 *	boolean combinations of "ATTR OP CONST" (yes, for now, the ATTR
 *	has to be on the left).
 */

static void
CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid)
{
    List *item;
    
    foreach (item, predList) {
	CheckPredExpr(lfirst(item), rangeTable, baseRelOid);
    }
}

static void
CheckPredExpr(Node *predicate, List *rangeTable, Oid baseRelOid)
{
    List *clauses = NIL, *clause;

    if (is_opclause(predicate)) {
	CheckPredClause((Expr*)predicate, rangeTable, baseRelOid);
	return;
    } else if (or_clause(predicate))
	clauses = ((Expr*)predicate)->args;
    else if (and_clause(predicate))
	clauses = ((Expr*)predicate)->args;
    else
	elog(WARN, "Unsupported partial-index predicate expression type");
    
    foreach (clause, clauses) {
	CheckPredExpr(lfirst(clause), rangeTable, baseRelOid);
    }
}

static void
CheckPredClause(Expr *predicate, List *rangeTable, Oid baseRelOid)
{
    Var		*pred_var;
    Const	*pred_const;
    
    pred_var = (Var *)get_leftop(predicate);
    pred_const = (Const *)get_rightop(predicate);
    
    if (!IsA(predicate->oper,Oper) ||
	!IsA(pred_var,Var) ||
	!IsA(pred_const,Const)) {
	elog(WARN, "Unsupported partial-index predicate clause type");
    }

    if (getrelid(pred_var->varno, rangeTable) != baseRelOid)
	elog(WARN,
	     "Partial-index predicates may refer only to the base relation");
}


static void 
FuncIndexArgs(IndexElem *funcIndex,
	      AttrNumber *attNumP,
	      Oid *argTypes,
	      Oid *opOidP,
	      Oid relId)
{
    List	*rest;
    HeapTuple	tuple;
    AttributeTupleForm	att;
    
    tuple = SearchSysCacheTuple(CLANAME,
				PointerGetDatum(funcIndex->class),
				0,0,0);
    
    if (!HeapTupleIsValid(tuple)) 
	{
	    elog(WARN, "DefineIndex: %s class not found",
		 funcIndex->class);
	}
    *opOidP = tuple->t_oid;
    
    memset(argTypes, 0, 8 * sizeof(Oid)); 
    
    /*
     * process the function arguments 
     */
    for (rest=funcIndex->args; rest != NIL; rest = lnext(rest)) {
	char *arg;
	
	arg = strVal(lfirst(rest));
	
	tuple = SearchSysCacheTuple(ATTNAME,
				    ObjectIdGetDatum(relId),
				    PointerGetDatum(arg),0,0);
	
	if (!HeapTupleIsValid(tuple)) {
	    elog(WARN, 
		 "DefineIndex: attribute \"%s\" not found",
		 arg);
	}
	att = (AttributeTupleForm)GETSTRUCT(tuple);
	*attNumP++ = att->attnum;
	*argTypes++ = att->atttypid;
    }
}

static void 	
NormIndexAttrs(List *attList,		/* list of IndexElem's */
	       AttrNumber *attNumP,
	       Oid *opOidP,
	       Oid relId)
{
    List	*rest;
    HeapTuple	tuple;
    
    /*
     * process attributeList
     */
    
    for (rest=attList; rest != NIL; rest = lnext(rest)) {
	IndexElem *attribute;
	
	attribute = lfirst(rest);
	
	if (attribute->class == NULL) {
	    elog(WARN,
		 "DefineIndex: default index class unsupported");
	}
	
	if (attribute->name == NULL)
	    elog(WARN, "missing attribute for define index");
	
	tuple = SearchSysCacheTuple(ATTNAME,
				    ObjectIdGetDatum(relId),
				    PointerGetDatum(attribute->name),
				    0,0);
	if (!HeapTupleIsValid(tuple)) {
	    elog(WARN, 
		 "DefineIndex: attribute \"%s\" not found",
		 attribute->name);
	}
	*attNumP++ = ((AttributeTupleForm)GETSTRUCT(tuple))->attnum;
	
	tuple = SearchSysCacheTuple(CLANAME,
				    PointerGetDatum(attribute->class),
				    0,0,0);
	
	if (!HeapTupleIsValid(tuple)) {
	    elog(WARN, "DefineIndex: %s class not found",
		 attribute->class);
	}
	*opOidP++ = tuple->t_oid;
    }
}

/*
 * RemoveIndex --
 *	Deletes an index.
 *
 * Exceptions:
 *	BadArg if name is invalid.
 *	"WARN" if index nonexistant.
 *	...
 */
void
RemoveIndex(char *name)
{
    HeapTuple	tuple;
    
    tuple = SearchSysCacheTuple(RELNAME, 
				PointerGetDatum(name),
				0,0,0);
    
    if (!HeapTupleIsValid(tuple)) {
	elog(WARN, "index \"%s\" nonexistant", name);
    }
    
    if (((Form_pg_class)GETSTRUCT(tuple))->relkind != RELKIND_INDEX) {
	elog(WARN, "relation \"%s\" is of type \"%c\"",
	     name,
	     ((Form_pg_class)GETSTRUCT(tuple))->relkind);
    }
    
    index_destroy(tuple->t_oid);
}