PostgreSQL Source Code  git master
shippable.c File Reference
#include "postgres.h"
#include "access/transam.h"
#include "catalog/dependency.h"
#include "postgres_fdw.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/syscache.h"
Include dependency graph for shippable.c:

Go to the source code of this file.

Data Structures

struct  ShippableCacheKey
 
struct  ShippableCacheEntry
 

Functions

static void InvalidateShippableCacheCallback (Datum arg, int cacheid, uint32 hashvalue)
 
static void InitializeShippableCache (void)
 
static bool lookup_shippable (Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
 
bool is_builtin (Oid objectId)
 
bool is_shippable (Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
 

Variables

static HTABShippableCacheHash = NULL
 

Function Documentation

◆ InitializeShippableCache()

static void InitializeShippableCache ( void  )
static

Definition at line 91 of file shippable.c.

References CacheRegisterSyscacheCallback(), HASHCTL::entrysize, FOREIGNSERVEROID, HASH_BLOBS, hash_create(), HASH_ELEM, InvalidateShippableCacheCallback(), HASHCTL::keysize, and MemSet.

Referenced by is_shippable().

92 {
93  HASHCTL ctl;
94 
95  /* Create the hash table. */
96  MemSet(&ctl, 0, sizeof(ctl));
97  ctl.keysize = sizeof(ShippableCacheKey);
98  ctl.entrysize = sizeof(ShippableCacheEntry);
100  hash_create("Shippability cache", 256, &ctl, HASH_ELEM | HASH_BLOBS);
101 
102  /* Set up invalidation callback on pg_foreign_server. */
105  (Datum) 0);
106 }
#define HASH_ELEM
Definition: hsearch.h:87
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:971
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
uintptr_t Datum
Definition: postgres.h:367
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:318
Size keysize
Definition: hsearch.h:72
static void InvalidateShippableCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: shippable.c:65
static HTAB * ShippableCacheHash
Definition: shippable.c:34

◆ InvalidateShippableCacheCallback()

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

Definition at line 65 of file shippable.c.

References elog, ERROR, HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), ShippableCacheEntry::key, and status().

Referenced by InitializeShippableCache().

66 {
68  ShippableCacheEntry *entry;
69 
70  /*
71  * In principle we could flush only cache entries relating to the
72  * pg_foreign_server entry being outdated; but that would be more
73  * complicated, and it's probably not worth the trouble. So for now, just
74  * flush all entries.
75  */
77  while ((entry = (ShippableCacheEntry *) hash_seq_search(&status)) != NULL)
78  {
80  (void *) &entry->key,
82  NULL) == NULL)
83  elog(ERROR, "hash table corrupted");
84  }
85 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
#define ERROR
Definition: elog.h:43
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1391
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1381
static HTAB * ShippableCacheHash
Definition: shippable.c:34
#define elog(elevel,...)
Definition: elog.h:214
ShippableCacheKey key
Definition: shippable.c:51
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ is_builtin()

bool is_builtin ( Oid  objectId)

Definition at line 153 of file shippable.c.

References FirstGenbkiObjectId.

Referenced by deparse_type_name(), and is_shippable().

154 {
155  return (objectId < FirstGenbkiObjectId);
156 }
#define FirstGenbkiObjectId
Definition: transam.h:153

◆ is_shippable()

bool is_shippable ( Oid  objectId,
Oid  classId,
PgFdwRelationInfo fpinfo 
)

Definition at line 163 of file shippable.c.

References ShippableCacheKey::classid, HASH_ENTER, HASH_FIND, hash_search(), InitializeShippableCache(), is_builtin(), sort-test::key, lookup_shippable(), NIL, ShippableCacheKey::objid, PgFdwRelationInfo::server, ForeignServer::serverid, ShippableCacheKey::serverid, ShippableCacheEntry::shippable, and PgFdwRelationInfo::shippable_extensions.

Referenced by foreign_expr_walker().

164 {
166  ShippableCacheEntry *entry;
167 
168  /* Built-in objects are presumed shippable. */
169  if (is_builtin(objectId))
170  return true;
171 
172  /* Otherwise, give up if user hasn't specified any shippable extensions. */
173  if (fpinfo->shippable_extensions == NIL)
174  return false;
175 
176  /* Initialize cache if first time through. */
177  if (!ShippableCacheHash)
179 
180  /* Set up cache hash key */
181  key.objid = objectId;
182  key.classid = classId;
183  key.serverid = fpinfo->server->serverid;
184 
185  /* See if we already cached the result. */
186  entry = (ShippableCacheEntry *)
188  (void *) &key,
189  HASH_FIND,
190  NULL);
191 
192  if (!entry)
193  {
194  /* Not found in cache, so perform shippability lookup. */
195  bool shippable = lookup_shippable(objectId, classId, fpinfo);
196 
197  /*
198  * Don't create a new hash entry until *after* we have the shippable
199  * result in hand, as the underlying catalog lookups might trigger a
200  * cache invalidation.
201  */
202  entry = (ShippableCacheEntry *)
204  (void *) &key,
205  HASH_ENTER,
206  NULL);
207 
208  entry->shippable = shippable;
209  }
210 
211  return entry->shippable;
212 }
#define NIL
Definition: pg_list.h:65
bool is_builtin(Oid objectId)
Definition: shippable.c:153
ForeignServer * server
Definition: postgres_fdw.h:84
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
static void InitializeShippableCache(void)
Definition: shippable.c:91
static bool lookup_shippable(Oid objectId, Oid classId, PgFdwRelationInfo *fpinfo)
Definition: shippable.c:117
static HTAB * ShippableCacheHash
Definition: shippable.c:34
List * shippable_extensions
Definition: postgres_fdw.h:80
Oid serverid
Definition: foreign.h:36

◆ lookup_shippable()

static bool lookup_shippable ( Oid  objectId,
Oid  classId,
PgFdwRelationInfo fpinfo 
)
static

Definition at line 117 of file shippable.c.

References getExtensionOfObject(), list_member_oid(), OidIsValid, and PgFdwRelationInfo::shippable_extensions.

Referenced by is_shippable().

118 {
119  Oid extensionOid;
120 
121  /*
122  * Is object a member of some extension? (Note: this is a fairly
123  * expensive lookup, which is why we try to cache the results.)
124  */
125  extensionOid = getExtensionOfObject(classId, objectId);
126 
127  /* If so, is that extension in fpinfo->shippable_extensions? */
128  if (OidIsValid(extensionOid) &&
129  list_member_oid(fpinfo->shippable_extensions, extensionOid))
130  return true;
131 
132  return false;
133 }
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:659
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
List * shippable_extensions
Definition: postgres_fdw.h:80

Variable Documentation

◆ ShippableCacheHash

HTAB* ShippableCacheHash = NULL
static

Definition at line 34 of file shippable.c.