PostgreSQL Source Code  git master
generic_xlog.h File Reference
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xloginsert.h"
#include "storage/bufpage.h"
#include "utils/rel.h"
Include dependency graph for generic_xlog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MAX_GENERIC_XLOG_PAGES   XLR_NORMAL_MAX_BLOCK_ID
 
#define GENERIC_XLOG_FULL_IMAGE   0x0001 /* write full-page image */
 

Typedefs

typedef struct GenericXLogState GenericXLogState
 

Functions

GenericXLogStateGenericXLogStart (Relation relation)
 
Page GenericXLogRegisterBuffer (GenericXLogState *state, Buffer buffer, int flags)
 
XLogRecPtr GenericXLogFinish (GenericXLogState *state)
 
void GenericXLogAbort (GenericXLogState *state)
 
void generic_redo (XLogReaderState *record)
 
const char * generic_identify (uint8 info)
 
void generic_desc (StringInfo buf, XLogReaderState *record)
 
void generic_mask (char *pagedata, BlockNumber blkno)
 

Macro Definition Documentation

◆ GENERIC_XLOG_FULL_IMAGE

#define GENERIC_XLOG_FULL_IMAGE   0x0001 /* write full-page image */

Definition at line 26 of file generic_xlog.h.

Referenced by blinsert(), BloomInitMetapage(), flushCachedPage(), and GenericXLogFinish().

◆ MAX_GENERIC_XLOG_PAGES

#define MAX_GENERIC_XLOG_PAGES   XLR_NORMAL_MAX_BLOCK_ID

Typedef Documentation

◆ GenericXLogState

Definition at line 30 of file generic_xlog.h.

Function Documentation

◆ generic_desc()

void generic_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 25 of file genericdesc.c.

References appendStringInfo(), XLogRecGetData, and XLogRecGetDataLen.

26 {
27  Pointer ptr = XLogRecGetData(record),
28  end = ptr + XLogRecGetDataLen(record);
29 
30  while (ptr < end)
31  {
32  OffsetNumber offset,
33  length;
34 
35  memcpy(&offset, ptr, sizeof(offset));
36  ptr += sizeof(offset);
37  memcpy(&length, ptr, sizeof(length));
38  ptr += sizeof(length);
39  ptr += length;
40 
41  if (ptr < end)
42  appendStringInfo(buf, "offset %u, length %u; ", offset, length);
43  else
44  appendStringInfo(buf, "offset %u, length %u", offset, length);
45  }
46 
47  return;
48 }
uint16 OffsetNumber
Definition: off.h:24
char * Pointer
Definition: c.h:335
#define XLogRecGetData(decoder)
Definition: xlogreader.h:246
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:247

◆ generic_identify()

const char* generic_identify ( uint8  info)

Definition at line 55 of file genericdesc.c.

56 {
57  return "Generic";
58 }

◆ generic_mask()

void generic_mask ( char *  pagedata,
BlockNumber  blkno 
)

Definition at line 539 of file generic_xlog.c.

References mask_page_lsn_and_checksum(), and mask_unused_space().

540 {
542 
543  mask_unused_space(page);
544 }
void mask_unused_space(Page page)
Definition: bufmask.c:71
void mask_page_lsn_and_checksum(Page page)
Definition: bufmask.c:31

◆ generic_redo()

void generic_redo ( XLogReaderState record)

Definition at line 478 of file generic_xlog.c.

References generate_unaccent_rules::action, applyPageRedo(), Assert, BLK_NEEDS_REDO, BufferGetPage, BufferIsValid, XLogReaderState::EndRecPtr, InvalidBuffer, MarkBufferDirty(), XLogReaderState::max_block_id, MAX_GENERIC_XLOG_PAGES, PageSetLSN, PageHeaderData::pd_lower, PageHeaderData::pd_upper, UnlockReleaseBuffer(), XLogReadBufferForRedo(), XLogRecGetBlockData(), and XLogRecHasBlockRef.

479 {
480  XLogRecPtr lsn = record->EndRecPtr;
482  uint8 block_id;
483 
484  /* Protect limited size of buffers[] array */
486 
487  /* Iterate over blocks */
488  for (block_id = 0; block_id <= record->max_block_id; block_id++)
489  {
491 
492  if (!XLogRecHasBlockRef(record, block_id))
493  {
494  buffers[block_id] = InvalidBuffer;
495  continue;
496  }
497 
498  action = XLogReadBufferForRedo(record, block_id, &buffers[block_id]);
499 
500  /* Apply redo to given block if needed */
501  if (action == BLK_NEEDS_REDO)
502  {
503  Page page;
504  PageHeader pageHeader;
505  char *blockDelta;
506  Size blockDeltaSize;
507 
508  page = BufferGetPage(buffers[block_id]);
509  blockDelta = XLogRecGetBlockData(record, block_id, &blockDeltaSize);
510  applyPageRedo(page, blockDelta, blockDeltaSize);
511 
512  /*
513  * Since the delta contains no information about what's in the
514  * "hole" between pd_lower and pd_upper, set that to zero to
515  * ensure we produce the same page state that application of the
516  * logged action by GenericXLogFinish did.
517  */
518  pageHeader = (PageHeader) page;
519  memset(page + pageHeader->pd_lower, 0,
520  pageHeader->pd_upper - pageHeader->pd_lower);
521 
522  PageSetLSN(page, lsn);
523  MarkBufferDirty(buffers[block_id]);
524  }
525  }
526 
527  /* Changes are done: unlock and release all buffers */
528  for (block_id = 0; block_id <= record->max_block_id; block_id++)
529  {
530  if (BufferIsValid(buffers[block_id]))
531  UnlockReleaseBuffer(buffers[block_id]);
532  }
533 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
unsigned char uint8
Definition: c.h:356
#define InvalidBuffer
Definition: buf.h:25
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:249
XLogRecPtr EndRecPtr
Definition: xlogreader.h:133
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1391
static void applyPageRedo(Page page, const char *delta, Size deltaSize)
Definition: generic_xlog.c:453
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:289
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
XLogRedoAction
Definition: xlogutils.h:27
size_t Size
Definition: c.h:466
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
LocationIndex pd_upper
Definition: bufpage.h:159
#define MAX_GENERIC_XLOG_PAGES
Definition: generic_xlog.h:23
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
LocationIndex pd_lower
Definition: bufpage.h:158

◆ GenericXLogAbort()

void GenericXLogAbort ( GenericXLogState state)

Definition at line 444 of file generic_xlog.c.

References pfree().

Referenced by blbulkdelete(), and blinsert().

445 {
446  pfree(state);
447 }
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ GenericXLogFinish()

XLogRecPtr GenericXLogFinish ( GenericXLogState state)

Definition at line 333 of file generic_xlog.c.

References PageData::buffer, BufferGetPage, BufferIsInvalid, computeDelta(), PageData::delta, PageData::deltaLen, END_CRIT_SECTION, PageData::flags, GENERIC_XLOG_FULL_IMAGE, i, PageData::image, InvalidXLogRecPtr, GenericXLogState::isLogged, MarkBufferDirty(), MAX_GENERIC_XLOG_PAGES, GenericXLogState::pages, PageSetLSN, PageHeaderData::pd_lower, PageHeaderData::pd_upper, pfree(), REGBUF_FORCE_IMAGE, REGBUF_STANDARD, START_CRIT_SECTION, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), and XLogRegisterBuffer().

Referenced by blbulkdelete(), blinsert(), BloomInitMetapage(), and flushCachedPage().

334 {
335  XLogRecPtr lsn;
336  int i;
337 
338  if (state->isLogged)
339  {
340  /* Logged relation: make xlog record in critical section. */
341  XLogBeginInsert();
342 
344 
345  for (i = 0; i < MAX_GENERIC_XLOG_PAGES; i++)
346  {
347  PageData *pageData = &state->pages[i];
348  Page page;
349  PageHeader pageHeader;
350 
351  if (BufferIsInvalid(pageData->buffer))
352  continue;
353 
354  page = BufferGetPage(pageData->buffer);
355  pageHeader = (PageHeader) pageData->image;
356 
357  if (pageData->flags & GENERIC_XLOG_FULL_IMAGE)
358  {
359  /*
360  * A full-page image does not require us to supply any xlog
361  * data. Just apply the image, being careful to zero the
362  * "hole" between pd_lower and pd_upper in order to avoid
363  * divergence between actual page state and what replay would
364  * produce.
365  */
366  memcpy(page, pageData->image, pageHeader->pd_lower);
367  memset(page + pageHeader->pd_lower, 0,
368  pageHeader->pd_upper - pageHeader->pd_lower);
369  memcpy(page + pageHeader->pd_upper,
370  pageData->image + pageHeader->pd_upper,
371  BLCKSZ - pageHeader->pd_upper);
372 
373  XLogRegisterBuffer(i, pageData->buffer,
375  }
376  else
377  {
378  /*
379  * In normal mode, calculate delta and write it as xlog data
380  * associated with this page.
381  */
382  computeDelta(pageData, page, (Page) pageData->image);
383 
384  /* Apply the image, with zeroed "hole" as above */
385  memcpy(page, pageData->image, pageHeader->pd_lower);
386  memset(page + pageHeader->pd_lower, 0,
387  pageHeader->pd_upper - pageHeader->pd_lower);
388  memcpy(page + pageHeader->pd_upper,
389  pageData->image + pageHeader->pd_upper,
390  BLCKSZ - pageHeader->pd_upper);
391 
393  XLogRegisterBufData(i, pageData->delta, pageData->deltaLen);
394  }
395  }
396 
397  /* Insert xlog record */
398  lsn = XLogInsert(RM_GENERIC_ID, 0);
399 
400  /* Set LSN and mark buffers dirty */
401  for (i = 0; i < MAX_GENERIC_XLOG_PAGES; i++)
402  {
403  PageData *pageData = &state->pages[i];
404 
405  if (BufferIsInvalid(pageData->buffer))
406  continue;
407  PageSetLSN(BufferGetPage(pageData->buffer), lsn);
408  MarkBufferDirty(pageData->buffer);
409  }
411  }
412  else
413  {
414  /* Unlogged relation: skip xlog-related stuff */
416  for (i = 0; i < MAX_GENERIC_XLOG_PAGES; i++)
417  {
418  PageData *pageData = &state->pages[i];
419 
420  if (BufferIsInvalid(pageData->buffer))
421  continue;
422  memcpy(BufferGetPage(pageData->buffer),
423  pageData->image,
424  BLCKSZ);
425  /* We don't worry about zeroing the "hole" in this case */
426  MarkBufferDirty(pageData->buffer);
427  }
429  /* We don't have a LSN to return, in this case */
430  lsn = InvalidXLogRecPtr;
431  }
432 
433  pfree(state);
434 
435  return lsn;
436 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:361
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
void pfree(void *pointer)
Definition: mcxt.c:1056
char delta[MAX_DELTA_SIZE]
Definition: generic_xlog.c:58
#define BufferIsInvalid(buffer)
Definition: buf.h:31
#define REGBUF_STANDARD
Definition: xloginsert.h:35
PageData pages[MAX_GENERIC_XLOG_PAGES]
Definition: generic_xlog.c:65
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
Buffer buffer
Definition: generic_xlog.c:53
static void computeDelta(PageData *pageData, Page curpage, Page targetpage)
Definition: generic_xlog.c:226
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
char * image
Definition: generic_xlog.c:56
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int i
LocationIndex pd_upper
Definition: bufpage.h:159
#define MAX_GENERIC_XLOG_PAGES
Definition: generic_xlog.h:23
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
#define GENERIC_XLOG_FULL_IMAGE
Definition: generic_xlog.h:26
Pointer Page
Definition: bufpage.h:78
LocationIndex pd_lower
Definition: bufpage.h:158
int deltaLen
Definition: generic_xlog.c:55

◆ GenericXLogRegisterBuffer()

Page GenericXLogRegisterBuffer ( GenericXLogState state,
Buffer  buffer,
int  flags 
)

Definition at line 295 of file generic_xlog.c.

References PageData::buffer, BufferGetPage, BufferIsInvalid, elog, ERROR, PageData::flags, PageData::image, MAX_GENERIC_XLOG_PAGES, and GenericXLogState::pages.

Referenced by blbulkdelete(), blinsert(), BloomInitMetapage(), and flushCachedPage().

296 {
297  int block_id;
298 
299  /* Search array for existing entry or first unused slot */
300  for (block_id = 0; block_id < MAX_GENERIC_XLOG_PAGES; block_id++)
301  {
302  PageData *page = &state->pages[block_id];
303 
304  if (BufferIsInvalid(page->buffer))
305  {
306  /* Empty slot, so use it (there cannot be a match later) */
307  page->buffer = buffer;
308  page->flags = flags;
309  memcpy(page->image, BufferGetPage(buffer), BLCKSZ);
310  return (Page) page->image;
311  }
312  else if (page->buffer == buffer)
313  {
314  /*
315  * Buffer is already registered. Just return the image, which is
316  * already prepared.
317  */
318  return (Page) page->image;
319  }
320  }
321 
322  elog(ERROR, "maximum number %d of generic xlog buffers is exceeded",
323  MAX_GENERIC_XLOG_PAGES);
324  /* keep compiler quiet */
325  return NULL;
326 }
#define ERROR
Definition: elog.h:43
#define BufferIsInvalid(buffer)
Definition: buf.h:31
PageData pages[MAX_GENERIC_XLOG_PAGES]
Definition: generic_xlog.c:65
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
Buffer buffer
Definition: generic_xlog.c:53
char * image
Definition: generic_xlog.c:56
#define elog(elevel,...)
Definition: elog.h:226
#define MAX_GENERIC_XLOG_PAGES
Definition: generic_xlog.h:23
Pointer Page
Definition: bufpage.h:78

◆ GenericXLogStart()

GenericXLogState* GenericXLogStart ( Relation  relation)

Definition at line 267 of file generic_xlog.c.

References PageData::buffer, PGAlignedBlock::data, i, PageData::image, GenericXLogState::images, InvalidBuffer, GenericXLogState::isLogged, MAX_GENERIC_XLOG_PAGES, GenericXLogState::pages, palloc(), and RelationNeedsWAL.

Referenced by blbulkdelete(), blinsert(), BloomInitMetapage(), and flushCachedPage().

268 {
270  int i;
271 
272  state = (GenericXLogState *) palloc(sizeof(GenericXLogState));
273  state->isLogged = RelationNeedsWAL(relation);
274 
275  for (i = 0; i < MAX_GENERIC_XLOG_PAGES; i++)
276  {
277  state->pages[i].image = state->images[i].data;
278  state->pages[i].buffer = InvalidBuffer;
279  }
280 
281  return state;
282 }
#define InvalidBuffer
Definition: buf.h:25
char data[BLCKSZ]
Definition: c.h:1060
PageData pages[MAX_GENERIC_XLOG_PAGES]
Definition: generic_xlog.c:65
Buffer buffer
Definition: generic_xlog.c:53
char * image
Definition: generic_xlog.c:56
Definition: regguts.h:298
#define RelationNeedsWAL(relation)
Definition: rel.h:521
void * palloc(Size size)
Definition: mcxt.c:949
int i
PGAlignedBlock images[MAX_GENERIC_XLOG_PAGES]
Definition: generic_xlog.c:68
#define MAX_GENERIC_XLOG_PAGES
Definition: generic_xlog.h:23