Skip to content

Commit 15fe086

Browse files
committed
Restructure system-catalog index updating logic. Instead of having
hardwired lists of index names for each catalog, use the relcache's mechanism for caching lists of OIDs of indexes of any table. This reduces the common case of updating system catalog indexes to a single line, makes it much easier to add a new system index (in fact, you can now do so on-the-fly if you want to), and as a nice side benefit improves performance a little. Per recent pghackers discussion.
1 parent 07f9682 commit 15fe086

29 files changed

+288
-851
lines changed

src/backend/catalog/aclchk.c

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.72 2002/07/29 22:14:10 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.73 2002/08/05 03:29:16 tgl Exp $
1212
*
1313
* NOTES
1414
* See acl.h.
@@ -236,15 +236,8 @@ ExecuteGrantStmt_Relation(GrantStmt *stmt)
236236

237237
simple_heap_update(relation, &newtuple->t_self, newtuple);
238238

239-
{
240-
/* keep the catalog indexes up to date */
241-
Relation idescs[Num_pg_class_indices];
242-
243-
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices,
244-
idescs);
245-
CatalogIndexInsert(idescs, Num_pg_class_indices, relation, newtuple);
246-
CatalogCloseIndices(Num_pg_class_indices, idescs);
247-
}
239+
/* keep the catalog indexes up to date */
240+
CatalogUpdateIndexes(relation, newtuple);
248241

249242
pfree(old_acl);
250243
pfree(new_acl);
@@ -332,15 +325,8 @@ ExecuteGrantStmt_Database(GrantStmt *stmt)
332325

333326
simple_heap_update(relation, &newtuple->t_self, newtuple);
334327

335-
{
336-
/* keep the catalog indexes up to date */
337-
Relation idescs[Num_pg_database_indices];
338-
339-
CatalogOpenIndices(Num_pg_database_indices, Name_pg_database_indices,
340-
idescs);
341-
CatalogIndexInsert(idescs, Num_pg_database_indices, relation, newtuple);
342-
CatalogCloseIndices(Num_pg_database_indices, idescs);
343-
}
328+
/* keep the catalog indexes up to date */
329+
CatalogUpdateIndexes(relation, newtuple);
344330

345331
pfree(old_acl);
346332
pfree(new_acl);
@@ -434,15 +420,8 @@ ExecuteGrantStmt_Function(GrantStmt *stmt)
434420

435421
simple_heap_update(relation, &newtuple->t_self, newtuple);
436422

437-
{
438-
/* keep the catalog indexes up to date */
439-
Relation idescs[Num_pg_proc_indices];
440-
441-
CatalogOpenIndices(Num_pg_proc_indices, Name_pg_proc_indices,
442-
idescs);
443-
CatalogIndexInsert(idescs, Num_pg_proc_indices, relation, newtuple);
444-
CatalogCloseIndices(Num_pg_proc_indices, idescs);
445-
}
423+
/* keep the catalog indexes up to date */
424+
CatalogUpdateIndexes(relation, newtuple);
446425

447426
pfree(old_acl);
448427
pfree(new_acl);
@@ -531,15 +510,8 @@ ExecuteGrantStmt_Language(GrantStmt *stmt)
531510

532511
simple_heap_update(relation, &newtuple->t_self, newtuple);
533512

534-
{
535-
/* keep the catalog indexes up to date */
536-
Relation idescs[Num_pg_language_indices];
537-
538-
CatalogOpenIndices(Num_pg_language_indices, Name_pg_language_indices,
539-
idescs);
540-
CatalogIndexInsert(idescs, Num_pg_language_indices, relation, newtuple);
541-
CatalogCloseIndices(Num_pg_language_indices, idescs);
542-
}
513+
/* keep the catalog indexes up to date */
514+
CatalogUpdateIndexes(relation, newtuple);
543515

544516
pfree(old_acl);
545517
pfree(new_acl);
@@ -628,15 +600,8 @@ ExecuteGrantStmt_Namespace(GrantStmt *stmt)
628600

629601
simple_heap_update(relation, &newtuple->t_self, newtuple);
630602

631-
{
632-
/* keep the catalog indexes up to date */
633-
Relation idescs[Num_pg_namespace_indices];
634-
635-
CatalogOpenIndices(Num_pg_namespace_indices, Name_pg_namespace_indices,
636-
idescs);
637-
CatalogIndexInsert(idescs, Num_pg_namespace_indices, relation, newtuple);
638-
CatalogCloseIndices(Num_pg_namespace_indices, idescs);
639-
}
603+
/* keep the catalog indexes up to date */
604+
CatalogUpdateIndexes(relation, newtuple);
640605

641606
pfree(old_acl);
642607
pfree(new_acl);

src/backend/catalog/heap.c

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.217 2002/08/05 02:30:50 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.218 2002/08/05 03:29:16 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -423,23 +423,17 @@ AddNewAttributeTuples(Oid new_rel_oid,
423423
int i;
424424
HeapTuple tup;
425425
Relation rel;
426-
bool hasindex;
427-
Relation idescs[Num_pg_attr_indices];
426+
CatalogIndexState indstate;
428427
int natts = tupdesc->natts;
429428
ObjectAddress myself,
430429
referenced;
431430

432431
/*
433-
* open pg_attribute
432+
* open pg_attribute and its indexes.
434433
*/
435434
rel = heap_openr(AttributeRelationName, RowExclusiveLock);
436435

437-
/*
438-
* Check if we have any indices defined on pg_attribute.
439-
*/
440-
hasindex = RelationGetForm(rel)->relhasindex;
441-
if (hasindex)
442-
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, idescs);
436+
indstate = CatalogOpenIndexes(rel);
443437

444438
/*
445439
* First we add the user attributes. This is also a convenient place
@@ -461,8 +455,7 @@ AddNewAttributeTuples(Oid new_rel_oid,
461455

462456
simple_heap_insert(rel, tup);
463457

464-
if (hasindex)
465-
CatalogIndexInsert(idescs, Num_pg_attr_indices, rel, tup);
458+
CatalogIndexInsert(indstate, tup);
466459

467460
heap_freetuple(tup);
468461

@@ -509,8 +502,7 @@ AddNewAttributeTuples(Oid new_rel_oid,
509502

510503
simple_heap_insert(rel, tup);
511504

512-
if (hasindex)
513-
CatalogIndexInsert(idescs, Num_pg_attr_indices, rel, tup);
505+
CatalogIndexInsert(indstate, tup);
514506

515507
heap_freetuple(tup);
516508
}
@@ -521,8 +513,7 @@ AddNewAttributeTuples(Oid new_rel_oid,
521513
/*
522514
* clean up
523515
*/
524-
if (hasindex)
525-
CatalogCloseIndices(Num_pg_attr_indices, idescs);
516+
CatalogCloseIndexes(indstate);
526517

527518
heap_close(rel, RowExclusiveLock);
528519
}
@@ -543,7 +534,6 @@ AddNewRelationTuple(Relation pg_class_desc,
543534
{
544535
Form_pg_class new_rel_reltup;
545536
HeapTuple tup;
546-
Relation idescs[Num_pg_class_indices];
547537

548538
/*
549539
* first we update some of the information in our uncataloged
@@ -606,20 +596,11 @@ AddNewRelationTuple(Relation pg_class_desc,
606596
HeapTupleSetOid(tup, new_rel_oid);
607597

608598
/*
609-
* finally insert the new tuple and free it.
599+
* finally insert the new tuple, update the indexes, and clean up.
610600
*/
611601
simple_heap_insert(pg_class_desc, tup);
612602

613-
if (!IsIgnoringSystemIndexes())
614-
{
615-
/*
616-
* First, open the catalog indices and insert index tuples for the
617-
* new relation.
618-
*/
619-
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
620-
CatalogIndexInsert(idescs, Num_pg_class_indices, pg_class_desc, tup);
621-
CatalogCloseIndices(Num_pg_class_indices, idescs);
622-
}
603+
CatalogUpdateIndexes(pg_class_desc, tup);
623604

624605
heap_freetuple(tup);
625606
}
@@ -953,15 +934,8 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
953934

954935
simple_heap_update(attr_rel, &tuple->t_self, tuple);
955936

956-
/* keep the system catalog indices current */
957-
if (RelationGetForm(attr_rel)->relhasindex)
958-
{
959-
Relation idescs[Num_pg_attr_indices];
960-
961-
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, idescs);
962-
CatalogIndexInsert(idescs, Num_pg_attr_indices, attr_rel, tuple);
963-
CatalogCloseIndices(Num_pg_attr_indices, idescs);
964-
}
937+
/* keep the system catalog indexes current */
938+
CatalogUpdateIndexes(attr_rel, tuple);
965939

966940
/*
967941
* Because updating the pg_attribute row will trigger a relcache flush
@@ -1087,15 +1061,8 @@ RemoveAttrDefaultById(Oid attrdefId)
10871061

10881062
simple_heap_update(attr_rel, &tuple->t_self, tuple);
10891063

1090-
/* keep the system catalog indices current */
1091-
if (RelationGetForm(attr_rel)->relhasindex)
1092-
{
1093-
Relation idescs[Num_pg_attr_indices];
1094-
1095-
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, idescs);
1096-
CatalogIndexInsert(idescs, Num_pg_attr_indices, attr_rel, tuple);
1097-
CatalogCloseIndices(Num_pg_attr_indices, idescs);
1098-
}
1064+
/* keep the system catalog indexes current */
1065+
CatalogUpdateIndexes(attr_rel, tuple);
10991066

11001067
/*
11011068
* Our update of the pg_attribute row will force a relcache rebuild,
@@ -1195,12 +1162,10 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin)
11951162
Node *expr;
11961163
char *adsrc;
11971164
Relation adrel;
1198-
Relation idescs[Num_pg_attrdef_indices];
11991165
HeapTuple tuple;
12001166
Datum values[4];
12011167
static char nulls[4] = {' ', ' ', ' ', ' '};
12021168
Relation attrrel;
1203-
Relation attridescs[Num_pg_attr_indices];
12041169
HeapTuple atttup;
12051170
Form_pg_attribute attStruct;
12061171
Oid attrdefOid;
@@ -1235,10 +1200,7 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin)
12351200
tuple = heap_formtuple(adrel->rd_att, values, nulls);
12361201
attrdefOid = simple_heap_insert(adrel, tuple);
12371202

1238-
CatalogOpenIndices(Num_pg_attrdef_indices, Name_pg_attrdef_indices,
1239-
idescs);
1240-
CatalogIndexInsert(idescs, Num_pg_attrdef_indices, adrel, tuple);
1241-
CatalogCloseIndices(Num_pg_attrdef_indices, idescs);
1203+
CatalogUpdateIndexes(adrel, tuple);
12421204

12431205
defobject.classId = RelationGetRelid(adrel);
12441206
defobject.objectId = attrdefOid;
@@ -1269,11 +1231,8 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin)
12691231
{
12701232
attStruct->atthasdef = true;
12711233
simple_heap_update(attrrel, &atttup->t_self, atttup);
1272-
/* keep catalog indices current */
1273-
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices,
1274-
attridescs);
1275-
CatalogIndexInsert(attridescs, Num_pg_attr_indices, attrrel, atttup);
1276-
CatalogCloseIndices(Num_pg_attr_indices, attridescs);
1234+
/* keep catalog indexes current */
1235+
CatalogUpdateIndexes(attrrel, atttup);
12771236
}
12781237
heap_close(attrrel, RowExclusiveLock);
12791238
heap_freetuple(atttup);
@@ -1655,7 +1614,6 @@ SetRelationNumChecks(Relation rel, int numchecks)
16551614
Relation relrel;
16561615
HeapTuple reltup;
16571616
Form_pg_class relStruct;
1658-
Relation relidescs[Num_pg_class_indices];
16591617

16601618
relrel = heap_openr(RelationRelationName, RowExclusiveLock);
16611619
reltup = SearchSysCacheCopy(RELOID,
@@ -1672,11 +1630,8 @@ SetRelationNumChecks(Relation rel, int numchecks)
16721630

16731631
simple_heap_update(relrel, &reltup->t_self, reltup);
16741632

1675-
/* keep catalog indices current */
1676-
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices,
1677-
relidescs);
1678-
CatalogIndexInsert(relidescs, Num_pg_class_indices, relrel, reltup);
1679-
CatalogCloseIndices(Num_pg_class_indices, relidescs);
1633+
/* keep catalog indexes current */
1634+
CatalogUpdateIndexes(relrel, reltup);
16801635
}
16811636
else
16821637
{
@@ -1866,9 +1821,9 @@ RemoveStatistics(Relation rel)
18661821

18671822
/*
18681823
* RelationTruncateIndexes - truncate all
1869-
* indices associated with the heap relation to zero tuples.
1824+
* indexes associated with the heap relation to zero tuples.
18701825
*
1871-
* The routine will truncate and then reconstruct the indices on
1826+
* The routine will truncate and then reconstruct the indexes on
18721827
* the relation specified by the heapId parameter.
18731828
*/
18741829
static void

0 commit comments

Comments
 (0)