PostgreSQL Source Code  git master
spccache.c File Reference
#include "postgres.h"
#include "access/reloptions.h"
#include "catalog/pg_tablespace.h"
#include "commands/tablespace.h"
#include "miscadmin.h"
#include "optimizer/optimizer.h"
#include "storage/bufmgr.h"
#include "utils/catcache.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/spccache.h"
#include "utils/syscache.h"
#include "varatt.h"
Include dependency graph for spccache.c:

Go to the source code of this file.

Data Structures

struct  TableSpaceCacheEntry
 

Functions

static void InvalidateTableSpaceCacheCallback (Datum arg, int cacheid, uint32 hashvalue)
 
static void InitializeTableSpaceCache (void)
 
static TableSpaceCacheEntryget_tablespace (Oid spcid)
 
void get_tablespace_page_costs (Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
 
int get_tablespace_io_concurrency (Oid spcid)
 
int get_tablespace_maintenance_io_concurrency (Oid spcid)
 

Variables

static HTABTableSpaceCacheHash = NULL
 

Function Documentation

◆ get_tablespace()

static TableSpaceCacheEntry* get_tablespace ( Oid  spcid)
static

Definition at line 107 of file spccache.c.

108 {
110  HeapTuple tp;
112 
113  /*
114  * Since spcid is always from a pg_class tuple, InvalidOid implies the
115  * default.
116  */
117  if (spcid == InvalidOid)
118  spcid = MyDatabaseTableSpace;
119 
120  /* Find existing cache entry, if any. */
121  if (!TableSpaceCacheHash)
124  &spcid,
125  HASH_FIND,
126  NULL);
127  if (spc)
128  return spc;
129 
130  /*
131  * Not found in TableSpace cache. Check catcache. If we don't find a
132  * valid HeapTuple, it must mean someone has managed to request tablespace
133  * details for a non-existent tablespace. We'll just treat that case as
134  * if no options were specified.
135  */
136  tp = SearchSysCache1(TABLESPACEOID, ObjectIdGetDatum(spcid));
137  if (!HeapTupleIsValid(tp))
138  opts = NULL;
139  else
140  {
141  Datum datum;
142  bool isNull;
143 
144  datum = SysCacheGetAttr(TABLESPACEOID,
145  tp,
146  Anum_pg_tablespace_spcoptions,
147  &isNull);
148  if (isNull)
149  opts = NULL;
150  else
151  {
152  bytea *bytea_opts = tablespace_reloptions(datum, false);
153 
155  memcpy(opts, bytea_opts, VARSIZE(bytea_opts));
156  }
157  ReleaseSysCache(tp);
158  }
159 
160  /*
161  * Now create the cache entry. It's important to do this only after
162  * reading the pg_tablespace entry, since doing so could cause a cache
163  * flush.
164  */
166  &spcid,
167  HASH_ENTER,
168  NULL);
169  spc->opts = opts;
170  return spc;
171 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
Oid MyDatabaseTableSpace
Definition: globals.c:95
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
MemoryContext CacheMemoryContext
Definition: mcxt.c:152
static AmcheckOptions opts
Definition: pg_amcheck.c:111
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
bytea * tablespace_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:2086
static void InitializeTableSpaceCache(void)
Definition: spccache.c:78
static HTAB * TableSpaceCacheHash
Definition: spccache.c:36
TableSpaceOpts * opts
Definition: spccache.c:41
Definition: c.h:678
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:596
#define VARSIZE(PTR)
Definition: varatt.h:279

References CacheMemoryContext, HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, InitializeTableSpaceCache(), InvalidOid, MemoryContextAlloc(), MyDatabaseTableSpace, ObjectIdGetDatum(), TableSpaceCacheEntry::opts, opts, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), tablespace_reloptions(), TableSpaceCacheHash, and VARSIZE.

Referenced by get_tablespace_io_concurrency(), get_tablespace_maintenance_io_concurrency(), and get_tablespace_page_costs().

◆ get_tablespace_io_concurrency()

int get_tablespace_io_concurrency ( Oid  spcid)

Definition at line 215 of file spccache.c.

216 {
217  TableSpaceCacheEntry *spc = get_tablespace(spcid);
218 
219  if (!spc->opts || spc->opts->effective_io_concurrency < 0)
221  else
222  return spc->opts->effective_io_concurrency;
223 }
int effective_io_concurrency
Definition: bufmgr.c:151
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition: spccache.c:107
int effective_io_concurrency
Definition: tablespace.h:46

References effective_io_concurrency, TableSpaceOpts::effective_io_concurrency, get_tablespace(), and TableSpaceCacheEntry::opts.

Referenced by ExecInitBitmapHeapScan(), and read_stream_begin_impl().

◆ get_tablespace_maintenance_io_concurrency()

int get_tablespace_maintenance_io_concurrency ( Oid  spcid)

Definition at line 229 of file spccache.c.

230 {
231  TableSpaceCacheEntry *spc = get_tablespace(spcid);
232 
233  if (!spc->opts || spc->opts->maintenance_io_concurrency < 0)
235  else
236  return spc->opts->maintenance_io_concurrency;
237 }
int maintenance_io_concurrency
Definition: bufmgr.c:158
int maintenance_io_concurrency
Definition: tablespace.h:47

References get_tablespace(), maintenance_io_concurrency, TableSpaceOpts::maintenance_io_concurrency, and TableSpaceCacheEntry::opts.

Referenced by heap_index_delete_tuples(), and read_stream_begin_impl().

◆ get_tablespace_page_costs()

void get_tablespace_page_costs ( Oid  spcid,
double *  spc_random_page_cost,
double *  spc_seq_page_cost 
)

Definition at line 182 of file spccache.c.

185 {
186  TableSpaceCacheEntry *spc = get_tablespace(spcid);
187 
188  Assert(spc != NULL);
189 
190  if (spc_random_page_cost)
191  {
192  if (!spc->opts || spc->opts->random_page_cost < 0)
193  *spc_random_page_cost = random_page_cost;
194  else
195  *spc_random_page_cost = spc->opts->random_page_cost;
196  }
197 
198  if (spc_seq_page_cost)
199  {
200  if (!spc->opts || spc->opts->seq_page_cost < 0)
201  *spc_seq_page_cost = seq_page_cost;
202  else
203  *spc_seq_page_cost = spc->opts->seq_page_cost;
204  }
205 }
#define Assert(condition)
Definition: c.h:849
double random_page_cost
Definition: costsize.c:131
double seq_page_cost
Definition: costsize.c:130
float8 random_page_cost
Definition: tablespace.h:44
float8 seq_page_cost
Definition: tablespace.h:45

References Assert, get_tablespace(), TableSpaceCacheEntry::opts, random_page_cost, TableSpaceOpts::random_page_cost, seq_page_cost, and TableSpaceOpts::seq_page_cost.

Referenced by brincostestimate(), cost_bitmap_heap_scan(), cost_index(), cost_samplescan(), cost_seqscan(), cost_tidrangescan(), cost_tidscan(), genericcostestimate(), gincostestimate(), and system_time_samplescangetsamplesize().

◆ InitializeTableSpaceCache()

static void InitializeTableSpaceCache ( void  )
static

Definition at line 78 of file spccache.c.

79 {
80  HASHCTL ctl;
81 
82  /* Initialize the hash table. */
83  ctl.keysize = sizeof(Oid);
84  ctl.entrysize = sizeof(TableSpaceCacheEntry);
86  hash_create("TableSpace cache", 16, &ctl,
88 
89  /* Make sure we've initialized CacheMemoryContext. */
90  if (!CacheMemoryContext)
92 
93  /* Watch for invalidation events. */
94  CacheRegisterSyscacheCallback(TABLESPACEOID,
96  (Datum) 0);
97 }
void CreateCacheMemoryContext(void)
Definition: catcache.c:680
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1516
unsigned int Oid
Definition: postgres_ext.h:31
tree ctl
Definition: radixtree.h:1853
static void InvalidateTableSpaceCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: spccache.c:55

References CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), ctl, HASH_BLOBS, hash_create(), HASH_ELEM, InvalidateTableSpaceCacheCallback(), and TableSpaceCacheHash.

Referenced by get_tablespace().

◆ InvalidateTableSpaceCacheCallback()

static void InvalidateTableSpaceCacheCallback ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 55 of file spccache.c.

56 {
57  HASH_SEQ_STATUS status;
59 
61  while ((spc = (TableSpaceCacheEntry *) hash_seq_search(&status)) != NULL)
62  {
63  if (spc->opts)
64  pfree(spc->opts);
66  &spc->oid,
68  NULL) == NULL)
69  elog(ERROR, "hash table corrupted");
70  }
71 }
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1420
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
@ HASH_REMOVE
Definition: hsearch.h:115
void pfree(void *pointer)
Definition: mcxt.c:1521

References elog, ERROR, HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), TableSpaceCacheEntry::oid, TableSpaceCacheEntry::opts, pfree(), and TableSpaceCacheHash.

Referenced by InitializeTableSpaceCache().

Variable Documentation

◆ TableSpaceCacheHash

HTAB* TableSpaceCacheHash = NULL
static