Skip to content

Commit d16b706

Browse files
committed
Allow indexes on system catalogs for use in cache code.
Thanks to Hiroshi
1 parent 1973e90 commit d16b706

File tree

6 files changed

+237
-53
lines changed

6 files changed

+237
-53
lines changed

src/backend/catalog/index.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.92 1999/10/26 03:12:33 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.93 1999/11/01 02:29:24 momjian Exp $
1111
*
1212
*
1313
* INTERFACE ROUTINES
@@ -54,26 +54,23 @@
5454

5555
/* non-export function prototypes */
5656
static Oid GetHeapRelationOid(char *heapRelationName, char *indexRelationName,
57-
bool istemp);
57+
bool istemp);
5858
static TupleDesc BuildFuncTupleDesc(FuncIndexInfo *funcInfo);
5959
static TupleDesc ConstructTupleDescriptor(Oid heapoid, Relation heapRelation,
60-
List *attributeList,
61-
int numatts, AttrNumber *attNums);
60+
List *attributeList, int numatts, AttrNumber *attNums);
6261

6362
static void ConstructIndexReldesc(Relation indexRelation, Oid amoid);
6463
static Oid UpdateRelationRelation(Relation indexRelation, char *temp_relname);
6564
static void InitializeAttributeOids(Relation indexRelation,
66-
int numatts,
67-
Oid indexoid);
68-
static void
69-
AppendAttributeTuples(Relation indexRelation, int numatts);
65+
int numatts, Oid indexoid);
66+
static void AppendAttributeTuples(Relation indexRelation, int numatts);
7067
static void UpdateIndexRelation(Oid indexoid, Oid heapoid,
71-
FuncIndexInfo *funcInfo, int natts,
72-
AttrNumber *attNums, Oid *classOids, Node *predicate,
73-
List *attributeList, bool islossy, bool unique, bool primary);
68+
FuncIndexInfo *funcInfo, int natts,
69+
AttrNumber *attNums, Oid *classOids, Node *predicate,
70+
List *attributeList, bool islossy, bool unique, bool primary);
7471
static void DefaultBuild(Relation heapRelation, Relation indexRelation,
75-
int numberOfAttributes, AttrNumber *attributeNumber,
76-
IndexStrategy indexStrategy, uint16 parameterCount,
72+
int numberOfAttributes, AttrNumber *attributeNumber,
73+
IndexStrategy indexStrategy, uint16 parameterCount,
7774
Datum *parameter, FuncIndexInfoPtr funcInfo, PredInfo *predInfo);
7875

7976
/* ----------------------------------------------------------------
@@ -661,6 +658,7 @@ UpdateIndexRelation(Oid indexoid,
661658
Relation pg_index;
662659
HeapTuple tuple;
663660
int i;
661+
Relation idescs[Num_pg_index_indices];
664662

665663
/* ----------------
666664
* allocate an Form_pg_index big enough to hold the
@@ -752,6 +750,16 @@ UpdateIndexRelation(Oid indexoid,
752750
*/
753751
heap_insert(pg_index, tuple);
754752

753+
/* ----------------
754+
* insert the index tuple into the pg_index
755+
* ----------------
756+
*/
757+
if (!IsBootstrapProcessingMode())
758+
{
759+
CatalogOpenIndices(Num_pg_index_indices, Name_pg_index_indices, idescs);
760+
CatalogIndexInsert(idescs, Num_pg_index_indices, pg_index, tuple);
761+
CatalogCloseIndices(Num_pg_index_indices, idescs);
762+
}
755763
/* ----------------
756764
* close the relation and free the tuple
757765
* ----------------

src/backend/catalog/indexing.c

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.48 1999/10/15 01:49:39 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.49 1999/11/01 02:29:25 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -38,15 +38,17 @@
3838
* pg_trigger
3939
*/
4040

41+
char *Name_pg_amop_indices[Num_pg_amop_indices] = {AccessMethodOpidIndex,
42+
AccessMethodStrategyIndex};
4143
char *Name_pg_attr_indices[Num_pg_attr_indices] = {AttributeNameIndex,
42-
AttributeNumIndex,
43-
AttributeRelidIndex};
44+
AttributeNumIndex, AttributeRelidIndex};
45+
char *Name_pg_index_indices[Num_pg_index_indices] = {IndexRelidIndex};
4446
char *Name_pg_proc_indices[Num_pg_proc_indices] = {ProcedureNameIndex,
45-
ProcedureOidIndex};
47+
ProcedureOidIndex};
4648
char *Name_pg_type_indices[Num_pg_type_indices] = {TypeNameIndex,
47-
TypeOidIndex};
49+
TypeOidIndex};
4850
char *Name_pg_class_indices[Num_pg_class_indices] = {ClassNameIndex,
49-
ClassOidIndex};
51+
ClassOidIndex};
5052
char *Name_pg_attrdef_indices[Num_pg_attrdef_indices] = {AttrDefaultIndex};
5153

5254
char *Name_pg_relcheck_indices[Num_pg_relcheck_indices] = {RelCheckIndex};
@@ -255,12 +257,89 @@ CatalogIndexFetchTuple(Relation heapRelation,
255257
}
256258

257259

260+
/*---------------------------------------------------------------------
261+
* Class-specific index lookups
262+
*---------------------------------------------------------------------
263+
*/
264+
258265
/*
259266
* The remainder of the file is for individual index scan routines. Each
260267
* index should be scanned according to how it was defined during bootstrap
261268
* (that is, functional or normal) and what arguments the cache lookup
262269
* requires. Each routine returns the heap tuple that qualifies.
263270
*/
271+
HeapTuple
272+
AccessMethodOpidIndexScan(Relation heapRelation,
273+
Oid claid,
274+
Oid opopr,
275+
Oid opid)
276+
{
277+
Relation idesc;
278+
ScanKeyData skey[3];
279+
HeapTuple tuple;
280+
281+
ScanKeyEntryInitialize(&skey[0],
282+
(bits16) 0x0,
283+
(AttrNumber) 1,
284+
(RegProcedure) F_OIDEQ,
285+
ObjectIdGetDatum(claid));
286+
287+
ScanKeyEntryInitialize(&skey[1],
288+
(bits16) 0x0,
289+
(AttrNumber) 2,
290+
(RegProcedure) F_OIDEQ,
291+
ObjectIdGetDatum(opopr));
292+
293+
ScanKeyEntryInitialize(&skey[2],
294+
(bits16) 0x0,
295+
(AttrNumber) 3,
296+
(RegProcedure) F_OIDEQ,
297+
ObjectIdGetDatum(opid));
298+
299+
idesc = index_openr(AccessMethodOpidIndex);
300+
tuple = CatalogIndexFetchTuple(heapRelation, idesc, skey, 3);
301+
302+
index_close(idesc);
303+
304+
return tuple;
305+
}
306+
307+
HeapTuple
308+
AccessMethodStrategyIndexScan(Relation heapRelation,
309+
Oid opid,
310+
Oid claid,
311+
int2 opstrategy)
312+
{
313+
Relation idesc;
314+
ScanKeyData skey[3];
315+
HeapTuple tuple;
316+
317+
ScanKeyEntryInitialize(&skey[0],
318+
(bits16) 0x0,
319+
(AttrNumber) 1,
320+
(RegProcedure) F_OIDEQ,
321+
ObjectIdGetDatum(opid));
322+
323+
ScanKeyEntryInitialize(&skey[1],
324+
(bits16) 0x0,
325+
(AttrNumber) 2,
326+
(RegProcedure) F_OIDEQ,
327+
ObjectIdGetDatum(claid));
328+
329+
ScanKeyEntryInitialize(&skey[2],
330+
(bits16) 0x0,
331+
(AttrNumber) 3,
332+
(RegProcedure) F_INT2EQ,
333+
Int16GetDatum(opstrategy));
334+
335+
idesc = index_openr(AccessMethodStrategyIndex);
336+
tuple = CatalogIndexFetchTuple(heapRelation, idesc, skey, 3);
337+
338+
index_close(idesc);
339+
340+
return tuple;
341+
}
342+
264343
HeapTuple
265344
AttributeNameIndexScan(Relation heapRelation,
266345
Oid relid,
@@ -320,6 +399,28 @@ AttributeNumIndexScan(Relation heapRelation,
320399
return tuple;
321400
}
322401

402+
HeapTuple
403+
IndexRelidIndexScan(Relation heapRelation, Oid relid)
404+
{
405+
Relation idesc;
406+
ScanKeyData skey[1];
407+
HeapTuple tuple;
408+
409+
ScanKeyEntryInitialize(&skey[0],
410+
(bits16) 0x0,
411+
(AttrNumber) 1,
412+
(RegProcedure) F_OIDEQ,
413+
ObjectIdGetDatum(relid));
414+
415+
idesc = index_openr(IndexRelidIndex);
416+
tuple = CatalogIndexFetchTuple(heapRelation, idesc, skey, 1);
417+
418+
index_close(idesc);
419+
420+
return tuple;
421+
}
422+
423+
323424

324425
HeapTuple
325426
ProcedureOidIndexScan(Relation heapRelation, Oid procId)

src/backend/utils/cache/catcache.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.49 1999/09/18 19:07:55 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.50 1999/11/01 02:29:25 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -16,9 +16,12 @@
1616
#include "access/heapam.h"
1717
#include "access/valid.h"
1818
#include "catalog/pg_type.h"
19+
#include "catalog/catname.h"
20+
#include "catalog/indexing.h"
1921
#include "miscadmin.h"
2022
#include "utils/builtins.h"
2123
#include "utils/catcache.h"
24+
#include "utils/syscache.h"
2225

2326
static void CatCacheRemoveCTup(CatCache *cache, Dlelem *e);
2427
static Index CatalogCacheComputeHashIndex(struct catcache * cacheInP);
@@ -805,6 +808,62 @@ InitSysCache(char *relname,
805808
}
806809

807810

811+
/* --------------------------------
812+
* SearchSelfReferences
813+
*
814+
* This call searches a self referencing information,
815+
*
816+
* which causes a cycle in system catalog cache
817+
*
818+
* cache should already be initailized
819+
* --------------------------------
820+
*/
821+
static HeapTuple
822+
SearchSelfReferences(const struct catcache * cache)
823+
{
824+
HeapTuple ntp;
825+
Relation rel;
826+
static Oid indexSelfOid = 0;
827+
static HeapTuple indexSelfTuple = 0;
828+
829+
if (cache->id != INDEXRELID)
830+
return (HeapTuple)0;
831+
832+
if (!indexSelfOid)
833+
{
834+
rel = heap_openr(RelationRelationName, AccessShareLock);
835+
ntp = ClassNameIndexScan(rel, IndexRelidIndex);
836+
if (!HeapTupleIsValid(ntp))
837+
elog(ERROR, "SearchSelfRefernces: %s not found in %s",
838+
IndexRelidIndex, RelationRelationName);
839+
indexSelfOid = ntp->t_data->t_oid;
840+
pfree(ntp);
841+
heap_close(rel, AccessShareLock);
842+
}
843+
if ((Oid)cache->cc_skey[0].sk_argument != indexSelfOid)
844+
return (HeapTuple)0;
845+
if (!indexSelfTuple)
846+
{
847+
HeapScanDesc sd;
848+
MemoryContext oldcxt;
849+
850+
if (!CacheCxt)
851+
CacheCxt = CreateGlobalMemory("Cache");
852+
rel = heap_open(cache->relationId, AccessShareLock);
853+
sd = heap_beginscan(rel, false, SnapshotNow, 1, cache->cc_skey);
854+
ntp = heap_getnext(sd, 0);
855+
if (!HeapTupleIsValid(ntp))
856+
elog(ERROR, "SearchSelfRefernces: tuple not found");
857+
oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
858+
indexSelfTuple = heap_copytuple(ntp);
859+
MemoryContextSwitchTo(oldcxt);
860+
heap_endscan(sd);
861+
heap_close(rel, AccessShareLock);
862+
}
863+
864+
return indexSelfTuple;
865+
}
866+
808867
/* --------------------------------
809868
* SearchSysCache
810869
*
@@ -845,6 +904,14 @@ SearchSysCache(struct catcache * cache,
845904
cache->cc_skey[2].sk_argument = v3;
846905
cache->cc_skey[3].sk_argument = v4;
847906

907+
/*
908+
* resolve self referencing informtion
909+
*/
910+
if (ntp = SearchSelfReferences(cache), ntp)
911+
{
912+
return heap_copytuple(ntp);
913+
}
914+
848915
/* ----------------
849916
* find the hash bucket in which to look for the tuple
850917
* ----------------

src/backend/utils/cache/syscache.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.37 1999/09/30 10:31:43 wieck Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.38 1999/11/01 02:29:25 momjian Exp $
1111
*
1212
* NOTES
1313
* These routines allow the parser/planner/executor to perform
@@ -59,8 +59,8 @@ static struct cachedesc cacheinfo[] = {
5959
0
6060
},
6161
sizeof(FormData_pg_amop),
62-
NULL,
63-
(ScanFunc) NULL},
62+
AccessMethodOpidIndex,
63+
(ScanFunc) AccessMethodOpidIndexScan},
6464
{AccessMethodOperatorRelationName, /* AMOPSTRATEGY */
6565
3,
6666
{
@@ -70,8 +70,8 @@ static struct cachedesc cacheinfo[] = {
7070
0
7171
},
7272
sizeof(FormData_pg_amop),
73-
NULL,
74-
(ScanFunc) NULL},
73+
AccessMethodStrategyIndex,
74+
(ScanFunc) AccessMethodStrategyIndexScan},
7575
{AttributeRelationName, /* ATTNAME */
7676
2,
7777
{
@@ -103,8 +103,8 @@ static struct cachedesc cacheinfo[] = {
103103
0
104104
},
105105
offsetof(FormData_pg_index, indpred),
106-
NULL,
107-
NULL},
106+
IndexRelidIndex,
107+
(ScanFunc) IndexRelidIndexScan},
108108
{LanguageRelationName, /* LANNAME */
109109
1,
110110
{
@@ -226,17 +226,6 @@ static struct cachedesc cacheinfo[] = {
226226
sizeof(FormData_pg_opclass),
227227
NULL,
228228
NULL},
229-
{IndexRelationName, /* INDRELIDKEY *//* never used */
230-
2,
231-
{
232-
Anum_pg_index_indrelid,
233-
Anum_pg_index_indkey,
234-
0,
235-
0
236-
},
237-
offsetof(FormData_pg_index, indpred),
238-
NULL,
239-
(ScanFunc) NULL},
240229
{InheritsRelationName, /* INHRELID */
241230
2,
242231
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy