8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.200 2002/09/23 00:42:48 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.201 2002/09/27 15:05:23 tgl Exp $
12
12
*
13
13
*
14
14
* INTERFACE ROUTINES
@@ -420,69 +420,67 @@ UpdateIndexRelation(Oid indexoid,
420
420
Oid * classOids ,
421
421
bool primary )
422
422
{
423
- Form_pg_index indexForm ;
424
- char * predString ;
425
- text * predText ;
426
- int predLen ,
427
- itupLen ;
423
+ int16 indkey [ INDEX_MAX_KEYS ] ;
424
+ Oid indclass [ INDEX_MAX_KEYS ] ;
425
+ Datum predDatum ;
426
+ Datum values [ Natts_pg_index ];
427
+ char nulls [ Natts_pg_index ] ;
428
428
Relation pg_index ;
429
429
HeapTuple tuple ;
430
430
int i ;
431
431
432
432
/*
433
- * allocate a Form_pg_index big enough to hold the index-predicate (if
434
- * any) in string form
433
+ * Copy the index key and opclass info into zero-filled vectors
434
+ *
435
+ * (zero filling is essential 'cause we don't store the number of
436
+ * index columns explicitly in pg_index, which is pretty grotty...)
437
+ */
438
+ MemSet (indkey , 0 , sizeof (indkey ));
439
+ for (i = 0 ; i < indexInfo -> ii_NumKeyAttrs ; i ++ )
440
+ indkey [i ] = indexInfo -> ii_KeyAttrNumbers [i ];
441
+
442
+ MemSet (indclass , 0 , sizeof (indclass ));
443
+ for (i = 0 ; i < indexInfo -> ii_NumIndexAttrs ; i ++ )
444
+ indclass [i ] = classOids [i ];
445
+
446
+ /*
447
+ * Convert the index-predicate (if any) to a text datum
435
448
*/
436
449
if (indexInfo -> ii_Predicate != NIL )
437
450
{
451
+ char * predString ;
452
+
438
453
predString = nodeToString (indexInfo -> ii_Predicate );
439
- predText = DatumGetTextP ( DirectFunctionCall1 (textin ,
440
- CStringGetDatum (predString ) ));
454
+ predDatum = DirectFunctionCall1 (textin ,
455
+ CStringGetDatum (predString ));
441
456
pfree (predString );
442
457
}
443
458
else
444
- predText = DatumGetTextP (DirectFunctionCall1 (textin ,
445
- CStringGetDatum ("" )));
446
-
447
- predLen = VARSIZE (predText );
448
- itupLen = predLen + sizeof (FormData_pg_index );
449
- indexForm = (Form_pg_index ) palloc (itupLen );
450
- MemSet (indexForm , 0 , sizeof (FormData_pg_index ));
451
-
452
- /*
453
- * store information into the index tuple form
454
- */
455
- indexForm -> indexrelid = indexoid ;
456
- indexForm -> indrelid = heapoid ;
457
- indexForm -> indproc = indexInfo -> ii_FuncOid ;
458
- indexForm -> indisclustered = false; /* not clustered, yet */
459
- indexForm -> indisunique = indexInfo -> ii_Unique ;
460
- indexForm -> indisprimary = primary ;
461
- memcpy ((char * ) & indexForm -> indpred , (char * ) predText , predLen );
462
-
463
- /*
464
- * copy index key and op class information
465
- *
466
- * We zeroed the extra slots (if any) above --- that's essential.
467
- */
468
- for (i = 0 ; i < indexInfo -> ii_NumKeyAttrs ; i ++ )
469
- indexForm -> indkey [i ] = indexInfo -> ii_KeyAttrNumbers [i ];
470
-
471
- for (i = 0 ; i < indexInfo -> ii_NumIndexAttrs ; i ++ )
472
- indexForm -> indclass [i ] = classOids [i ];
459
+ predDatum = DirectFunctionCall1 (textin ,
460
+ CStringGetDatum ("" ));
473
461
474
462
/*
475
463
* open the system catalog index relation
476
464
*/
477
465
pg_index = heap_openr (IndexRelationName , RowExclusiveLock );
478
466
479
467
/*
480
- * form a tuple to insert into pg_index
468
+ * Build a pg_index tuple
481
469
*/
482
- tuple = heap_addheader (Natts_pg_index ,
483
- false,
484
- itupLen ,
485
- (void * ) indexForm );
470
+ MemSet (nulls , ' ' , sizeof (nulls ));
471
+
472
+ values [Anum_pg_index_indexrelid - 1 ] = ObjectIdGetDatum (indexoid );
473
+ values [Anum_pg_index_indrelid - 1 ] = ObjectIdGetDatum (heapoid );
474
+ values [Anum_pg_index_indproc - 1 ] = ObjectIdGetDatum (indexInfo -> ii_FuncOid );
475
+ values [Anum_pg_index_indkey - 1 ] = PointerGetDatum (indkey );
476
+ values [Anum_pg_index_indclass - 1 ] = PointerGetDatum (indclass );
477
+ values [Anum_pg_index_indisclustered - 1 ] = BoolGetDatum (false);
478
+ values [Anum_pg_index_indisunique - 1 ] = BoolGetDatum (indexInfo -> ii_Unique );
479
+ values [Anum_pg_index_indisprimary - 1 ] = BoolGetDatum (primary );
480
+ values [Anum_pg_index_indreference - 1 ] = ObjectIdGetDatum (InvalidOid );
481
+ values [Anum_pg_index_indpred - 1 ] = predDatum ;
482
+
483
+ tuple = heap_formtuple (RelationGetDescr (pg_index ), values , nulls );
486
484
487
485
/*
488
486
* insert the tuple into the pg_index catalog
@@ -496,8 +494,6 @@ UpdateIndexRelation(Oid indexoid,
496
494
* close the relation and free the tuple
497
495
*/
498
496
heap_close (pg_index , RowExclusiveLock );
499
- pfree (predText );
500
- pfree (indexForm );
501
497
heap_freetuple (tuple );
502
498
}
503
499
0 commit comments