PostgreSQL Source Code  git master
txid.c File Reference
#include "postgres.h"
#include "access/clog.h"
#include "access/transam.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "funcapi.h"
#include "lib/qunique.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "postmaster/postmaster.h"
#include "storage/lwlock.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
Include dependency graph for txid.c:

Go to the source code of this file.

Data Structures

struct  TxidSnapshot
 
struct  TxidEpoch
 

Macros

#define MAX_TXID   ((uint64) PG_INT64_MAX)
 
#define TXID_FMT   UINT64_FORMAT
 
#define USE_BSEARCH_IF_NXIP_GREATER   30
 
#define TXID_SNAPSHOT_SIZE(nxip)   (offsetof(TxidSnapshot, xip) + sizeof(txid) * (nxip))
 
#define TXID_SNAPSHOT_MAX_NXIP   ((MaxAllocSize - offsetof(TxidSnapshot, xip)) / sizeof(txid))
 

Typedefs

typedef uint64 txid
 

Functions

static void load_xid_epoch (TxidEpoch *state)
 
static bool TransactionIdInRecentPast (uint64 xid_with_epoch, TransactionId *extracted_xid)
 
static txid convert_xid (TransactionId xid, const TxidEpoch *state)
 
static int cmp_txid (const void *aa, const void *bb)
 
static void sort_snapshot (TxidSnapshot *snap)
 
static bool is_visible_txid (txid value, const TxidSnapshot *snap)
 
static StringInfo buf_init (txid xmin, txid xmax)
 
static void buf_add_txid (StringInfo buf, txid xid)
 
static TxidSnapshotbuf_finalize (StringInfo buf)
 
static txid str2txid (const char *s, const char **endp)
 
static TxidSnapshotparse_snapshot (const char *str)
 
Datum txid_current (PG_FUNCTION_ARGS)
 
Datum txid_current_if_assigned (PG_FUNCTION_ARGS)
 
Datum txid_current_snapshot (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_in (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_out (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_recv (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_send (PG_FUNCTION_ARGS)
 
Datum txid_visible_in_snapshot (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_xmin (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_xmax (PG_FUNCTION_ARGS)
 
Datum txid_snapshot_xip (PG_FUNCTION_ARGS)
 
Datum txid_status (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MAX_TXID

#define MAX_TXID   ((uint64) PG_INT64_MAX)

Definition at line 39 of file txid.c.

Referenced by str2txid(), and txid_snapshot_recv().

◆ TXID_FMT

#define TXID_FMT   UINT64_FORMAT

Definition at line 45 of file txid.c.

Referenced by txid_snapshot_out().

◆ TXID_SNAPSHOT_MAX_NXIP

#define TXID_SNAPSHOT_MAX_NXIP   ((MaxAllocSize - offsetof(TxidSnapshot, xip)) / sizeof(txid))

Definition at line 75 of file txid.c.

Referenced by txid_current_snapshot(), and txid_snapshot_recv().

◆ TXID_SNAPSHOT_SIZE

#define TXID_SNAPSHOT_SIZE (   nxip)    (offsetof(TxidSnapshot, xip) + sizeof(txid) * (nxip))

Definition at line 73 of file txid.c.

Referenced by buf_init(), txid_current_snapshot(), and txid_snapshot_recv().

◆ USE_BSEARCH_IF_NXIP_GREATER

#define USE_BSEARCH_IF_NXIP_GREATER   30

Definition at line 51 of file txid.c.

Referenced by is_visible_txid().

Typedef Documentation

◆ txid

typedef uint64 txid

Definition at line 42 of file txid.c.

Function Documentation

◆ buf_add_txid()

static void buf_add_txid ( StringInfo  buf,
txid  xid 
)
static

Definition at line 276 of file txid.c.

References appendBinaryStringInfo(), StringInfoData::data, and TxidSnapshot::nxip.

Referenced by parse_snapshot().

277 {
278  TxidSnapshot *snap = (TxidSnapshot *) buf->data;
279 
280  /* do this before possible realloc */
281  snap->nxip++;
282 
283  appendBinaryStringInfo(buf, (char *) &xid, sizeof(xid));
284 }
uint32 nxip
Definition: txid.c:66
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ buf_finalize()

static TxidSnapshot* buf_finalize ( StringInfo  buf)
static

Definition at line 287 of file txid.c.

References StringInfoData::data, StringInfoData::len, pfree(), and SET_VARSIZE.

Referenced by parse_snapshot().

288 {
289  TxidSnapshot *snap = (TxidSnapshot *) buf->data;
290 
291  SET_VARSIZE(snap, buf->len);
292 
293  /* buf is not needed anymore */
294  buf->data = NULL;
295  pfree(buf);
296 
297  return snap;
298 }
void pfree(void *pointer)
Definition: mcxt.c:1056
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ buf_init()

static StringInfo buf_init ( txid  xmin,
txid  xmax 
)
static

Definition at line 261 of file txid.c.

References appendBinaryStringInfo(), buf, makeStringInfo(), TxidSnapshot::nxip, TXID_SNAPSHOT_SIZE, TxidSnapshot::xmax, and TxidSnapshot::xmin.

Referenced by parse_snapshot().

262 {
263  TxidSnapshot snap;
264  StringInfo buf;
265 
266  snap.xmin = xmin;
267  snap.xmax = xmax;
268  snap.nxip = 0;
269 
270  buf = makeStringInfo();
271  appendBinaryStringInfo(buf, (char *) &snap, TXID_SNAPSHOT_SIZE(0));
272  return buf;
273 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
txid xmin
Definition: txid.c:67
static char * buf
Definition: pg_test_fsync.c:67
#define TXID_SNAPSHOT_SIZE(nxip)
Definition: txid.c:73
uint32 nxip
Definition: txid.c:66
txid xmax
Definition: txid.c:68
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ cmp_txid()

static int cmp_txid ( const void *  aa,
const void *  bb 
)
static

Definition at line 194 of file txid.c.

Referenced by is_visible_txid(), and sort_snapshot().

195 {
196  txid a = *(const txid *) aa;
197  txid b = *(const txid *) bb;
198 
199  if (a < b)
200  return -1;
201  if (a > b)
202  return 1;
203  return 0;
204 }
uint64 txid
Definition: txid.c:42

◆ convert_xid()

static txid convert_xid ( TransactionId  xid,
const TxidEpoch state 
)
static

Definition at line 170 of file txid.c.

References epoch, TxidEpoch::epoch, TxidEpoch::last_xid, TransactionIdFollows(), TransactionIdIsNormal, and TransactionIdPrecedes().

Referenced by txid_current(), txid_current_if_assigned(), and txid_current_snapshot().

171 {
172  uint64 epoch;
173 
174  /* return special xid's as-is */
175  if (!TransactionIdIsNormal(xid))
176  return (txid) xid;
177 
178  /* xid can be on either side when near wrap-around */
179  epoch = (uint64) state->epoch;
180  if (xid > state->last_xid &&
181  TransactionIdPrecedes(xid, state->last_xid))
182  epoch--;
183  else if (xid < state->last_xid &&
184  TransactionIdFollows(xid, state->last_xid))
185  epoch++;
186 
187  return (epoch << 32) | xid;
188 }
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
TransactionId last_xid
Definition: txid.c:83
uint64 txid
Definition: txid.c:42
uint32 epoch
Definition: txid.c:84
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

◆ is_visible_txid()

static bool is_visible_txid ( txid  value,
const TxidSnapshot snap 
)
static

Definition at line 227 of file txid.c.

References cmp_txid(), false, i, TxidSnapshot::nxip, USE_BSEARCH_IF_NXIP_GREATER, TxidSnapshot::xip, and TxidSnapshot::xmax.

Referenced by txid_visible_in_snapshot().

228 {
229  if (value < snap->xmin)
230  return true;
231  else if (value >= snap->xmax)
232  return false;
233 #ifdef USE_BSEARCH_IF_NXIP_GREATER
234  else if (snap->nxip > USE_BSEARCH_IF_NXIP_GREATER)
235  {
236  void *res;
237 
238  res = bsearch(&value, snap->xip, snap->nxip, sizeof(txid), cmp_txid);
239  /* if found, transaction is still in progress */
240  return (res) ? false : true;
241  }
242 #endif
243  else
244  {
245  uint32 i;
246 
247  for (i = 0; i < snap->nxip; i++)
248  {
249  if (value == snap->xip[i])
250  return false;
251  }
252  return true;
253  }
254 }
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
#define USE_BSEARCH_IF_NXIP_GREATER
Definition: txid.c:51
static struct @145 value
#define false
Definition: c.h:325
uint64 txid
Definition: txid.c:42
static int cmp_txid(const void *aa, const void *bb)
Definition: txid.c:194
unsigned int uint32
Definition: c.h:367
int i
uint32 nxip
Definition: txid.c:66
txid xmax
Definition: txid.c:68

◆ load_xid_epoch()

static void load_xid_epoch ( TxidEpoch state)
static

Definition at line 92 of file txid.c.

References TxidEpoch::epoch, EpochFromFullTransactionId, TxidEpoch::last_xid, ReadNextFullTransactionId(), and XidFromFullTransactionId.

Referenced by txid_current(), txid_current_if_assigned(), and txid_current_snapshot().

93 {
95 
96  state->last_xid = XidFromFullTransactionId(fullXid);
97  state->epoch = EpochFromFullTransactionId(fullXid);
98 }
TransactionId last_xid
Definition: txid.c:83
#define XidFromFullTransactionId(x)
Definition: transam.h:48
uint32 epoch
Definition: txid.c:84
FullTransactionId ReadNextFullTransactionId(void)
Definition: varsup.c:246
#define EpochFromFullTransactionId(x)
Definition: transam.h:47

◆ parse_snapshot()

static TxidSnapshot* parse_snapshot ( const char *  str)
static

Definition at line 340 of file txid.c.

References buf, buf_add_txid(), buf_finalize(), buf_init(), ereport, errcode(), errmsg(), ERROR, generate_unaccent_rules::str, str2txid(), and val.

Referenced by txid_snapshot_in().

341 {
342  txid xmin;
343  txid xmax;
344  txid last_val = 0,
345  val;
346  const char *str_start = str;
347  const char *endp;
348  StringInfo buf;
349 
350  xmin = str2txid(str, &endp);
351  if (*endp != ':')
352  goto bad_format;
353  str = endp + 1;
354 
355  xmax = str2txid(str, &endp);
356  if (*endp != ':')
357  goto bad_format;
358  str = endp + 1;
359 
360  /* it should look sane */
361  if (xmin == 0 || xmax == 0 || xmin > xmax)
362  goto bad_format;
363 
364  /* allocate buffer */
365  buf = buf_init(xmin, xmax);
366 
367  /* loop over values */
368  while (*str != '\0')
369  {
370  /* read next value */
371  val = str2txid(str, &endp);
372  str = endp;
373 
374  /* require the input to be in order */
375  if (val < xmin || val >= xmax || val < last_val)
376  goto bad_format;
377 
378  /* skip duplicates */
379  if (val != last_val)
380  buf_add_txid(buf, val);
381  last_val = val;
382 
383  if (*str == ',')
384  str++;
385  else if (*str != '\0')
386  goto bad_format;
387  }
388 
389  return buf_finalize(buf);
390 
391 bad_format:
392  ereport(ERROR,
393  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
394  errmsg("invalid input syntax for type %s: \"%s\"",
395  "txid_snapshot", str_start)));
396  return NULL; /* keep compiler quiet */
397 }
static void buf_add_txid(StringInfo buf, txid xid)
Definition: txid.c:276
static TxidSnapshot * buf_finalize(StringInfo buf)
Definition: txid.c:287
int errcode(int sqlerrcode)
Definition: elog.c:608
uint64 txid
Definition: txid.c:42
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
static StringInfo buf_init(txid xmin, txid xmax)
Definition: txid.c:261
#define ereport(elevel, rest)
Definition: elog.h:141
static txid str2txid(const char *s, const char **endp)
Definition: txid.c:306
int errmsg(const char *fmt,...)
Definition: elog.c:822
long val
Definition: informix.c:664

◆ sort_snapshot()

static void sort_snapshot ( TxidSnapshot snap)
static

Definition at line 214 of file txid.c.

References cmp_txid(), TxidSnapshot::nxip, qsort, qunique(), and TxidSnapshot::xip.

Referenced by txid_current_snapshot().

215 {
216  if (snap->nxip > 1)
217  {
218  qsort(snap->xip, snap->nxip, sizeof(txid), cmp_txid);
219  snap->nxip = qunique(snap->xip, snap->nxip, sizeof(txid), cmp_txid);
220  }
221 }
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
uint64 txid
Definition: txid.c:42
static int cmp_txid(const void *aa, const void *bb)
Definition: txid.c:194
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
uint32 nxip
Definition: txid.c:66
#define qsort(a, b, c, d)
Definition: port.h:474

◆ str2txid()

static txid str2txid ( const char *  s,
const char **  endp 
)
static

Definition at line 306 of file txid.c.

References MAX_TXID, and val.

Referenced by parse_snapshot().

307 {
308  txid val = 0;
309  txid cutoff = MAX_TXID / 10;
310  txid cutlim = MAX_TXID % 10;
311 
312  for (; *s; s++)
313  {
314  unsigned d;
315 
316  if (*s < '0' || *s > '9')
317  break;
318  d = *s - '0';
319 
320  /*
321  * check for overflow
322  */
323  if (val > cutoff || (val == cutoff && d > cutlim))
324  {
325  val = 0;
326  break;
327  }
328 
329  val = val * 10 + d;
330  }
331  if (endp)
332  *endp = s;
333  return val;
334 }
uint64 txid
Definition: txid.c:42
#define MAX_TXID
Definition: txid.c:39
long val
Definition: informix.c:664

◆ TransactionIdInRecentPast()

static bool TransactionIdInRecentPast ( uint64  xid_with_epoch,
TransactionId extracted_xid 
)
static

Definition at line 114 of file txid.c.

References Assert, EpochFromFullTransactionId, ereport, errcode(), errmsg(), ERROR, LWLockHeldByMe(), VariableCacheData::oldestClogXid, psprintf(), ReadNextFullTransactionId(), ShmemVariableCache, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), U64FromFullTransactionId, UINT64_FORMAT, and XidFromFullTransactionId.

Referenced by txid_status().

115 {
116  uint32 xid_epoch = (uint32) (xid_with_epoch >> 32);
117  TransactionId xid = (TransactionId) xid_with_epoch;
118  uint32 now_epoch;
119  TransactionId now_epoch_next_xid;
120  FullTransactionId now_fullxid;
121 
122  now_fullxid = ReadNextFullTransactionId();
123  now_epoch_next_xid = XidFromFullTransactionId(now_fullxid);
124  now_epoch = EpochFromFullTransactionId(now_fullxid);
125 
126  if (extracted_xid != NULL)
127  *extracted_xid = xid;
128 
129  if (!TransactionIdIsValid(xid))
130  return false;
131 
132  /* For non-normal transaction IDs, we can ignore the epoch. */
133  if (!TransactionIdIsNormal(xid))
134  return true;
135 
136  /* If the transaction ID is in the future, throw an error. */
137  if (xid_with_epoch >= U64FromFullTransactionId(now_fullxid))
138  ereport(ERROR,
139  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
140  errmsg("transaction ID %s is in the future",
141  psprintf(UINT64_FORMAT, xid_with_epoch))));
142 
143  /*
144  * ShmemVariableCache->oldestClogXid is protected by CLogTruncationLock,
145  * but we don't acquire that lock here. Instead, we require the caller to
146  * acquire it, because the caller is presumably going to look up the
147  * returned XID. If we took and released the lock within this function, a
148  * CLOG truncation could occur before the caller finished with the XID.
149  */
150  Assert(LWLockHeldByMe(CLogTruncationLock));
151 
152  /*
153  * If the transaction ID has wrapped around, it's definitely too old to
154  * determine the commit status. Otherwise, we can compare it to
155  * ShmemVariableCache->oldestClogXid to determine whether the relevant
156  * CLOG entry is guaranteed to still exist.
157  */
158  if (xid_epoch + 1 < now_epoch
159  || (xid_epoch + 1 == now_epoch && xid < now_epoch_next_xid)
161  return false;
162 
163  return true;
164 }
uint32 TransactionId
Definition: c.h:513
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1843
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:608
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define ERROR
Definition: elog.h:43
TransactionId oldestClogXid
Definition: transam.h:188
VariableCache ShmemVariableCache
Definition: varsup.c:34
unsigned int uint32
Definition: c.h:367
#define ereport(elevel, rest)
Definition: elog.h:141
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
FullTransactionId ReadNextFullTransactionId(void)
Definition: varsup.c:246
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:738
#define U64FromFullTransactionId(x)
Definition: transam.h:49
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
#define UINT64_FORMAT
Definition: c.h:410

◆ txid_current()

Datum txid_current ( PG_FUNCTION_ARGS  )

Definition at line 417 of file txid.c.

References convert_xid(), GetTopTransactionId(), load_xid_epoch(), PG_RETURN_INT64, PreventCommandDuringRecovery(), and val.

418 {
419  txid val;
421 
422  /*
423  * Must prevent during recovery because if an xid is not assigned we try
424  * to assign one, which would fail. Programs already rely on this function
425  * to always return a valid current xid, so we should not change this to
426  * return NULL or similar invalid xid.
427  */
428  PreventCommandDuringRecovery("txid_current()");
429 
430  load_xid_epoch(&state);
431 
432  val = convert_xid(GetTopTransactionId(), &state);
433 
434  PG_RETURN_INT64(val);
435 }
static void load_xid_epoch(TxidEpoch *state)
Definition: txid.c:92
Definition: txid.c:81
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
TransactionId GetTopTransactionId(void)
Definition: xact.c:394
uint64 txid
Definition: txid.c:42
static txid convert_xid(TransactionId xid, const TxidEpoch *state)
Definition: txid.c:170
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:442
ts_parserstate state
Definition: tsquery.c:81
long val
Definition: informix.c:664

◆ txid_current_if_assigned()

Datum txid_current_if_assigned ( PG_FUNCTION_ARGS  )

Definition at line 442 of file txid.c.

References convert_xid(), GetTopTransactionIdIfAny(), InvalidTransactionId, load_xid_epoch(), PG_RETURN_INT64, PG_RETURN_NULL, and val.

443 {
444  txid val;
447 
448  if (topxid == InvalidTransactionId)
449  PG_RETURN_NULL();
450 
451  load_xid_epoch(&state);
452 
453  val = convert_xid(topxid, &state);
454 
455  PG_RETURN_INT64(val);
456 }
static void load_xid_epoch(TxidEpoch *state)
Definition: txid.c:92
Definition: txid.c:81
uint32 TransactionId
Definition: c.h:513
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
uint64 txid
Definition: txid.c:42
static txid convert_xid(TransactionId xid, const TxidEpoch *state)
Definition: txid.c:170
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:409
ts_parserstate state
Definition: tsquery.c:81
long val
Definition: informix.c:664
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ txid_current_snapshot()

Datum txid_current_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 466 of file txid.c.

References convert_xid(), cur, elog, ERROR, GetActiveSnapshot(), i, load_xid_epoch(), MAX_BACKENDS, TxidSnapshot::nxip, palloc(), PG_RETURN_POINTER, SET_VARSIZE, sort_snapshot(), StaticAssertStmt, TXID_SNAPSHOT_MAX_NXIP, TXID_SNAPSHOT_SIZE, SnapshotData::xcnt, TxidSnapshot::xip, SnapshotData::xip, TxidSnapshot::xmax, SnapshotData::xmax, TxidSnapshot::xmin, and SnapshotData::xmin.

467 {
468  TxidSnapshot *snap;
469  uint32 nxip,
470  i;
472  Snapshot cur;
473 
474  cur = GetActiveSnapshot();
475  if (cur == NULL)
476  elog(ERROR, "no active snapshot set");
477 
478  load_xid_epoch(&state);
479 
480  /*
481  * Compile-time limits on the procarray (MAX_BACKENDS processes plus
482  * MAX_BACKENDS prepared transactions) guarantee nxip won't be too large.
483  */
485  "possible overflow in txid_current_snapshot()");
486 
487  /* allocate */
488  nxip = cur->xcnt;
489  snap = palloc(TXID_SNAPSHOT_SIZE(nxip));
490 
491  /* fill */
492  snap->xmin = convert_xid(cur->xmin, &state);
493  snap->xmax = convert_xid(cur->xmax, &state);
494  snap->nxip = nxip;
495  for (i = 0; i < nxip; i++)
496  snap->xip[i] = convert_xid(cur->xip[i], &state);
497 
498  /*
499  * We want them guaranteed to be in ascending order. This also removes
500  * any duplicate xids. Normally, an XID can only be assigned to one
501  * backend, but when preparing a transaction for two-phase commit, there
502  * is a transient state when both the original backend and the dummy
503  * PGPROC entry reserved for the prepared transaction hold the same XID.
504  */
505  sort_snapshot(snap);
506 
507  /* set size after sorting, because it may have removed duplicate xips */
508  SET_VARSIZE(snap, TXID_SNAPSHOT_SIZE(snap->nxip));
509 
510  PG_RETURN_POINTER(snap);
511 }
static void load_xid_epoch(TxidEpoch *state)
Definition: txid.c:92
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
Definition: txid.c:81
static void sort_snapshot(TxidSnapshot *snap)
Definition: txid.c:214
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
struct cursor * cur
Definition: ecpg.c:28
txid xmin
Definition: txid.c:67
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:852
static txid convert_xid(TransactionId xid, const TxidEpoch *state)
Definition: txid.c:170
#define ERROR
Definition: elog.h:43
#define MAX_BACKENDS
Definition: postmaster.h:75
unsigned int uint32
Definition: c.h:367
TransactionId xmax
Definition: snapshot.h:158
TransactionId xmin
Definition: snapshot.h:157
TransactionId * xip
Definition: snapshot.h:168
ts_parserstate state
Definition: tsquery.c:81
#define TXID_SNAPSHOT_SIZE(nxip)
Definition: txid.c:73
uint32 xcnt
Definition: snapshot.h:169
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i
uint32 nxip
Definition: txid.c:66
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
txid xmax
Definition: txid.c:68
#define TXID_SNAPSHOT_MAX_NXIP
Definition: txid.c:75

◆ txid_snapshot_in()

Datum txid_snapshot_in ( PG_FUNCTION_ARGS  )

Definition at line 519 of file txid.c.

References parse_snapshot(), PG_GETARG_CSTRING, PG_RETURN_POINTER, and generate_unaccent_rules::str.

520 {
521  char *str = PG_GETARG_CSTRING(0);
522  TxidSnapshot *snap;
523 
524  snap = parse_snapshot(str);
525 
526  PG_RETURN_POINTER(snap);
527 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
static TxidSnapshot * parse_snapshot(const char *str)
Definition: txid.c:340
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272

◆ txid_snapshot_out()

Datum txid_snapshot_out ( PG_FUNCTION_ARGS  )

Definition at line 535 of file txid.c.

References appendStringInfo(), appendStringInfoChar(), StringInfoData::data, i, initStringInfo(), TxidSnapshot::nxip, PG_GETARG_VARLENA_P, PG_RETURN_CSTRING, generate_unaccent_rules::str, TXID_FMT, TxidSnapshot::xip, TxidSnapshot::xmax, and TxidSnapshot::xmin.

536 {
539  uint32 i;
540 
541  initStringInfo(&str);
542 
543  appendStringInfo(&str, TXID_FMT ":", snap->xmin);
544  appendStringInfo(&str, TXID_FMT ":", snap->xmax);
545 
546  for (i = 0; i < snap->nxip; i++)
547  {
548  if (i > 0)
549  appendStringInfoChar(&str, ',');
550  appendStringInfo(&str, TXID_FMT, snap->xip[i]);
551  }
552 
553  PG_RETURN_CSTRING(str.data);
554 }
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
txid xmin
Definition: txid.c:67
#define TXID_FMT
Definition: txid.c:45
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:281
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
unsigned int uint32
Definition: c.h:367
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
int i
uint32 nxip
Definition: txid.c:66
txid xmax
Definition: txid.c:68

◆ txid_snapshot_recv()

Datum txid_snapshot_recv ( PG_FUNCTION_ARGS  )

Definition at line 564 of file txid.c.

References buf, cur, ereport, errcode(), errmsg(), ERROR, i, MAX_TXID, TxidSnapshot::nxip, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, pq_getmsgint(), pq_getmsgint64(), SET_VARSIZE, TXID_SNAPSHOT_MAX_NXIP, TXID_SNAPSHOT_SIZE, TxidSnapshot::xip, TxidSnapshot::xmax, and TxidSnapshot::xmin.

565 {
567  TxidSnapshot *snap;
568  txid last = 0;
569  int nxip;
570  int i;
571  txid xmin,
572  xmax;
573 
574  /* load and validate nxip */
575  nxip = pq_getmsgint(buf, 4);
576  if (nxip < 0 || nxip > TXID_SNAPSHOT_MAX_NXIP)
577  goto bad_format;
578 
579  xmin = pq_getmsgint64(buf);
580  xmax = pq_getmsgint64(buf);
581  if (xmin == 0 || xmax == 0 || xmin > xmax || xmax > MAX_TXID)
582  goto bad_format;
583 
584  snap = palloc(TXID_SNAPSHOT_SIZE(nxip));
585  snap->xmin = xmin;
586  snap->xmax = xmax;
587 
588  for (i = 0; i < nxip; i++)
589  {
590  txid cur = pq_getmsgint64(buf);
591 
592  if (cur < last || cur < xmin || cur >= xmax)
593  goto bad_format;
594 
595  /* skip duplicate xips */
596  if (cur == last)
597  {
598  i--;
599  nxip--;
600  continue;
601  }
602 
603  snap->xip[i] = cur;
604  last = cur;
605  }
606  snap->nxip = nxip;
607  SET_VARSIZE(snap, TXID_SNAPSHOT_SIZE(nxip));
608  PG_RETURN_POINTER(snap);
609 
610 bad_format:
611  ereport(ERROR,
612  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
613  errmsg("invalid external txid_snapshot data")));
614  PG_RETURN_POINTER(NULL); /* keep compiler quiet */
615 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
StringInfoData * StringInfo
Definition: stringinfo.h:44
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
txid xmin
Definition: txid.c:67
uint64 txid
Definition: txid.c:42
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:141
#define TXID_SNAPSHOT_SIZE(nxip)
Definition: txid.c:73
#define MAX_TXID
Definition: txid.c:39
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
uint32 nxip
Definition: txid.c:66
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
txid xmax
Definition: txid.c:68
#define TXID_SNAPSHOT_MAX_NXIP
Definition: txid.c:75

◆ txid_snapshot_send()

Datum txid_snapshot_send ( PG_FUNCTION_ARGS  )

Definition at line 625 of file txid.c.

References buf, i, TxidSnapshot::nxip, PG_GETARG_VARLENA_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint32(), pq_sendint64(), TxidSnapshot::xip, TxidSnapshot::xmax, and TxidSnapshot::xmin.

626 {
629  uint32 i;
630 
631  pq_begintypsend(&buf);
632  pq_sendint32(&buf, snap->nxip);
633  pq_sendint64(&buf, snap->xmin);
634  pq_sendint64(&buf, snap->xmax);
635  for (i = 0; i < snap->nxip; i++)
636  pq_sendint64(&buf, snap->xip[i]);
638 }
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
txid xmin
Definition: txid.c:67
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:281
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:367
int i
uint32 nxip
Definition: txid.c:66
txid xmax
Definition: txid.c:68

◆ txid_snapshot_xip()

Datum txid_snapshot_xip ( PG_FUNCTION_ARGS  )

Definition at line 686 of file txid.c.

References arg, FuncCallContext::call_cntr, Int64GetDatum(), MemoryContextAlloc(), FuncCallContext::multi_call_memory_ctx, TxidSnapshot::nxip, PG_GETARG_VARLENA_P, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::user_fctx, value, VARSIZE, and TxidSnapshot::xip.

687 {
688  FuncCallContext *fctx;
689  TxidSnapshot *snap;
690  txid value;
691 
692  /* on first call initialize fctx and get copy of snapshot */
693  if (SRF_IS_FIRSTCALL())
694  {
696 
697  fctx = SRF_FIRSTCALL_INIT();
698 
699  /* make a copy of user snapshot */
701  memcpy(snap, arg, VARSIZE(arg));
702 
703  fctx->user_fctx = snap;
704  }
705 
706  /* return values one-by-one */
707  fctx = SRF_PERCALL_SETUP();
708  snap = fctx->user_fctx;
709  if (fctx->call_cntr < snap->nxip)
710  {
711  value = snap->xip[fctx->call_cntr];
712  SRF_RETURN_NEXT(fctx, Int64GetDatum(value));
713  }
714  else
715  {
716  SRF_RETURN_DONE(fctx);
717  }
718 }
uint64 call_cntr
Definition: funcapi.h:65
txid xip[FLEXIBLE_ARRAY_MEMBER]
Definition: txid.c:70
#define VARSIZE(PTR)
Definition: postgres.h:303
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
static struct @145 value
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:286
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:288
uint64 txid
Definition: txid.c:42
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:281
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1699
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
void * user_fctx
Definition: funcapi.h:82
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
uint32 nxip
Definition: txid.c:66
void * arg
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:284

◆ txid_snapshot_xmax()

Datum txid_snapshot_xmax ( PG_FUNCTION_ARGS  )

Definition at line 673 of file txid.c.

References PG_GETARG_VARLENA_P, PG_RETURN_INT64, and TxidSnapshot::xmax.

674 {
676 
677  PG_RETURN_INT64(snap->xmax);
678 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:281
txid xmax
Definition: txid.c:68

◆ txid_snapshot_xmin()

Datum txid_snapshot_xmin ( PG_FUNCTION_ARGS  )

Definition at line 660 of file txid.c.

References PG_GETARG_VARLENA_P, PG_RETURN_INT64, and TxidSnapshot::xmin.

661 {
663 
664  PG_RETURN_INT64(snap->xmin);
665 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
txid xmin
Definition: txid.c:67
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:281

◆ txid_status()

Datum txid_status ( PG_FUNCTION_ARGS  )

Definition at line 731 of file txid.c.

References Assert, cstring_to_text(), GetActiveSnapshot(), LW_SHARED, LWLockAcquire(), LWLockRelease(), PG_GETARG_INT64, PG_RETURN_NULL, PG_RETURN_TEXT_P, status(), TransactionIdDidAbort(), TransactionIdDidCommit(), TransactionIdInRecentPast(), TransactionIdIsCurrentTransactionId(), TransactionIdIsValid, and TransactionIdPrecedes().

732 {
733  const char *status;
734  uint64 xid_with_epoch = PG_GETARG_INT64(0);
735  TransactionId xid;
736 
737  /*
738  * We must protect against concurrent truncation of clog entries to avoid
739  * an I/O error on SLRU lookup.
740  */
741  LWLockAcquire(CLogTruncationLock, LW_SHARED);
742  if (TransactionIdInRecentPast(xid_with_epoch, &xid))
743  {
745 
747  status = "in progress";
748  else if (TransactionIdDidCommit(xid))
749  status = "committed";
750  else if (TransactionIdDidAbort(xid))
751  status = "aborted";
752  else
753  {
754  /*
755  * The xact is not marked as either committed or aborted in clog.
756  *
757  * It could be a transaction that ended without updating clog or
758  * writing an abort record due to a crash. We can safely assume
759  * it's aborted if it isn't committed and is older than our
760  * snapshot xmin.
761  *
762  * Otherwise it must be in-progress (or have been at the time we
763  * checked commit/abort status).
764  */
765  if (TransactionIdPrecedes(xid, GetActiveSnapshot()->xmin))
766  status = "aborted";
767  else
768  status = "in progress";
769  }
770  }
771  else
772  {
773  status = NULL;
774  }
775  LWLockRelease(CLogTruncationLock);
776 
777  if (status == NULL)
778  PG_RETURN_NULL();
779  else
781 }
uint32 TransactionId
Definition: c.h:513
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:853
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1727
static bool TransactionIdInRecentPast(uint64 xid_with_epoch, TransactionId *extracted_xid)
Definition: txid.c:114
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:181
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define Assert(condition)
Definition: c.h:738
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1123
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ txid_visible_in_snapshot()

Datum txid_visible_in_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 646 of file txid.c.

References is_visible_txid(), PG_GETARG_INT64, PG_GETARG_VARLENA_P, PG_RETURN_BOOL, and value.

647 {
650 
651  PG_RETURN_BOOL(is_visible_txid(value, snap));
652 }
static struct @145 value
uint64 txid
Definition: txid.c:42
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:281
static bool is_visible_txid(txid value, const TxidSnapshot *snap)
Definition: txid.c:227
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277