Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d0755dc

Browse files
committed
Avoid searching for callback functions in CallSyscacheCallbacks().
We have now grown enough registerable syscache-invalidation callback functions that the original assumption that there would be few of them is causing performance problems. In particular, let's fix things so that CallSyscacheCallbacks doesn't have to search the whole array to find which callback(s) to invoke for a given cache ID. Preserve the original behavior that callbacks are called in order of registration, just in case there's someplace that depends on that (which I doubt). In support of this, export the number of syscaches from syscache.h. People could have found that out anyway from the enum, but adding a #define makes that much safer. This provides a useful additional speedup in Mathieu Fenniak's logical-decoding test case, although we're reaching the point of diminishing returns there. I think any further improvement will have to come from reducing the number of cache invalidations that are triggered in the first place. Still, we can hope that this change gives some incremental benefit for all invalidation scenarios. Back-patch to 9.4 where logical decoding was introduced. Discussion: https://postgr.es/m/CAHoiPjzea6N0zuCi=+f9v_j94nfsy6y8SU7-=bp4=7qw6_i=Rg@mail.gmail.com
1 parent 610926e commit d0755dc

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/backend/utils/cache/inval.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ static int maxSharedInvalidMessagesArray;
175175

176176
/*
177177
* Dynamically-registered callback functions. Current implementation
178-
* assumes there won't be very many of these at once; could improve if needed.
178+
* assumes there won't be enough of these to justify a dynamically resizable
179+
* array; it'd be easy to improve that if needed.
180+
*
181+
* To avoid searching in CallSyscacheCallbacks, all callbacks for a given
182+
* syscache are linked into a list pointed to by syscache_callback_links[id].
183+
* The link values are syscache_callback_list[] index plus 1, or 0 for none.
179184
*/
180185

181186
#define MAX_SYSCACHE_CALLBACKS 64
@@ -184,10 +189,13 @@ static int maxSharedInvalidMessagesArray;
184189
static struct SYSCACHECALLBACK
185190
{
186191
int16 id; /* cache number */
192+
int16 link; /* next callback index+1 for same cache */
187193
SyscacheCallbackFunction function;
188194
Datum arg;
189195
} syscache_callback_list[MAX_SYSCACHE_CALLBACKS];
190196

197+
static int16 syscache_callback_links[SysCacheSize];
198+
191199
static int syscache_callback_count = 0;
192200

193201
static struct RELCACHECALLBACK
@@ -1355,10 +1363,28 @@ CacheRegisterSyscacheCallback(int cacheid,
13551363
SyscacheCallbackFunction func,
13561364
Datum arg)
13571365
{
1366+
if (cacheid < 0 || cacheid >= SysCacheSize)
1367+
elog(FATAL, "invalid cache ID: %d", cacheid);
13581368
if (syscache_callback_count >= MAX_SYSCACHE_CALLBACKS)
13591369
elog(FATAL, "out of syscache_callback_list slots");
13601370

1371+
if (syscache_callback_links[cacheid] == 0)
1372+
{
1373+
/* first callback for this cache */
1374+
syscache_callback_links[cacheid] = syscache_callback_count + 1;
1375+
}
1376+
else
1377+
{
1378+
/* add to end of chain, so that older callbacks are called first */
1379+
int i = syscache_callback_links[cacheid] - 1;
1380+
1381+
while (syscache_callback_list[i].link > 0)
1382+
i = syscache_callback_list[i].link - 1;
1383+
syscache_callback_list[i].link = syscache_callback_count + 1;
1384+
}
1385+
13611386
syscache_callback_list[syscache_callback_count].id = cacheid;
1387+
syscache_callback_list[syscache_callback_count].link = 0;
13621388
syscache_callback_list[syscache_callback_count].function = func;
13631389
syscache_callback_list[syscache_callback_count].arg = arg;
13641390

@@ -1398,11 +1424,16 @@ CallSyscacheCallbacks(int cacheid, uint32 hashvalue)
13981424
{
13991425
int i;
14001426

1401-
for (i = 0; i < syscache_callback_count; i++)
1427+
if (cacheid < 0 || cacheid >= SysCacheSize)
1428+
elog(ERROR, "invalid cache ID: %d", cacheid);
1429+
1430+
i = syscache_callback_links[cacheid] - 1;
1431+
while (i >= 0)
14021432
{
14031433
struct SYSCACHECALLBACK *ccitem = syscache_callback_list + i;
14041434

1405-
if (ccitem->id == cacheid)
1406-
(*ccitem->function) (ccitem->arg, cacheid, hashvalue);
1435+
Assert(ccitem->id == cacheid);
1436+
(*ccitem->function) (ccitem->arg, cacheid, hashvalue);
1437+
i = ccitem->link - 1;
14071438
}
14081439
}

src/backend/utils/cache/syscache.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,6 @@ static const struct cachedesc cacheinfo[] = {
845845
}
846846
};
847847

848-
#define SysCacheSize ((int) lengthof(cacheinfo))
849-
850848
static CatCache *SysCache[SysCacheSize];
851849

852850
static bool CacheInitialized = false;
@@ -877,6 +875,9 @@ InitCatalogCache(void)
877875
int i,
878876
j;
879877

878+
StaticAssertStmt(SysCacheSize == (int) lengthof(cacheinfo),
879+
"SysCacheSize does not match syscache.c's array");
880+
880881
Assert(!CacheInitialized);
881882

882883
SysCacheRelationOidSize = SysCacheSupportingRelOidSize = 0;

src/include/utils/syscache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ enum SysCacheIdentifier
9797
TYPEOID,
9898
USERMAPPINGOID,
9999
USERMAPPINGUSERSERVER
100+
101+
#define SysCacheSize (USERMAPPINGUSERSERVER + 1)
100102
};
101103

102104
extern void InitCatalogCache(void);

0 commit comments

Comments
 (0)