Skip to content

Commit 3e3f283

Browse files
committed
Avoid scanning the relcache during AtEOSubXact_RelationCache when there
is nothing to do, which is most of the time. This is another simple improvement to cut subtransaction entry/exit overhead.
1 parent 8ecbc46 commit 3e3f283

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.211 2004/09/16 16:58:35 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.212 2004/11/20 20:19:52 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -120,6 +120,11 @@ static long relcacheInvalsReceived = 0L;
120120
*/
121121
static List *initFileRelationIds = NIL;
122122

123+
/*
124+
* This flag lets us optimize away work in AtEOSubXact_RelationCache().
125+
*/
126+
static bool need_eosubxact_work = false;
127+
123128
/*
124129
* RelationBuildDescInfo exists so code can be shared
125130
* between RelationIdGetRelation() and RelationSysNameGetRelation()
@@ -2087,6 +2092,9 @@ AtEOXact_RelationCache(bool isCommit)
20872092
relation->rd_indexvalid = 0;
20882093
}
20892094
}
2095+
2096+
/* Once done with the transaction, we can reset need_eosubxact_work */
2097+
need_eosubxact_work = false;
20902098
}
20912099

20922100
/*
@@ -2103,6 +2111,21 @@ AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
21032111
HASH_SEQ_STATUS status;
21042112
RelIdCacheEnt *idhentry;
21052113

2114+
/*
2115+
* In the majority of subtransactions there is not anything for this
2116+
* routine to do, and since there are usually many entries in the
2117+
* relcache, uselessly scanning the cache represents a surprisingly
2118+
* large fraction of the subtransaction entry/exit overhead. To avoid
2119+
* this, we keep a static flag that must be set whenever a condition
2120+
* is created that requires subtransaction-end work. (Currently, this
2121+
* means either a relation is created in the current xact, or an index
2122+
* list is forced.) For simplicity, the flag remains set till end of
2123+
* top-level transaction, even though we could clear it earlier in some
2124+
* cases.
2125+
*/
2126+
if (!need_eosubxact_work)
2127+
return;
2128+
21062129
hash_seq_init(&status, RelationIdCache);
21072130

21082131
while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
@@ -2184,6 +2207,9 @@ RelationBuildLocalRelation(const char *relname,
21842207
/* it's being created in this transaction */
21852208
rel->rd_createSubid = GetCurrentSubTransactionId();
21862209

2210+
/* must flag that we have rels created in this transaction */
2211+
need_eosubxact_work = true;
2212+
21872213
/* is it a temporary relation? */
21882214
rel->rd_istemp = isTempNamespace(relnamespace);
21892215

@@ -2745,6 +2771,8 @@ RelationSetIndexList(Relation relation, List *indexIds)
27452771
list_free(relation->rd_indexlist);
27462772
relation->rd_indexlist = indexIds;
27472773
relation->rd_indexvalid = 2; /* mark list as forced */
2774+
/* must flag that we have a forced index list */
2775+
need_eosubxact_work = true;
27482776
}
27492777

27502778
/*
@@ -3172,6 +3200,7 @@ load_relcache_init_file(void)
31723200
rel->rd_refcnt = 0;
31733201
rel->rd_indexvalid = 0;
31743202
rel->rd_indexlist = NIL;
3203+
rel->rd_createSubid = InvalidSubTransactionId;
31753204
MemSet(&rel->pgstat_info, 0, sizeof(rel->pgstat_info));
31763205

31773206
/*

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