PostgreSQL Source Code  git master
time_mapping.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "storage/lwlock.h"
#include "utils/old_snapshot.h"
#include "utils/snapmgr.h"
#include "utils/timestamp.h"
Include dependency graph for time_mapping.c:

Go to the source code of this file.

Data Structures

struct  OldSnapshotTimeMapping
 

Macros

#define NUM_TIME_MAPPING_COLUMNS   3
 

Functions

 PG_FUNCTION_INFO_V1 (pg_old_snapshot_time_mapping)
 
static OldSnapshotTimeMappingGetOldSnapshotTimeMapping (void)
 
static TupleDesc MakeOldSnapshotTimeMappingTupleDesc (void)
 
static HeapTuple MakeOldSnapshotTimeMappingTuple (TupleDesc tupdesc, OldSnapshotTimeMapping *mapping)
 
Datum pg_old_snapshot_time_mapping (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ NUM_TIME_MAPPING_COLUMNS

#define NUM_TIME_MAPPING_COLUMNS   3

Function Documentation

◆ GetOldSnapshotTimeMapping()

static OldSnapshotTimeMapping * GetOldSnapshotTimeMapping ( void  )
static

Definition at line 85 of file time_mapping.c.

References OldSnapshotTimeMapping::count_used, OldSnapshotControlData::count_used, OldSnapshotTimeMapping::current_index, OldSnapshotTimeMapping::head_offset, OldSnapshotControlData::head_offset, OldSnapshotTimeMapping::head_timestamp, OldSnapshotControlData::head_timestamp, i, LW_SHARED, LWLockAcquire(), LWLockRelease(), offsetof, OLD_SNAPSHOT_TIME_MAP_ENTRIES, oldSnapshotControl, palloc(), OldSnapshotTimeMapping::xid_by_minute, and OldSnapshotControlData::xid_by_minute.

Referenced by pg_old_snapshot_time_mapping().

86 {
87  OldSnapshotTimeMapping *mapping;
88 
89  mapping = palloc(offsetof(OldSnapshotTimeMapping, xid_by_minute)
91  mapping->current_index = 0;
92 
93  LWLockAcquire(OldSnapshotTimeMapLock, LW_SHARED);
97  for (int i = 0; i < OLD_SNAPSHOT_TIME_MAP_ENTRIES; ++i)
99  LWLockRelease(OldSnapshotTimeMapLock);
100 
101  return mapping;
102 }
uint32 TransactionId
Definition: c.h:521
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
volatile OldSnapshotControlData * oldSnapshotControl
Definition: snapmgr.c:80
TransactionId xid_by_minute[FLEXIBLE_ARRAY_MEMBER]
Definition: time_mapping.c:32
TimestampTz head_timestamp
Definition: time_mapping.c:30
TransactionId xid_by_minute[FLEXIBLE_ARRAY_MEMBER]
Definition: old_snapshot.h:70
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
void * palloc(Size size)
Definition: mcxt.c:950
TimestampTz head_timestamp
Definition: old_snapshot.h:68
int i
#define OLD_SNAPSHOT_TIME_MAP_ENTRIES
Definition: snapmgr.h:32
#define offsetof(type, field)
Definition: c.h:669

◆ MakeOldSnapshotTimeMappingTuple()

static HeapTuple MakeOldSnapshotTimeMappingTuple ( TupleDesc  tupdesc,
OldSnapshotTimeMapping mapping 
)
static

Definition at line 128 of file time_mapping.c.

References array_position(), OldSnapshotTimeMapping::current_index, OldSnapshotTimeMapping::head_offset, OldSnapshotTimeMapping::head_timestamp, heap_form_tuple(), Int32GetDatum, NUM_TIME_MAPPING_COLUMNS, OLD_SNAPSHOT_TIME_MAP_ENTRIES, TimestampTzGetDatum, TimestampTzPlusMilliseconds, TransactionIdGetDatum, values, and OldSnapshotTimeMapping::xid_by_minute.

Referenced by pg_old_snapshot_time_mapping().

129 {
131  bool nulls[NUM_TIME_MAPPING_COLUMNS];
132  int array_position;
134 
135  /*
136  * Figure out the array position corresponding to the current index.
137  *
138  * Index 0 means the oldest entry in the mapping, which is stored at
139  * mapping->head_offset. Index 1 means the next-oldest entry, which is a the
140  * following index, and so on. We wrap around when we reach the end of the array.
141  */
142  array_position = (mapping->head_offset + mapping->current_index)
144 
145  /*
146  * No explicit timestamp is stored for any entry other than the oldest one,
147  * but each entry corresponds to 1-minute period, so we can just add.
148  */
149  timestamp = TimestampTzPlusMilliseconds(mapping->head_timestamp,
150  mapping->current_index * 60000);
151 
152  /* Initialize nulls and values arrays. */
153  memset(nulls, 0, sizeof(nulls));
154  values[0] = Int32GetDatum(array_position);
155  values[1] = TimestampTzGetDatum(timestamp);
156  values[2] = TransactionIdGetDatum(mapping->xid_by_minute[array_position]);
157 
158  return heap_form_tuple(tupdesc, values, nulls);
159 }
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
Datum array_position(PG_FUNCTION_ARGS)
TransactionId xid_by_minute[FLEXIBLE_ARRAY_MEMBER]
Definition: time_mapping.c:32
#define TransactionIdGetDatum(X)
Definition: postgres.h:521
uintptr_t Datum
Definition: postgres.h:367
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
TimestampTz head_timestamp
Definition: time_mapping.c:30
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define Int32GetDatum(X)
Definition: postgres.h:479
#define NUM_TIME_MAPPING_COLUMNS
Definition: time_mapping.c:35
#define OLD_SNAPSHOT_TIME_MAP_ENTRIES
Definition: snapmgr.h:32

◆ MakeOldSnapshotTimeMappingTupleDesc()

static TupleDesc MakeOldSnapshotTimeMappingTupleDesc ( void  )
static

Definition at line 108 of file time_mapping.c.

References BlessTupleDesc(), CreateTemplateTupleDesc(), NUM_TIME_MAPPING_COLUMNS, and TupleDescInitEntry().

Referenced by pg_old_snapshot_time_mapping().

109 {
110  TupleDesc tupdesc;
111 
113 
114  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "array_offset",
115  INT4OID, -1, 0);
116  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "end_timestamp",
117  TIMESTAMPTZOID, -1, 0);
118  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "newest_xmin",
119  XIDOID, -1, 0);
120 
121  return BlessTupleDesc(tupdesc);
122 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define NUM_TIME_MAPPING_COLUMNS
Definition: time_mapping.c:35
int16 AttrNumber
Definition: attnum.h:21

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( pg_old_snapshot_time_mapping  )

◆ pg_old_snapshot_time_mapping()

Datum pg_old_snapshot_time_mapping ( PG_FUNCTION_ARGS  )

Definition at line 49 of file time_mapping.c.

References OldSnapshotTimeMapping::count_used, OldSnapshotTimeMapping::current_index, GetOldSnapshotTimeMapping(), HeapTupleGetDatum, MakeOldSnapshotTimeMappingTuple(), MakeOldSnapshotTimeMappingTupleDesc(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::tuple_desc, and FuncCallContext::user_fctx.

50 {
51  FuncCallContext *funcctx;
52  OldSnapshotTimeMapping *mapping;
53 
54  if (SRF_IS_FIRSTCALL())
55  {
56  MemoryContext oldcontext;
57 
58  funcctx = SRF_FIRSTCALL_INIT();
59  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
60  mapping = GetOldSnapshotTimeMapping();
61  funcctx->user_fctx = mapping;
63  MemoryContextSwitchTo(oldcontext);
64  }
65 
66  funcctx = SRF_PERCALL_SETUP();
67  mapping = (OldSnapshotTimeMapping *) funcctx->user_fctx;
68 
69  while (mapping->current_index < mapping->count_used)
70  {
71  HeapTuple tuple;
72 
73  tuple = MakeOldSnapshotTimeMappingTuple(funcctx->tuple_desc, mapping);
74  ++mapping->current_index;
75  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
76  }
77 
78  SRF_RETURN_DONE(funcctx);
79 }
static OldSnapshotTimeMapping * GetOldSnapshotTimeMapping(void)
Definition: time_mapping.c:85
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static TupleDesc MakeOldSnapshotTimeMappingTupleDesc(void)
Definition: time_mapping.c:108
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
TupleDesc tuple_desc
Definition: funcapi.h:112
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
void * user_fctx
Definition: funcapi.h:82
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
static HeapTuple MakeOldSnapshotTimeMappingTuple(TupleDesc tupdesc, OldSnapshotTimeMapping *mapping)
Definition: time_mapping.c:128
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 37 of file time_mapping.c.