Skip to content

Commit 27fee81

Browse files
committed
Replace SearchSysCacheGetAttribute with SysCacheGetAttr, which fetches
an attribute of a tuple previously fetched with SearchSysCacheTuple. This avoids a lot of redundant cache lookups, particularly in selfuncs.c. Also, remove SearchSysCacheStruct, which was unused and grotty.
1 parent 1161077 commit 27fee81

File tree

5 files changed

+107
-240
lines changed

5 files changed

+107
-240
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.50 2000/01/23 02:06:56 tgl Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.51 2000/01/23 03:43:23 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -587,9 +587,6 @@ getattproperties(Oid relid, AttrNumber attnum,
587587
* commonval, loval, hival are returned as Datums holding the internal
588588
* representation of the values. (Note that these should be pfree'd
589589
* after use if the data type is not by-value.)
590-
*
591-
* XXX currently, this does a linear search of pg_statistic because there
592-
* is no index nor syscache for pg_statistic. FIX THIS!
593590
*/
594591
static bool
595592
getattstatistics(Oid relid, AttrNumber attnum, Oid opid, Oid typid,
@@ -600,29 +597,26 @@ getattstatistics(Oid relid, AttrNumber attnum, Oid opid, Oid typid,
600597
Datum *loval,
601598
Datum *hival)
602599
{
603-
Relation rel;
604-
bool isnull;
605600
HeapTuple tuple;
606601
HeapTuple typeTuple;
607602
FmgrInfo inputproc;
608603
Oid typelem;
604+
bool isnull;
609605

610-
rel = heap_openr(StatisticRelationName, AccessShareLock);
611-
606+
/* We assume that there will only be one entry in pg_statistic
607+
* for the given rel/att. Someday, VACUUM might store more than one...
608+
*/
612609
tuple = SearchSysCacheTuple(STATRELID,
613-
ObjectIdGetDatum(relid),
614-
Int16GetDatum((int16) attnum),
615-
opid, 0);
610+
ObjectIdGetDatum(relid),
611+
Int16GetDatum((int16) attnum),
612+
opid,
613+
0);
616614
if (!HeapTupleIsValid(tuple))
617615
{
618616
/* no such stats entry */
619-
heap_close(rel, AccessShareLock);
620617
return false;
621618
}
622619

623-
/* We assume that there will only be one entry in pg_statistic
624-
* for the given rel/att. Someday, VACUUM might store more than one...
625-
*/
626620
if (nullfrac)
627621
*nullfrac = ((Form_pg_statistic) GETSTRUCT(tuple))->stanullfrac;
628622
if (commonfrac)
@@ -639,14 +633,13 @@ getattstatistics(Oid relid, AttrNumber attnum, Oid opid, Oid typid,
639633
typelem = ((Form_pg_type) GETSTRUCT(typeTuple))->typelem;
640634

641635
/* Values are variable-length fields, so cannot access as struct fields.
642-
* Must do it the hard way with heap_getattr.
636+
* Must do it the hard way with SysCacheGetAttr.
643637
*/
644638
if (commonval)
645639
{
646-
text *val = (text *) heap_getattr(tuple,
647-
Anum_pg_statistic_stacommonval,
648-
RelationGetDescr(rel),
649-
&isnull);
640+
text *val = (text *) SysCacheGetAttr(STATRELID, tuple,
641+
Anum_pg_statistic_stacommonval,
642+
&isnull);
650643
if (isnull)
651644
{
652645
elog(DEBUG, "getattstatistics: stacommonval is null");
@@ -663,10 +656,9 @@ getattstatistics(Oid relid, AttrNumber attnum, Oid opid, Oid typid,
663656

664657
if (loval)
665658
{
666-
text *val = (text *) heap_getattr(tuple,
667-
Anum_pg_statistic_staloval,
668-
RelationGetDescr(rel),
669-
&isnull);
659+
text *val = (text *) SysCacheGetAttr(STATRELID, tuple,
660+
Anum_pg_statistic_staloval,
661+
&isnull);
670662
if (isnull)
671663
{
672664
elog(DEBUG, "getattstatistics: staloval is null");
@@ -683,10 +675,9 @@ getattstatistics(Oid relid, AttrNumber attnum, Oid opid, Oid typid,
683675

684676
if (hival)
685677
{
686-
text *val = (text *) heap_getattr(tuple,
687-
Anum_pg_statistic_stahival,
688-
RelationGetDescr(rel),
689-
&isnull);
678+
text *val = (text *) SysCacheGetAttr(STATRELID, tuple,
679+
Anum_pg_statistic_stahival,
680+
&isnull);
690681
if (isnull)
691682
{
692683
elog(DEBUG, "getattstatistics: stahival is null");
@@ -701,7 +692,6 @@ getattstatistics(Oid relid, AttrNumber attnum, Oid opid, Oid typid,
701692
}
702693
}
703694

704-
heap_close(rel, AccessShareLock);
705695
return true;
706696
}
707697

src/backend/utils/cache/fcache.c

Lines changed: 33 additions & 52 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/Attic/fcache.c,v 1.27 1999/11/22 17:56:32 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/fcache.c,v 1.28 2000/01/23 03:43:24 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -74,13 +74,12 @@ init_fcache(Oid foid,
7474
Form_pg_proc procedureStruct;
7575
Form_pg_type typeStruct;
7676
FunctionCachePtr retval;
77-
text *tmp;
7877
int nargs;
78+
text *tmp;
79+
bool isNull;
7980

8081
/* ----------------
81-
* get the procedure tuple corresponding to the given
82-
* functionOid. If this fails, returnValue has been
83-
* pre-initialized to "null" so we just return it.
82+
* get the procedure tuple corresponding to the given functionOid
8483
* ----------------
8584
*/
8685
retval = (FunctionCachePtr) palloc(sizeof(FunctionCache));
@@ -94,48 +93,38 @@ init_fcache(Oid foid,
9493
0, 0, 0);
9594

9695
if (!HeapTupleIsValid(procedureTuple))
97-
elog(ERROR,
98-
"init_fcache: %s %u",
99-
"Cache lookup failed for procedure", foid);
96+
elog(ERROR, "init_fcache: Cache lookup failed for procedure %u",
97+
foid);
10098

101-
/* ----------------
102-
* get the return type from the procedure tuple
103-
* ----------------
104-
*/
10599
procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
106100

107101
/* ----------------
108-
* get the type tuple corresponding to the return type
109-
* If this fails, returnValue has been pre-initialized
110-
* to "null" so we just return it.
102+
* get the return type from the procedure tuple
111103
* ----------------
112104
*/
113105
typeTuple = SearchSysCacheTuple(TYPEOID,
114106
ObjectIdGetDatum(procedureStruct->prorettype),
115107
0, 0, 0);
116108

117109
if (!HeapTupleIsValid(typeTuple))
118-
elog(ERROR,
119-
"init_fcache: %s %u",
120-
"Cache lookup failed for type",
121-
(procedureStruct)->prorettype);
110+
elog(ERROR, "init_fcache: Cache lookup failed for type %u",
111+
procedureStruct->prorettype);
112+
113+
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
122114

123115
/* ----------------
124116
* get the type length and by-value from the type tuple and
125117
* save the information in our one element cache.
126118
* ----------------
127119
*/
128-
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
129-
130-
retval->typlen = (typeStruct)->typlen;
131-
if ((typeStruct)->typrelid == InvalidOid)
120+
retval->typlen = typeStruct->typlen;
121+
if (typeStruct->typrelid == InvalidOid)
132122
{
133123
/* The return type is not a relation, so just use byval */
134-
retval->typbyval = (typeStruct)->typbyval ? true : false;
124+
retval->typbyval = typeStruct->typbyval;
135125
}
136126
else
137127
{
138-
139128
/*
140129
* This is a hack. We assume here that any function returning a
141130
* relation returns it by reference. This needs to be fixed.
@@ -147,7 +136,7 @@ init_fcache(Oid foid,
147136
retval->func_state = (char *) NULL;
148137
retval->setArg = NULL;
149138
retval->hasSetArg = false;
150-
retval->oneResult = !procedureStruct->proretset;
139+
retval->oneResult = ! procedureStruct->proretset;
151140
retval->istrusted = procedureStruct->proistrusted;
152141

153142
/*
@@ -157,8 +146,8 @@ init_fcache(Oid foid,
157146
* allocated by the executor (i.e. slots and tuples) is freed.
158147
*/
159148
if ((retval->language == SQLlanguageId) &&
160-
(retval->oneResult) &&
161-
!(retval->typbyval))
149+
retval->oneResult &&
150+
! retval->typbyval)
162151
{
163152
Form_pg_class relationStruct;
164153
HeapTuple relationTuple;
@@ -198,7 +187,7 @@ init_fcache(Oid foid,
198187
{
199188
Oid *argTypes;
200189

201-
retval->nullVect = (bool *) palloc((retval->nargs) * sizeof(bool));
190+
retval->nullVect = (bool *) palloc(retval->nargs * sizeof(bool));
202191

203192
if (retval->language == SQLlanguageId)
204193
{
@@ -230,43 +219,36 @@ init_fcache(Oid foid,
230219
retval->nullVect = (BoolPtr) NULL;
231220
}
232221

233-
/*
234-
* XXX this is the first varlena in the struct. If the order changes
235-
* for some reason this will fail.
236-
*/
237222
if (procedureStruct->prolang == SQLlanguageId)
238223
{
239-
retval->src = textout(&(procedureStruct->prosrc));
224+
tmp = (text *) SysCacheGetAttr(PROCOID,
225+
procedureTuple,
226+
Anum_pg_proc_prosrc,
227+
&isNull);
228+
if (isNull)
229+
elog(ERROR, "init_fcache: null prosrc for procedure %u",
230+
foid);
231+
retval->src = textout(tmp);
240232
retval->bin = (char *) NULL;
241233
}
242234
else
243235
{
244-
245-
/*
246-
* I'm not sure that we even need to do this at all.
247-
*/
248-
249-
/*
250-
* We do for untrusted functions.
251-
*/
252-
236+
retval->src = (char *) NULL;
253237
if (procedureStruct->proistrusted)
254238
retval->bin = (char *) NULL;
255239
else
256240
{
257-
tmp = (text *)
258-
SearchSysCacheGetAttribute(PROCOID,
241+
tmp = (text *) SysCacheGetAttr(PROCOID,
242+
procedureTuple,
259243
Anum_pg_proc_probin,
260-
ObjectIdGetDatum(foid),
261-
0, 0, 0);
244+
&isNull);
245+
if (isNull)
246+
elog(ERROR, "init_fcache: null probin for procedure %u",
247+
foid);
262248
retval->bin = textout(tmp);
263249
}
264-
retval->src = (char *) NULL;
265250
}
266251

267-
268-
269-
270252
if (retval->language != SQLlanguageId)
271253
{
272254
fmgr_info(foid, &(retval->func));
@@ -275,7 +257,6 @@ init_fcache(Oid foid,
275257
else
276258
retval->func.fn_addr = (func_ptr) NULL;
277259

278-
279260
return retval;
280261
}
281262

src/backend/utils/cache/lsyscache.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 1994, Regents of the University of California
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.37 1999/12/31 03:18:43 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.38 2000/01/23 03:43:24 tgl Exp $
1010
*
1111
* NOTES
1212
* Eventually, the index information should go through here, too.
@@ -617,35 +617,15 @@ get_typalign(Oid typid)
617617
Datum
618618
get_typdefault(Oid typid)
619619
{
620-
struct varlena *typDefault;
621-
int32 dataSize;
622620
HeapTuple typeTuple;
623621
Form_pg_type type;
622+
struct varlena *typDefault;
623+
bool isNull;
624+
int32 dataSize;
624625
int32 typLen;
625626
bool typByVal;
626627
Datum returnValue;
627628

628-
/*
629-
* First, see if there is a non-null typdefault field (usually there isn't)
630-
*/
631-
typDefault = (struct varlena *)
632-
SearchSysCacheGetAttribute(TYPEOID,
633-
Anum_pg_type_typdefault,
634-
ObjectIdGetDatum(typid),
635-
0, 0, 0);
636-
637-
if (typDefault == NULL)
638-
return PointerGetDatum(NULL);
639-
640-
dataSize = VARSIZE(typDefault) - VARHDRSZ;
641-
642-
/*
643-
* Need the type's length and byVal fields.
644-
*
645-
* XXX silly to repeat the syscache search that SearchSysCacheGetAttribute
646-
* just did --- but at present this path isn't taken often enough to
647-
* make it worth fixing.
648-
*/
649629
typeTuple = SearchSysCacheTuple(TYPEOID,
650630
ObjectIdGetDatum(typid),
651631
0, 0, 0);
@@ -654,6 +634,22 @@ get_typdefault(Oid typid)
654634
elog(ERROR, "get_typdefault: failed to lookup type %u", typid);
655635

656636
type = (Form_pg_type) GETSTRUCT(typeTuple);
637+
638+
/*
639+
* First, see if there is a non-null typdefault field (usually there isn't)
640+
*/
641+
typDefault = (struct varlena *) SysCacheGetAttr(TYPEOID,
642+
typeTuple,
643+
Anum_pg_type_typdefault,
644+
&isNull);
645+
646+
if (isNull)
647+
return PointerGetDatum(NULL);
648+
649+
/*
650+
* Otherwise, extract/copy the value.
651+
*/
652+
dataSize = VARSIZE(typDefault) - VARHDRSZ;
657653
typLen = type->typlen;
658654
typByVal = type->typbyval;
659655

0 commit comments

Comments
 (0)