PostgreSQL Source Code  git master
pg_visibility.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/visibilitymap.h"
#include "access/xloginsert.h"
#include "catalog/pg_type.h"
#include "catalog/storage_xlog.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/procarray.h"
#include "storage/smgr.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
Include dependency graph for pg_visibility.c:

Go to the source code of this file.

Data Structures

struct  vbits
 
struct  corrupt_items
 

Typedefs

typedef struct vbits vbits
 
typedef struct corrupt_items corrupt_items
 

Functions

 PG_FUNCTION_INFO_V1 (pg_visibility_map)
 
 PG_FUNCTION_INFO_V1 (pg_visibility_map_rel)
 
 PG_FUNCTION_INFO_V1 (pg_visibility)
 
 PG_FUNCTION_INFO_V1 (pg_visibility_rel)
 
 PG_FUNCTION_INFO_V1 (pg_visibility_map_summary)
 
 PG_FUNCTION_INFO_V1 (pg_check_frozen)
 
 PG_FUNCTION_INFO_V1 (pg_check_visible)
 
 PG_FUNCTION_INFO_V1 (pg_truncate_visibility_map)
 
static TupleDesc pg_visibility_tupdesc (bool include_blkno, bool include_pd)
 
static vbitscollect_visibility_data (Oid relid, bool include_pd)
 
static corrupt_itemscollect_corrupt_items (Oid relid, bool all_visible, bool all_frozen)
 
static void record_corrupt_item (corrupt_items *items, ItemPointer tid)
 
static bool tuple_all_visible (HeapTuple tup, TransactionId OldestXmin, Buffer buffer)
 
static void check_relation_relkind (Relation rel)
 
Datum pg_visibility_map (PG_FUNCTION_ARGS)
 
Datum pg_visibility (PG_FUNCTION_ARGS)
 
Datum pg_visibility_map_rel (PG_FUNCTION_ARGS)
 
Datum pg_visibility_rel (PG_FUNCTION_ARGS)
 
Datum pg_visibility_map_summary (PG_FUNCTION_ARGS)
 
Datum pg_check_frozen (PG_FUNCTION_ARGS)
 
Datum pg_check_visible (PG_FUNCTION_ARGS)
 
Datum pg_truncate_visibility_map (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Typedef Documentation

◆ corrupt_items

typedef struct corrupt_items corrupt_items

◆ vbits

typedef struct vbits vbits

Function Documentation

◆ check_relation_relkind()

static void check_relation_relkind ( Relation  rel)
static

Definition at line 771 of file pg_visibility.c.

772 {
773  if (!RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
774  ereport(ERROR,
775  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
776  errmsg("relation \"%s\" is of wrong relation kind",
778  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
779 }
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
#define RelationGetRelationName(relation)
Definition: rel.h:538
Form_pg_class rd_rel
Definition: rel.h:111

References ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, RelationData::rd_rel, and RelationGetRelationName.

Referenced by collect_corrupt_items(), collect_visibility_data(), pg_truncate_visibility_map(), pg_visibility(), pg_visibility_map(), and pg_visibility_map_summary().

◆ collect_corrupt_items()

static corrupt_items * collect_corrupt_items ( Oid  relid,
bool  all_visible,
bool  all_frozen 
)
static

Definition at line 550 of file pg_visibility.c.

551 {
552  Relation rel;
553  BlockNumber nblocks;
554  corrupt_items *items;
555  BlockNumber blkno;
556  Buffer vmbuffer = InvalidBuffer;
558  TransactionId OldestXmin = InvalidTransactionId;
559 
560  rel = relation_open(relid, AccessShareLock);
561 
562  /* Only some relkinds have a visibility map */
564 
565  if (all_visible)
566  OldestXmin = GetOldestNonRemovableTransactionId(rel);
567 
568  nblocks = RelationGetNumberOfBlocks(rel);
569 
570  /*
571  * Guess an initial array size. We don't expect many corrupted tuples, so
572  * start with a small array. This function uses the "next" field to track
573  * the next offset where we can store an item (which is the same thing as
574  * the number of items found so far) and the "count" field to track the
575  * number of entries allocated. We'll repurpose these fields before
576  * returning.
577  */
578  items = palloc0(sizeof(corrupt_items));
579  items->next = 0;
580  items->count = 64;
581  items->tids = palloc(items->count * sizeof(ItemPointerData));
582 
583  /* Loop over every block in the relation. */
584  for (blkno = 0; blkno < nblocks; ++blkno)
585  {
586  bool check_frozen = false;
587  bool check_visible = false;
588  Buffer buffer;
589  Page page;
590  OffsetNumber offnum,
591  maxoff;
592 
593  /* Make sure we are interruptible. */
595 
596  /* Use the visibility map to decide whether to check this page. */
597  if (all_frozen && VM_ALL_FROZEN(rel, blkno, &vmbuffer))
598  check_frozen = true;
599  if (all_visible && VM_ALL_VISIBLE(rel, blkno, &vmbuffer))
600  check_visible = true;
601  if (!check_visible && !check_frozen)
602  continue;
603 
604  /* Read and lock the page. */
605  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
606  bstrategy);
607  LockBuffer(buffer, BUFFER_LOCK_SHARE);
608 
609  page = BufferGetPage(buffer);
610  maxoff = PageGetMaxOffsetNumber(page);
611 
612  /*
613  * The visibility map bits might have changed while we were acquiring
614  * the page lock. Recheck to avoid returning spurious results.
615  */
616  if (check_frozen && !VM_ALL_FROZEN(rel, blkno, &vmbuffer))
617  check_frozen = false;
618  if (check_visible && !VM_ALL_VISIBLE(rel, blkno, &vmbuffer))
619  check_visible = false;
620  if (!check_visible && !check_frozen)
621  {
622  UnlockReleaseBuffer(buffer);
623  continue;
624  }
625 
626  /* Iterate over each tuple on the page. */
627  for (offnum = FirstOffsetNumber;
628  offnum <= maxoff;
629  offnum = OffsetNumberNext(offnum))
630  {
631  HeapTupleData tuple;
632  ItemId itemid;
633 
634  itemid = PageGetItemId(page, offnum);
635 
636  /* Unused or redirect line pointers are of no interest. */
637  if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid))
638  continue;
639 
640  /* Dead line pointers are neither all-visible nor frozen. */
641  if (ItemIdIsDead(itemid))
642  {
643  ItemPointerSet(&(tuple.t_self), blkno, offnum);
644  record_corrupt_item(items, &tuple.t_self);
645  continue;
646  }
647 
648  /* Initialize a HeapTupleData structure for checks below. */
649  ItemPointerSet(&(tuple.t_self), blkno, offnum);
650  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
651  tuple.t_len = ItemIdGetLength(itemid);
652  tuple.t_tableOid = relid;
653 
654  /*
655  * If we're checking whether the page is all-visible, we expect
656  * the tuple to be all-visible.
657  */
658  if (check_visible &&
659  !tuple_all_visible(&tuple, OldestXmin, buffer))
660  {
661  TransactionId RecomputedOldestXmin;
662 
663  /*
664  * Time has passed since we computed OldestXmin, so it's
665  * possible that this tuple is all-visible in reality even
666  * though it doesn't appear so based on our
667  * previously-computed value. Let's compute a new value so we
668  * can be certain whether there is a problem.
669  *
670  * From a concurrency point of view, it sort of sucks to
671  * retake ProcArrayLock here while we're holding the buffer
672  * exclusively locked, but it should be safe against
673  * deadlocks, because surely
674  * GetOldestNonRemovableTransactionId() should never take a
675  * buffer lock. And this shouldn't happen often, so it's worth
676  * being careful so as to avoid false positives.
677  */
678  RecomputedOldestXmin = GetOldestNonRemovableTransactionId(rel);
679 
680  if (!TransactionIdPrecedes(OldestXmin, RecomputedOldestXmin))
681  record_corrupt_item(items, &tuple.t_self);
682  else
683  {
684  OldestXmin = RecomputedOldestXmin;
685  if (!tuple_all_visible(&tuple, OldestXmin, buffer))
686  record_corrupt_item(items, &tuple.t_self);
687  }
688  }
689 
690  /*
691  * If we're checking whether the page is all-frozen, we expect the
692  * tuple to be in a state where it will never need freezing.
693  */
694  if (check_frozen)
695  {
697  record_corrupt_item(items, &tuple.t_self);
698  }
699  }
700 
701  UnlockReleaseBuffer(buffer);
702  }
703 
704  /* Clean up. */
705  if (vmbuffer != InvalidBuffer)
706  ReleaseBuffer(vmbuffer);
708 
709  /*
710  * Before returning, repurpose the fields to match caller's expectations.
711  * next is now the next item that should be read (rather than written) and
712  * count is now the number of items we wrote (rather than the number we
713  * allocated).
714  */
715  items->count = items->next;
716  items->next = 0;
717 
718  return items;
719 }
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4561
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4578
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4796
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:782
@ BAS_BULKREAD
Definition: bufmgr.h:35
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:158
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:229
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
@ RBM_NORMAL
Definition: bufmgr.h:44
Pointer Page
Definition: bufpage.h:78
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:351
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
uint32 TransactionId
Definition: c.h:641
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple)
Definition: heapam.c:7339
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define ItemIdIsUsed(itemId)
Definition: itemid.h:92
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:106
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
#define AccessShareLock
Definition: lockdefs.h:36
void * palloc0(Size size)
Definition: mcxt.c:1232
void * palloc(Size size)
Definition: mcxt.c:1201
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
static void check_relation_relkind(Relation rel)
static void record_corrupt_item(corrupt_items *items, ItemPointer tid)
static bool tuple_all_visible(HeapTuple tup, TransactionId OldestXmin, Buffer buffer)
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:1994
@ MAIN_FORKNUM
Definition: relpath.h:50
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
ItemPointer tids
Definition: pg_visibility.c:40
BlockNumber count
Definition: pg_visibility.c:39
BlockNumber next
Definition: pg_visibility.c:38
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define InvalidTransactionId
Definition: transam.h:31
#define VM_ALL_VISIBLE(r, b, v)
Definition: visibilitymap.h:24
#define VM_ALL_FROZEN(r, b, v)
Definition: visibilitymap.h:26

References AccessShareLock, BAS_BULKREAD, BUFFER_LOCK_SHARE, BufferGetPage(), CHECK_FOR_INTERRUPTS, check_relation_relkind(), corrupt_items::count, FirstOffsetNumber, GetAccessStrategy(), GetOldestNonRemovableTransactionId(), heap_tuple_needs_eventual_freeze(), InvalidBuffer, InvalidTransactionId, ItemIdGetLength, ItemIdIsDead, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerSet(), LockBuffer(), MAIN_FORKNUM, corrupt_items::next, OffsetNumberNext, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), palloc(), palloc0(), RBM_NORMAL, ReadBufferExtended(), record_corrupt_item(), relation_close(), relation_open(), RelationGetNumberOfBlocks, ReleaseBuffer(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, corrupt_items::tids, TransactionIdPrecedes(), tuple_all_visible(), UnlockReleaseBuffer(), VM_ALL_FROZEN, and VM_ALL_VISIBLE.

Referenced by pg_check_frozen(), and pg_check_visible().

◆ collect_visibility_data()

static vbits * collect_visibility_data ( Oid  relid,
bool  include_pd 
)
static

Definition at line 472 of file pg_visibility.c.

473 {
474  Relation rel;
475  BlockNumber nblocks;
476  vbits *info;
477  BlockNumber blkno;
478  Buffer vmbuffer = InvalidBuffer;
480 
481  rel = relation_open(relid, AccessShareLock);
482 
483  /* Only some relkinds have a visibility map */
485 
486  nblocks = RelationGetNumberOfBlocks(rel);
487  info = palloc0(offsetof(vbits, bits) + nblocks);
488  info->next = 0;
489  info->count = nblocks;
490 
491  for (blkno = 0; blkno < nblocks; ++blkno)
492  {
493  int32 mapbits;
494 
495  /* Make sure we are interruptible. */
497 
498  /* Get map info. */
499  mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
500  if ((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0)
501  info->bits[blkno] |= (1 << 0);
502  if ((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0)
503  info->bits[blkno] |= (1 << 1);
504 
505  /*
506  * Page-level data requires reading every block, so only get it if the
507  * caller needs it. Use a buffer access strategy, too, to prevent
508  * cache-trashing.
509  */
510  if (include_pd)
511  {
512  Buffer buffer;
513  Page page;
514 
515  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
516  bstrategy);
517  LockBuffer(buffer, BUFFER_LOCK_SHARE);
518 
519  page = BufferGetPage(buffer);
520  if (PageIsAllVisible(page))
521  info->bits[blkno] |= (1 << 2);
522 
523  UnlockReleaseBuffer(buffer);
524  }
525  }
526 
527  /* Clean up. */
528  if (vmbuffer != InvalidBuffer)
529  ReleaseBuffer(vmbuffer);
531 
532  return info;
533 }
static bool PageIsAllVisible(Page page)
Definition: bufpage.h:426
signed int int32
Definition: c.h:483
BlockNumber next
Definition: pg_visibility.c:31
uint8 bits[FLEXIBLE_ARRAY_MEMBER]
Definition: pg_visibility.c:33
BlockNumber count
Definition: pg_visibility.c:32
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
#define VISIBILITYMAP_ALL_FROZEN
#define VISIBILITYMAP_ALL_VISIBLE

References AccessShareLock, BAS_BULKREAD, vbits::bits, BUFFER_LOCK_SHARE, BufferGetPage(), CHECK_FOR_INTERRUPTS, check_relation_relkind(), vbits::count, GetAccessStrategy(), InvalidBuffer, LockBuffer(), MAIN_FORKNUM, vbits::next, PageIsAllVisible(), palloc0(), RBM_NORMAL, ReadBufferExtended(), relation_close(), relation_open(), RelationGetNumberOfBlocks, ReleaseBuffer(), UnlockReleaseBuffer(), VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, and visibilitymap_get_status().

Referenced by pg_visibility_map_rel(), and pg_visibility_rel().

◆ pg_check_frozen()

Datum pg_check_frozen ( PG_FUNCTION_ARGS  )

Definition at line 309 of file pg_visibility.c.

310 {
311  FuncCallContext *funcctx;
312  corrupt_items *items;
313 
314  if (SRF_IS_FIRSTCALL())
315  {
316  Oid relid = PG_GETARG_OID(0);
317  MemoryContext oldcontext;
318 
319  funcctx = SRF_FIRSTCALL_INIT();
320  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
321  /* collect_corrupt_items will verify the relkind */
322  funcctx->user_fctx = collect_corrupt_items(relid, false, true);
323  MemoryContextSwitchTo(oldcontext);
324  }
325 
326  funcctx = SRF_PERCALL_SETUP();
327  items = (corrupt_items *) funcctx->user_fctx;
328 
329  if (items->next < items->count)
330  SRF_RETURN_NEXT(funcctx, PointerGetDatum(&items->tids[items->next++]));
331 
332  SRF_RETURN_DONE(funcctx);
333 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#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
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static corrupt_items * collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
unsigned int Oid
Definition: postgres_ext.h:31
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101

References collect_corrupt_items(), corrupt_items::count, if(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, corrupt_items::next, PG_GETARG_OID, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, corrupt_items::tids, and FuncCallContext::user_fctx.

◆ pg_check_visible()

Datum pg_check_visible ( PG_FUNCTION_ARGS  )

Definition at line 341 of file pg_visibility.c.

342 {
343  FuncCallContext *funcctx;
344  corrupt_items *items;
345 
346  if (SRF_IS_FIRSTCALL())
347  {
348  Oid relid = PG_GETARG_OID(0);
349  MemoryContext oldcontext;
350 
351  funcctx = SRF_FIRSTCALL_INIT();
352  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
353  /* collect_corrupt_items will verify the relkind */
354  funcctx->user_fctx = collect_corrupt_items(relid, true, false);
355  MemoryContextSwitchTo(oldcontext);
356  }
357 
358  funcctx = SRF_PERCALL_SETUP();
359  items = (corrupt_items *) funcctx->user_fctx;
360 
361  if (items->next < items->count)
362  SRF_RETURN_NEXT(funcctx, PointerGetDatum(&items->tids[items->next++]));
363 
364  SRF_RETURN_DONE(funcctx);
365 }

References collect_corrupt_items(), corrupt_items::count, if(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, corrupt_items::next, PG_GETARG_OID, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, corrupt_items::tids, and FuncCallContext::user_fctx.

◆ PG_FUNCTION_INFO_V1() [1/8]

PG_FUNCTION_INFO_V1 ( pg_check_frozen  )

◆ PG_FUNCTION_INFO_V1() [2/8]

PG_FUNCTION_INFO_V1 ( pg_check_visible  )

◆ PG_FUNCTION_INFO_V1() [3/8]

PG_FUNCTION_INFO_V1 ( pg_truncate_visibility_map  )

◆ PG_FUNCTION_INFO_V1() [4/8]

PG_FUNCTION_INFO_V1 ( pg_visibility  )

◆ PG_FUNCTION_INFO_V1() [5/8]

PG_FUNCTION_INFO_V1 ( pg_visibility_map  )

◆ PG_FUNCTION_INFO_V1() [6/8]

PG_FUNCTION_INFO_V1 ( pg_visibility_map_rel  )

◆ PG_FUNCTION_INFO_V1() [7/8]

PG_FUNCTION_INFO_V1 ( pg_visibility_map_summary  )

◆ PG_FUNCTION_INFO_V1() [8/8]

PG_FUNCTION_INFO_V1 ( pg_visibility_rel  )

◆ pg_truncate_visibility_map()

Datum pg_truncate_visibility_map ( PG_FUNCTION_ARGS  )

Definition at line 376 of file pg_visibility.c.

377 {
378  Oid relid = PG_GETARG_OID(0);
379  Relation rel;
380  ForkNumber fork;
381  BlockNumber block;
382 
383  rel = relation_open(relid, AccessExclusiveLock);
384 
385  /* Only some relkinds have a visibility map */
387 
388  /* Forcibly reset cached file size */
390 
391  block = visibilitymap_prepare_truncate(rel, 0);
392  if (BlockNumberIsValid(block))
393  {
394  fork = VISIBILITYMAP_FORKNUM;
395  smgrtruncate(RelationGetSmgr(rel), &fork, 1, &block);
396  }
397 
398  if (RelationNeedsWAL(rel))
399  {
400  xl_smgr_truncate xlrec;
401 
402  xlrec.blkno = 0;
403  xlrec.rlocator = rel->rd_locator;
404  xlrec.flags = SMGR_TRUNCATE_VM;
405 
406  XLogBeginInsert();
407  XLogRegisterData((char *) &xlrec, sizeof(xlrec));
408 
410  }
411 
412  /*
413  * Release the lock right away, not at commit time.
414  *
415  * It would be a problem to release the lock prior to commit if this
416  * truncate operation sends any transactional invalidation messages. Other
417  * backends would potentially be able to lock the relation without
418  * processing them in the window of time between when we release the lock
419  * here and when we sent the messages at our eventual commit. However,
420  * we're currently only sending a non-transactional smgr invalidation,
421  * which will have been posted to shared memory immediately from within
422  * smgr_truncate. Therefore, there should be no race here.
423  *
424  * The reason why it's desirable to release the lock early here is because
425  * of the possibility that someone will need to use this to blow away many
426  * visibility map forks at once. If we can't release the lock until
427  * commit time, the transaction doing this will accumulate
428  * AccessExclusiveLocks on all of those relations at the same time, which
429  * is undesirable. However, if this turns out to be unsafe we may have no
430  * choice...
431  */
433 
434  /* Nothing to return. */
435  PG_RETURN_VOID();
436 }
#define InvalidBlockNumber
Definition: block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define AccessExclusiveLock
Definition: lockdefs.h:43
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:566
#define RelationNeedsWAL(relation)
Definition: rel.h:627
ForkNumber
Definition: relpath.h:48
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:52
void smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *nblocks)
Definition: smgr.c:695
#define SMGR_TRUNCATE_VM
Definition: storage_xlog.h:41
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
RelFileLocator rd_locator
Definition: rel.h:57
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:46
RelFileLocator rlocator
Definition: storage_xlog.h:49
BlockNumber blkno
Definition: storage_xlog.h:48
BlockNumber visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:365
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:475
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:82

References AccessExclusiveLock, xl_smgr_truncate::blkno, BlockNumberIsValid(), check_relation_relkind(), xl_smgr_truncate::flags, InvalidBlockNumber, PG_GETARG_OID, PG_RETURN_VOID, RelationData::rd_locator, relation_close(), relation_open(), RelationGetSmgr(), RelationNeedsWAL, xl_smgr_truncate::rlocator, SMgrRelationData::smgr_cached_nblocks, SMGR_TRUNCATE_VM, smgrtruncate(), VISIBILITYMAP_FORKNUM, visibilitymap_prepare_truncate(), XLOG_SMGR_TRUNCATE, XLogBeginInsert(), XLogInsert(), XLogRegisterData(), and XLR_SPECIAL_REL_UPDATE.

◆ pg_visibility()

Datum pg_visibility ( PG_FUNCTION_ARGS  )

Definition at line 108 of file pg_visibility.c.

109 {
110  Oid relid = PG_GETARG_OID(0);
111  int64 blkno = PG_GETARG_INT64(1);
112  int32 mapbits;
113  Relation rel;
114  Buffer vmbuffer = InvalidBuffer;
115  Buffer buffer;
116  Page page;
117  TupleDesc tupdesc;
118  Datum values[3];
119  bool nulls[3] = {0};
120 
121  rel = relation_open(relid, AccessShareLock);
122 
123  /* Only some relkinds have a visibility map */
125 
126  if (blkno < 0 || blkno > MaxBlockNumber)
127  ereport(ERROR,
128  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
129  errmsg("invalid block number")));
130 
131  tupdesc = pg_visibility_tupdesc(false, true);
132 
133  mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
134  if (vmbuffer != InvalidBuffer)
135  ReleaseBuffer(vmbuffer);
136  values[0] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0);
137  values[1] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0);
138 
139  /* Here we have to explicitly check rel size ... */
140  if (blkno < RelationGetNumberOfBlocks(rel))
141  {
142  buffer = ReadBuffer(rel, blkno);
143  LockBuffer(buffer, BUFFER_LOCK_SHARE);
144 
145  page = BufferGetPage(buffer);
147 
148  UnlockReleaseBuffer(buffer);
149  }
150  else
151  {
152  /* As with the vismap, silently return 0 for pages past EOF */
153  values[2] = BoolGetDatum(false);
154  }
155 
157 
159 }
#define MaxBlockNumber
Definition: block.h:35
static Datum values[MAXATTR]
Definition: bootstrap.c:156
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:735
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
static TupleDesc pg_visibility_tupdesc(bool include_blkno, bool include_pd)
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102

References AccessShareLock, BoolGetDatum(), BUFFER_LOCK_SHARE, BufferGetPage(), check_relation_relkind(), ereport, errcode(), errmsg(), ERROR, heap_form_tuple(), HeapTupleGetDatum(), InvalidBuffer, LockBuffer(), MaxBlockNumber, PageIsAllVisible(), PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_DATUM, pg_visibility_tupdesc(), ReadBuffer(), relation_close(), relation_open(), RelationGetNumberOfBlocks, ReleaseBuffer(), UnlockReleaseBuffer(), values, VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, and visibilitymap_get_status().

◆ pg_visibility_map()

Datum pg_visibility_map ( PG_FUNCTION_ARGS  )

Definition at line 69 of file pg_visibility.c.

70 {
71  Oid relid = PG_GETARG_OID(0);
72  int64 blkno = PG_GETARG_INT64(1);
73  int32 mapbits;
74  Relation rel;
75  Buffer vmbuffer = InvalidBuffer;
76  TupleDesc tupdesc;
77  Datum values[2];
78  bool nulls[2] = {0};
79 
80  rel = relation_open(relid, AccessShareLock);
81 
82  /* Only some relkinds have a visibility map */
84 
85  if (blkno < 0 || blkno > MaxBlockNumber)
86  ereport(ERROR,
87  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
88  errmsg("invalid block number")));
89 
90  tupdesc = pg_visibility_tupdesc(false, false);
91 
92  mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
93  if (vmbuffer != InvalidBuffer)
94  ReleaseBuffer(vmbuffer);
95  values[0] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0);
96  values[1] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0);
97 
99 
101 }

References AccessShareLock, BoolGetDatum(), check_relation_relkind(), ereport, errcode(), errmsg(), ERROR, heap_form_tuple(), HeapTupleGetDatum(), InvalidBuffer, MaxBlockNumber, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_DATUM, pg_visibility_tupdesc(), relation_close(), relation_open(), ReleaseBuffer(), values, VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, and visibilitymap_get_status().

◆ pg_visibility_map_rel()

Datum pg_visibility_map_rel ( PG_FUNCTION_ARGS  )

Definition at line 165 of file pg_visibility.c.

166 {
167  FuncCallContext *funcctx;
168  vbits *info;
169 
170  if (SRF_IS_FIRSTCALL())
171  {
172  Oid relid = PG_GETARG_OID(0);
173  MemoryContext oldcontext;
174 
175  funcctx = SRF_FIRSTCALL_INIT();
176  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
177  funcctx->tuple_desc = pg_visibility_tupdesc(true, false);
178  /* collect_visibility_data will verify the relkind */
179  funcctx->user_fctx = collect_visibility_data(relid, false);
180  MemoryContextSwitchTo(oldcontext);
181  }
182 
183  funcctx = SRF_PERCALL_SETUP();
184  info = (vbits *) funcctx->user_fctx;
185 
186  if (info->next < info->count)
187  {
188  Datum values[3];
189  bool nulls[3] = {0};
190  HeapTuple tuple;
191 
192  values[0] = Int64GetDatum(info->next);
193  values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
194  values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
195  info->next++;
196 
197  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
198  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
199  }
200 
201  SRF_RETURN_DONE(funcctx);
202 }
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
static vbits * collect_visibility_data(Oid relid, bool include_pd)
TupleDesc tuple_desc
Definition: funcapi.h:112

References vbits::bits, BoolGetDatum(), collect_visibility_data(), vbits::count, heap_form_tuple(), HeapTupleGetDatum(), if(), Int64GetDatum(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, vbits::next, PG_GETARG_OID, pg_visibility_tupdesc(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::tuple_desc, FuncCallContext::user_fctx, and values.

◆ pg_visibility_map_summary()

Datum pg_visibility_map_summary ( PG_FUNCTION_ARGS  )

Definition at line 254 of file pg_visibility.c.

255 {
256  Oid relid = PG_GETARG_OID(0);
257  Relation rel;
258  BlockNumber nblocks;
259  BlockNumber blkno;
260  Buffer vmbuffer = InvalidBuffer;
261  int64 all_visible = 0;
262  int64 all_frozen = 0;
263  TupleDesc tupdesc;
264  Datum values[2];
265  bool nulls[2] = {0};
266 
267  rel = relation_open(relid, AccessShareLock);
268 
269  /* Only some relkinds have a visibility map */
271 
272  nblocks = RelationGetNumberOfBlocks(rel);
273 
274  for (blkno = 0; blkno < nblocks; ++blkno)
275  {
276  int32 mapbits;
277 
278  /* Make sure we are interruptible. */
280 
281  /* Get map info. */
282  mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
283  if ((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0)
284  ++all_visible;
285  if ((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0)
286  ++all_frozen;
287  }
288 
289  /* Clean up. */
290  if (vmbuffer != InvalidBuffer)
291  ReleaseBuffer(vmbuffer);
293 
294  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
295  elog(ERROR, "return type must be a row type");
296 
297  values[0] = Int64GetDatum(all_visible);
298  values[1] = Int64GetDatum(all_frozen);
299 
301 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149

References AccessShareLock, CHECK_FOR_INTERRUPTS, check_relation_relkind(), elog(), ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), InvalidBuffer, PG_GETARG_OID, PG_RETURN_DATUM, relation_close(), relation_open(), RelationGetNumberOfBlocks, ReleaseBuffer(), TYPEFUNC_COMPOSITE, values, VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, and visibilitymap_get_status().

◆ pg_visibility_rel()

Datum pg_visibility_rel ( PG_FUNCTION_ARGS  )

Definition at line 209 of file pg_visibility.c.

210 {
211  FuncCallContext *funcctx;
212  vbits *info;
213 
214  if (SRF_IS_FIRSTCALL())
215  {
216  Oid relid = PG_GETARG_OID(0);
217  MemoryContext oldcontext;
218 
219  funcctx = SRF_FIRSTCALL_INIT();
220  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
221  funcctx->tuple_desc = pg_visibility_tupdesc(true, true);
222  /* collect_visibility_data will verify the relkind */
223  funcctx->user_fctx = collect_visibility_data(relid, true);
224  MemoryContextSwitchTo(oldcontext);
225  }
226 
227  funcctx = SRF_PERCALL_SETUP();
228  info = (vbits *) funcctx->user_fctx;
229 
230  if (info->next < info->count)
231  {
232  Datum values[4];
233  bool nulls[4] = {0};
234  HeapTuple tuple;
235 
236  values[0] = Int64GetDatum(info->next);
237  values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
238  values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
239  values[3] = BoolGetDatum((info->bits[info->next] & (1 << 2)) != 0);
240  info->next++;
241 
242  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
243  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
244  }
245 
246  SRF_RETURN_DONE(funcctx);
247 }

References vbits::bits, BoolGetDatum(), collect_visibility_data(), vbits::count, heap_form_tuple(), HeapTupleGetDatum(), if(), Int64GetDatum(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, vbits::next, PG_GETARG_OID, pg_visibility_tupdesc(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::tuple_desc, FuncCallContext::user_fctx, and values.

◆ pg_visibility_tupdesc()

static TupleDesc pg_visibility_tupdesc ( bool  include_blkno,
bool  include_pd 
)
static

Definition at line 443 of file pg_visibility.c.

444 {
445  TupleDesc tupdesc;
446  AttrNumber maxattr = 2;
447  AttrNumber a = 0;
448 
449  if (include_blkno)
450  ++maxattr;
451  if (include_pd)
452  ++maxattr;
453  tupdesc = CreateTemplateTupleDesc(maxattr);
454  if (include_blkno)
455  TupleDescInitEntry(tupdesc, ++a, "blkno", INT8OID, -1, 0);
456  TupleDescInitEntry(tupdesc, ++a, "all_visible", BOOLOID, -1, 0);
457  TupleDescInitEntry(tupdesc, ++a, "all_frozen", BOOLOID, -1, 0);
458  if (include_pd)
459  TupleDescInitEntry(tupdesc, ++a, "pd_all_visible", BOOLOID, -1, 0);
460  Assert(a == maxattr);
461 
462  return BlessTupleDesc(tupdesc);
463 }
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2070
int a
Definition: isn.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603

References a, Assert(), BlessTupleDesc(), CreateTemplateTupleDesc(), and TupleDescInitEntry().

Referenced by pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), and pg_visibility_rel().

◆ record_corrupt_item()

static void record_corrupt_item ( corrupt_items items,
ItemPointer  tid 
)
static

Definition at line 725 of file pg_visibility.c.

726 {
727  /* enlarge output array if needed. */
728  if (items->next >= items->count)
729  {
730  items->count *= 2;
731  items->tids = repalloc(items->tids,
732  items->count * sizeof(ItemPointerData));
733  }
734  /* and add the new item */
735  items->tids[items->next++] = *tid;
736 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1451

References corrupt_items::count, corrupt_items::next, repalloc(), and corrupt_items::tids.

Referenced by collect_corrupt_items().

◆ tuple_all_visible()

static bool tuple_all_visible ( HeapTuple  tup,
TransactionId  OldestXmin,
Buffer  buffer 
)
static

Definition at line 743 of file pg_visibility.c.

744 {
746  TransactionId xmin;
747 
748  state = HeapTupleSatisfiesVacuum(tup, OldestXmin, buffer);
749  if (state != HEAPTUPLE_LIVE)
750  return false; /* all-visible implies live */
751 
752  /*
753  * Neither lazy_scan_heap nor heap_page_is_all_visible will mark a page
754  * all-visible unless every tuple is hinted committed. However, those hint
755  * bits could be lost after a crash, so we can't be certain that they'll
756  * be set here. So just check the xmin.
757  */
758 
759  xmin = HeapTupleHeaderGetXmin(tup->t_data);
760  if (!TransactionIdPrecedes(xmin, OldestXmin))
761  return false; /* xmin not old enough for all to see */
762 
763  return true;
764 }
HTSV_Result
Definition: heapam.h:95
@ HEAPTUPLE_LIVE
Definition: heapam.h:97
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:309
Definition: regguts.h:323

References HEAPTUPLE_LIVE, HeapTupleHeaderGetXmin, HeapTupleSatisfiesVacuum(), HeapTupleData::t_data, and TransactionIdPrecedes().

Referenced by collect_corrupt_items().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 27 of file pg_visibility.c.