PostgreSQL Source Code git master
Loading...
Searching...
No Matches
spccache.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * spccache.c
4 * Tablespace cache management.
5 *
6 * We cache the parsed version of spcoptions for each tablespace to avoid
7 * needing to reparse on every lookup. Right now, there doesn't appear to
8 * be a measurable performance gain from doing this, but that might change
9 * in the future as we add more options.
10 *
11 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
13 *
14 * IDENTIFICATION
15 * src/backend/utils/cache/spccache.c
16 *
17 *-------------------------------------------------------------------------
18 */
19#include "postgres.h"
20
21#include "access/reloptions.h"
23#include "commands/tablespace.h"
24#include "miscadmin.h"
25#include "optimizer/optimizer.h"
26#include "storage/bufmgr.h"
27#include "utils/catcache.h"
28#include "utils/hsearch.h"
29#include "utils/inval.h"
30#include "utils/spccache.h"
31#include "utils/syscache.h"
32#include "varatt.h"
33
34
35/* Hash table for information about each tablespace */
37
38typedef struct
39{
40 Oid oid; /* lookup key - must be first */
41 TableSpaceOpts *opts; /* options, or NULL if none */
43
44
45/*
46 * InvalidateTableSpaceCacheCallback
47 * Flush all cache entries when pg_tablespace is updated.
48 *
49 * When pg_tablespace is updated, we must flush the cache entry at least
50 * for that tablespace. Currently, we just flush them all. This is quick
51 * and easy and doesn't cost much, since there shouldn't be terribly many
52 * tablespaces, nor do we expect them to be frequently modified.
53 */
54static void
56 uint32 hashvalue)
57{
58 HASH_SEQ_STATUS status;
60
62 while ((spc = (TableSpaceCacheEntry *) hash_seq_search(&status)) != NULL)
63 {
64 if (spc->opts)
65 pfree(spc->opts);
67 &spc->oid,
69 NULL) == NULL)
70 elog(ERROR, "hash table corrupted");
71 }
72}
73
74/*
75 * InitializeTableSpaceCache
76 * Initialize the tablespace cache.
77 */
78static void
80{
82
83 /* Initialize the hash table. */
84 ctl.keysize = sizeof(Oid);
85 ctl.entrysize = sizeof(TableSpaceCacheEntry);
87 hash_create("TableSpace cache", 16, &ctl,
89
90 /* Make sure we've initialized CacheMemoryContext. */
93
94 /* Watch for invalidation events. */
97 (Datum) 0);
98}
99
100/*
101 * get_tablespace
102 * Fetch TableSpaceCacheEntry structure for a specified table OID.
103 *
104 * Pointers returned by this function should not be stored, since a cache
105 * flush will invalidate them.
106 */
109{
111 HeapTuple tp;
113
114 /*
115 * Since spcid is always from a pg_class tuple, InvalidOid implies the
116 * default.
117 */
118 if (spcid == InvalidOid)
120
121 /* Find existing cache entry, if any. */
125 &spcid,
126 HASH_FIND,
127 NULL);
128 if (spc)
129 return spc;
130
131 /*
132 * Not found in TableSpace cache. Check catcache. If we don't find a
133 * valid HeapTuple, it must mean someone has managed to request tablespace
134 * details for a non-existent tablespace. We'll just treat that case as
135 * if no options were specified.
136 */
138 if (!HeapTupleIsValid(tp))
139 opts = NULL;
140 else
141 {
142 Datum datum;
143 bool isNull;
144
146 tp,
148 &isNull);
149 if (isNull)
150 opts = NULL;
151 else
152 {
153 bytea *bytea_opts = tablespace_reloptions(datum, false);
154
157 }
158 ReleaseSysCache(tp);
159 }
160
161 /*
162 * Now create the cache entry. It's important to do this only after
163 * reading the pg_tablespace entry, since doing so could cause a cache
164 * flush.
165 */
167 &spcid,
169 NULL);
170 spc->opts = opts;
171 return spc;
172}
173
174/*
175 * get_tablespace_page_costs
176 * Return random and/or sequential page costs for a given tablespace.
177 *
178 * This value is not locked by the transaction, so this value may
179 * be changed while a SELECT that has used these values for planning
180 * is still executing.
181 */
182void
184 double *spc_random_page_cost,
185 double *spc_seq_page_cost)
186{
188
189 Assert(spc != NULL);
190
191 if (spc_random_page_cost)
192 {
193 if (!spc->opts || spc->opts->random_page_cost < 0)
194 *spc_random_page_cost = random_page_cost;
195 else
196 *spc_random_page_cost = spc->opts->random_page_cost;
197 }
198
200 {
201 if (!spc->opts || spc->opts->seq_page_cost < 0)
203 else
204 *spc_seq_page_cost = spc->opts->seq_page_cost;
205 }
206}
207
208/*
209 * get_tablespace_io_concurrency
210 *
211 * This value is not locked by the transaction, so this value may
212 * be changed while a SELECT that has used these values for planning
213 * is still executing.
214 */
215int
217{
219
220 if (!spc->opts || spc->opts->effective_io_concurrency < 0)
222 else
223 return spc->opts->effective_io_concurrency;
224}
225
226/*
227 * get_tablespace_maintenance_io_concurrency
228 */
229int
231{
233
234 if (!spc->opts || spc->opts->maintenance_io_concurrency < 0)
236 else
237 return spc->opts->maintenance_io_concurrency;
238}
int maintenance_io_concurrency
Definition bufmgr.c:192
int effective_io_concurrency
Definition bufmgr.c:185
#define Assert(condition)
Definition c.h:906
uint32_t uint32
Definition c.h:579
void CreateCacheMemoryContext(void)
Definition catcache.c:715
double random_page_cost
Definition costsize.c:131
double seq_page_cost
Definition costsize.c:130
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
Datum arg
Definition elog.c:1322
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
Oid MyDatabaseTableSpace
Definition globals.c:96
@ HASH_FIND
Definition hsearch.h:113
@ HASH_REMOVE
Definition hsearch.h:115
@ HASH_ENTER
Definition hsearch.h:114
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void CacheRegisterSyscacheCallback(SysCacheIdentifier cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext CacheMemoryContext
Definition mcxt.c:169
static AmcheckOptions opts
Definition pg_amcheck.c:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
static int fb(int x)
tree ctl
Definition radixtree.h:1838
bytea * tablespace_reloptions(Datum reloptions, bool validate)
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition spccache.c:108
static void InvalidateTableSpaceCacheCallback(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
Definition spccache.c:55
int get_tablespace_io_concurrency(Oid spcid)
Definition spccache.c:216
static void InitializeTableSpaceCache(void)
Definition spccache.c:79
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition spccache.c:183
static HTAB * TableSpaceCacheHash
Definition spccache.c:36
int get_tablespace_maintenance_io_concurrency(Oid spcid)
Definition spccache.c:230
Size keysize
Definition hsearch.h:75
TableSpaceOpts * opts
Definition spccache.c:41
Definition c.h:739
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595
static Size VARSIZE(const void *PTR)
Definition varatt.h:298