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/cost.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 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)
 

Variables

static HTABTableSpaceCacheHash = NULL
 

Function Documentation

◆ get_tablespace()

static TableSpaceCacheEntry* get_tablespace ( Oid  spcid)
static

Definition at line 107 of file spccache.c.

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

Referenced by get_tablespace_io_concurrency(), and get_tablespace_page_costs().

108 {
110  HeapTuple tp;
111  TableSpaceOpts *opts;
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  (void *) &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  */
137  if (!HeapTupleIsValid(tp))
138  opts = NULL;
139  else
140  {
141  Datum datum;
142  bool isNull;
143 
145  tp,
147  &isNull);
148  if (isNull)
149  opts = NULL;
150  else
151  {
152  bytea *bytea_opts = tablespace_reloptions(datum, false);
153 
154  opts = MemoryContextAlloc(CacheMemoryContext, VARSIZE(bytea_opts));
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  (void *) &spcid,
167  HASH_ENTER,
168  NULL);
169  spc->opts = opts;
170  return spc;
171 }
#define VARSIZE(PTR)
Definition: postgres.h:304
bytea * tablespace_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1505
TableSpaceOpts * opts
Definition: spccache.c:40
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
Oid MyDatabaseTableSpace
Definition: globals.c:79
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
static HTAB * TableSpaceCacheHash
Definition: spccache.c:35
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
static void InitializeTableSpaceCache(void)
Definition: spccache.c:77
#define Anum_pg_tablespace_spcoptions
Definition: pg_tablespace.h:58
Definition: c.h:487
MemoryContext CacheMemoryContext
Definition: mcxt.c:46

◆ get_tablespace_io_concurrency()

int get_tablespace_io_concurrency ( Oid  spcid)

Definition at line 215 of file spccache.c.

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

Referenced by ExecInitBitmapHeapScan().

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 }
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition: spccache.c:107
TableSpaceOpts * opts
Definition: spccache.c:40
int effective_io_concurrency
Definition: bufmgr.c:112
int effective_io_concurrency
Definition: tablespace.h:42

◆ 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.

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

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

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 }
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition: spccache.c:107
float8 random_page_cost
Definition: tablespace.h:40
TableSpaceOpts * opts
Definition: spccache.c:40
float8 seq_page_cost
Definition: tablespace.h:41
double random_page_cost
Definition: costsize.c:105
#define Assert(condition)
Definition: c.h:670
double seq_page_cost
Definition: costsize.c:104

◆ InitializeTableSpaceCache()

static void InitializeTableSpaceCache ( void  )
static

Definition at line 77 of file spccache.c.

References CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, InvalidateTableSpaceCacheCallback(), HASHCTL::keysize, MemSet, and TABLESPACEOID.

Referenced by get_tablespace().

78 {
79  HASHCTL ctl;
80 
81  /* Initialize the hash table. */
82  MemSet(&ctl, 0, sizeof(ctl));
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. */
96  (Datum) 0);
97 }
#define HASH_ELEM
Definition: hsearch.h:87
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:853
unsigned int Oid
Definition: postgres_ext.h:31
static HTAB * TableSpaceCacheHash
Definition: spccache.c:35
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:372
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
static void InvalidateTableSpaceCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: spccache.c:54
void CreateCacheMemoryContext(void)
Definition: catcache.c:636
MemoryContext CacheMemoryContext
Definition: mcxt.c:46

◆ InvalidateTableSpaceCacheCallback()

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

Definition at line 54 of file spccache.c.

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

Referenced by InitializeTableSpaceCache().

55 {
58 
60  while ((spc = (TableSpaceCacheEntry *) hash_seq_search(&status)) != NULL)
61  {
62  if (spc->opts)
63  pfree(spc->opts);
65  (void *) &spc->oid,
67  NULL) == NULL)
68  elog(ERROR, "hash table corrupted");
69  }
70 }
TableSpaceOpts * opts
Definition: spccache.c:40
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
static HTAB * TableSpaceCacheHash
Definition: spccache.c:35
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1385
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1375
#define elog
Definition: elog.h:219
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

Variable Documentation

◆ TableSpaceCacheHash

HTAB* TableSpaceCacheHash = NULL
static

Definition at line 35 of file spccache.c.