PostgreSQL Source Code  git master
xid8funcs.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 "utils/xid8.h"
Include dependency graph for xid8funcs.c:

Go to the source code of this file.

Data Structures

struct  pg_snapshot
 

Macros

#define USE_BSEARCH_IF_NXIP_GREATER   30
 
#define PG_SNAPSHOT_SIZE(nxip)   (offsetof(pg_snapshot, xip) + sizeof(FullTransactionId) * (nxip))
 
#define PG_SNAPSHOT_MAX_NXIP   ((MaxAllocSize - offsetof(pg_snapshot, xip)) / sizeof(FullTransactionId))
 

Functions

static bool TransactionIdInRecentPast (FullTransactionId fxid, TransactionId *extracted_xid)
 
static FullTransactionId widen_snapshot_xid (TransactionId xid, FullTransactionId next_fxid)
 
static int cmp_fxid (const void *aa, const void *bb)
 
static void sort_snapshot (pg_snapshot *snap)
 
static bool is_visible_fxid (FullTransactionId value, const pg_snapshot *snap)
 
static StringInfo buf_init (FullTransactionId xmin, FullTransactionId xmax)
 
static void buf_add_txid (StringInfo buf, FullTransactionId fxid)
 
static pg_snapshotbuf_finalize (StringInfo buf)
 
static pg_snapshotparse_snapshot (const char *str)
 
Datum pg_current_xact_id (PG_FUNCTION_ARGS)
 
Datum pg_current_xact_id_if_assigned (PG_FUNCTION_ARGS)
 
Datum pg_current_snapshot (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_in (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_out (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_recv (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_send (PG_FUNCTION_ARGS)
 
Datum pg_visible_in_snapshot (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_xmin (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_xmax (PG_FUNCTION_ARGS)
 
Datum pg_snapshot_xip (PG_FUNCTION_ARGS)
 
Datum pg_xact_status (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ PG_SNAPSHOT_MAX_NXIP

#define PG_SNAPSHOT_MAX_NXIP   ((MaxAllocSize - offsetof(pg_snapshot, xip)) / sizeof(FullTransactionId))

Definition at line 73 of file xid8funcs.c.

Referenced by pg_current_snapshot(), and pg_snapshot_recv().

◆ PG_SNAPSHOT_SIZE

#define PG_SNAPSHOT_SIZE (   nxip)    (offsetof(pg_snapshot, xip) + sizeof(FullTransactionId) * (nxip))

Definition at line 71 of file xid8funcs.c.

Referenced by buf_init(), pg_current_snapshot(), and pg_snapshot_recv().

◆ USE_BSEARCH_IF_NXIP_GREATER

#define USE_BSEARCH_IF_NXIP_GREATER   30

Definition at line 49 of file xid8funcs.c.

Referenced by is_visible_fxid().

Function Documentation

◆ buf_add_txid()

static void buf_add_txid ( StringInfo  buf,
FullTransactionId  fxid 
)
static

Definition at line 260 of file xid8funcs.c.

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

Referenced by parse_snapshot().

261 {
262  pg_snapshot *snap = (pg_snapshot *) buf->data;
263 
264  /* do this before possible realloc */
265  snap->nxip++;
266 
267  appendBinaryStringInfo(buf, (char *) &fxid, sizeof(fxid));
268 }
uint32 nxip
Definition: xid8funcs.c:64
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ buf_finalize()

static pg_snapshot* buf_finalize ( StringInfo  buf)
static

Definition at line 271 of file xid8funcs.c.

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

Referenced by parse_snapshot().

272 {
273  pg_snapshot *snap = (pg_snapshot *) buf->data;
274 
275  SET_VARSIZE(snap, buf->len);
276 
277  /* buf is not needed anymore */
278  buf->data = NULL;
279  pfree(buf);
280 
281  return snap;
282 }
void pfree(void *pointer)
Definition: mcxt.c:1057
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ buf_init()

static StringInfo buf_init ( FullTransactionId  xmin,
FullTransactionId  xmax 
)
static

Definition at line 245 of file xid8funcs.c.

References appendBinaryStringInfo(), buf, makeStringInfo(), pg_snapshot::nxip, PG_SNAPSHOT_SIZE, pg_snapshot::xmax, and pg_snapshot::xmin.

Referenced by parse_snapshot().

246 {
247  pg_snapshot snap;
248  StringInfo buf;
249 
250  snap.xmin = xmin;
251  snap.xmax = xmax;
252  snap.nxip = 0;
253 
254  buf = makeStringInfo();
255  appendBinaryStringInfo(buf, (char *) &snap, PG_SNAPSHOT_SIZE(0));
256  return buf;
257 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
FullTransactionId xmax
Definition: xid8funcs.c:66
#define PG_SNAPSHOT_SIZE(nxip)
Definition: xid8funcs.c:71
uint32 nxip
Definition: xid8funcs.c:64
static char * buf
Definition: pg_test_fsync.c:68
FullTransactionId xmin
Definition: xid8funcs.c:65
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ cmp_fxid()

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

Definition at line 176 of file xid8funcs.c.

References FullTransactionIdPrecedes.

Referenced by is_visible_fxid(), and sort_snapshot().

177 {
178  FullTransactionId a = *(const FullTransactionId *) aa;
179  FullTransactionId b = *(const FullTransactionId *) bb;
180 
181  if (FullTransactionIdPrecedes(a, b))
182  return -1;
183  if (FullTransactionIdPrecedes(b, a))
184  return 1;
185  return 0;
186 }
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51

◆ is_visible_fxid()

static bool is_visible_fxid ( FullTransactionId  value,
const pg_snapshot snap 
)
static

Definition at line 210 of file xid8funcs.c.

References cmp_fxid(), false, FullTransactionIdEquals, FullTransactionIdPrecedes, i, pg_snapshot::nxip, USE_BSEARCH_IF_NXIP_GREATER, pg_snapshot::xip, pg_snapshot::xmax, and pg_snapshot::xmin.

Referenced by pg_visible_in_snapshot().

211 {
212  if (FullTransactionIdPrecedes(value, snap->xmin))
213  return true;
214  else if (!FullTransactionIdPrecedes(value, snap->xmax))
215  return false;
216 #ifdef USE_BSEARCH_IF_NXIP_GREATER
217  else if (snap->nxip > USE_BSEARCH_IF_NXIP_GREATER)
218  {
219  void *res;
220 
221  res = bsearch(&value, snap->xip, snap->nxip, sizeof(FullTransactionId),
222  cmp_fxid);
223  /* if found, transaction is still in progress */
224  return (res) ? false : true;
225  }
226 #endif
227  else
228  {
229  uint32 i;
230 
231  for (i = 0; i < snap->nxip; i++)
232  {
233  if (FullTransactionIdEquals(value, snap->xip[i]))
234  return false;
235  }
236  return true;
237  }
238 }
static int cmp_fxid(const void *aa, const void *bb)
Definition: xid8funcs.c:176
#define false
Definition: c.h:333
FullTransactionId xmax
Definition: xid8funcs.c:66
uint32 nxip
Definition: xid8funcs.c:64
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
unsigned int uint32
Definition: c.h:375
#define FullTransactionIdEquals(a, b)
Definition: transam.h:50
#define USE_BSEARCH_IF_NXIP_GREATER
Definition: xid8funcs.c:49
FullTransactionId xmin
Definition: xid8funcs.c:65
int i
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51

◆ parse_snapshot()

static pg_snapshot* parse_snapshot ( const char *  str)
static

Definition at line 288 of file xid8funcs.c.

References buf, buf_add_txid(), buf_finalize(), buf_init(), ereport, errcode(), errmsg(), ERROR, FullTransactionIdEquals, FullTransactionIdFollowsOrEquals, FullTransactionIdFromU64(), FullTransactionIdIsValid, FullTransactionIdPrecedes, InvalidFullTransactionId, pg_strtouint64(), generate_unaccent_rules::str, and val.

Referenced by pg_snapshot_in().

289 {
290  FullTransactionId xmin;
291  FullTransactionId xmax;
294  const char *str_start = str;
295  char *endp;
296  StringInfo buf;
297 
298  xmin = FullTransactionIdFromU64(pg_strtouint64(str, &endp, 10));
299  if (*endp != ':')
300  goto bad_format;
301  str = endp + 1;
302 
303  xmax = FullTransactionIdFromU64(pg_strtouint64(str, &endp, 10));
304  if (*endp != ':')
305  goto bad_format;
306  str = endp + 1;
307 
308  /* it should look sane */
309  if (!FullTransactionIdIsValid(xmin) ||
310  !FullTransactionIdIsValid(xmax) ||
311  FullTransactionIdPrecedes(xmax, xmin))
312  goto bad_format;
313 
314  /* allocate buffer */
315  buf = buf_init(xmin, xmax);
316 
317  /* loop over values */
318  while (*str != '\0')
319  {
320  /* read next value */
321  val = FullTransactionIdFromU64(pg_strtouint64(str, &endp, 10));
322  str = endp;
323 
324  /* require the input to be in order */
325  if (FullTransactionIdPrecedes(val, xmin) ||
327  FullTransactionIdPrecedes(val, last_val))
328  goto bad_format;
329 
330  /* skip duplicates */
331  if (!FullTransactionIdEquals(val, last_val))
332  buf_add_txid(buf, val);
333  last_val = val;
334 
335  if (*str == ',')
336  str++;
337  else if (*str != '\0')
338  goto bad_format;
339  }
340 
341  return buf_finalize(buf);
342 
343 bad_format:
344  ereport(ERROR,
345  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
346  errmsg("invalid input syntax for type %s: \"%s\"",
347  "pg_snapshot", str_start)));
348  return NULL; /* keep compiler quiet */
349 }
static StringInfo buf_init(FullTransactionId xmin, FullTransactionId xmax)
Definition: xid8funcs.c:245
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
int errcode(int sqlerrcode)
Definition: elog.c:610
static FullTransactionId FullTransactionIdFromU64(uint64 value)
Definition: transam.h:81
static void buf_add_txid(StringInfo buf, FullTransactionId fxid)
Definition: xid8funcs.c:260
static pg_snapshot * buf_finalize(StringInfo buf)
Definition: xid8funcs.c:271
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
uint64 pg_strtouint64(const char *str, char **endptr, int base)
Definition: numutils.c:621
#define ereport(elevel,...)
Definition: elog.h:144
#define FullTransactionIdEquals(a, b)
Definition: transam.h:50
#define FullTransactionIdFollowsOrEquals(a, b)
Definition: transam.h:54
#define InvalidFullTransactionId
Definition: transam.h:56
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51
long val
Definition: informix.c:664

◆ pg_current_snapshot()

Datum pg_current_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 394 of file xid8funcs.c.

References cur, elog, ERROR, GetActiveSnapshot(), i, MAX_BACKENDS, pg_snapshot::nxip, palloc(), PG_RETURN_POINTER, PG_SNAPSHOT_MAX_NXIP, PG_SNAPSHOT_SIZE, ReadNextFullTransactionId(), SET_VARSIZE, sort_snapshot(), StaticAssertStmt, widen_snapshot_xid(), SnapshotData::xcnt, pg_snapshot::xip, SnapshotData::xip, pg_snapshot::xmax, SnapshotData::xmax, pg_snapshot::xmin, and SnapshotData::xmin.

395 {
396  pg_snapshot *snap;
397  uint32 nxip,
398  i;
399  Snapshot cur;
401 
402  cur = GetActiveSnapshot();
403  if (cur == NULL)
404  elog(ERROR, "no active snapshot set");
405 
406  /*
407  * Compile-time limits on the procarray (MAX_BACKENDS processes plus
408  * MAX_BACKENDS prepared transactions) guarantee nxip won't be too large.
409  */
411  "possible overflow in pg_current_snapshot()");
412 
413  /* allocate */
414  nxip = cur->xcnt;
415  snap = palloc(PG_SNAPSHOT_SIZE(nxip));
416 
417  /* fill */
418  snap->xmin = widen_snapshot_xid(cur->xmin, next_fxid);
419  snap->xmax = widen_snapshot_xid(cur->xmax, next_fxid);
420  snap->nxip = nxip;
421  for (i = 0; i < nxip; i++)
422  snap->xip[i] = widen_snapshot_xid(cur->xip[i], next_fxid);
423 
424  /*
425  * We want them guaranteed to be in ascending order. This also removes
426  * any duplicate xids. Normally, an XID can only be assigned to one
427  * backend, but when preparing a transaction for two-phase commit, there
428  * is a transient state when both the original backend and the dummy
429  * PGPROC entry reserved for the prepared transaction hold the same XID.
430  */
431  sort_snapshot(snap);
432 
433  /* set size after sorting, because it may have removed duplicate xips */
434  SET_VARSIZE(snap, PG_SNAPSHOT_SIZE(snap->nxip));
435 
436  PG_RETURN_POINTER(snap);
437 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
struct cursor * cur
Definition: ecpg.c:28
static FullTransactionId widen_snapshot_xid(TransactionId xid, FullTransactionId next_fxid)
Definition: xid8funcs.c:150
FullTransactionId xmax
Definition: xid8funcs.c:66
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:860
#define PG_SNAPSHOT_SIZE(nxip)
Definition: xid8funcs.c:71
#define ERROR
Definition: elog.h:43
uint32 nxip
Definition: xid8funcs.c:64
#define MAX_BACKENDS
Definition: postmaster.h:75
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
unsigned int uint32
Definition: c.h:375
TransactionId xmax
Definition: snapshot.h:158
TransactionId xmin
Definition: snapshot.h:157
TransactionId * xip
Definition: snapshot.h:168
FullTransactionId ReadNextFullTransactionId(void)
Definition: varsup.c:261
#define PG_SNAPSHOT_MAX_NXIP
Definition: xid8funcs.c:73
uint32 xcnt
Definition: snapshot.h:169
void * palloc(Size size)
Definition: mcxt.c:950
FullTransactionId xmin
Definition: xid8funcs.c:65
#define elog(elevel,...)
Definition: elog.h:214
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
static void sort_snapshot(pg_snapshot *snap)
Definition: xid8funcs.c:196

◆ pg_current_xact_id()

Datum pg_current_xact_id ( PG_FUNCTION_ARGS  )

Definition at line 358 of file xid8funcs.c.

References GetTopFullTransactionId(), PG_RETURN_FULLTRANSACTIONID, and PreventCommandDuringRecovery().

359 {
360  /*
361  * Must prevent during recovery because if an xid is not assigned we try
362  * to assign one, which would fail. Programs already rely on this function
363  * to always return a valid current xid, so we should not change this to
364  * return NULL or similar invalid xid.
365  */
366  PreventCommandDuringRecovery("pg_current_xact_id()");
367 
369 }
#define PG_RETURN_FULLTRANSACTIONID(X)
Definition: xid8.h:20
FullTransactionId GetTopFullTransactionId(void)
Definition: xact.c:467
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:444

◆ pg_current_xact_id_if_assigned()

Datum pg_current_xact_id_if_assigned ( PG_FUNCTION_ARGS  )

Definition at line 376 of file xid8funcs.c.

References FullTransactionIdIsValid, GetTopFullTransactionIdIfAny(), PG_RETURN_FULLTRANSACTIONID, and PG_RETURN_NULL.

377 {
379 
380  if (!FullTransactionIdIsValid(topfxid))
381  PG_RETURN_NULL();
382 
384 }
#define PG_RETURN_FULLTRANSACTIONID(X)
Definition: xid8.h:20
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId GetTopFullTransactionIdIfAny(void)
Definition: xact.c:483
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_snapshot_in()

Datum pg_snapshot_in ( PG_FUNCTION_ARGS  )

Definition at line 445 of file xid8funcs.c.

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

446 {
447  char *str = PG_GETARG_CSTRING(0);
448  pg_snapshot *snap;
449 
450  snap = parse_snapshot(str);
451 
452  PG_RETURN_POINTER(snap);
453 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
static pg_snapshot * parse_snapshot(const char *str)
Definition: xid8funcs.c:288
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277

◆ pg_snapshot_out()

Datum pg_snapshot_out ( PG_FUNCTION_ARGS  )

Definition at line 461 of file xid8funcs.c.

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

462 {
465  uint32 i;
466 
467  initStringInfo(&str);
468 
473 
474  for (i = 0; i < snap->nxip; i++)
475  {
476  if (i > 0)
477  appendStringInfoChar(&str, ',');
479  U64FromFullTransactionId(snap->xip[i]));
480  }
481 
482  PG_RETURN_CSTRING(str.data);
483 }
FullTransactionId xmax
Definition: xid8funcs.c:66
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:286
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
uint32 nxip
Definition: xid8funcs.c:64
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
unsigned int uint32
Definition: c.h:375
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define U64FromFullTransactionId(x)
Definition: transam.h:49
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
FullTransactionId xmin
Definition: xid8funcs.c:65
int i
#define UINT64_FORMAT
Definition: c.h:418

◆ pg_snapshot_recv()

Datum pg_snapshot_recv ( PG_FUNCTION_ARGS  )

Definition at line 493 of file xid8funcs.c.

References buf, cur, ereport, errcode(), errmsg(), ERROR, FullTransactionIdEquals, FullTransactionIdFromU64(), FullTransactionIdIsValid, FullTransactionIdPrecedes, i, InvalidFullTransactionId, pg_snapshot::nxip, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PG_SNAPSHOT_MAX_NXIP, PG_SNAPSHOT_SIZE, pq_getmsgint(), pq_getmsgint64(), SET_VARSIZE, pg_snapshot::xip, pg_snapshot::xmax, and pg_snapshot::xmin.

494 {
496  pg_snapshot *snap;
498  int nxip;
499  int i;
500  FullTransactionId xmin;
501  FullTransactionId xmax;
502 
503  /* load and validate nxip */
504  nxip = pq_getmsgint(buf, 4);
505  if (nxip < 0 || nxip > PG_SNAPSHOT_MAX_NXIP)
506  goto bad_format;
507 
508  xmin = FullTransactionIdFromU64((uint64) pq_getmsgint64(buf));
509  xmax = FullTransactionIdFromU64((uint64) pq_getmsgint64(buf));
510  if (!FullTransactionIdIsValid(xmin) ||
511  !FullTransactionIdIsValid(xmax) ||
512  FullTransactionIdPrecedes(xmax, xmin))
513  goto bad_format;
514 
515  snap = palloc(PG_SNAPSHOT_SIZE(nxip));
516  snap->xmin = xmin;
517  snap->xmax = xmax;
518 
519  for (i = 0; i < nxip; i++)
520  {
523 
524  if (FullTransactionIdPrecedes(cur, last) ||
525  FullTransactionIdPrecedes(cur, xmin) ||
526  FullTransactionIdPrecedes(xmax, cur))
527  goto bad_format;
528 
529  /* skip duplicate xips */
530  if (FullTransactionIdEquals(cur, last))
531  {
532  i--;
533  nxip--;
534  continue;
535  }
536 
537  snap->xip[i] = cur;
538  last = cur;
539  }
540  snap->nxip = nxip;
541  SET_VARSIZE(snap, PG_SNAPSHOT_SIZE(nxip));
542  PG_RETURN_POINTER(snap);
543 
544 bad_format:
545  ereport(ERROR,
546  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
547  errmsg("invalid external pg_snapshot data")));
548  PG_RETURN_POINTER(NULL); /* keep compiler quiet */
549 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
StringInfoData * StringInfo
Definition: stringinfo.h:44
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static FullTransactionId FullTransactionIdFromU64(uint64 value)
Definition: transam.h:81
FullTransactionId xmax
Definition: xid8funcs.c:66
#define PG_SNAPSHOT_SIZE(nxip)
Definition: xid8funcs.c:71
#define ERROR
Definition: elog.h:43
uint32 nxip
Definition: xid8funcs.c:64
static char * buf
Definition: pg_test_fsync.c:68
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
#define ereport(elevel,...)
Definition: elog.h:144
#define FullTransactionIdEquals(a, b)
Definition: transam.h:50
#define PG_SNAPSHOT_MAX_NXIP
Definition: xid8funcs.c:73
#define InvalidFullTransactionId
Definition: transam.h:56
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
FullTransactionId xmin
Definition: xid8funcs.c:65
int i
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ pg_snapshot_send()

Datum pg_snapshot_send ( PG_FUNCTION_ARGS  )

Definition at line 559 of file xid8funcs.c.

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

560 {
563  uint32 i;
564 
565  pq_begintypsend(&buf);
566  pq_sendint32(&buf, snap->nxip);
567  pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xmin));
568  pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xmax));
569  for (i = 0; i < snap->nxip; i++)
570  pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xip[i]));
572 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
FullTransactionId xmax
Definition: xid8funcs.c:66
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:286
uint32 nxip
Definition: xid8funcs.c:64
static char * buf
Definition: pg_test_fsync.c:68
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
unsigned int uint32
Definition: c.h:375
#define U64FromFullTransactionId(x)
Definition: transam.h:49
FullTransactionId xmin
Definition: xid8funcs.c:65
int i

◆ pg_snapshot_xip()

Datum pg_snapshot_xip ( PG_FUNCTION_ARGS  )

Definition at line 620 of file xid8funcs.c.

References arg, FuncCallContext::call_cntr, FullTransactionIdGetDatum, MemoryContextAlloc(), FuncCallContext::multi_call_memory_ctx, pg_snapshot::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 pg_snapshot::xip.

621 {
622  FuncCallContext *fctx;
623  pg_snapshot *snap;
625 
626  /* on first call initialize fctx and get copy of snapshot */
627  if (SRF_IS_FIRSTCALL())
628  {
630 
631  fctx = SRF_FIRSTCALL_INIT();
632 
633  /* make a copy of user snapshot */
635  memcpy(snap, arg, VARSIZE(arg));
636 
637  fctx->user_fctx = snap;
638  }
639 
640  /* return values one-by-one */
641  fctx = SRF_PERCALL_SETUP();
642  snap = fctx->user_fctx;
643  if (fctx->call_cntr < snap->nxip)
644  {
645  value = snap->xip[fctx->call_cntr];
647  }
648  else
649  {
650  SRF_RETURN_DONE(fctx);
651  }
652 }
uint64 call_cntr
Definition: funcapi.h:65
#define VARSIZE(PTR)
Definition: postgres.h:303
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
#define FullTransactionIdGetDatum(X)
Definition: xid8.h:18
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:286
uint32 nxip
Definition: xid8funcs.c:64
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
static struct @143 value
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:797
void * arg
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ pg_snapshot_xmax()

Datum pg_snapshot_xmax ( PG_FUNCTION_ARGS  )

Definition at line 607 of file xid8funcs.c.

References PG_GETARG_VARLENA_P, PG_RETURN_FULLTRANSACTIONID, and pg_snapshot::xmax.

608 {
610 
612 }
#define PG_RETURN_FULLTRANSACTIONID(X)
Definition: xid8.h:20
FullTransactionId xmax
Definition: xid8funcs.c:66
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:286

◆ pg_snapshot_xmin()

Datum pg_snapshot_xmin ( PG_FUNCTION_ARGS  )

Definition at line 594 of file xid8funcs.c.

References PG_GETARG_VARLENA_P, PG_RETURN_FULLTRANSACTIONID, and pg_snapshot::xmin.

595 {
597 
599 }
#define PG_RETURN_FULLTRANSACTIONID(X)
Definition: xid8.h:20
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:286
FullTransactionId xmin
Definition: xid8funcs.c:65

◆ pg_visible_in_snapshot()

Datum pg_visible_in_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 580 of file xid8funcs.c.

References is_visible_fxid(), PG_GETARG_FULLTRANSACTIONID, PG_GETARG_VARLENA_P, PG_RETURN_BOOL, and value.

581 {
584 
585  PG_RETURN_BOOL(is_visible_fxid(value, snap));
586 }
#define PG_GETARG_FULLTRANSACTIONID(X)
Definition: xid8.h:19
static bool is_visible_fxid(FullTransactionId value, const pg_snapshot *snap)
Definition: xid8funcs.c:210
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:286
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static struct @143 value

◆ pg_xact_status()

Datum pg_xact_status ( PG_FUNCTION_ARGS  )

Definition at line 665 of file xid8funcs.c.

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

666 {
667  const char *status;
669  TransactionId xid;
670 
671  /*
672  * We must protect against concurrent truncation of clog entries to avoid
673  * an I/O error on SLRU lookup.
674  */
675  LWLockAcquire(XactTruncationLock, LW_SHARED);
676  if (TransactionIdInRecentPast(fxid, &xid))
677  {
679 
681  status = "in progress";
682  else if (TransactionIdDidCommit(xid))
683  status = "committed";
684  else if (TransactionIdDidAbort(xid))
685  status = "aborted";
686  else
687  {
688  /*
689  * The xact is not marked as either committed or aborted in clog.
690  *
691  * It could be a transaction that ended without updating clog or
692  * writing an abort record due to a crash. We can safely assume
693  * it's aborted if it isn't committed and is older than our
694  * snapshot xmin.
695  *
696  * Otherwise it must be in-progress (or have been at the time we
697  * checked commit/abort status).
698  */
699  if (TransactionIdPrecedes(xid, GetActiveSnapshot()->xmin))
700  status = "aborted";
701  else
702  status = "in progress";
703  }
704  }
705  else
706  {
707  status = NULL;
708  }
709  LWLockRelease(XactTruncationLock);
710 
711  if (status == NULL)
712  PG_RETURN_NULL();
713  else
715 }
#define PG_GETARG_FULLTRANSACTIONID(X)
Definition: xid8.h:19
uint32 TransactionId
Definition: c.h:521
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:869
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
static bool TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
Definition: xid8funcs.c:90
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:370
text * cstring_to_text(const char *s)
Definition: varlena.c:188
#define Assert(condition)
Definition: c.h:746
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ sort_snapshot()

static void sort_snapshot ( pg_snapshot snap)
static

Definition at line 196 of file xid8funcs.c.

References cmp_fxid(), pg_snapshot::nxip, qsort, qunique(), and pg_snapshot::xip.

Referenced by pg_current_snapshot().

197 {
198  if (snap->nxip > 1)
199  {
200  qsort(snap->xip, snap->nxip, sizeof(FullTransactionId), cmp_fxid);
201  snap->nxip = qunique(snap->xip, snap->nxip, sizeof(FullTransactionId),
202  cmp_fxid);
203  }
204 }
static int cmp_fxid(const void *aa, const void *bb)
Definition: xid8funcs.c:176
uint32 nxip
Definition: xid8funcs.c:64
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:68
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
#define qsort(a, b, c, d)
Definition: port.h:497

◆ TransactionIdInRecentPast()

static bool TransactionIdInRecentPast ( FullTransactionId  fxid,
TransactionId extracted_xid 
)
static

Definition at line 90 of file xid8funcs.c.

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

Referenced by pg_xact_status().

91 {
92  uint32 xid_epoch = EpochFromFullTransactionId(fxid);
94  uint32 now_epoch;
95  TransactionId now_epoch_next_xid;
96  FullTransactionId now_fullxid;
97 
98  now_fullxid = ReadNextFullTransactionId();
99  now_epoch_next_xid = XidFromFullTransactionId(now_fullxid);
100  now_epoch = EpochFromFullTransactionId(now_fullxid);
101 
102  if (extracted_xid != NULL)
103  *extracted_xid = xid;
104 
105  if (!TransactionIdIsValid(xid))
106  return false;
107 
108  /* For non-normal transaction IDs, we can ignore the epoch. */
109  if (!TransactionIdIsNormal(xid))
110  return true;
111 
112  /* If the transaction ID is in the future, throw an error. */
113  if (!FullTransactionIdPrecedes(fxid, now_fullxid))
114  ereport(ERROR,
115  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
116  errmsg("transaction ID %s is in the future",
118  U64FromFullTransactionId(fxid)))));
119 
120  /*
121  * ShmemVariableCache->oldestClogXid is protected by XactTruncationLock,
122  * but we don't acquire that lock here. Instead, we require the caller to
123  * acquire it, because the caller is presumably going to look up the
124  * returned XID. If we took and released the lock within this function, a
125  * CLOG truncation could occur before the caller finished with the XID.
126  */
127  Assert(LWLockHeldByMe(XactTruncationLock));
128 
129  /*
130  * If the transaction ID has wrapped around, it's definitely too old to
131  * determine the commit status. Otherwise, we can compare it to
132  * ShmemVariableCache->oldestClogXid to determine whether the relevant
133  * CLOG entry is guaranteed to still exist.
134  */
135  if (xid_epoch + 1 < now_epoch
136  || (xid_epoch + 1 == now_epoch && xid < now_epoch_next_xid)
138  return false;
139 
140  return true;
141 }
uint32 TransactionId
Definition: c.h:521
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1928
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:610
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define ERROR
Definition: elog.h:43
TransactionId oldestClogXid
Definition: transam.h:246
VariableCache ShmemVariableCache
Definition: varsup.c:34
unsigned int uint32
Definition: c.h:375
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
FullTransactionId ReadNextFullTransactionId(void)
Definition: varsup.c:261
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:746
#define U64FromFullTransactionId(x)
Definition: transam.h:49
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
#define UINT64_FORMAT
Definition: c.h:418

◆ widen_snapshot_xid()

static FullTransactionId widen_snapshot_xid ( TransactionId  xid,
FullTransactionId  next_fxid 
)
static

Definition at line 150 of file xid8funcs.c.

References epoch, EpochFromFullTransactionId, FullTransactionIdFromEpochAndXid(), TransactionIdIsNormal, and XidFromFullTransactionId.

Referenced by pg_current_snapshot().

151 {
152  TransactionId next_xid = XidFromFullTransactionId(next_fxid);
154 
155  /* Special transaction ID. */
156  if (!TransactionIdIsNormal(xid))
157  return FullTransactionIdFromEpochAndXid(0, xid);
158 
159  /*
160  * The 64 bit result must be <= next_fxid, since next_fxid hadn't been
161  * issued yet when the snapshot was created. Every TransactionId in the
162  * snapshot must therefore be from the same epoch as next_fxid, or the
163  * epoch before. We know this because next_fxid is never allow to get
164  * more than one epoch ahead of the TransactionIds in any snapshot.
165  */
166  if (xid > next_xid)
167  epoch--;
168 
169  return FullTransactionIdFromEpochAndXid(epoch, xid);
170 }
uint32 TransactionId
Definition: c.h:521
#define XidFromFullTransactionId(x)
Definition: transam.h:48
unsigned int uint32
Definition: c.h:375
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define TransactionIdIsNormal(xid)
Definition: transam.h:42