PostgreSQL Source Code  git master
time_mapping.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * time_mapping.c
4  * time to XID mapping information
5  *
6  * Copyright (c) 2020-2023, PostgreSQL Global Development Group
7  *
8  * contrib/old_snapshot/time_mapping.c
9  *-------------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 
13 #include "funcapi.h"
14 #include "storage/lwlock.h"
15 #include "utils/old_snapshot.h"
16 #include "utils/snapmgr.h"
17 #include "utils/timestamp.h"
18 
19 /*
20  * Backend-private copy of the information from oldSnapshotControl which relates
21  * to the time to XID mapping, plus an index so that we can iterate.
22  *
23  * Note that the length of the xid_by_minute array is given by
24  * OLD_SNAPSHOT_TIME_MAP_ENTRIES (which is not a compile-time constant).
25  */
26 typedef struct
27 {
34 
35 #define NUM_TIME_MAPPING_COLUMNS 3
36 
39 
42  OldSnapshotTimeMapping *mapping);
43 
44 /*
45  * SQL-callable set-returning function.
46  */
47 Datum
49 {
50  FuncCallContext *funcctx;
51  OldSnapshotTimeMapping *mapping;
52 
53  if (SRF_IS_FIRSTCALL())
54  {
55  MemoryContext oldcontext;
56  TupleDesc tupdesc;
57 
58  funcctx = SRF_FIRSTCALL_INIT();
59  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
60  mapping = GetOldSnapshotTimeMapping();
61  funcctx->user_fctx = mapping;
62  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
63  elog(ERROR, "return type must be a row type");
64  funcctx->tuple_desc = tupdesc;
65  MemoryContextSwitchTo(oldcontext);
66  }
67 
68  funcctx = SRF_PERCALL_SETUP();
69  mapping = (OldSnapshotTimeMapping *) funcctx->user_fctx;
70 
71  while (mapping->current_index < mapping->count_used)
72  {
73  HeapTuple tuple;
74 
75  tuple = MakeOldSnapshotTimeMappingTuple(funcctx->tuple_desc, mapping);
76  ++mapping->current_index;
77  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
78  }
79 
80  SRF_RETURN_DONE(funcctx);
81 }
82 
83 /*
84  * Get the old snapshot time mapping data from shared memory.
85  */
88 {
89  OldSnapshotTimeMapping *mapping;
90 
91  mapping = palloc(offsetof(OldSnapshotTimeMapping, xid_by_minute)
93  mapping->current_index = 0;
94 
95  LWLockAcquire(OldSnapshotTimeMapLock, LW_SHARED);
99  for (int i = 0; i < OLD_SNAPSHOT_TIME_MAP_ENTRIES; ++i)
101  LWLockRelease(OldSnapshotTimeMapLock);
102 
103  return mapping;
104 }
105 
106 /*
107  * Convert one entry from the old snapshot time mapping to a HeapTuple.
108  */
109 static HeapTuple
111 {
113  bool nulls[NUM_TIME_MAPPING_COLUMNS];
114  int array_position;
116 
117  /*
118  * Figure out the array position corresponding to the current index.
119  *
120  * Index 0 means the oldest entry in the mapping, which is stored at
121  * mapping->head_offset. Index 1 means the next-oldest entry, which is a
122  * the following index, and so on. We wrap around when we reach the end of
123  * the array.
124  */
125  array_position = (mapping->head_offset + mapping->current_index)
127 
128  /*
129  * No explicit timestamp is stored for any entry other than the oldest
130  * one, but each entry corresponds to 1-minute period, so we can just add.
131  */
133  mapping->current_index * 60000);
134 
135  /* Initialize nulls and values arrays. */
136  memset(nulls, 0, sizeof(nulls));
140 
141  return heap_form_tuple(tupdesc, values, nulls);
142 }
Datum array_position(PG_FUNCTION_ARGS)
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:382
uint32 TransactionId
Definition: c.h:636
int64 TimestampTz
Definition: timestamp.h:39
#define ERROR
Definition: elog.h:39
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
int i
Definition: isn.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:117
void * palloc(Size size)
Definition: mcxt.c:1226
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
while(p+4<=pend)
int64 timestamp
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:272
uintptr_t Datum
Definition: postgres.h:64
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
volatile OldSnapshotControlData * oldSnapshotControl
Definition: snapmgr.c:81
#define OLD_SNAPSHOT_TIME_MAP_ENTRIES
Definition: snapmgr.h:32
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112
TimestampTz head_timestamp
Definition: old_snapshot.h:68
TransactionId xid_by_minute[FLEXIBLE_ARRAY_MEMBER]
Definition: old_snapshot.h:70
TransactionId xid_by_minute[FLEXIBLE_ARRAY_MEMBER]
Definition: time_mapping.c:32
TimestampTz head_timestamp
Definition: time_mapping.c:30
#define NUM_TIME_MAPPING_COLUMNS
Definition: time_mapping.c:35
static HeapTuple MakeOldSnapshotTimeMappingTuple(TupleDesc tupdesc, OldSnapshotTimeMapping *mapping)
Definition: time_mapping.c:110
PG_MODULE_MAGIC
Definition: time_mapping.c:37
Datum pg_old_snapshot_time_mapping(PG_FUNCTION_ARGS)
Definition: time_mapping.c:48
static OldSnapshotTimeMapping * GetOldSnapshotTimeMapping(void)
Definition: time_mapping.c:87
PG_FUNCTION_INFO_V1(pg_old_snapshot_time_mapping)
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85