PostgreSQL Source Code  git master
xid8funcs.c File Reference
#include "postgres.h"
#include "access/transam.h"
#include "access/xact.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 72 of file xid8funcs.c.

◆ PG_SNAPSHOT_SIZE

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

Definition at line 70 of file xid8funcs.c.

◆ USE_BSEARCH_IF_NXIP_GREATER

#define USE_BSEARCH_IF_NXIP_GREATER   30

Definition at line 48 of file xid8funcs.c.

Function Documentation

◆ buf_add_txid()

static void buf_add_txid ( StringInfo  buf,
FullTransactionId  fxid 
)
static

Definition at line 274 of file xid8funcs.c.

275 {
276  pg_snapshot *snap = (pg_snapshot *) buf->data;
277 
278  /* do this before possible realloc */
279  snap->nxip++;
280 
281  appendBinaryStringInfo(buf, &fxid, sizeof(fxid));
282 }
static char * buf
Definition: pg_test_fsync.c:73
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:233
uint32 nxip
Definition: xid8funcs.c:63

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

Referenced by parse_snapshot().

◆ buf_finalize()

static pg_snapshot* buf_finalize ( StringInfo  buf)
static

Definition at line 285 of file xid8funcs.c.

286 {
287  pg_snapshot *snap = (pg_snapshot *) buf->data;
288 
289  SET_VARSIZE(snap, buf->len);
290 
291  /* buf is not needed anymore */
292  buf->data = NULL;
293  pfree(buf);
294 
295  return snap;
296 }
void pfree(void *pointer)
Definition: mcxt.c:1508
#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 259 of file xid8funcs.c.

260 {
261  pg_snapshot snap;
262  StringInfo buf;
263 
264  snap.xmin = xmin;
265  snap.xmax = xmax;
266  snap.nxip = 0;
267 
268  buf = makeStringInfo();
270  return buf;
271 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
FullTransactionId xmax
Definition: xid8funcs.c:65
FullTransactionId xmin
Definition: xid8funcs.c:64
#define PG_SNAPSHOT_SIZE(nxip)
Definition: xid8funcs.c:70

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 190 of file xid8funcs.c.

191 {
192  FullTransactionId a = *(const FullTransactionId *) aa;
193  FullTransactionId b = *(const FullTransactionId *) bb;
194 
196  return -1;
198  return 1;
199  return 0;
200 }
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 224 of file xid8funcs.c.

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

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 302 of file xid8funcs.c.

303 {
304  FullTransactionId xmin;
305  FullTransactionId xmax;
308  const char *str_start = str;
309  char *endp;
310  StringInfo buf;
311 
312  xmin = FullTransactionIdFromU64(strtou64(str, &endp, 10));
313  if (*endp != ':')
314  goto bad_format;
315  str = endp + 1;
316 
317  xmax = FullTransactionIdFromU64(strtou64(str, &endp, 10));
318  if (*endp != ':')
319  goto bad_format;
320  str = endp + 1;
321 
322  /* it should look sane */
323  if (!FullTransactionIdIsValid(xmin) ||
324  !FullTransactionIdIsValid(xmax) ||
325  FullTransactionIdPrecedes(xmax, xmin))
326  goto bad_format;
327 
328  /* allocate buffer */
329  buf = buf_init(xmin, xmax);
330 
331  /* loop over values */
332  while (*str != '\0')
333  {
334  /* read next value */
335  val = FullTransactionIdFromU64(strtou64(str, &endp, 10));
336  str = endp;
337 
338  /* require the input to be in order */
339  if (FullTransactionIdPrecedes(val, xmin) ||
341  FullTransactionIdPrecedes(val, last_val))
342  goto bad_format;
343 
344  /* skip duplicates */
345  if (!FullTransactionIdEquals(val, last_val))
346  buf_add_txid(buf, val);
347  last_val = val;
348 
349  if (*str == ',')
350  str++;
351  else if (*str != '\0')
352  goto bad_format;
353  }
354 
355  return buf_finalize(buf);
356 
357 bad_format:
358  ereturn(escontext, NULL,
359  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
360  errmsg("invalid input syntax for type %s: \"%s\"",
361  "pg_snapshot", str_start)));
362 }
#define strtou64(str, endptr, base)
Definition: c.h:1285
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
long val
Definition: informix.c:670
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:259
static pg_snapshot * buf_finalize(StringInfo buf)
Definition: xid8funcs.c:285
static void buf_add_txid(StringInfo buf, FullTransactionId fxid)
Definition: xid8funcs.c:274

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 407 of file xid8funcs.c.

408 {
409  pg_snapshot *snap;
410  uint32 nxip,
411  i;
412  Snapshot cur;
414 
416  if (cur == NULL)
417  elog(ERROR, "no active snapshot set");
418 
419  /* allocate */
420  nxip = cur->xcnt;
421  snap = palloc(PG_SNAPSHOT_SIZE(nxip));
422 
423  /* fill */
424  snap->xmin = widen_snapshot_xid(cur->xmin, next_fxid);
425  snap->xmax = widen_snapshot_xid(cur->xmax, next_fxid);
426  snap->nxip = nxip;
427  for (i = 0; i < nxip; i++)
428  snap->xip[i] = widen_snapshot_xid(cur->xip[i], next_fxid);
429 
430  /*
431  * We want them guaranteed to be in ascending order. This also removes
432  * any duplicate xids. Normally, an XID can only be assigned to one
433  * backend, but when preparing a transaction for two-phase commit, there
434  * is a transient state when both the original backend and the dummy
435  * PGPROC entry reserved for the prepared transaction hold the same XID.
436  */
437  sort_snapshot(snap);
438 
439  /* set size after sorting, because it may have removed duplicate xips */
440  SET_VARSIZE(snap, PG_SNAPSHOT_SIZE(snap->nxip));
441 
442  PG_RETURN_POINTER(snap);
443 }
struct cursor * cur
Definition: ecpg.c:28
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc(Size size)
Definition: mcxt.c:1304
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:770
FullTransactionId ReadNextFullTransactionId(void)
Definition: varsup.c:288
static void sort_snapshot(pg_snapshot *snap)
Definition: xid8funcs.c:210
static FullTransactionId widen_snapshot_xid(TransactionId xid, FullTransactionId next_fxid)
Definition: xid8funcs.c:164

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 371 of file xid8funcs.c.

372 {
373  /*
374  * Must prevent during recovery because if an xid is not assigned we try
375  * to assign one, which would fail. Programs already rely on this function
376  * to always return a valid current xid, so we should not change this to
377  * return NULL or similar invalid xid.
378  */
379  PreventCommandDuringRecovery("pg_current_xact_id()");
380 
382 }
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:441
FullTransactionId GetTopFullTransactionId(void)
Definition: xact.c:475
#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 389 of file xid8funcs.c.

390 {
392 
393  if (!FullTransactionIdIsValid(topfxid))
394  PG_RETURN_NULL();
395 
397 }
#define PG_RETURN_NULL()
Definition: fmgr.h:345
FullTransactionId GetTopFullTransactionIdIfAny(void)
Definition: xact.c:491

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

◆ pg_snapshot_in()

Datum pg_snapshot_in ( PG_FUNCTION_ARGS  )

Definition at line 451 of file xid8funcs.c.

452 {
453  char *str = PG_GETARG_CSTRING(0);
454  pg_snapshot *snap;
455 
456  snap = parse_snapshot(str, fcinfo->context);
457 
458  PG_RETURN_POINTER(snap);
459 }
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
static pg_snapshot * parse_snapshot(const char *str, Node *escontext)
Definition: xid8funcs.c:302

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 467 of file xid8funcs.c.

468 {
471  uint32 i;
472 
474 
479 
480  for (i = 0; i < snap->nxip; i++)
481  {
482  if (i > 0)
483  appendStringInfoChar(&str, ',');
485  U64FromFullTransactionId(snap->xip[i]));
486  }
487 
488  PG_RETURN_CSTRING(str.data);
489 }
#define UINT64_FORMAT
Definition: c.h:536
#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:97
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
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 499 of file xid8funcs.c.

500 {
502  pg_snapshot *snap;
504  int nxip;
505  int i;
506  FullTransactionId xmin;
507  FullTransactionId xmax;
508 
509  /* load and validate nxip */
510  nxip = pq_getmsgint(buf, 4);
511  if (nxip < 0 || nxip > PG_SNAPSHOT_MAX_NXIP)
512  goto bad_format;
513 
514  xmin = FullTransactionIdFromU64((uint64) pq_getmsgint64(buf));
515  xmax = FullTransactionIdFromU64((uint64) pq_getmsgint64(buf));
516  if (!FullTransactionIdIsValid(xmin) ||
517  !FullTransactionIdIsValid(xmax) ||
518  FullTransactionIdPrecedes(xmax, xmin))
519  goto bad_format;
520 
521  snap = palloc(PG_SNAPSHOT_SIZE(nxip));
522  snap->xmin = xmin;
523  snap->xmax = xmax;
524 
525  for (i = 0; i < nxip; i++)
526  {
529 
530  if (FullTransactionIdPrecedes(cur, last) ||
533  goto bad_format;
534 
535  /* skip duplicate xips */
536  if (FullTransactionIdEquals(cur, last))
537  {
538  i--;
539  nxip--;
540  continue;
541  }
542 
543  snap->xip[i] = cur;
544  last = cur;
545  }
546  snap->nxip = nxip;
547  SET_VARSIZE(snap, PG_SNAPSHOT_SIZE(nxip));
548  PG_RETURN_POINTER(snap);
549 
550 bad_format:
551  ereport(ERROR,
552  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
553  errmsg("invalid external pg_snapshot data")));
554  PG_RETURN_POINTER(NULL); /* keep compiler quiet */
555 }
#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:415
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453
StringInfoData * StringInfo
Definition: stringinfo.h:54
#define PG_SNAPSHOT_MAX_NXIP
Definition: xid8funcs.c:72

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 565 of file xid8funcs.c.

566 {
569  uint32 i;
570 
572  pq_sendint32(&buf, snap->nxip);
573  pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xmin));
574  pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xmax));
575  for (i = 0; i < snap->nxip; i++)
576  pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xip[i]));
578 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:152

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 626 of file xid8funcs.c.

627 {
628  FuncCallContext *fctx;
629  pg_snapshot *snap;
631 
632  /* on first call initialize fctx and get copy of snapshot */
633  if (SRF_IS_FIRSTCALL())
634  {
636 
637  fctx = SRF_FIRSTCALL_INIT();
638 
639  /* make a copy of user snapshot */
641  memcpy(snap, arg, VARSIZE(arg));
642 
643  fctx->user_fctx = snap;
644  }
645 
646  /* return values one-by-one */
647  fctx = SRF_PERCALL_SETUP();
648  snap = fctx->user_fctx;
649  if (fctx->call_cntr < snap->nxip)
650  {
651  value = snap->xip[fctx->call_cntr];
653  }
654  else
655  {
656  SRF_RETURN_DONE(fctx);
657  }
658 }
#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:1168
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 613 of file xid8funcs.c.

614 {
616 
618 }

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 600 of file xid8funcs.c.

601 {
603 
605 }

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 586 of file xid8funcs.c.

587 {
590 
592 }
#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:224

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 671 of file xid8funcs.c.

672 {
673  const char *status;
675  TransactionId xid;
676 
677  /*
678  * We must protect against concurrent truncation of clog entries to avoid
679  * an I/O error on SLRU lookup.
680  */
681  LWLockAcquire(XactTruncationLock, LW_SHARED);
682  if (TransactionIdInRecentPast(fxid, &xid))
683  {
685 
686  /*
687  * Like when doing visibility checks on a row, check whether the
688  * transaction is still in progress before looking into the CLOG.
689  * Otherwise we would incorrectly return "committed" for a transaction
690  * that is committing and has already updated the CLOG, but hasn't
691  * removed its XID from the proc array yet. (See comment on that race
692  * condition at the top of heapam_visibility.c)
693  */
694  if (TransactionIdIsInProgress(xid))
695  status = "in progress";
696  else if (TransactionIdDidCommit(xid))
697  status = "committed";
698  else
699  {
700  /* it must have aborted or crashed */
701  status = "aborted";
702  }
703  }
704  else
705  {
706  status = NULL;
707  }
708  LWLockRelease(XactTruncationLock);
709 
710  if (status == NULL)
711  PG_RETURN_NULL();
712  else
714 }
uint32 TransactionId
Definition: c.h:639
#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:1169
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1782
@ LW_SHARED
Definition: lwlock.h:115
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1390
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:184
static bool TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
Definition: xid8funcs.c:97

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 210 of file xid8funcs.c.

211 {
212  if (snap->nxip > 1)
213  {
214  qsort(snap->xip, snap->nxip, sizeof(FullTransactionId), cmp_fxid);
215  snap->nxip = qunique(snap->xip, snap->nxip, sizeof(FullTransactionId),
216  cmp_fxid);
217  }
218 }
#define qsort(a, b, c, d)
Definition: port.h:449
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 97 of file xid8funcs.c.

98 {
100  uint32 now_epoch;
101  TransactionId now_epoch_next_xid;
102  FullTransactionId now_fullxid;
103  TransactionId oldest_xid;
104  FullTransactionId oldest_fxid;
105 
106  now_fullxid = ReadNextFullTransactionId();
107  now_epoch_next_xid = XidFromFullTransactionId(now_fullxid);
108  now_epoch = EpochFromFullTransactionId(now_fullxid);
109 
110  if (extracted_xid != NULL)
111  *extracted_xid = xid;
112 
113  if (!TransactionIdIsValid(xid))
114  return false;
115 
116  /* For non-normal transaction IDs, we can ignore the epoch. */
117  if (!TransactionIdIsNormal(xid))
118  return true;
119 
120  /* If the transaction ID is in the future, throw an error. */
121  if (!FullTransactionIdPrecedes(fxid, now_fullxid))
122  ereport(ERROR,
123  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
124  errmsg("transaction ID %llu is in the future",
125  (unsigned long long) U64FromFullTransactionId(fxid))));
126 
127  /*
128  * TransamVariables->oldestClogXid is protected by XactTruncationLock, but
129  * we don't acquire that lock here. Instead, we require the caller to
130  * acquire it, because the caller is presumably going to look up the
131  * returned XID. If we took and released the lock within this function, a
132  * CLOG truncation could occur before the caller finished with the XID.
133  */
134  Assert(LWLockHeldByMe(XactTruncationLock));
135 
136  /*
137  * If fxid is not older than TransamVariables->oldestClogXid, the relevant
138  * CLOG entry is guaranteed to still exist. Convert
139  * TransamVariables->oldestClogXid into a FullTransactionId to compare it
140  * with fxid. Determine the right epoch knowing that oldest_fxid
141  * shouldn't be more than 2^31 older than now_fullxid.
142  */
143  oldest_xid = TransamVariables->oldestClogXid;
144  Assert(TransactionIdPrecedesOrEquals(oldest_xid, now_epoch_next_xid));
145  if (oldest_xid <= now_epoch_next_xid)
146  {
147  oldest_fxid = FullTransactionIdFromEpochAndXid(now_epoch, oldest_xid);
148  }
149  else
150  {
151  Assert(now_epoch > 0);
152  oldest_fxid = FullTransactionIdFromEpochAndXid(now_epoch - 1, oldest_xid);
153  }
154  return !FullTransactionIdPrecedes(fxid, oldest_fxid);
155 }
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1894
TransactionId oldestClogXid
Definition: transam.h:253
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
TransamVariablesData * TransamVariables
Definition: varsup.c:34

References Assert(), EpochFromFullTransactionId, ereport, errcode(), errmsg(), ERROR, FullTransactionIdFromEpochAndXid(), FullTransactionIdPrecedes, LWLockHeldByMe(), TransamVariablesData::oldestClogXid, ReadNextFullTransactionId(), TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedesOrEquals(), TransamVariables, 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 164 of file xid8funcs.c.

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

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

Referenced by pg_current_snapshot().