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 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 106 of file spccache.c.

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

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

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

◆ get_tablespace_io_concurrency()

int get_tablespace_io_concurrency ( Oid  spcid)

Definition at line 214 of file spccache.c.

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

Referenced by acquire_sample_rows(), and ExecInitBitmapHeapScan().

215 {
216  TableSpaceCacheEntry *spc = get_tablespace(spcid);
217 
218  if (!spc->opts || spc->opts->effective_io_concurrency < 0)
220  else
221  return spc->opts->effective_io_concurrency;
222 }
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition: spccache.c:106
TableSpaceOpts * opts
Definition: spccache.c:40
int effective_io_concurrency
Definition: bufmgr.c:143
int effective_io_concurrency
Definition: tablespace.h:42

◆ get_tablespace_maintenance_io_concurrency()

int get_tablespace_maintenance_io_concurrency ( Oid  spcid)

Definition at line 228 of file spccache.c.

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

Referenced by heap_index_delete_tuples().

229 {
230  TableSpaceCacheEntry *spc = get_tablespace(spcid);
231 
232  if (!spc->opts || spc->opts->maintenance_io_concurrency < 0)
234  else
235  return spc->opts->maintenance_io_concurrency;
236 }
int maintenance_io_concurrency
Definition: tablespace.h:43
int maintenance_io_concurrency
Definition: bufmgr.c:150
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition: spccache.c:106
TableSpaceOpts * opts
Definition: spccache.c:40

◆ get_tablespace_page_costs()

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

Definition at line 181 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_tidrangescan(), cost_tidscan(), genericcostestimate(), gincostestimate(), and system_time_samplescangetsamplesize().

184 {
185  TableSpaceCacheEntry *spc = get_tablespace(spcid);
186 
187  Assert(spc != NULL);
188 
189  if (spc_random_page_cost)
190  {
191  if (!spc->opts || spc->opts->random_page_cost < 0)
192  *spc_random_page_cost = random_page_cost;
193  else
194  *spc_random_page_cost = spc->opts->random_page_cost;
195  }
196 
197  if (spc_seq_page_cost)
198  {
199  if (!spc->opts || spc->opts->seq_page_cost < 0)
200  *spc_seq_page_cost = seq_page_cost;
201  else
202  *spc_seq_page_cost = spc->opts->seq_page_cost;
203  }
204 }
static TableSpaceCacheEntry * get_tablespace(Oid spcid)
Definition: spccache.c:106
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:120
#define Assert(condition)
Definition: c.h:804
double seq_page_cost
Definition: costsize.c:119

◆ 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, and TABLESPACEOID.

Referenced by get_tablespace().

78 {
79  HASHCTL ctl;
80 
81  /* Initialize the hash table. */
82  ctl.keysize = sizeof(Oid);
83  ctl.entrysize = sizeof(TableSpaceCacheEntry);
85  hash_create("TableSpace cache", 16, &ctl,
87 
88  /* Make sure we've initialized CacheMemoryContext. */
89  if (!CacheMemoryContext)
91 
92  /* Watch for invalidation events. */
95  (Datum) 0);
96 }
#define HASH_ELEM
Definition: hsearch.h:95
Size entrysize
Definition: hsearch.h:76
unsigned int Oid
Definition: postgres_ext.h:31
static HTAB * TableSpaceCacheHash
Definition: spccache.c:35
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1434
uintptr_t Datum
Definition: postgres.h:411
Size keysize
Definition: hsearch.h:75
static void InvalidateTableSpaceCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: spccache.c:54
void CreateCacheMemoryContext(void)
Definition: catcache.c:620
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ 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:954
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
static HTAB * TableSpaceCacheHash
Definition: spccache.c:35
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
#define elog(elevel,...)
Definition: elog.h:232
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

Variable Documentation

◆ TableSpaceCacheHash

HTAB* TableSpaceCacheHash = NULL
static

Definition at line 35 of file spccache.c.