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 "storage/procarray.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

 StaticAssertDecl (MAX_BACKENDS *2<=PG_SNAPSHOT_MAX_NXIP, "possible overflow in pg_current_snapshot()")
 
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, Node *escontext)
 
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 74 of file xid8funcs.c.

◆ PG_SNAPSHOT_SIZE

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

Definition at line 72 of file xid8funcs.c.

◆ USE_BSEARCH_IF_NXIP_GREATER

#define USE_BSEARCH_IF_NXIP_GREATER   30

Definition at line 50 of file xid8funcs.c.

Function Documentation

◆ buf_add_txid()

static void buf_add_txid ( StringInfo  buf,
FullTransactionId  fxid 
)
static

Definition at line 268 of file xid8funcs.c.

269 {
270  pg_snapshot *snap = (pg_snapshot *) buf->data;
271 
272  /* do this before possible realloc */
273  snap->nxip++;
274 
275  appendBinaryStringInfo(buf, &fxid, sizeof(fxid));
276 }
static char * buf
Definition: pg_test_fsync.c:67
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:227
uint32 nxip
Definition: xid8funcs.c:65

References appendBinaryStringInfo(), buf, and pg_snapshot::nxip.

Referenced by parse_snapshot().

◆ buf_finalize()

static pg_snapshot* buf_finalize ( StringInfo  buf)
static

Definition at line 279 of file xid8funcs.c.

280 {
281  pg_snapshot *snap = (pg_snapshot *) buf->data;
282 
283  SET_VARSIZE(snap, buf->len);
284 
285  /* buf is not needed anymore */
286  buf->data = NULL;
287  pfree(buf);
288 
289  return snap;
290 }
void pfree(void *pointer)
Definition: mcxt.c:1456
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References buf, pfree(), and SET_VARSIZE.

Referenced by parse_snapshot().

◆ buf_init()

static StringInfo buf_init ( FullTransactionId  xmin,
FullTransactionId  xmax 
)
static

Definition at line 253 of file xid8funcs.c.

254 {
255  pg_snapshot snap;
256  StringInfo buf;
257 
258  snap.xmin = xmin;
259  snap.xmax = xmax;
260  snap.nxip = 0;
261 
262  buf = makeStringInfo();
264  return buf;
265 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
FullTransactionId xmax
Definition: xid8funcs.c:67
FullTransactionId xmin
Definition: xid8funcs.c:66
#define PG_SNAPSHOT_SIZE(nxip)
Definition: xid8funcs.c:72

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

Referenced by parse_snapshot().

◆ cmp_fxid()

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

Definition at line 184 of file xid8funcs.c.

185 {
186  FullTransactionId a = *(const FullTransactionId *) aa;
187  FullTransactionId b = *(const FullTransactionId *) bb;
188 
190  return -1;
192  return 1;
193  return 0;
194 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51

References a, b, and FullTransactionIdPrecedes.

Referenced by is_visible_fxid(), and sort_snapshot().

◆ is_visible_fxid()

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

Definition at line 218 of file xid8funcs.c.

219 {
221  return true;
222  else if (!FullTransactionIdPrecedes(value, snap->xmax))
223  return false;
224 #ifdef USE_BSEARCH_IF_NXIP_GREATER
225  else if (snap->nxip > USE_BSEARCH_IF_NXIP_GREATER)
226  {
227  void *res;
228 
229  res = bsearch(&value, snap->xip, snap->nxip, sizeof(FullTransactionId),
230  cmp_fxid);
231  /* if found, transaction is still in progress */
232  return (res) ? false : true;
233  }
234 #endif
235  else
236  {
237  uint32 i;
238 
239  for (i = 0; i < snap->nxip; i++)
240  {
241  if (FullTransactionIdEquals(value, snap->xip[i]))
242  return false;
243  }
244  return true;
245  }
246 }
unsigned int uint32
Definition: c.h:490
static struct @147 value
return false
Definition: isn.c:131
int i
Definition: isn.c:73
FullTransactionId xip[FLEXIBLE_ARRAY_MEMBER]
Definition: xid8funcs.c:69
#define FullTransactionIdEquals(a, b)
Definition: transam.h:50
static int cmp_fxid(const void *aa, const void *bb)
Definition: xid8funcs.c:184
#define USE_BSEARCH_IF_NXIP_GREATER
Definition: xid8funcs.c:50

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

Referenced by pg_visible_in_snapshot().

◆ parse_snapshot()

static pg_snapshot* parse_snapshot ( const char *  str,
Node escontext 
)
static

Definition at line 296 of file xid8funcs.c.

297 {
298  FullTransactionId xmin;
299  FullTransactionId xmax;
302  const char *str_start = str;
303  char *endp;
304  StringInfo buf;
305 
306  xmin = FullTransactionIdFromU64(strtou64(str, &endp, 10));
307  if (*endp != ':')
308  goto bad_format;
309  str = endp + 1;
310 
311  xmax = FullTransactionIdFromU64(strtou64(str, &endp, 10));
312  if (*endp != ':')
313  goto bad_format;
314  str = endp + 1;
315 
316  /* it should look sane */
317  if (!FullTransactionIdIsValid(xmin) ||
318  !FullTransactionIdIsValid(xmax) ||
319  FullTransactionIdPrecedes(xmax, xmin))
320  goto bad_format;
321 
322  /* allocate buffer */
323  buf = buf_init(xmin, xmax);
324 
325  /* loop over values */
326  while (*str != '\0')
327  {
328  /* read next value */
329  val = FullTransactionIdFromU64(strtou64(str, &endp, 10));
330  str = endp;
331 
332  /* require the input to be in order */
333  if (FullTransactionIdPrecedes(val, xmin) ||
335  FullTransactionIdPrecedes(val, last_val))
336  goto bad_format;
337 
338  /* skip duplicates */
339  if (!FullTransactionIdEquals(val, last_val))
340  buf_add_txid(buf, val);
341  last_val = val;
342 
343  if (*str == ',')
344  str++;
345  else if (*str != '\0')
346  goto bad_format;
347  }
348 
349  return buf_finalize(buf);
350 
351 bad_format:
352  ereturn(escontext, NULL,
353  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
354  errmsg("invalid input syntax for type %s: \"%s\"",
355  "pg_snapshot", str_start)));
356 }
#define strtou64(str, endptr, base)
Definition: c.h:1303
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
long val
Definition: informix.c:664
static FullTransactionId FullTransactionIdFromU64(uint64 value)
Definition: transam.h:81
#define FullTransactionIdFollowsOrEquals(a, b)
Definition: transam.h:54
#define InvalidFullTransactionId
Definition: transam.h:56
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
static StringInfo buf_init(FullTransactionId xmin, FullTransactionId xmax)
Definition: xid8funcs.c:253
static pg_snapshot * buf_finalize(StringInfo buf)
Definition: xid8funcs.c:279
static void buf_add_txid(StringInfo buf, FullTransactionId fxid)
Definition: xid8funcs.c:268

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

Referenced by pg_snapshot_in().

◆ pg_current_snapshot()

Datum pg_current_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 401 of file xid8funcs.c.

402 {
403  pg_snapshot *snap;
404  uint32 nxip,
405  i;
406  Snapshot cur;
408 
410  if (cur == NULL)
411  elog(ERROR, "no active snapshot set");
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 }
struct cursor * cur
Definition: ecpg.c:28
#define ERROR
Definition: elog.h:39
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc(Size size)
Definition: mcxt.c:1226
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:805
FullTransactionId ReadNextFullTransactionId(void)
Definition: varsup.c:261
static void sort_snapshot(pg_snapshot *snap)
Definition: xid8funcs.c:204
static FullTransactionId widen_snapshot_xid(TransactionId xid, FullTransactionId next_fxid)
Definition: xid8funcs.c:158

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

◆ pg_current_xact_id()

Datum pg_current_xact_id ( PG_FUNCTION_ARGS  )

Definition at line 365 of file xid8funcs.c.

366 {
367  /*
368  * Must prevent during recovery because if an xid is not assigned we try
369  * to assign one, which would fail. Programs already rely on this function
370  * to always return a valid current xid, so we should not change this to
371  * return NULL or similar invalid xid.
372  */
373  PreventCommandDuringRecovery("pg_current_xact_id()");
374 
376 }
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:448
FullTransactionId GetTopFullTransactionId(void)
Definition: xact.c:474
#define PG_RETURN_FULLTRANSACTIONID(X)
Definition: xid8.h:30

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

◆ pg_current_xact_id_if_assigned()

Datum pg_current_xact_id_if_assigned ( PG_FUNCTION_ARGS  )

Definition at line 383 of file xid8funcs.c.

384 {
386 
387  if (!FullTransactionIdIsValid(topfxid))
388  PG_RETURN_NULL();
389 
391 }
#define PG_RETURN_NULL()
Definition: fmgr.h:345
FullTransactionId GetTopFullTransactionIdIfAny(void)
Definition: xact.c:490

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

◆ pg_snapshot_in()

Datum pg_snapshot_in ( PG_FUNCTION_ARGS  )

Definition at line 445 of file xid8funcs.c.

446 {
447  char *str = PG_GETARG_CSTRING(0);
448  pg_snapshot *snap;
449 
450  snap = parse_snapshot(str, fcinfo->context);
451 
452  PG_RETURN_POINTER(snap);
453 }
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
static pg_snapshot * parse_snapshot(const char *str, Node *escontext)
Definition: xid8funcs.c:296

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

◆ pg_snapshot_out()

Datum pg_snapshot_out ( PG_FUNCTION_ARGS  )

Definition at line 461 of file xid8funcs.c.

462 {
465  uint32 i;
466 
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 }
#define UINT64_FORMAT
Definition: c.h:533
#define PG_GETARG_VARLENA_P(n)
Definition: fmgr.h:287
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
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

References appendStringInfo(), appendStringInfoChar(), 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.

◆ pg_snapshot_recv()

Datum pg_snapshot_recv ( PG_FUNCTION_ARGS  )

Definition at line 493 of file xid8funcs.c.

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) ||
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 ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:418
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:456
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_SNAPSHOT_MAX_NXIP
Definition: xid8funcs.c:74

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.

◆ pg_snapshot_send()

Datum pg_snapshot_send ( PG_FUNCTION_ARGS  )

Definition at line 559 of file xid8funcs.c.

560 {
563  uint32 i;
564 
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 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153

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.

◆ pg_snapshot_xip()

Datum pg_snapshot_xip ( PG_FUNCTION_ARGS  )

Definition at line 620 of file xid8funcs.c.

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 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
void * arg
void * user_fctx
Definition: funcapi.h:82
uint64 call_cntr
Definition: funcapi.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define VARSIZE(PTR)
Definition: varatt.h:279
static Datum FullTransactionIdGetDatum(FullTransactionId X)
Definition: xid8.h:24

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.

◆ pg_snapshot_xmax()

Datum pg_snapshot_xmax ( PG_FUNCTION_ARGS  )

Definition at line 607 of file xid8funcs.c.

608 {
610 
612 }

References PG_GETARG_VARLENA_P, PG_RETURN_FULLTRANSACTIONID, and pg_snapshot::xmax.

◆ pg_snapshot_xmin()

Datum pg_snapshot_xmin ( PG_FUNCTION_ARGS  )

Definition at line 594 of file xid8funcs.c.

595 {
597 
599 }

References PG_GETARG_VARLENA_P, PG_RETURN_FULLTRANSACTIONID, and pg_snapshot::xmin.

◆ pg_visible_in_snapshot()

Datum pg_visible_in_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 580 of file xid8funcs.c.

581 {
584 
586 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_FULLTRANSACTIONID(X)
Definition: xid8.h:29
static bool is_visible_fxid(FullTransactionId value, const pg_snapshot *snap)
Definition: xid8funcs.c:218

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

◆ pg_xact_status()

Datum pg_xact_status ( PG_FUNCTION_ARGS  )

Definition at line 665 of file xid8funcs.c.

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 
680  /*
681  * Like when doing visibility checks on a row, check whether the
682  * transaction is still in progress before looking into the CLOG.
683  * Otherwise we would incorrectly return "committed" for a transaction
684  * that is committing and has already updated the CLOG, but hasn't
685  * removed its XID from the proc array yet. (See comment on that race
686  * condition at the top of heapam_visibility.c)
687  */
688  if (TransactionIdIsInProgress(xid))
689  status = "in progress";
690  else if (TransactionIdDidCommit(xid))
691  status = "committed";
692  else
693  {
694  /* it must have aborted or crashed */
695  status = "aborted";
696  }
697  }
698  else
699  {
700  status = NULL;
701  }
702  LWLockRelease(XactTruncationLock);
703 
704  if (status == NULL)
705  PG_RETURN_NULL();
706  else
708 }
uint32 TransactionId
Definition: c.h:636
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:117
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1386
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:126
#define TransactionIdIsValid(xid)
Definition: transam.h:41
text * cstring_to_text(const char *s)
Definition: varlena.c:182
static bool TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
Definition: xid8funcs.c:99

References Assert(), cstring_to_text(), LW_SHARED, LWLockAcquire(), LWLockRelease(), PG_GETARG_FULLTRANSACTIONID, PG_RETURN_NULL, PG_RETURN_TEXT_P, TransactionIdDidCommit(), TransactionIdInRecentPast(), TransactionIdIsInProgress(), and TransactionIdIsValid.

◆ sort_snapshot()

static void sort_snapshot ( pg_snapshot snap)
static

Definition at line 204 of file xid8funcs.c.

205 {
206  if (snap->nxip > 1)
207  {
208  qsort(snap->xip, snap->nxip, sizeof(FullTransactionId), cmp_fxid);
209  snap->nxip = qunique(snap->xip, snap->nxip, sizeof(FullTransactionId),
210  cmp_fxid);
211  }
212 }
#define qsort(a, b, c, d)
Definition: port.h:445
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21

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

Referenced by pg_current_snapshot().

◆ StaticAssertDecl()

StaticAssertDecl ( MAX_BACKENDS *2<=  PG_SNAPSHOT_MAX_NXIP,
"possible overflow in pg_current_snapshot()"   
)

◆ TransactionIdInRecentPast()

static bool TransactionIdInRecentPast ( FullTransactionId  fxid,
TransactionId extracted_xid 
)
static

Definition at line 99 of file xid8funcs.c.

100 {
101  uint32 xid_epoch = EpochFromFullTransactionId(fxid);
103  uint32 now_epoch;
104  TransactionId now_epoch_next_xid;
105  FullTransactionId now_fullxid;
106 
107  now_fullxid = ReadNextFullTransactionId();
108  now_epoch_next_xid = XidFromFullTransactionId(now_fullxid);
109  now_epoch = EpochFromFullTransactionId(now_fullxid);
110 
111  if (extracted_xid != NULL)
112  *extracted_xid = xid;
113 
114  if (!TransactionIdIsValid(xid))
115  return false;
116 
117  /* For non-normal transaction IDs, we can ignore the epoch. */
118  if (!TransactionIdIsNormal(xid))
119  return true;
120 
121  /* If the transaction ID is in the future, throw an error. */
122  if (!FullTransactionIdPrecedes(fxid, now_fullxid))
123  ereport(ERROR,
124  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
125  errmsg("transaction ID %llu is in the future",
126  (unsigned long long) U64FromFullTransactionId(fxid))));
127 
128  /*
129  * ShmemVariableCache->oldestClogXid is protected by XactTruncationLock,
130  * but we don't acquire that lock here. Instead, we require the caller to
131  * acquire it, because the caller is presumably going to look up the
132  * returned XID. If we took and released the lock within this function, a
133  * CLOG truncation could occur before the caller finished with the XID.
134  */
135  Assert(LWLockHeldByMe(XactTruncationLock));
136 
137  /*
138  * If the transaction ID has wrapped around, it's definitely too old to
139  * determine the commit status. Otherwise, we can compare it to
140  * ShmemVariableCache->oldestClogXid to determine whether the relevant
141  * CLOG entry is guaranteed to still exist.
142  */
143  if (xid_epoch + 1 < now_epoch
144  || (xid_epoch + 1 == now_epoch && xid < now_epoch_next_xid)
146  return false;
147 
148  return true;
149 }
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1919
TransactionId oldestClogXid
Definition: transam.h:253
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
VariableCache ShmemVariableCache
Definition: varsup.c:34

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

Referenced by pg_xact_status().

◆ widen_snapshot_xid()

static FullTransactionId widen_snapshot_xid ( TransactionId  xid,
FullTransactionId  next_fxid 
)
static

Definition at line 158 of file xid8funcs.c.

159 {
160  TransactionId next_xid = XidFromFullTransactionId(next_fxid);
162 
163  /* Special transaction ID. */
164  if (!TransactionIdIsNormal(xid))
165  return FullTransactionIdFromEpochAndXid(0, xid);
166 
167  /*
168  * The 64 bit result must be <= next_fxid, since next_fxid hadn't been
169  * issued yet when the snapshot was created. Every TransactionId in the
170  * snapshot must therefore be from the same epoch as next_fxid, or the
171  * epoch before. We know this because next_fxid is never allow to get
172  * more than one epoch ahead of the TransactionIds in any snapshot.
173  */
174  if (xid > next_xid)
175  epoch--;
176 
178 }
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
static const unsigned __int64 epoch

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

Referenced by pg_current_snapshot().