PostgreSQL Source Code  git master
partdesc.h File Reference
Include dependency graph for partdesc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PartitionDescData
 

Typedefs

typedef struct PartitionDescData PartitionDescData
 

Functions

PartitionDesc RelationGetPartitionDesc (Relation rel, bool omit_detached)
 
PartitionDirectory CreatePartitionDirectory (MemoryContext mcxt, bool omit_detached)
 
PartitionDesc PartitionDirectoryLookup (PartitionDirectory, Relation)
 
void DestroyPartitionDirectory (PartitionDirectory pdir)
 
Oid get_default_oid_from_partdesc (PartitionDesc partdesc)
 

Typedef Documentation

◆ PartitionDescData

Function Documentation

◆ CreatePartitionDirectory()

PartitionDirectory CreatePartitionDirectory ( MemoryContext  mcxt,
bool  omit_detached 
)

Definition at line 377 of file partdesc.c.

References HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, MemoryContextSwitchTo(), PartitionDirectoryData::omit_detached, palloc(), PartitionDirectoryData::pdir_hash, and PartitionDirectoryData::pdir_mcxt.

Referenced by ExecCreatePartitionPruneState(), ExecInitPartitionDispatchInfo(), and set_relation_partition_info().

378 {
379  MemoryContext oldcontext = MemoryContextSwitchTo(mcxt);
380  PartitionDirectory pdir;
381  HASHCTL ctl;
382 
383  pdir = palloc(sizeof(PartitionDirectoryData));
384  pdir->pdir_mcxt = mcxt;
385 
386  ctl.keysize = sizeof(Oid);
387  ctl.entrysize = sizeof(PartitionDirectoryEntry);
388  ctl.hcxt = mcxt;
389 
390  pdir->pdir_hash = hash_create("partition directory", 256, &ctl,
392  pdir->omit_detached = omit_detached;
393 
394  MemoryContextSwitchTo(oldcontext);
395  return pdir;
396 }
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
MemoryContext hcxt
Definition: hsearch.h:86
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:76
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContext pdir_mcxt
Definition: partdesc.c:38
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
struct PartitionDirectoryEntry PartitionDirectoryEntry
#define HASH_BLOBS
Definition: hsearch.h:97
Size keysize
Definition: hsearch.h:75
void * palloc(Size size)
Definition: mcxt.c:1062

◆ DestroyPartitionDirectory()

void DestroyPartitionDirectory ( PartitionDirectory  pdir)

Definition at line 438 of file partdesc.c.

References hash_seq_init(), hash_seq_search(), PartitionDirectoryData::pdir_hash, PartitionDirectoryEntry::rel, RelationDecrementReferenceCount(), and status().

Referenced by FreeExecutorState(), and standard_planner().

439 {
442 
443  hash_seq_init(&status, pdir->pdir_hash);
444  while ((pde = hash_seq_search(&status)) != NULL)
446 }
void RelationDecrementReferenceCount(Relation rel)
Definition: relcache.c:2132
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
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229

◆ get_default_oid_from_partdesc()

Oid get_default_oid_from_partdesc ( PartitionDesc  partdesc)

Definition at line 455 of file partdesc.c.

References PartitionDescData::boundinfo, PartitionBoundInfoData::default_index, InvalidOid, PartitionDescData::oids, and partition_bound_has_default.

Referenced by ATExecAttachPartition(), ATExecDetachPartition(), DefineRelation(), and StorePartitionBound().

456 {
457  if (partdesc && partdesc->boundinfo &&
459  return partdesc->oids[partdesc->boundinfo->default_index];
460 
461  return InvalidOid;
462 }
PartitionBoundInfo boundinfo
Definition: partdesc.h:38
#define partition_bound_has_default(bi)
Definition: partbounds.h:99
#define InvalidOid
Definition: postgres_ext.h:36

◆ PartitionDirectoryLookup()

PartitionDesc PartitionDirectoryLookup ( PartitionDirectory  ,
Relation   
)

Definition at line 410 of file partdesc.c.

References Assert, HASH_ENTER, hash_search(), PartitionDirectoryData::omit_detached, PartitionDirectoryEntry::pd, PartitionDirectoryData::pdir_hash, PartitionDirectoryEntry::rel, RelationGetPartitionDesc(), RelationGetRelid, and RelationIncrementReferenceCount().

Referenced by ExecCreatePartitionPruneState(), ExecInitPartitionDispatchInfo(), expand_partitioned_rtentry(), and set_relation_partition_info().

411 {
413  Oid relid = RelationGetRelid(rel);
414  bool found;
415 
416  pde = hash_search(pdir->pdir_hash, &relid, HASH_ENTER, &found);
417  if (!found)
418  {
419  /*
420  * We must keep a reference count on the relation so that the
421  * PartitionDesc to which we are pointing can't get destroyed.
422  */
424  pde->rel = rel;
425  pde->pd = RelationGetPartitionDesc(rel, pdir->omit_detached);
426  Assert(pde->pd != NULL);
427  }
428  return pde->pd;
429 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
PartitionDesc pd
Definition: partdesc.c:47
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:72
void RelationIncrementReferenceCount(Relation rel)
Definition: relcache.c:2119
#define Assert(condition)
Definition: c.h:804
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ RelationGetPartitionDesc()

PartitionDesc RelationGetPartitionDesc ( Relation  rel,
bool  omit_detached 
)

Definition at line 72 of file partdesc.c.

References ActiveSnapshotSet(), Assert, PartitionDescData::detached_exist, GetActiveSnapshot(), likely, RelationData::rd_partdesc, RelationData::rd_partdesc_nodetached, RelationData::rd_partdesc_nodetached_xmin, RelationData::rd_rel, RelationBuildPartitionDesc(), TransactionIdIsValid, and XidInMVCCSnapshot().

Referenced by addFkRecurseReferenced(), addFkRecurseReferencing(), ATExecAttachPartition(), ATExecAttachPartitionIdx(), ATExecDetachPartition(), ATPrepDropNotNull(), check_new_partition_bound(), CreateTriggerFiringOn(), DefineIndex(), DefineRelation(), get_qual_for_list(), get_qual_for_range(), PartitionDirectoryLookup(), QueuePartitionConstraintValidation(), renametrig(), renametrig_partition(), StorePartitionBound(), and validatePartitionedIndex().

73 {
74  Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
75 
76  /*
77  * If relcache has a partition descriptor, use that. However, we can only
78  * do so when we are asked to include all partitions including detached;
79  * and also when we know that there are no detached partitions.
80  *
81  * If there is no active snapshot, detached partitions aren't omitted
82  * either, so we can use the cached descriptor too in that case.
83  */
84  if (likely(rel->rd_partdesc &&
85  (!rel->rd_partdesc->detached_exist || !omit_detached ||
86  !ActiveSnapshotSet())))
87  return rel->rd_partdesc;
88 
89  /*
90  * If we're asked to omit detached partitions, we may be able to use a
91  * cached descriptor too. We determine that based on the pg_inherits.xmin
92  * that was saved alongside that descriptor: if the xmin that was not in
93  * progress for that active snapshot is also not in progress for the
94  * current active snapshot, then we can use it. Otherwise build one
95  * from scratch.
96  */
97  if (omit_detached &&
100  {
101  Snapshot activesnap;
102 
104  activesnap = GetActiveSnapshot();
105 
106  if (!XidInMVCCSnapshot(rel->rd_partdesc_nodetached_xmin, activesnap))
107  return rel->rd_partdesc_nodetached;
108  }
109 
110  return RelationBuildPartitionDesc(rel, omit_detached);
111 }
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: snapmgr.c:2257
#define likely(x)
Definition: c.h:272
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:801
Form_pg_class rd_rel
Definition: rel.h:109
bool detached_exist
Definition: partdesc.h:32
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:813
PartitionDesc rd_partdesc
Definition: rel.h:128
TransactionId rd_partdesc_nodetached_xmin
Definition: rel.h:142
PartitionDesc rd_partdesc_nodetached
Definition: rel.h:132
#define Assert(condition)
Definition: c.h:804
static PartitionDesc RelationBuildPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:135
#define TransactionIdIsValid(xid)
Definition: transam.h:41