PostgreSQL Source Code  git master
smgr.c File Reference
#include "postgres.h"
#include "access/xlogutils.h"
#include "lib/ilist.h"
#include "storage/bufmgr.h"
#include "storage/ipc.h"
#include "storage/md.h"
#include "storage/smgr.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
Include dependency graph for smgr.c:

Go to the source code of this file.

Data Structures

struct  f_smgr
 

Typedefs

typedef struct f_smgr f_smgr
 

Functions

static void smgrshutdown (int code, Datum arg)
 
static void smgrdestroy (SMgrRelation reln)
 
void smgrinit (void)
 
SMgrRelation smgropen (RelFileLocator rlocator, ProcNumber backend)
 
void smgrpin (SMgrRelation reln)
 
void smgrunpin (SMgrRelation reln)
 
void smgrrelease (SMgrRelation reln)
 
void smgrclose (SMgrRelation reln)
 
void smgrdestroyall (void)
 
void smgrreleaseall (void)
 
void smgrreleaserellocator (RelFileLocatorBackend rlocator)
 
bool smgrexists (SMgrRelation reln, ForkNumber forknum)
 
void smgrcreate (SMgrRelation reln, ForkNumber forknum, bool isRedo)
 
void smgrdosyncall (SMgrRelation *rels, int nrels)
 
void smgrdounlinkall (SMgrRelation *rels, int nrels, bool isRedo)
 
void smgrextend (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
 
void smgrzeroextend (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
 
bool smgrprefetch (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
 
uint32 smgrmaxcombine (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
 
void smgrreadv (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
 
void smgrwritev (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
 
void smgrwriteback (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
 
BlockNumber smgrnblocks (SMgrRelation reln, ForkNumber forknum)
 
BlockNumber smgrnblocks_cached (SMgrRelation reln, ForkNumber forknum)
 
void smgrtruncate (SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *nblocks)
 
void smgrregistersync (SMgrRelation reln, ForkNumber forknum)
 
void smgrimmedsync (SMgrRelation reln, ForkNumber forknum)
 
void AtEOXact_SMgr (void)
 
bool ProcessBarrierSmgrRelease (void)
 

Variables

static const f_smgr smgrsw []
 
static const int NSmgr = lengthof(smgrsw)
 
static HTABSMgrRelationHash = NULL
 
static dlist_head unpinned_relns
 

Typedef Documentation

◆ f_smgr

typedef struct f_smgr f_smgr

Function Documentation

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 829 of file smgr.c.

830 {
831  smgrdestroyall();
832 }
void smgrdestroyall(void)
Definition: smgr.c:335

References smgrdestroyall().

Referenced by AbortTransaction(), BackgroundWriterMain(), CheckpointerMain(), CommitTransaction(), PrepareTransaction(), and WalWriterMain().

◆ ProcessBarrierSmgrRelease()

bool ProcessBarrierSmgrRelease ( void  )

Definition at line 839 of file smgr.c.

840 {
841  smgrreleaseall();
842  return true;
843 }
void smgrreleaseall(void)
Definition: smgr.c:356

References smgrreleaseall().

Referenced by ProcessProcSignalBarrier().

◆ smgrclose()

◆ smgrcreate()

void smgrcreate ( SMgrRelation  reln,
ForkNumber  forknum,
bool  isRedo 
)

Definition at line 414 of file smgr.c.

415 {
416  smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
417 }
static const f_smgr smgrsw[]
Definition: smgr.c:109
int smgr_which
Definition: smgr.h:54
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:80

References f_smgr::smgr_create, SMgrRelationData::smgr_which, and smgrsw.

Referenced by CreateAndCopyRelationData(), ExtendBufferedRelTo(), fill_seq_with_data(), heapam_relation_copy_data(), heapam_relation_set_new_filelocator(), index_build(), index_copy_data(), RelationCreateStorage(), smgr_redo(), and XLogReadBufferExtended().

◆ smgrdestroy()

static void smgrdestroy ( SMgrRelation  reln)
static

Definition at line 280 of file smgr.c.

281 {
282  ForkNumber forknum;
283 
284  Assert(reln->pincount == 0);
285 
286  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
287  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
288 
289  dlist_delete(&reln->node);
290 
292  &(reln->smgr_rlocator),
293  HASH_REMOVE, NULL) == NULL)
294  elog(ERROR, "SMgrRelation hashtable corrupted");
295 }
#define Assert(condition)
Definition: c.h:837
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
@ HASH_REMOVE
Definition: hsearch.h:115
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
ForkNumber
Definition: relpath.h:56
#define MAX_FORKNUM
Definition: relpath.h:70
static HTAB * SMgrRelationHash
Definition: smgr.c:139
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:37
dlist_node node
Definition: smgr.h:68
int pincount
Definition: smgr.h:67
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:79

References Assert, dlist_delete(), elog, ERROR, HASH_REMOVE, hash_search(), MAX_FORKNUM, SMgrRelationData::node, SMgrRelationData::pincount, f_smgr::smgr_close, SMgrRelationData::smgr_rlocator, SMgrRelationData::smgr_which, SMgrRelationHash, and smgrsw.

Referenced by smgrdestroyall().

◆ smgrdestroyall()

void smgrdestroyall ( void  )

Definition at line 335 of file smgr.c.

336 {
337  dlist_mutable_iter iter;
338 
339  /*
340  * Zap all unpinned SMgrRelations. We rely on smgrdestroy() to remove
341  * each one from the list.
342  */
344  {
346  iter.cur);
347 
348  smgrdestroy(rel);
349  }
350 }
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:640
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
static void smgrdestroy(SMgrRelation reln)
Definition: smgr.c:280
static dlist_head unpinned_relns
Definition: smgr.c:141
dlist_node * cur
Definition: ilist.h:200

References dlist_mutable_iter::cur, dlist_container, dlist_foreach_modify, smgrdestroy(), and unpinned_relns.

Referenced by AtEOXact_SMgr(), BackgroundWriterMain(), CheckpointerMain(), RequestCheckpoint(), and XLogDropDatabase().

◆ smgrdosyncall()

void smgrdosyncall ( SMgrRelation rels,
int  nrels 
)

Definition at line 429 of file smgr.c.

430 {
431  int i = 0;
432  ForkNumber forknum;
433 
434  if (nrels == 0)
435  return;
436 
437  FlushRelationsAllBuffers(rels, nrels);
438 
439  /*
440  * Sync the physical file(s).
441  */
442  for (i = 0; i < nrels; i++)
443  {
444  int which = rels[i]->smgr_which;
445 
446  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
447  {
448  if (smgrsw[which].smgr_exists(rels[i], forknum))
449  smgrsw[which].smgr_immedsync(rels[i], forknum);
450  }
451  }
452 }
void FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
Definition: bufmgr.c:4590
int i
Definition: isn.c:72
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:105

References FlushRelationsAllBuffers(), i, MAX_FORKNUM, f_smgr::smgr_immedsync, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgrDoPendingSyncs().

◆ smgrdounlinkall()

void smgrdounlinkall ( SMgrRelation rels,
int  nrels,
bool  isRedo 
)

Definition at line 465 of file smgr.c.

466 {
467  int i = 0;
468  RelFileLocatorBackend *rlocators;
469  ForkNumber forknum;
470 
471  if (nrels == 0)
472  return;
473 
474  /*
475  * Get rid of any remaining buffers for the relations. bufmgr will just
476  * drop them without bothering to write the contents.
477  */
478  DropRelationsAllBuffers(rels, nrels);
479 
480  /*
481  * create an array which contains all relations to be dropped, and close
482  * each relation's forks at the smgr level while at it
483  */
484  rlocators = palloc(sizeof(RelFileLocatorBackend) * nrels);
485  for (i = 0; i < nrels; i++)
486  {
487  RelFileLocatorBackend rlocator = rels[i]->smgr_rlocator;
488  int which = rels[i]->smgr_which;
489 
490  rlocators[i] = rlocator;
491 
492  /* Close the forks at smgr level */
493  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
494  smgrsw[which].smgr_close(rels[i], forknum);
495  }
496 
497  /*
498  * Send a shared-inval message to force other backends to close any
499  * dangling smgr references they may have for these rels. We should do
500  * this before starting the actual unlinking, in case we fail partway
501  * through that step. Note that the sinval messages will eventually come
502  * back to this backend, too, and thereby provide a backstop that we
503  * closed our own smgr rel.
504  */
505  for (i = 0; i < nrels; i++)
506  CacheInvalidateSmgr(rlocators[i]);
507 
508  /*
509  * Delete the physical file(s).
510  *
511  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
512  * ERROR, because we've already decided to commit or abort the current
513  * xact.
514  */
515 
516  for (i = 0; i < nrels; i++)
517  {
518  int which = rels[i]->smgr_which;
519 
520  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
521  smgrsw[which].smgr_unlink(rlocators[i], forknum, isRedo);
522  }
523 
524  pfree(rlocators);
525 }
void DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators)
Definition: bufmgr.c:4154
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition: inval.c:1647
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317

References CacheInvalidateSmgr(), DropRelationsAllBuffers(), i, MAX_FORKNUM, palloc(), pfree(), SMgrRelationData::smgr_rlocator, SMgrRelationData::smgr_which, and smgrsw.

Referenced by DropRelationFiles(), RelationSetNewRelfilenumber(), and smgrDoPendingDeletes().

◆ smgrexists()

◆ smgrextend()

void smgrextend ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
const void *  buffer,
bool  skipFsync 
)

Definition at line 538 of file smgr.c.

540 {
541  smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
542  buffer, skipFsync);
543 
544  /*
545  * Normally we expect this to increase nblocks by one, but if the cached
546  * value isn't as expected, just invalidate it so the next call asks the
547  * kernel.
548  */
549  if (reln->smgr_cached_nblocks[forknum] == blocknum)
550  reln->smgr_cached_nblocks[forknum] = blocknum + 1;
551  else
552  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
553 }
#define InvalidBlockNumber
Definition: block.h:33
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:46
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.c:85

References InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_extend, SMgrRelationData::smgr_which, and smgrsw.

Referenced by _hash_alloc_buckets(), RelationCopyStorageUsingBuffer(), and smgr_bulk_flush().

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 811 of file smgr.c.

812 {
813  smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
814 }

References f_smgr::smgr_immedsync, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgr_bulk_finish().

◆ smgrinit()

void smgrinit ( void  )

Definition at line 157 of file smgr.c.

158 {
159  int i;
160 
161  for (i = 0; i < NSmgr; i++)
162  {
163  if (smgrsw[i].smgr_init)
164  smgrsw[i].smgr_init();
165  }
166 
167  /* register the shutdown proc */
169 }
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:309
static const int NSmgr
Definition: smgr.c:133
static void smgrshutdown(int code, Datum arg)
Definition: smgr.c:175
void(* smgr_init)(void)
Definition: smgr.c:76

References i, NSmgr, on_proc_exit(), f_smgr::smgr_init, smgrshutdown(), and smgrsw.

Referenced by BaseInit().

◆ smgrmaxcombine()

uint32 smgrmaxcombine ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum 
)

Definition at line 601 of file smgr.c.

603 {
604  return smgrsw[reln->smgr_which].smgr_maxcombine(reln, forknum, blocknum);
605 }
uint32(* smgr_maxcombine)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:91

References f_smgr::smgr_maxcombine, SMgrRelationData::smgr_which, and smgrsw.

Referenced by StartReadBuffersImpl().

◆ smgrnblocks()

BlockNumber smgrnblocks ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 677 of file smgr.c.

678 {
679  BlockNumber result;
680 
681  /* Check and return if we get the cached value for the number of blocks. */
682  result = smgrnblocks_cached(reln, forknum);
683  if (result != InvalidBlockNumber)
684  return result;
685 
686  result = smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
687 
688  reln->smgr_cached_nblocks[forknum] = result;
689 
690  return result;
691 }
uint32 BlockNumber
Definition: block.h:31
BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:701
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:102

References InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_nblocks, SMgrRelationData::smgr_which, smgrnblocks_cached(), and smgrsw.

Referenced by ExtendBufferedRelLocal(), ExtendBufferedRelShared(), ExtendBufferedRelTo(), FreeSpaceMapPrepareTruncateRel(), fsm_readbuf(), gistBuildCallback(), RelationCopyStorage(), RelationCopyStorageUsingBuffer(), RelationGetNumberOfBlocksInFork(), ScanSourceDatabasePgClass(), smgr_bulk_start_smgr(), smgrDoPendingSyncs(), table_block_relation_size(), visibilitymap_prepare_truncate(), vm_readbuf(), XLogPrefetcherNextBlock(), and XLogReadBufferExtended().

◆ smgrnblocks_cached()

BlockNumber smgrnblocks_cached ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 701 of file smgr.c.

702 {
703  /*
704  * For now, this function uses cached values only in recovery due to lack
705  * of a shared invalidation mechanism for changes in file size. Code
706  * elsewhere reads smgr_cached_nblocks and copes with stale data.
707  */
708  if (InRecovery && reln->smgr_cached_nblocks[forknum] != InvalidBlockNumber)
709  return reln->smgr_cached_nblocks[forknum];
710 
711  return InvalidBlockNumber;
712 }
bool InRecovery
Definition: xlogutils.c:50

References InRecovery, InvalidBlockNumber, and SMgrRelationData::smgr_cached_nblocks.

Referenced by DropRelationBuffers(), DropRelationsAllBuffers(), and smgrnblocks().

◆ smgropen()

SMgrRelation smgropen ( RelFileLocator  rlocator,
ProcNumber  backend 
)

Definition at line 201 of file smgr.c.

202 {
203  RelFileLocatorBackend brlocator;
204  SMgrRelation reln;
205  bool found;
206 
208 
209  if (SMgrRelationHash == NULL)
210  {
211  /* First time through: initialize the hash table */
212  HASHCTL ctl;
213 
214  ctl.keysize = sizeof(RelFileLocatorBackend);
215  ctl.entrysize = sizeof(SMgrRelationData);
216  SMgrRelationHash = hash_create("smgr relation table", 400,
217  &ctl, HASH_ELEM | HASH_BLOBS);
219  }
220 
221  /* Look up or create an entry */
222  brlocator.locator = rlocator;
223  brlocator.backend = backend;
225  &brlocator,
226  HASH_ENTER, &found);
227 
228  /* Initialize it if not present before */
229  if (!found)
230  {
231  /* hash_search already filled in the lookup key */
233  for (int i = 0; i <= MAX_FORKNUM; ++i)
235  reln->smgr_which = 0; /* we only have md.c at present */
236 
237  /* implementation-specific initialization */
238  smgrsw[reln->smgr_which].smgr_open(reln);
239 
240  /* it is not pinned yet */
241  reln->pincount = 0;
243  }
244 
245  return reln;
246 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
tree ctl
Definition: radixtree.h:1853
struct RelFileLocatorBackend RelFileLocatorBackend
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
struct SMgrRelationData SMgrRelationData
SMgrRelationData * SMgrRelation
Definition: smgr.h:71
RelFileLocator locator
RelFileNumber relNumber
BlockNumber smgr_targblock
Definition: smgr.h:45
void(* smgr_open)(SMgrRelation reln)
Definition: smgr.c:78

References Assert, RelFileLocatorBackend::backend, ctl, dlist_init(), dlist_push_tail(), HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), i, InvalidBlockNumber, RelFileLocatorBackend::locator, MAX_FORKNUM, SMgrRelationData::node, SMgrRelationData::pincount, RelFileNumberIsValid, RelFileLocator::relNumber, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_open, SMgrRelationData::smgr_targblock, SMgrRelationData::smgr_which, SMgrRelationHash, smgrsw, and unpinned_relns.

Referenced by CreateAndCopyRelationData(), CreateFakeRelcacheEntry(), DropRelationFiles(), fill_seq_with_data(), FlushBuffer(), GetLocalVictimBuffer(), IssuePendingWritebacks(), mdsyncfiletag(), ReadBufferWithoutRelcache(), RelationCopyStorageUsingBuffer(), RelationCreateStorage(), RelationGetSmgr(), RelationSetNewRelfilenumber(), ScanSourceDatabasePgClass(), smgr_redo(), smgrDoPendingDeletes(), smgrDoPendingSyncs(), XLogPrefetcherNextBlock(), and XLogReadBufferExtended().

◆ smgrpin()

void smgrpin ( SMgrRelation  reln)

Definition at line 253 of file smgr.c.

254 {
255  if (reln->pincount == 0)
256  dlist_delete(&reln->node);
257  reln->pincount++;
258 }

References dlist_delete(), SMgrRelationData::node, and SMgrRelationData::pincount.

Referenced by RelationGetSmgr().

◆ smgrprefetch()

bool smgrprefetch ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
int  nblocks 
)

Definition at line 588 of file smgr.c.

590 {
591  return smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum, nblocks);
592 }
bool(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition: smgr.c:89

References f_smgr::smgr_prefetch, SMgrRelationData::smgr_which, and smgrsw.

Referenced by PrefetchLocalBuffer(), PrefetchSharedBuffer(), and StartReadBuffersImpl().

◆ smgrreadv()

void smgrreadv ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
void **  buffers,
BlockNumber  nblocks 
)

Definition at line 619 of file smgr.c.

621 {
622  smgrsw[reln->smgr_which].smgr_readv(reln, forknum, blocknum, buffers,
623  nblocks);
624 }
void(* smgr_readv)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: smgr.c:93

References f_smgr::smgr_readv, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgrread(), and WaitReadBuffers().

◆ smgrregistersync()

void smgrregistersync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 779 of file smgr.c.

780 {
781  smgrsw[reln->smgr_which].smgr_registersync(reln, forknum);
782 }
void(* smgr_registersync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:106

References f_smgr::smgr_registersync, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgr_bulk_finish().

◆ smgrrelease()

void smgrrelease ( SMgrRelation  reln)

Definition at line 303 of file smgr.c.

304 {
305  for (ForkNumber forknum = 0; forknum <= MAX_FORKNUM; forknum++)
306  {
307  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
308  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
309  }
311 }

References InvalidBlockNumber, MAX_FORKNUM, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_close, SMgrRelationData::smgr_targblock, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgrclose(), smgrreleaseall(), and smgrreleaserellocator().

◆ smgrreleaseall()

void smgrreleaseall ( void  )

Definition at line 356 of file smgr.c.

357 {
358  HASH_SEQ_STATUS status;
359  SMgrRelation reln;
360 
361  /* Nothing to do if hashtable not set up */
362  if (SMgrRelationHash == NULL)
363  return;
364 
366 
367  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
368  {
369  smgrrelease(reln);
370  }
371 }
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1420
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385

References hash_seq_init(), hash_seq_search(), SMgrRelationHash, and smgrrelease().

Referenced by ProcessBarrierSmgrRelease(), and RelationCacheInvalidate().

◆ smgrreleaserellocator()

void smgrreleaserellocator ( RelFileLocatorBackend  rlocator)

Definition at line 382 of file smgr.c.

383 {
384  SMgrRelation reln;
385 
386  /* Nothing to do if hashtable not set up */
387  if (SMgrRelationHash == NULL)
388  return;
389 
391  &rlocator,
392  HASH_FIND, NULL);
393  if (reln != NULL)
394  smgrrelease(reln);
395 }
@ HASH_FIND
Definition: hsearch.h:113

References HASH_FIND, hash_search(), SMgrRelationHash, and smgrrelease().

Referenced by LocalExecuteInvalidationMessage().

◆ smgrshutdown()

static void smgrshutdown ( int  code,
Datum  arg 
)
static

Definition at line 175 of file smgr.c.

176 {
177  int i;
178 
179  for (i = 0; i < NSmgr; i++)
180  {
181  if (smgrsw[i].smgr_shutdown)
183  }
184 }
void(* smgr_shutdown)(void)
Definition: smgr.c:77

References i, NSmgr, f_smgr::smgr_shutdown, and smgrsw.

Referenced by smgrinit().

◆ smgrtruncate()

void smgrtruncate ( SMgrRelation  reln,
ForkNumber forknum,
int  nforks,
BlockNumber nblocks 
)

Definition at line 725 of file smgr.c.

726 {
727  int i;
728 
729  /*
730  * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
731  * just drop them without bothering to write the contents.
732  */
733  DropRelationBuffers(reln, forknum, nforks, nblocks);
734 
735  /*
736  * Send a shared-inval message to force other backends to close any smgr
737  * references they may have for this rel. This is useful because they
738  * might have open file pointers to segments that got removed, and/or
739  * smgr_targblock variables pointing past the new rel end. (The inval
740  * message will come back to our backend, too, causing a
741  * probably-unnecessary local smgr flush. But we don't expect that this
742  * is a performance-critical path.) As in the unlink code, we want to be
743  * sure the message is sent before we start changing things on-disk.
744  */
746 
747  /* Do the truncation */
748  for (i = 0; i < nforks; i++)
749  {
750  /* Make the cached size is invalid if we encounter an error. */
751  reln->smgr_cached_nblocks[forknum[i]] = InvalidBlockNumber;
752 
753  smgrsw[reln->smgr_which].smgr_truncate(reln, forknum[i], nblocks[i]);
754 
755  /*
756  * We might as well update the local smgr_cached_nblocks values. The
757  * smgr cache inval message that this function sent will cause other
758  * backends to invalidate their copies of smgr_cached_nblocks, and
759  * these ones too at the next command boundary. But ensure they aren't
760  * outright wrong until then.
761  */
762  reln->smgr_cached_nblocks[forknum[i]] = nblocks[i];
763  }
764 }
void DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Definition: bufmgr.c:4031
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:103

References CacheInvalidateSmgr(), DropRelationBuffers(), i, InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_rlocator, f_smgr::smgr_truncate, SMgrRelationData::smgr_which, and smgrsw.

Referenced by pg_truncate_visibility_map(), RelationTruncate(), and smgr_redo().

◆ smgrunpin()

void smgrunpin ( SMgrRelation  reln)

Definition at line 268 of file smgr.c.

269 {
270  Assert(reln->pincount > 0);
271  reln->pincount--;
272  if (reln->pincount == 0)
274 }

References Assert, dlist_push_tail(), SMgrRelationData::node, SMgrRelationData::pincount, and unpinned_relns.

Referenced by RelationCloseSmgr().

◆ smgrwriteback()

void smgrwriteback ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
BlockNumber  nblocks 
)

Definition at line 665 of file smgr.c.

667 {
668  smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
669  nblocks);
670 }
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:100

References SMgrRelationData::smgr_which, f_smgr::smgr_writeback, and smgrsw.

Referenced by IssuePendingWritebacks().

◆ smgrwritev()

void smgrwritev ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
const void **  buffers,
BlockNumber  nblocks,
bool  skipFsync 
)

Definition at line 653 of file smgr.c.

655 {
656  smgrsw[reln->smgr_which].smgr_writev(reln, forknum, blocknum,
657  buffers, nblocks, skipFsync);
658 }
void(* smgr_writev)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition: smgr.c:96

References SMgrRelationData::smgr_which, f_smgr::smgr_writev, and smgrsw.

Referenced by smgrwrite().

◆ smgrzeroextend()

void smgrzeroextend ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
int  nblocks,
bool  skipFsync 
)

Definition at line 563 of file smgr.c.

565 {
566  smgrsw[reln->smgr_which].smgr_zeroextend(reln, forknum, blocknum,
567  nblocks, skipFsync);
568 
569  /*
570  * Normally we expect this to increase the fork size by nblocks, but if
571  * the cached value isn't as expected, just invalidate it so the next call
572  * asks the kernel.
573  */
574  if (reln->smgr_cached_nblocks[forknum] == blocknum)
575  reln->smgr_cached_nblocks[forknum] = blocknum + nblocks;
576  else
577  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
578 }
void(* smgr_zeroextend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: smgr.c:87

References InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_which, f_smgr::smgr_zeroextend, and smgrsw.

Referenced by ExtendBufferedRelLocal(), and ExtendBufferedRelShared().

Variable Documentation

◆ NSmgr

const int NSmgr = lengthof(smgrsw)
static

Definition at line 133 of file smgr.c.

Referenced by smgrinit(), and smgrshutdown().

◆ SMgrRelationHash

HTAB* SMgrRelationHash = NULL
static

Definition at line 139 of file smgr.c.

Referenced by smgrdestroy(), smgropen(), smgrreleaseall(), and smgrreleaserellocator().

◆ smgrsw

const f_smgr smgrsw[]
static
Initial value:
= {
{
.smgr_init = mdinit,
.smgr_shutdown = NULL,
.smgr_open = mdopen,
.smgr_close = mdclose,
.smgr_create = mdcreate,
.smgr_exists = mdexists,
.smgr_unlink = mdunlink,
.smgr_extend = mdextend,
.smgr_zeroextend = mdzeroextend,
.smgr_prefetch = mdprefetch,
.smgr_maxcombine = mdmaxcombine,
.smgr_readv = mdreadv,
.smgr_writev = mdwritev,
.smgr_writeback = mdwriteback,
.smgr_nblocks = mdnblocks,
.smgr_truncate = mdtruncate,
.smgr_immedsync = mdimmedsync,
.smgr_registersync = mdregistersync,
}
}
void mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition: md.c:307
void mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition: md.c:946
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:171
void mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: md.c:825
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1110
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: md.c:190
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:460
void mdinit(void)
Definition: md.c:158
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:691
void mdzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: md.c:525
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:1051
uint32 mdmaxcombine(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:811
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition: md.c:714
void mdregistersync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1261
void mdopen(SMgrRelation reln)
Definition: md.c:680
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:1167
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1312

Definition at line 109 of file smgr.c.

Referenced by smgrcreate(), smgrdestroy(), smgrdosyncall(), smgrdounlinkall(), smgrexists(), smgrextend(), smgrimmedsync(), smgrinit(), smgrmaxcombine(), smgrnblocks(), smgropen(), smgrprefetch(), smgrreadv(), smgrregistersync(), smgrrelease(), smgrshutdown(), smgrtruncate(), smgrwriteback(), smgrwritev(), and smgrzeroextend().

◆ unpinned_relns

dlist_head unpinned_relns
static

Definition at line 141 of file smgr.c.

Referenced by smgrdestroyall(), smgropen(), and smgrunpin().