Skip to content

Commit d4bf3c9

Browse files
committed
Expose an API for calculating catcache hash values.
Now that cache invalidation callbacks get only a hash value, and not a tuple TID (per commits 632ae68 and b5282aa), the only way they can restrict what they invalidate is to know what the hash values mean. setrefs.c was doing this via a hard-wired assumption but that seems pretty grotty, and it'll only get worse as more cases come up. So let's expose a calculation function that takes the same parameters as SearchSysCache. Per complaint from Marko Kreen.
1 parent e685a8e commit d4bf3c9

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

src/backend/optimizer/plan/setrefs.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
#include "postgres.h"
1717

18-
#include "access/hash.h"
1918
#include "access/transam.h"
2019
#include "catalog/pg_type.h"
2120
#include "nodes/makefuncs.h"
@@ -1830,14 +1829,11 @@ record_plan_function_dependency(PlannerInfo *root, Oid funcid)
18301829
/*
18311830
* It would work to use any syscache on pg_proc, but the easiest is
18321831
* PROCOID since we already have the function's OID at hand. Note
1833-
* that plancache.c knows we use PROCOID. Also, we're perhaps
1834-
* assuming more than we should about how CatalogCacheComputeHashValue
1835-
* computes hash values...
1832+
* that plancache.c knows we use PROCOID.
18361833
*/
18371834
inval_item->cacheId = PROCOID;
1838-
inval_item->hashValue =
1839-
DatumGetUInt32(DirectFunctionCall1(hashoid,
1840-
ObjectIdGetDatum(funcid)));
1835+
inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
1836+
ObjectIdGetDatum(funcid));
18411837

18421838
root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
18431839
}

src/backend/utils/cache/catcache.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,46 @@ ReleaseCatCache(HeapTuple tuple)
12811281
}
12821282

12831283

1284+
/*
1285+
* GetCatCacheHashValue
1286+
*
1287+
* Compute the hash value for a given set of search keys.
1288+
*
1289+
* The reason for exposing this as part of the API is that the hash value is
1290+
* exposed in cache invalidation operations, so there are places outside the
1291+
* catcache code that need to be able to compute the hash values.
1292+
*/
1293+
uint32
1294+
GetCatCacheHashValue(CatCache *cache,
1295+
Datum v1,
1296+
Datum v2,
1297+
Datum v3,
1298+
Datum v4)
1299+
{
1300+
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
1301+
1302+
/*
1303+
* one-time startup overhead for each cache
1304+
*/
1305+
if (cache->cc_tupdesc == NULL)
1306+
CatalogCacheInitializeCache(cache);
1307+
1308+
/*
1309+
* initialize the search key information
1310+
*/
1311+
memcpy(cur_skey, cache->cc_skey, sizeof(cur_skey));
1312+
cur_skey[0].sk_argument = v1;
1313+
cur_skey[1].sk_argument = v2;
1314+
cur_skey[2].sk_argument = v3;
1315+
cur_skey[3].sk_argument = v4;
1316+
1317+
/*
1318+
* calculate the hash value
1319+
*/
1320+
return CatalogCacheComputeHashValue(cache, cache->cc_nkeys, cur_skey);
1321+
}
1322+
1323+
12841324
/*
12851325
* SearchCatCacheList
12861326
*

src/backend/utils/cache/syscache.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,30 @@ SysCacheGetAttr(int cacheId, HeapTuple tup,
10501050
isNull);
10511051
}
10521052

1053+
/*
1054+
* GetSysCacheHashValue
1055+
*
1056+
* Get the hash value that would be used for a tuple in the specified cache
1057+
* with the given search keys.
1058+
*
1059+
* The reason for exposing this as part of the API is that the hash value is
1060+
* exposed in cache invalidation operations, so there are places outside the
1061+
* catcache code that need to be able to compute the hash values.
1062+
*/
1063+
uint32
1064+
GetSysCacheHashValue(int cacheId,
1065+
Datum key1,
1066+
Datum key2,
1067+
Datum key3,
1068+
Datum key4)
1069+
{
1070+
if (cacheId < 0 || cacheId >= SysCacheSize ||
1071+
!PointerIsValid(SysCache[cacheId]))
1072+
elog(ERROR, "invalid cache ID: %d", cacheId);
1073+
1074+
return GetCatCacheHashValue(SysCache[cacheId], key1, key2, key3, key4);
1075+
}
1076+
10531077
/*
10541078
* List-search interface
10551079
*/

src/include/utils/catcache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ extern HeapTuple SearchCatCache(CatCache *cache,
174174
Datum v3, Datum v4);
175175
extern void ReleaseCatCache(HeapTuple tuple);
176176

177+
extern uint32 GetCatCacheHashValue(CatCache *cache,
178+
Datum v1, Datum v2,
179+
Datum v3, Datum v4);
180+
177181
extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
178182
Datum v1, Datum v2,
179183
Datum v3, Datum v4);

src/include/utils/syscache.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ extern bool SearchSysCacheExistsAttName(Oid relid, const char *attname);
113113
extern Datum SysCacheGetAttr(int cacheId, HeapTuple tup,
114114
AttrNumber attributeNumber, bool *isNull);
115115

116+
extern uint32 GetSysCacheHashValue(int cacheId,
117+
Datum key1, Datum key2, Datum key3, Datum key4);
118+
116119
/* list-search interface. Users of this must import catcache.h too */
117120
extern struct catclist *SearchSysCacheList(int cacheId, int nkeys,
118121
Datum key1, Datum key2, Datum key3, Datum key4);
@@ -158,6 +161,15 @@ extern struct catclist *SearchSysCacheList(int cacheId, int nkeys,
158161
#define GetSysCacheOid4(cacheId, key1, key2, key3, key4) \
159162
GetSysCacheOid(cacheId, key1, key2, key3, key4)
160163

164+
#define GetSysCacheHashValue1(cacheId, key1) \
165+
GetSysCacheHashValue(cacheId, key1, 0, 0, 0)
166+
#define GetSysCacheHashValue2(cacheId, key1, key2) \
167+
GetSysCacheHashValue(cacheId, key1, key2, 0, 0)
168+
#define GetSysCacheHashValue3(cacheId, key1, key2, key3) \
169+
GetSysCacheHashValue(cacheId, key1, key2, key3, 0)
170+
#define GetSysCacheHashValue4(cacheId, key1, key2, key3, key4) \
171+
GetSysCacheHashValue(cacheId, key1, key2, key3, key4)
172+
161173
#define SearchSysCacheList1(cacheId, key1) \
162174
SearchSysCacheList(cacheId, 1, key1, 0, 0, 0)
163175
#define SearchSysCacheList2(cacheId, key1, key2) \

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