PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
reorderbuffer.h File Reference
#include "access/htup_details.h"
#include "lib/ilist.h"
#include "storage/sinval.h"
#include "utils/hsearch.h"
#include "utils/relcache.h"
#include "utils/snapshot.h"
#include "utils/timestamp.h"
Include dependency graph for reorderbuffer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ReorderBufferTupleBuf
 
struct  ReorderBufferChange
 
struct  ReorderBufferTXN
 
struct  ReorderBuffer
 

Macros

#define ReorderBufferTupleBufData(p)   ((HeapTupleHeader) MAXALIGN(((char *) p) + sizeof(ReorderBufferTupleBuf)))
 

Typedefs

typedef struct
ReorderBufferTupleBuf 
ReorderBufferTupleBuf
 
typedef struct ReorderBufferChange ReorderBufferChange
 
typedef struct ReorderBufferTXN ReorderBufferTXN
 
typedef struct ReorderBuffer ReorderBuffer
 
typedef void(* ReorderBufferApplyChangeCB )(ReorderBuffer *rb, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
typedef void(* ReorderBufferBeginCB )(ReorderBuffer *rb, ReorderBufferTXN *txn)
 
typedef void(* ReorderBufferCommitCB )(ReorderBuffer *rb, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
typedef void(* ReorderBufferMessageCB )(ReorderBuffer *rb, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
 

Enumerations

enum  ReorderBufferChangeType {
  REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_MESSAGE,
  REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT, REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID, REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT,
  REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM
}
 

Functions

ReorderBufferReorderBufferAllocate (void)
 
void ReorderBufferFree (ReorderBuffer *)
 
ReorderBufferTupleBufReorderBufferGetTupleBuf (ReorderBuffer *, Size tuple_len)
 
void ReorderBufferReturnTupleBuf (ReorderBuffer *, ReorderBufferTupleBuf *tuple)
 
ReorderBufferChangeReorderBufferGetChange (ReorderBuffer *)
 
void ReorderBufferReturnChange (ReorderBuffer *, ReorderBufferChange *)
 
void ReorderBufferQueueChange (ReorderBuffer *, TransactionId, XLogRecPtr lsn, ReorderBufferChange *)
 
void ReorderBufferQueueMessage (ReorderBuffer *, TransactionId, Snapshot snapshot, XLogRecPtr lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
void ReorderBufferCommit (ReorderBuffer *, TransactionId, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn)
 
void ReorderBufferAssignChild (ReorderBuffer *, TransactionId, TransactionId, XLogRecPtr commit_lsn)
 
void ReorderBufferCommitChild (ReorderBuffer *, TransactionId, TransactionId, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
 
void ReorderBufferAbort (ReorderBuffer *, TransactionId, XLogRecPtr lsn)
 
void ReorderBufferAbortOld (ReorderBuffer *, TransactionId xid)
 
void ReorderBufferForget (ReorderBuffer *, TransactionId, XLogRecPtr lsn)
 
void ReorderBufferSetBaseSnapshot (ReorderBuffer *, TransactionId, XLogRecPtr lsn, struct SnapshotData *snap)
 
void ReorderBufferAddSnapshot (ReorderBuffer *, TransactionId, XLogRecPtr lsn, struct SnapshotData *snap)
 
void ReorderBufferAddNewCommandId (ReorderBuffer *, TransactionId, XLogRecPtr lsn, CommandId cid)
 
void ReorderBufferAddNewTupleCids (ReorderBuffer *, TransactionId, XLogRecPtr lsn, RelFileNode node, ItemPointerData pt, CommandId cmin, CommandId cmax, CommandId combocid)
 
void ReorderBufferAddInvalidations (ReorderBuffer *, TransactionId, XLogRecPtr lsn, Size nmsgs, SharedInvalidationMessage *msgs)
 
void ReorderBufferImmediateInvalidation (ReorderBuffer *, uint32 ninvalidations, SharedInvalidationMessage *invalidations)
 
void ReorderBufferProcessXid (ReorderBuffer *, TransactionId xid, XLogRecPtr lsn)
 
void ReorderBufferXidSetCatalogChanges (ReorderBuffer *, TransactionId xid, XLogRecPtr lsn)
 
bool ReorderBufferXidHasCatalogChanges (ReorderBuffer *, TransactionId xid)
 
bool ReorderBufferXidHasBaseSnapshot (ReorderBuffer *, TransactionId xid)
 
ReorderBufferTXNReorderBufferGetOldestTXN (ReorderBuffer *)
 
void ReorderBufferSetRestartPoint (ReorderBuffer *, XLogRecPtr ptr)
 
void StartupReorderBuffer (void)
 

Macro Definition Documentation

#define ReorderBufferTupleBufData (   p)    ((HeapTupleHeader) MAXALIGN(((char *) p) + sizeof(ReorderBufferTupleBuf)))

Typedef Documentation

Definition at line 266 of file reorderbuffer.h.

typedef void(* ReorderBufferApplyChangeCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)

Definition at line 269 of file reorderbuffer.h.

typedef void(* ReorderBufferBeginCB)(ReorderBuffer *rb, ReorderBufferTXN *txn)

Definition at line 276 of file reorderbuffer.h.

typedef void(* ReorderBufferCommitCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)

Definition at line 281 of file reorderbuffer.h.

typedef void(* ReorderBufferMessageCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)

Definition at line 287 of file reorderbuffer.h.

Enumeration Type Documentation

Enumerator
REORDER_BUFFER_CHANGE_INSERT 
REORDER_BUFFER_CHANGE_UPDATE 
REORDER_BUFFER_CHANGE_DELETE 
REORDER_BUFFER_CHANGE_MESSAGE 
REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT 
REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID 
REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID 
REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT 
REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM 

Definition at line 52 of file reorderbuffer.h.

Function Documentation

void ReorderBufferAbort ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn 
)

Definition at line 1683 of file reorderbuffer.c.

References ReorderBufferTXN::final_lsn, InvalidXLogRecPtr, NULL, ReorderBufferCleanupTXN(), and ReorderBufferTXNByXid().

Referenced by DecodeAbort().

1684 {
1685  ReorderBufferTXN *txn;
1686 
1687  txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1688  false);
1689 
1690  /* unknown, nothing to remove */
1691  if (txn == NULL)
1692  return;
1693 
1694  /* cosmetic... */
1695  txn->final_lsn = lsn;
1696 
1697  /* remove potential on-disk data, and deallocate */
1698  ReorderBufferCleanupTXN(rb, txn);
1699 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr final_lsn
static void ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
#define NULL
Definition: c.h:229
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void ReorderBufferAbortOld ( ReorderBuffer ,
TransactionId  xid 
)

Definition at line 1709 of file reorderbuffer.c.

References dlist_mutable_iter::cur, DEBUG2, dlist_container, dlist_foreach_modify, elog, ReorderBufferCleanupTXN(), ReorderBuffer::toplevel_by_lsn, TransactionIdPrecedes(), and ReorderBufferTXN::xid.

Referenced by DecodeStandbyOp().

1710 {
1711  dlist_mutable_iter it;
1712 
1713  /*
1714  * Iterate through all (potential) toplevel TXNs and abort all that are
1715  * older than what possibly can be running. Once we've found the first
1716  * that is alive we stop, there might be some that acquired an xid earlier
1717  * but started writing later, but it's unlikely and they will cleaned up
1718  * in a later call to ReorderBufferAbortOld().
1719  */
1720  dlist_foreach_modify(it, &rb->toplevel_by_lsn)
1721  {
1722  ReorderBufferTXN *txn;
1723 
1724  txn = dlist_container(ReorderBufferTXN, node, it.cur);
1725 
1726  if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
1727  {
1728  elog(DEBUG2, "aborting old transaction %u", txn->xid);
1729 
1730  /* remove potential on-disk data, and deallocate this tx */
1731  ReorderBufferCleanupTXN(rb, txn);
1732  }
1733  else
1734  return;
1735  }
1736 }
dlist_node * cur
Definition: ilist.h:180
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define DEBUG2
Definition: elog.h:24
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static void ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
TransactionId xid
#define elog
Definition: elog.h:219
void ReorderBufferAddInvalidations ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn,
Size  nmsgs,
SharedInvalidationMessage msgs 
)

Definition at line 1921 of file reorderbuffer.c.

References Assert, ReorderBuffer::context, elog, ERROR, ReorderBufferTXN::invalidations, MemoryContextAlloc(), ReorderBufferTXN::ninvalidations, NULL, and ReorderBufferTXNByXid().

Referenced by DecodeCommit().

1924 {
1925  ReorderBufferTXN *txn;
1926 
1927  txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1928 
1929  if (txn->ninvalidations != 0)
1930  elog(ERROR, "only ever add one set of invalidations");
1931 
1932  Assert(nmsgs > 0);
1933 
1934  txn->ninvalidations = nmsgs;
1936  MemoryContextAlloc(rb->context,
1937  sizeof(SharedInvalidationMessage) * nmsgs);
1938  memcpy(txn->invalidations, msgs,
1939  sizeof(SharedInvalidationMessage) * nmsgs);
1940 }
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
SharedInvalidationMessage * invalidations
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
#define elog
Definition: elog.h:219
void ReorderBufferAddNewCommandId ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn,
CommandId  cid 
)

Definition at line 1877 of file reorderbuffer.c.

References ReorderBufferChange::action, ReorderBufferChange::command_id, ReorderBufferChange::data, REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID, ReorderBufferGetChange(), and ReorderBufferQueueChange().

Referenced by SnapBuildProcessNewCid().

1879 {
1881 
1882  change->data.command_id = cid;
1884 
1885  ReorderBufferQueueChange(rb, xid, lsn, change);
1886 }
union ReorderBufferChange::@93 data
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:77
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change)
void ReorderBufferAddNewTupleCids ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn,
RelFileNode  node,
ItemPointerData  pt,
CommandId  cmin,
CommandId  cmax,
CommandId  combocid 
)

Definition at line 1893 of file reorderbuffer.c.

References ReorderBufferChange::action, ReorderBufferChange::data, dlist_push_tail(), ReorderBufferChange::lsn, ReorderBufferChange::node, ReorderBufferTXN::ntuplecids, NULL, REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID, ReorderBufferGetChange(), ReorderBufferTXNByXid(), ReorderBufferChange::tuplecid, and ReorderBufferTXN::tuplecids.

Referenced by SnapBuildProcessNewCid().

1897 {
1899  ReorderBufferTXN *txn;
1900 
1901  txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1902 
1903  change->data.tuplecid.node = node;
1904  change->data.tuplecid.tid = tid;
1905  change->data.tuplecid.cmin = cmin;
1906  change->data.tuplecid.cmax = cmax;
1907  change->data.tuplecid.combocid = combocid;
1908  change->lsn = lsn;
1910 
1911  dlist_push_tail(&txn->tuplecids, &change->node);
1912  txn->ntuplecids++;
1913 }
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
union ReorderBufferChange::@93 data
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:77
struct ReorderBufferChange::@93::@96 tuplecid
#define NULL
Definition: c.h:229
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
dlist_head tuplecids
void ReorderBufferAddSnapshot ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn,
struct SnapshotData snap 
)
ReorderBuffer* ReorderBufferAllocate ( void  )

Definition at line 224 of file reorderbuffer.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), buffer, ReorderBuffer::by_txn, ReorderBuffer::by_txn_last_txn, ReorderBuffer::by_txn_last_xid, ReorderBuffer::cached_tuplebufs, ReorderBuffer::change_context, ReorderBuffer::context, ReorderBuffer::current_restart_decoding_lsn, CurrentMemoryContext, dlist_init(), HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, InvalidTransactionId, InvalidXLogRecPtr, HASHCTL::keysize, MemoryContextAlloc(), ReorderBuffer::nr_cached_tuplebufs, NULL, ReorderBuffer::outbuf, ReorderBuffer::outbufsize, SLAB_DEFAULT_BLOCK_SIZE, SlabContextCreate(), slist_init(), ReorderBuffer::toplevel_by_lsn, and ReorderBuffer::txn_context.

Referenced by StartupDecodingContext().

225 {
227  HASHCTL hash_ctl;
228  MemoryContext new_ctx;
229 
230  /* allocate memory in own context, to have better accountability */
232  "ReorderBuffer",
234 
235  buffer =
236  (ReorderBuffer *) MemoryContextAlloc(new_ctx, sizeof(ReorderBuffer));
237 
238  memset(&hash_ctl, 0, sizeof(hash_ctl));
239 
240  buffer->context = new_ctx;
241 
242  buffer->change_context = SlabContextCreate(new_ctx,
243  "Change",
245  sizeof(ReorderBufferChange));
246 
247  buffer->txn_context = SlabContextCreate(new_ctx,
248  "TXN",
250  sizeof(ReorderBufferTXN));
251 
252  hash_ctl.keysize = sizeof(TransactionId);
253  hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);
254  hash_ctl.hcxt = buffer->context;
255 
256  buffer->by_txn = hash_create("ReorderBufferByXid", 1000, &hash_ctl,
258 
260  buffer->by_txn_last_txn = NULL;
261 
262  buffer->nr_cached_tuplebufs = 0;
263 
264  buffer->outbuf = NULL;
265  buffer->outbufsize = 0;
266 
268 
269  dlist_init(&buffer->toplevel_by_lsn);
270  slist_init(&buffer->cached_tuplebufs);
271 
272  return buffer;
273 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
uint32 TransactionId
Definition: c.h:397
MemoryContext hcxt
Definition: hsearch.h:78
TransactionId by_txn_last_xid
XLogRecPtr current_restart_decoding_lsn
Size entrysize
Definition: hsearch.h:73
MemoryContext SlabContextCreate(MemoryContext parent, const char *name, Size blockSize, Size chunkSize)
Definition: slab.c:183
MemoryContext change_context
static void slist_init(slist_head *head)
Definition: ilist.h:554
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define InvalidTransactionId
Definition: transam.h:31
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define HASH_BLOBS
Definition: hsearch.h:88
MemoryContext context
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
ReorderBufferTXN * by_txn_last_txn
dlist_head toplevel_by_lsn
Size keysize
Definition: hsearch.h:72
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
#define NULL
Definition: c.h:229
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:211
#define SLAB_DEFAULT_BLOCK_SIZE
Definition: memutils.h:194
Size nr_cached_tuplebufs
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
struct ReorderBufferTXNByIdEnt ReorderBufferTXNByIdEnt
MemoryContext txn_context
slist_head cached_tuplebufs
void ReorderBufferAssignChild ( ReorderBuffer ,
TransactionId  ,
TransactionId  ,
XLogRecPtr  commit_lsn 
)

Definition at line 700 of file reorderbuffer.c.

References Assert, dlist_delete(), dlist_push_tail(), elog, ERROR, ReorderBufferTXN::is_known_as_subxact, ReorderBufferTXN::node, ReorderBufferTXN::nsubtxns, ReorderBufferTXNByXid(), and ReorderBufferTXN::subtxns.

Referenced by DecodeXactOp().

702 {
703  ReorderBufferTXN *txn;
704  ReorderBufferTXN *subtxn;
705  bool new_top;
706  bool new_sub;
707 
708  txn = ReorderBufferTXNByXid(rb, xid, true, &new_top, lsn, true);
709  subtxn = ReorderBufferTXNByXid(rb, subxid, true, &new_sub, lsn, false);
710 
711  if (new_sub)
712  {
713  /*
714  * we assign subtransactions to top level transaction even if we don't
715  * have data for it yet, assignment records frequently reference xids
716  * that have not yet produced any records. Knowing those aren't top
717  * level xids allows us to make processing cheaper in some places.
718  */
719  dlist_push_tail(&txn->subtxns, &subtxn->node);
720  txn->nsubtxns++;
721  }
722  else if (!subtxn->is_known_as_subxact)
723  {
724  subtxn->is_known_as_subxact = true;
725  Assert(subtxn->nsubtxns == 0);
726 
727  /* remove from lsn order list of top-level transactions */
728  dlist_delete(&subtxn->node);
729 
730  /* add to toplevel transaction */
731  dlist_push_tail(&txn->subtxns, &subtxn->node);
732  txn->nsubtxns++;
733  }
734  else if (new_top)
735  {
736  elog(ERROR, "existing subxact assigned to unknown toplevel xact");
737  }
738 }
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
#define ERROR
Definition: elog.h:43
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define Assert(condition)
Definition: c.h:675
dlist_head subtxns
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
#define elog
Definition: elog.h:219
void ReorderBufferCommit ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  commit_lsn,
XLogRecPtr  end_lsn,
TimestampTz  commit_time,
RepOriginId  origin_id,
XLogRecPtr  origin_lsn 
)

Definition at line 1312 of file reorderbuffer.c.

References AbortCurrentTransaction(), ReorderBufferChange::action, ReorderBuffer::apply_change, Assert, ReorderBufferTXN::base_snapshot, ReorderBuffer::begin, BeginInternalSubTransaction(), ReorderBufferChange::command_id, ReorderBuffer::commit, ReorderBufferTXN::commit_time, SnapshotData::copied, SnapshotData::curcid, ReorderBufferChange::data, dlist_delete(), elog, ReorderBufferTXN::end_lsn, ERROR, ReorderBufferTXN::final_lsn, FirstCommandId, GetCurrentTransactionId(), GetCurrentTransactionIdIfAny(), InvalidCommandId, InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, IsToastRelation(), IsTransactionOrTransactionBlock(), ReorderBufferChange::lsn, MAIN_FORKNUM, ReorderBuffer::message, ReorderBufferChange::msg, ReorderBufferTXN::ninvalidations, ReorderBufferChange::node, NULL, ReorderBufferTXN::origin_id, ReorderBufferTXN::origin_lsn, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, RelationData::rd_rel, RelationClose(), RelationIdGetRelation(), RelationIsLogicallyLogged, RelidByRelfilenode(), RELKIND_SEQUENCE, relpathperm, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID, REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT, REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID, REORDER_BUFFER_CHANGE_MESSAGE, REORDER_BUFFER_CHANGE_UPDATE, ReorderBufferBuildTupleCidHash(), ReorderBufferCleanupTXN(), ReorderBufferCopySnap(), ReorderBufferExecuteInvalidations(), ReorderBufferFreeSnap(), ReorderBufferIterTXNFinish(), ReorderBufferIterTXNInit(), ReorderBufferIterTXNNext(), ReorderBufferReturnChange(), ReorderBufferToastAppendChunk(), ReorderBufferToastReplace(), ReorderBufferToastReset(), ReorderBufferTXNByXid(), RollbackAndReleaseCurrentSubTransaction(), SetupHistoricSnapshot(), ReorderBufferChange::snapshot, StartTransactionCommand(), TeardownHistoricSnapshot(), ReorderBufferChange::tp, and ReorderBufferTXN::tuplecid_hash.

Referenced by DecodeCommit().

1316 {
1317  ReorderBufferTXN *txn;
1318  volatile Snapshot snapshot_now;
1319  volatile CommandId command_id = FirstCommandId;
1320  bool using_subtxn;
1321  ReorderBufferIterTXNState *volatile iterstate = NULL;
1322 
1323  txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1324  false);
1325 
1326  /* unknown transaction, nothing to replay */
1327  if (txn == NULL)
1328  return;
1329 
1330  txn->final_lsn = commit_lsn;
1331  txn->end_lsn = end_lsn;
1332  txn->commit_time = commit_time;
1333  txn->origin_id = origin_id;
1334  txn->origin_lsn = origin_lsn;
1335 
1336  /*
1337  * If this transaction didn't have any real changes in our database, it's
1338  * OK not to have a snapshot. Note that ReorderBufferCommitChild will have
1339  * transferred its snapshot to this transaction if it had one and the
1340  * toplevel tx didn't.
1341  */
1342  if (txn->base_snapshot == NULL)
1343  {
1344  Assert(txn->ninvalidations == 0);
1345  ReorderBufferCleanupTXN(rb, txn);
1346  return;
1347  }
1348 
1349  snapshot_now = txn->base_snapshot;
1350 
1351  /* build data to be able to lookup the CommandIds of catalog tuples */
1353 
1354  /* setup the initial snapshot */
1355  SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1356 
1357  /*
1358  * Decoding needs access to syscaches et al., which in turn use
1359  * heavyweight locks and such. Thus we need to have enough state around to
1360  * keep track of those. The easiest way is to simply use a transaction
1361  * internally. That also allows us to easily enforce that nothing writes
1362  * to the database by checking for xid assignments.
1363  *
1364  * When we're called via the SQL SRF there's already a transaction
1365  * started, so start an explicit subtransaction there.
1366  */
1367  using_subtxn = IsTransactionOrTransactionBlock();
1368 
1369  PG_TRY();
1370  {
1371  ReorderBufferChange *change;
1372  ReorderBufferChange *specinsert = NULL;
1373 
1374  if (using_subtxn)
1375  BeginInternalSubTransaction("replay");
1376  else
1378 
1379  rb->begin(rb, txn);
1380 
1381  iterstate = ReorderBufferIterTXNInit(rb, txn);
1382  while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
1383  {
1384  Relation relation = NULL;
1385  Oid reloid;
1386 
1387  switch (change->action)
1388  {
1390 
1391  /*
1392  * Confirmation for speculative insertion arrived. Simply
1393  * use as a normal record. It'll be cleaned up at the end
1394  * of INSERT processing.
1395  */
1396  Assert(specinsert->data.tp.oldtuple == NULL);
1397  change = specinsert;
1399 
1400  /* intentionally fall through */
1404  Assert(snapshot_now);
1405 
1406  reloid = RelidByRelfilenode(change->data.tp.relnode.spcNode,
1407  change->data.tp.relnode.relNode);
1408 
1409  /*
1410  * Catalog tuple without data, emitted while catalog was
1411  * in the process of being rewritten.
1412  */
1413  if (reloid == InvalidOid &&
1414  change->data.tp.newtuple == NULL &&
1415  change->data.tp.oldtuple == NULL)
1416  goto change_done;
1417  else if (reloid == InvalidOid)
1418  elog(ERROR, "could not map filenode \"%s\" to relation OID",
1419  relpathperm(change->data.tp.relnode,
1420  MAIN_FORKNUM));
1421 
1422  relation = RelationIdGetRelation(reloid);
1423 
1424  if (relation == NULL)
1425  elog(ERROR, "could not open relation with OID %u (for filenode \"%s\")",
1426  reloid,
1427  relpathperm(change->data.tp.relnode,
1428  MAIN_FORKNUM));
1429 
1430  if (!RelationIsLogicallyLogged(relation))
1431  goto change_done;
1432 
1433  /*
1434  * For now ignore sequence changes entirely. Most of the
1435  * time they don't log changes using records we
1436  * understand, so it doesn't make sense to handle the few
1437  * cases we do.
1438  */
1439  if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
1440  goto change_done;
1441 
1442  /* user-triggered change */
1443  if (!IsToastRelation(relation))
1444  {
1445  ReorderBufferToastReplace(rb, txn, relation, change);
1446  rb->apply_change(rb, txn, relation, change);
1447 
1448  /*
1449  * Only clear reassembled toast chunks if we're sure
1450  * they're not required anymore. The creator of the
1451  * tuple tells us.
1452  */
1453  if (change->data.tp.clear_toast_afterwards)
1454  ReorderBufferToastReset(rb, txn);
1455  }
1456  /* we're not interested in toast deletions */
1457  else if (change->action == REORDER_BUFFER_CHANGE_INSERT)
1458  {
1459  /*
1460  * Need to reassemble the full toasted Datum in
1461  * memory, to ensure the chunks don't get reused till
1462  * we're done remove it from the list of this
1463  * transaction's changes. Otherwise it will get
1464  * freed/reused while restoring spooled data from
1465  * disk.
1466  */
1467  dlist_delete(&change->node);
1468  ReorderBufferToastAppendChunk(rb, txn, relation,
1469  change);
1470  }
1471 
1472  change_done:
1473 
1474  /*
1475  * Either speculative insertion was confirmed, or it was
1476  * unsuccessful and the record isn't needed anymore.
1477  */
1478  if (specinsert != NULL)
1479  {
1480  ReorderBufferReturnChange(rb, specinsert);
1481  specinsert = NULL;
1482  }
1483 
1484  if (relation != NULL)
1485  {
1486  RelationClose(relation);
1487  relation = NULL;
1488  }
1489  break;
1490 
1492 
1493  /*
1494  * Speculative insertions are dealt with by delaying the
1495  * processing of the insert until the confirmation record
1496  * arrives. For that we simply unlink the record from the
1497  * chain, so it does not get freed/reused while restoring
1498  * spooled data from disk.
1499  *
1500  * This is safe in the face of concurrent catalog changes
1501  * because the relevant relation can't be changed between
1502  * speculative insertion and confirmation due to
1503  * CheckTableNotInUse() and locking.
1504  */
1505 
1506  /* clear out a pending (and thus failed) speculation */
1507  if (specinsert != NULL)
1508  {
1509  ReorderBufferReturnChange(rb, specinsert);
1510  specinsert = NULL;
1511  }
1512 
1513  /* and memorize the pending insertion */
1514  dlist_delete(&change->node);
1515  specinsert = change;
1516  break;
1517 
1519  rb->message(rb, txn, change->lsn, true,
1520  change->data.msg.prefix,
1521  change->data.msg.message_size,
1522  change->data.msg.message);
1523  break;
1524 
1526  /* get rid of the old */
1527  TeardownHistoricSnapshot(false);
1528 
1529  if (snapshot_now->copied)
1530  {
1531  ReorderBufferFreeSnap(rb, snapshot_now);
1532  snapshot_now =
1533  ReorderBufferCopySnap(rb, change->data.snapshot,
1534  txn, command_id);
1535  }
1536 
1537  /*
1538  * Restored from disk, need to be careful not to double
1539  * free. We could introduce refcounting for that, but for
1540  * now this seems infrequent enough not to care.
1541  */
1542  else if (change->data.snapshot->copied)
1543  {
1544  snapshot_now =
1545  ReorderBufferCopySnap(rb, change->data.snapshot,
1546  txn, command_id);
1547  }
1548  else
1549  {
1550  snapshot_now = change->data.snapshot;
1551  }
1552 
1553 
1554  /* and continue with the new one */
1555  SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1556  break;
1557 
1559  Assert(change->data.command_id != InvalidCommandId);
1560 
1561  if (command_id < change->data.command_id)
1562  {
1563  command_id = change->data.command_id;
1564 
1565  if (!snapshot_now->copied)
1566  {
1567  /* we don't use the global one anymore */
1568  snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
1569  txn, command_id);
1570  }
1571 
1572  snapshot_now->curcid = command_id;
1573 
1574  TeardownHistoricSnapshot(false);
1575  SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1576 
1577  /*
1578  * Every time the CommandId is incremented, we could
1579  * see new catalog contents, so execute all
1580  * invalidations.
1581  */
1583  }
1584 
1585  break;
1586 
1588  elog(ERROR, "tuplecid value in changequeue");
1589  break;
1590  }
1591  }
1592 
1593  /*
1594  * There's a speculative insertion remaining, just clean in up, it
1595  * can't have been successful, otherwise we'd gotten a confirmation
1596  * record.
1597  */
1598  if (specinsert)
1599  {
1600  ReorderBufferReturnChange(rb, specinsert);
1601  specinsert = NULL;
1602  }
1603 
1604  /* clean up the iterator */
1605  ReorderBufferIterTXNFinish(rb, iterstate);
1606  iterstate = NULL;
1607 
1608  /* call commit callback */
1609  rb->commit(rb, txn, commit_lsn);
1610 
1611  /* this is just a sanity check against bad output plugin behaviour */
1613  elog(ERROR, "output plugin used XID %u",
1615 
1616  /* cleanup */
1617  TeardownHistoricSnapshot(false);
1618 
1619  /*
1620  * Aborting the current (sub-)transaction as a whole has the right
1621  * semantics. We want all locks acquired in here to be released, not
1622  * reassigned to the parent and we do not want any database access
1623  * have persistent effects.
1624  */
1626 
1627  /* make sure there's no cache pollution */
1629 
1630  if (using_subtxn)
1632 
1633  if (snapshot_now->copied)
1634  ReorderBufferFreeSnap(rb, snapshot_now);
1635 
1636  /* remove potential on-disk data, and deallocate */
1637  ReorderBufferCleanupTXN(rb, txn);
1638  }
1639  PG_CATCH();
1640  {
1641  /* TODO: Encapsulate cleanup from the PG_TRY and PG_CATCH blocks */
1642  if (iterstate)
1643  ReorderBufferIterTXNFinish(rb, iterstate);
1644 
1646 
1647  /*
1648  * Force cache invalidation to happen outside of a valid transaction
1649  * to prevent catalog access as we just caught an error.
1650  */
1652 
1653  /* make sure there's no cache pollution */
1655 
1656  if (using_subtxn)
1658 
1659  if (snapshot_now->copied)
1660  ReorderBufferFreeSnap(rb, snapshot_now);
1661 
1662  /* remove potential on-disk data, and deallocate */
1663  ReorderBufferCleanupTXN(rb, txn);
1664 
1665  PG_RE_THROW();
1666  }
1667  PG_END_TRY();
1668 }
static void ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
uint32 CommandId
Definition: c.h:411
void ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBufferChange *change)
TimestampTz commit_time
void AbortCurrentTransaction(void)
Definition: xact.c:2988
bool IsToastRelation(Relation relation)
Definition: catalog.c:135
#define relpathperm(rnode, forknum)
Definition: relpath.h:67
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Snapshot base_snapshot
static void ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
RepOriginId origin_id
bool copied
Definition: snapshot.h:94
Oid RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
union ReorderBufferChange::@93 data
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4324
static void ReorderBufferIterTXNFinish(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
void TeardownHistoricSnapshot(bool is_error)
Definition: snapmgr.c:1973
#define RelationIsLogicallyLogged(relation)
Definition: rel.h:576
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:77
XLogRecPtr origin_lsn
#define FirstCommandId
Definition: c.h:413
#define ERROR
Definition: elog.h:43
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:417
struct ReorderBufferChange::@93::@94 tp
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4159
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:434
#define InvalidTransactionId
Definition: transam.h:31
XLogRecPtr final_lsn
void RelationClose(Relation relation)
Definition: relcache.c:2156
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static Snapshot ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap, ReorderBufferTXN *txn, CommandId cid)
#define InvalidCommandId
Definition: c.h:414
static void ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
static void ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
#define InvalidOid
Definition: postgres_ext.h:36
CommandId curcid
Definition: snapshot.h:96
static void ReorderBufferExecuteInvalidations(ReorderBuffer *rb, ReorderBufferTXN *txn)
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static void ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn)
static ReorderBufferChange * ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
XLogRecPtr end_lsn
void StartTransactionCommand(void)
Definition: xact.c:2679
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4055
#define PG_RE_THROW()
Definition: elog.h:314
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void SetupHistoricSnapshot(Snapshot historic_snapshot, HTAB *tuplecids)
Definition: snapmgr.c:1957
struct ReorderBufferChange::@93::@95 msg
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static void ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
static ReorderBufferIterTXNState * ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:2067
#define PG_END_TRY()
Definition: elog.h:300
void ReorderBufferCommitChild ( ReorderBuffer ,
TransactionId  ,
TransactionId  ,
XLogRecPtr  commit_lsn,
XLogRecPtr  end_lsn 
)

Definition at line 745 of file reorderbuffer.c.

References Assert, ReorderBufferTXN::base_snapshot, ReorderBufferTXN::base_snapshot_lsn, dlist_delete(), dlist_push_tail(), elog, ReorderBufferTXN::end_lsn, ERROR, ReorderBufferTXN::final_lsn, InvalidXLogRecPtr, ReorderBufferTXN::is_known_as_subxact, ReorderBufferTXN::node, ReorderBufferTXN::nsubtxns, NULL, ReorderBufferTXNByXid(), and ReorderBufferTXN::subtxns.

Referenced by DecodeCommit().

748 {
749  ReorderBufferTXN *txn;
750  ReorderBufferTXN *subtxn;
751 
752  subtxn = ReorderBufferTXNByXid(rb, subxid, false, NULL,
753  InvalidXLogRecPtr, false);
754 
755  /*
756  * No need to do anything if that subtxn didn't contain any changes
757  */
758  if (!subtxn)
759  return;
760 
761  txn = ReorderBufferTXNByXid(rb, xid, false, NULL, commit_lsn, true);
762 
763  if (txn == NULL)
764  elog(ERROR, "subxact logged without previous toplevel record");
765 
766  /*
767  * Pass our base snapshot to the parent transaction if it doesn't have
768  * one, or ours is older. That can happen if there are no changes in the
769  * toplevel transaction but in one of the child transactions. This allows
770  * the parent to simply use its base snapshot initially.
771  */
772  if (subtxn->base_snapshot != NULL &&
773  (txn->base_snapshot == NULL ||
774  txn->base_snapshot_lsn > subtxn->base_snapshot_lsn))
775  {
776  txn->base_snapshot = subtxn->base_snapshot;
777  txn->base_snapshot_lsn = subtxn->base_snapshot_lsn;
778  subtxn->base_snapshot = NULL;
780  }
781 
782  subtxn->final_lsn = commit_lsn;
783  subtxn->end_lsn = end_lsn;
784 
785  if (!subtxn->is_known_as_subxact)
786  {
787  subtxn->is_known_as_subxact = true;
788  Assert(subtxn->nsubtxns == 0);
789 
790  /* remove from lsn order list of top-level transactions */
791  dlist_delete(&subtxn->node);
792 
793  /* add to subtransaction list */
794  dlist_push_tail(&txn->subtxns, &subtxn->node);
795  txn->nsubtxns++;
796  }
797 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Snapshot base_snapshot
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
XLogRecPtr base_snapshot_lsn
#define ERROR
Definition: elog.h:43
XLogRecPtr final_lsn
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
XLogRecPtr end_lsn
dlist_head subtxns
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
#define elog
Definition: elog.h:219
void ReorderBufferForget ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn 
)

Definition at line 1752 of file reorderbuffer.c.

References Assert, ReorderBufferTXN::base_snapshot, ReorderBufferTXN::final_lsn, ReorderBufferTXN::invalidations, InvalidXLogRecPtr, ReorderBufferTXN::ninvalidations, NULL, ReorderBufferCleanupTXN(), ReorderBufferImmediateInvalidation(), and ReorderBufferTXNByXid().

Referenced by DecodeCommit().

1753 {
1754  ReorderBufferTXN *txn;
1755 
1756  txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1757  false);
1758 
1759  /* unknown, nothing to forget */
1760  if (txn == NULL)
1761  return;
1762 
1763  /* cosmetic... */
1764  txn->final_lsn = lsn;
1765 
1766  /*
1767  * Process cache invalidation messages if there are any. Even if we're not
1768  * interested in the transaction's contents, it could have manipulated the
1769  * catalog and we need to update the caches according to that.
1770  */
1771  if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
1773  txn->invalidations);
1774  else
1775  Assert(txn->ninvalidations == 0);
1776 
1777  /* remove potential on-disk data, and deallocate */
1778  ReorderBufferCleanupTXN(rb, txn);
1779 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Snapshot base_snapshot
void ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations, SharedInvalidationMessage *invalidations)
XLogRecPtr final_lsn
static void ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
SharedInvalidationMessage * invalidations
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void ReorderBufferFree ( ReorderBuffer )

Definition at line 279 of file reorderbuffer.c.

References ReorderBuffer::context, and MemoryContextDelete().

Referenced by FreeDecodingContext().

280 {
281  MemoryContext context = rb->context;
282 
283  /*
284  * We free separately allocated data by entirely scrapping reorderbuffer's
285  * memory context.
286  */
287  MemoryContextDelete(context);
288 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
ReorderBufferChange* ReorderBufferGetChange ( ReorderBuffer )

Definition at line 347 of file reorderbuffer.c.

References ReorderBuffer::change_context, and MemoryContextAlloc().

Referenced by DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), ReorderBufferAddNewCommandId(), ReorderBufferAddNewTupleCids(), ReorderBufferAddSnapshot(), ReorderBufferQueueMessage(), and ReorderBufferRestoreChange().

348 {
349  ReorderBufferChange *change;
350 
351  change = (ReorderBufferChange *)
352  MemoryContextAlloc(rb->change_context, sizeof(ReorderBufferChange));
353 
354  memset(change, 0, sizeof(ReorderBufferChange));
355  return change;
356 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
ReorderBufferTXN* ReorderBufferGetOldestTXN ( ReorderBuffer )

Definition at line 677 of file reorderbuffer.c.

References Assert, AssertTXNLsnOrder(), dlist_head_element, dlist_is_empty(), ReorderBufferTXN::first_lsn, InvalidXLogRecPtr, ReorderBufferTXN::is_known_as_subxact, NULL, and ReorderBuffer::toplevel_by_lsn.

Referenced by SnapBuildProcessRunningXacts().

678 {
679  ReorderBufferTXN *txn;
680 
681  if (dlist_is_empty(&rb->toplevel_by_lsn))
682  return NULL;
683 
684  AssertTXNLsnOrder(rb);
685 
686  txn = dlist_head_element(ReorderBufferTXN, node, &rb->toplevel_by_lsn);
687 
690  return txn;
691 }
XLogRecPtr first_lsn
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static void AssertTXNLsnOrder(ReorderBuffer *rb)
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
ReorderBufferTupleBuf* ReorderBufferGetTupleBuf ( ReorderBuffer ,
Size  tuple_len 
)

Definition at line 416 of file reorderbuffer.c.

References ReorderBufferTupleBuf::alloc_tuple_size, Assert, ReorderBuffer::cached_tuplebufs, ReorderBuffer::context, MaxHeapTupleSize, MemoryContextAlloc(), ReorderBuffer::nr_cached_tuplebufs, ReorderBufferTupleBufData, SizeofHeapTupleHeader, slist_container, slist_pop_head_node(), HeapTupleData::t_data, ReorderBufferTupleBuf::tuple, and VALGRIND_MAKE_MEM_UNDEFINED.

Referenced by DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeUpdate(), and ReorderBufferRestoreChange().

417 {
418  ReorderBufferTupleBuf *tuple;
419  Size alloc_len;
420 
421  alloc_len = tuple_len + SizeofHeapTupleHeader;
422 
423  /*
424  * Most tuples are below MaxHeapTupleSize, so we use a slab allocator for
425  * those. Thus always allocate at least MaxHeapTupleSize. Note that tuples
426  * generated for oldtuples can be bigger, as they don't have out-of-line
427  * toast columns.
428  */
429  if (alloc_len < MaxHeapTupleSize)
430  alloc_len = MaxHeapTupleSize;
431 
432 
433  /* if small enough, check the slab cache */
434  if (alloc_len <= MaxHeapTupleSize && rb->nr_cached_tuplebufs)
435  {
436  rb->nr_cached_tuplebufs--;
438  slist_pop_head_node(&rb->cached_tuplebufs));
440 #ifdef USE_ASSERT_CHECKING
441  memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
443 #endif
444  tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
445 #ifdef USE_ASSERT_CHECKING
446  memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
448 #endif
449  }
450  else
451  {
452  tuple = (ReorderBufferTupleBuf *)
453  MemoryContextAlloc(rb->context,
454  sizeof(ReorderBufferTupleBuf) +
455  MAXIMUM_ALIGNOF + alloc_len);
456  tuple->alloc_tuple_size = alloc_len;
457  tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
458  }
459 
460  return tuple;
461 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:170
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
HeapTupleHeader t_data
Definition: htup.h:67
#define ReorderBufferTupleBufData(p)
Definition: reorderbuffer.h:36
#define MaxHeapTupleSize
Definition: htup_details.h:561
HeapTupleData tuple
Definition: reorderbuffer.h:27
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:596
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define Assert(condition)
Definition: c.h:675
size_t Size
Definition: c.h:356
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
void ReorderBufferImmediateInvalidation ( ReorderBuffer ,
uint32  ninvalidations,
SharedInvalidationMessage invalidations 
)

Definition at line 1788 of file reorderbuffer.c.

References AbortCurrentTransaction(), BeginInternalSubTransaction(), i, IsTransactionOrTransactionBlock(), LocalExecuteInvalidationMessage(), and RollbackAndReleaseCurrentSubTransaction().

Referenced by DecodeStandbyOp(), and ReorderBufferForget().

1790 {
1791  bool use_subtxn = IsTransactionOrTransactionBlock();
1792  int i;
1793 
1794  if (use_subtxn)
1795  BeginInternalSubTransaction("replay");
1796 
1797  /*
1798  * Force invalidations to happen outside of a valid transaction - that way
1799  * entries will just be marked as invalid without accessing the catalog.
1800  * That's advantageous because we don't need to setup the full state
1801  * necessary for catalog access.
1802  */
1803  if (use_subtxn)
1805 
1806  for (i = 0; i < ninvalidations; i++)
1807  LocalExecuteInvalidationMessage(&invalidations[i]);
1808 
1809  if (use_subtxn)
1811 }
void AbortCurrentTransaction(void)
Definition: xact.c:2988
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4324
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4159
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4055
void LocalExecuteInvalidationMessage(SharedInvalidationMessage *msg)
Definition: inval.c:554
int i
void ReorderBufferProcessXid ( ReorderBuffer ,
TransactionId  xid,
XLogRecPtr  lsn 
)

Definition at line 1824 of file reorderbuffer.c.

References InvalidTransactionId, NULL, and ReorderBufferTXNByXid().

Referenced by DecodeHeap2Op(), DecodeHeapOp(), DecodeLogicalMsgOp(), DecodeStandbyOp(), DecodeXactOp(), DecodeXLogOp(), and LogicalDecodingProcessRecord().

1825 {
1826  /* many records won't have an xid assigned, centralize check here */
1827  if (xid != InvalidTransactionId)
1828  ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1829 }
#define InvalidTransactionId
Definition: transam.h:31
#define NULL
Definition: c.h:229
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void ReorderBufferQueueChange ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn,
ReorderBufferChange  
)

Definition at line 579 of file reorderbuffer.c.

References Assert, ReorderBufferTXN::changes, dlist_push_tail(), InvalidXLogRecPtr, ReorderBufferChange::lsn, ReorderBufferTXN::nentries, ReorderBufferTXN::nentries_mem, ReorderBufferChange::node, NULL, ReorderBufferCheckSerializeTXN(), and ReorderBufferTXNByXid().

Referenced by DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), ReorderBufferAddNewCommandId(), ReorderBufferAddSnapshot(), and ReorderBufferQueueMessage().

581 {
582  ReorderBufferTXN *txn;
583 
584  txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
585 
586  change->lsn = lsn;
587  Assert(InvalidXLogRecPtr != lsn);
588  dlist_push_tail(&txn->changes, &change->node);
589  txn->nentries++;
590  txn->nentries_mem++;
591 
593 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
dlist_head changes
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
static void ReorderBufferCheckSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
void ReorderBufferQueueMessage ( ReorderBuffer ,
TransactionId  ,
Snapshot  snapshot,
XLogRecPtr  lsn,
bool  transactional,
const char *  prefix,
Size  message_size,
const char *  message 
)

Definition at line 599 of file reorderbuffer.c.

References ReorderBufferChange::action, Assert, ReorderBuffer::context, ReorderBufferChange::data, InvalidTransactionId, MemoryContextSwitchTo(), ReorderBuffer::message, ReorderBufferChange::msg, NULL, palloc(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pstrdup(), REORDER_BUFFER_CHANGE_MESSAGE, ReorderBufferGetChange(), ReorderBufferQueueChange(), ReorderBufferTXNByXid(), SetupHistoricSnapshot(), and TeardownHistoricSnapshot().

Referenced by DecodeLogicalMsgOp().

603 {
604  if (transactional)
605  {
606  MemoryContext oldcontext;
607  ReorderBufferChange *change;
608 
610 
611  oldcontext = MemoryContextSwitchTo(rb->context);
612 
613  change = ReorderBufferGetChange(rb);
615  change->data.msg.prefix = pstrdup(prefix);
616  change->data.msg.message_size = message_size;
617  change->data.msg.message = palloc(message_size);
618  memcpy(change->data.msg.message, message, message_size);
619 
620  ReorderBufferQueueChange(rb, xid, lsn, change);
621 
622  MemoryContextSwitchTo(oldcontext);
623  }
624  else
625  {
626  ReorderBufferTXN *txn = NULL;
627  volatile Snapshot snapshot_now = snapshot;
628 
629  if (xid != InvalidTransactionId)
630  txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
631 
632  /* setup snapshot to allow catalog access */
633  SetupHistoricSnapshot(snapshot_now, NULL);
634  PG_TRY();
635  {
636  rb->message(rb, txn, lsn, false, prefix, message_size, message);
637 
639  }
640  PG_CATCH();
641  {
643  PG_RE_THROW();
644  }
645  PG_END_TRY();
646  }
647 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
union ReorderBufferChange::@93 data
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void TeardownHistoricSnapshot(bool is_error)
Definition: snapmgr.c:1973
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:77
#define InvalidTransactionId
Definition: transam.h:31
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define PG_RE_THROW()
Definition: elog.h:314
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change)
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void * palloc(Size size)
Definition: mcxt.c:849
void SetupHistoricSnapshot(Snapshot historic_snapshot, HTAB *tuplecids)
Definition: snapmgr.c:1957
struct ReorderBufferChange::@93::@95 msg
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
void ReorderBufferReturnChange ( ReorderBuffer ,
ReorderBufferChange  
)

Definition at line 365 of file reorderbuffer.c.

References ReorderBufferChange::action, ReorderBufferChange::data, ReorderBufferChange::msg, NULL, pfree(), REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID, REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT, REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID, REORDER_BUFFER_CHANGE_MESSAGE, REORDER_BUFFER_CHANGE_UPDATE, ReorderBufferFreeSnap(), ReorderBufferReturnTupleBuf(), ReorderBufferChange::snapshot, and ReorderBufferChange::tp.

Referenced by ReorderBufferCleanupTXN(), ReorderBufferCommit(), ReorderBufferIterTXNFinish(), ReorderBufferIterTXNNext(), ReorderBufferRestoreChanges(), ReorderBufferSerializeTXN(), and ReorderBufferToastReset().

366 {
367  /* free contained data */
368  switch (change->action)
369  {
374  if (change->data.tp.newtuple)
375  {
376  ReorderBufferReturnTupleBuf(rb, change->data.tp.newtuple);
377  change->data.tp.newtuple = NULL;
378  }
379 
380  if (change->data.tp.oldtuple)
381  {
382  ReorderBufferReturnTupleBuf(rb, change->data.tp.oldtuple);
383  change->data.tp.oldtuple = NULL;
384  }
385  break;
387  if (change->data.msg.prefix != NULL)
388  pfree(change->data.msg.prefix);
389  change->data.msg.prefix = NULL;
390  if (change->data.msg.message != NULL)
391  pfree(change->data.msg.message);
392  change->data.msg.message = NULL;
393  break;
395  if (change->data.snapshot)
396  {
397  ReorderBufferFreeSnap(rb, change->data.snapshot);
398  change->data.snapshot = NULL;
399  }
400  break;
401  /* no data in addition to the struct itself */
405  break;
406  }
407 
408  pfree(change);
409 }
void pfree(void *pointer)
Definition: mcxt.c:950
#define NULL
Definition: c.h:229
void ReorderBufferReturnTupleBuf(ReorderBuffer *rb, ReorderBufferTupleBuf *tuple)
static void ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
void ReorderBufferReturnTupleBuf ( ReorderBuffer ,
ReorderBufferTupleBuf tuple 
)

Definition at line 470 of file reorderbuffer.c.

References ReorderBufferTupleBuf::alloc_tuple_size, ReorderBuffer::cached_tuplebufs, max_cached_tuplebufs, MaxHeapTupleSize, ReorderBufferTupleBuf::node, ReorderBuffer::nr_cached_tuplebufs, pfree(), slist_push_head(), HeapTupleData::t_data, ReorderBufferTupleBuf::tuple, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_UNDEFINED.

Referenced by ReorderBufferReturnChange().

471 {
472  /* check whether to put into the slab cache, oversized tuples never are */
473  if (tuple->alloc_tuple_size == MaxHeapTupleSize &&
474  rb->nr_cached_tuplebufs < max_cached_tuplebufs)
475  {
476  rb->nr_cached_tuplebufs++;
477  slist_push_head(&rb->cached_tuplebufs, &tuple->node);
480  VALGRIND_MAKE_MEM_DEFINED(&tuple->node, sizeof(tuple->node));
482  }
483  else
484  {
485  pfree(tuple);
486  }
487 }
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
static const Size max_cached_tuplebufs
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
HeapTupleHeader t_data
Definition: htup.h:67
void pfree(void *pointer)
Definition: mcxt.c:950
#define MaxHeapTupleSize
Definition: htup_details.h:561
HeapTupleData tuple
Definition: reorderbuffer.h:27
void ReorderBufferSetBaseSnapshot ( ReorderBuffer ,
TransactionId  ,
XLogRecPtr  lsn,
struct SnapshotData snap 
)
void ReorderBufferSetRestartPoint ( ReorderBuffer ,
XLogRecPtr  ptr 
)

Definition at line 694 of file reorderbuffer.c.

References ReorderBuffer::current_restart_decoding_lsn.

Referenced by SnapBuildRestore(), and SnapBuildSerialize().

695 {
696  rb->current_restart_decoding_lsn = ptr;
697 }
bool ReorderBufferXidHasBaseSnapshot ( ReorderBuffer ,
TransactionId  xid 
)

Definition at line 1990 of file reorderbuffer.c.

References ReorderBufferTXN::base_snapshot, InvalidXLogRecPtr, NULL, and ReorderBufferTXNByXid().

Referenced by SnapBuildCommitTxn(), SnapBuildDistributeNewCatalogSnapshot(), and SnapBuildProcessChange().

1991 {
1992  ReorderBufferTXN *txn;
1993 
1994  txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1995  false);
1996 
1997  /* transaction isn't known yet, ergo no snapshot */
1998  if (txn == NULL)
1999  return false;
2000 
2001  /*
2002  * TODO: It would be a nice improvement if we would check the toplevel
2003  * transaction in subtransactions, but we'd need to keep track of a bit
2004  * more state.
2005  */
2006  return txn->base_snapshot != NULL;
2007 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Snapshot base_snapshot
#define NULL
Definition: c.h:229
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
bool ReorderBufferXidHasCatalogChanges ( ReorderBuffer ,
TransactionId  xid 
)

Definition at line 1974 of file reorderbuffer.c.

References ReorderBufferTXN::has_catalog_changes, InvalidXLogRecPtr, NULL, and ReorderBufferTXNByXid().

Referenced by SnapBuildCommitTxn().

1975 {
1976  ReorderBufferTXN *txn;
1977 
1978  txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1979  false);
1980  if (txn == NULL)
1981  return false;
1982 
1983  return txn->has_catalog_changes;
1984 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define NULL
Definition: c.h:229
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void ReorderBufferXidSetCatalogChanges ( ReorderBuffer ,
TransactionId  xid,
XLogRecPtr  lsn 
)

Definition at line 1959 of file reorderbuffer.c.

References ReorderBufferTXN::has_catalog_changes, NULL, and ReorderBufferTXNByXid().

Referenced by DecodeCommit(), DecodeHeapOp(), and SnapBuildProcessNewCid().

1961 {
1962  ReorderBufferTXN *txn;
1963 
1964  txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1965 
1966  txn->has_catalog_changes = true;
1967 }
#define NULL
Definition: c.h:229
static ReorderBufferTXN * ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create, bool *is_new, XLogRecPtr lsn, bool create_as_top)
void StartupReorderBuffer ( void  )

Definition at line 2604 of file reorderbuffer.c.

References AllocateDir(), dirent::d_name, DEBUG2, ereport, errcode_for_file_access(), errmsg(), FreeDir(), lstat, MAXPGPATH, NULL, PANIC, ReadDir(), ReplicationSlotValidateName(), and unlink().

Referenced by StartupXLOG().

2605 {
2606  DIR *logical_dir;
2607  struct dirent *logical_de;
2608 
2609  DIR *spill_dir;
2610  struct dirent *spill_de;
2611 
2612  logical_dir = AllocateDir("pg_replslot");
2613  while ((logical_de = ReadDir(logical_dir, "pg_replslot")) != NULL)
2614  {
2615  struct stat statbuf;
2616  char path[MAXPGPATH * 2 + 12];
2617 
2618  if (strcmp(logical_de->d_name, ".") == 0 ||
2619  strcmp(logical_de->d_name, "..") == 0)
2620  continue;
2621 
2622  /* if it cannot be a slot, skip the directory */
2623  if (!ReplicationSlotValidateName(logical_de->d_name, DEBUG2))
2624  continue;
2625 
2626  /*
2627  * ok, has to be a surviving logical slot, iterate and delete
2628  * everything starting with xid-*
2629  */
2630  sprintf(path, "pg_replslot/%s", logical_de->d_name);
2631 
2632  /* we're only creating directories here, skip if it's not our's */
2633  if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
2634  continue;
2635 
2636  spill_dir = AllocateDir(path);
2637  while ((spill_de = ReadDir(spill_dir, path)) != NULL)
2638  {
2639  if (strcmp(spill_de->d_name, ".") == 0 ||
2640  strcmp(spill_de->d_name, "..") == 0)
2641  continue;
2642 
2643  /* only look at names that can be ours */
2644  if (strncmp(spill_de->d_name, "xid", 3) == 0)
2645  {
2646  sprintf(path, "pg_replslot/%s/%s", logical_de->d_name,
2647  spill_de->d_name);
2648 
2649  if (unlink(path) != 0)
2650  ereport(PANIC,
2652  errmsg("could not remove file \"%s\": %m",
2653  path)));
2654  }
2655  }
2656  FreeDir(spill_dir);
2657  }
2658  FreeDir(logical_dir);
2659 }
Definition: dirent.h:9
#define PANIC
Definition: elog.h:53
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:173
Definition: dirent.c:25
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2335
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2401
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
#define lstat(path, sb)
Definition: win32.h:262
int FreeDir(DIR *dir)
Definition: fd.c:2444