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/fd.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)
 
void smgrinit (void)
 
SMgrRelation smgropen (RelFileLocator rlocator, BackendId backend)
 
void smgrsetowner (SMgrRelation *owner, SMgrRelation reln)
 
void smgrclearowner (SMgrRelation *owner, SMgrRelation reln)
 
bool smgrexists (SMgrRelation reln, ForkNumber forknum)
 
void smgrclose (SMgrRelation reln)
 
void smgrrelease (SMgrRelation reln)
 
void smgrreleaseall (void)
 
void smgrcloseall (void)
 
void smgrcloserellocator (RelFileLocatorBackend rlocator)
 
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)
 
void smgrread (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
 
void smgrwrite (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, 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 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 unowned_relns
 

Typedef Documentation

◆ f_smgr

typedef struct f_smgr f_smgr

Function Documentation

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 739 of file smgr.c.

740 {
741  dlist_mutable_iter iter;
742 
743  /*
744  * Zap all unowned SMgrRelations. We rely on smgrclose() to remove each
745  * one from the list.
746  */
748  {
750  iter.cur);
751 
752  Assert(rel->smgr_owner == NULL);
753 
754  smgrclose(rel);
755  }
756 }
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:640
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
Assert(fmt[strlen(fmt) - 1] !='\n')
void smgrclose(SMgrRelation reln)
Definition: smgr.c:260
static dlist_head unowned_relns
Definition: smgr.c:100
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:45
dlist_node * cur
Definition: ilist.h:200

References Assert(), dlist_mutable_iter::cur, dlist_container, dlist_foreach_modify, SMgrRelationData::smgr_owner, smgrclose(), and unowned_relns.

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

◆ ProcessBarrierSmgrRelease()

bool ProcessBarrierSmgrRelease ( void  )

Definition at line 763 of file smgr.c.

764 {
765  smgrreleaseall();
766  return true;
767 }
void smgrreleaseall(void)
Definition: smgr.c:308

References smgrreleaseall().

Referenced by ProcessProcSignalBarrier().

◆ smgrclearowner()

void smgrclearowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 231 of file smgr.c.

232 {
233  /* Do nothing if the SMgrRelation object is not owned by the owner */
234  if (reln->smgr_owner != owner)
235  return;
236 
237  /* unset the owner's reference */
238  *owner = NULL;
239 
240  /* unset our reference to the owner */
241  reln->smgr_owner = NULL;
242 
243  /* add to list of unowned relations */
245 }
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
dlist_node node
Definition: smgr.h:72

References dlist_push_tail(), SMgrRelationData::node, SMgrRelationData::smgr_owner, and unowned_relns.

Referenced by FreeFakeRelcacheEntry().

◆ smgrclose()

void smgrclose ( SMgrRelation  reln)

Definition at line 260 of file smgr.c.

261 {
262  SMgrRelation *owner;
263  ForkNumber forknum;
264 
265  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
266  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
267 
268  owner = reln->smgr_owner;
269 
270  if (!owner)
271  dlist_delete(&reln->node);
272 
274  &(reln->smgr_rlocator),
275  HASH_REMOVE, NULL) == NULL)
276  elog(ERROR, "SMgrRelation hashtable corrupted");
277 
278  /*
279  * Unhook the owner pointer, if any. We do this last since in the remote
280  * possibility of failure above, the SMgrRelation object will still exist.
281  */
282  if (owner)
283  *owner = NULL;
284 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
#define ERROR
Definition: elog.h:39
@ HASH_REMOVE
Definition: hsearch.h:115
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
ForkNumber
Definition: relpath.h:48
#define MAX_FORKNUM
Definition: relpath.h:62
static HTAB * SMgrRelationHash
Definition: smgr.c:98
static const f_smgr smgrsw[]
Definition: smgr.c:70
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:42
int smgr_which
Definition: smgr.h:62
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:46

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

Referenced by AtEOXact_SMgr(), DropRelationFiles(), fill_seq_with_data(), heapam_relation_copy_data(), heapam_relation_set_new_filelocator(), index_copy_data(), RelationCloseSmgr(), RelationSetNewRelfilenumber(), ScanSourceDatabasePgClass(), smgrcloseall(), smgrcloserellocator(), and smgrDoPendingDeletes().

◆ smgrcloseall()

void smgrcloseall ( void  )

Definition at line 327 of file smgr.c.

328 {
329  HASH_SEQ_STATUS status;
330  SMgrRelation reln;
331 
332  /* Nothing to do if hashtable not set up */
333  if (SMgrRelationHash == NULL)
334  return;
335 
337 
338  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
339  smgrclose(reln);
340 }
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1431
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1421

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

Referenced by BackgroundWriterMain(), CheckpointerMain(), RelationCacheInvalidate(), RequestCheckpoint(), WalWriterMain(), and XLogDropDatabase().

◆ smgrcloserellocator()

void smgrcloserellocator ( RelFileLocatorBackend  rlocator)

Definition at line 351 of file smgr.c.

352 {
353  SMgrRelation reln;
354 
355  /* Nothing to do if hashtable not set up */
356  if (SMgrRelationHash == NULL)
357  return;
358 
360  &rlocator,
361  HASH_FIND, NULL);
362  if (reln != NULL)
363  smgrclose(reln);
364 }
@ HASH_FIND
Definition: hsearch.h:113
SMgrRelationData * SMgrRelation
Definition: smgr.h:75

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

Referenced by CreateAndCopyRelationData(), and LocalExecuteInvalidationMessage().

◆ smgrcreate()

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

Definition at line 374 of file smgr.c.

375 {
376  smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
377 }
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:47

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().

◆ smgrdosyncall()

void smgrdosyncall ( SMgrRelation rels,
int  nrels 
)

Definition at line 389 of file smgr.c.

390 {
391  int i = 0;
392  ForkNumber forknum;
393 
394  if (nrels == 0)
395  return;
396 
397  FlushRelationsAllBuffers(rels, nrels);
398 
399  /*
400  * Sync the physical file(s).
401  */
402  for (i = 0; i < nrels; i++)
403  {
404  int which = rels[i]->smgr_which;
405 
406  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
407  {
408  if (smgrsw[which].smgr_exists(rels[i], forknum))
409  smgrsw[which].smgr_immedsync(rels[i], forknum);
410  }
411  }
412 }
void FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
Definition: bufmgr.c:4156
int i
Definition: isn.c:73
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:67

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 425 of file smgr.c.

426 {
427  int i = 0;
428  RelFileLocatorBackend *rlocators;
429  ForkNumber forknum;
430 
431  if (nrels == 0)
432  return;
433 
434  /*
435  * Get rid of any remaining buffers for the relations. bufmgr will just
436  * drop them without bothering to write the contents.
437  */
438  DropRelationsAllBuffers(rels, nrels);
439 
440  /*
441  * create an array which contains all relations to be dropped, and close
442  * each relation's forks at the smgr level while at it
443  */
444  rlocators = palloc(sizeof(RelFileLocatorBackend) * nrels);
445  for (i = 0; i < nrels; i++)
446  {
447  RelFileLocatorBackend rlocator = rels[i]->smgr_rlocator;
448  int which = rels[i]->smgr_which;
449 
450  rlocators[i] = rlocator;
451 
452  /* Close the forks at smgr level */
453  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
454  smgrsw[which].smgr_close(rels[i], forknum);
455  }
456 
457  /*
458  * Send a shared-inval message to force other backends to close any
459  * dangling smgr references they may have for these rels. We should do
460  * this before starting the actual unlinking, in case we fail partway
461  * through that step. Note that the sinval messages will eventually come
462  * back to this backend, too, and thereby provide a backstop that we
463  * closed our own smgr rel.
464  */
465  for (i = 0; i < nrels; i++)
466  CacheInvalidateSmgr(rlocators[i]);
467 
468  /*
469  * Delete the physical file(s).
470  *
471  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
472  * ERROR, because we've already decided to commit or abort the current
473  * xact.
474  */
475 
476  for (i = 0; i < nrels; i++)
477  {
478  int which = rels[i]->smgr_which;
479 
480  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
481  smgrsw[which].smgr_unlink(rlocators[i], forknum, isRedo);
482  }
483 
484  pfree(rlocators);
485 }
void DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators)
Definition: bufmgr.c:3720
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition: inval.c:1462
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc(Size size)
Definition: mcxt.c:1226

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 498 of file smgr.c.

500 {
501  smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
502  buffer, skipFsync);
503 
504  /*
505  * Normally we expect this to increase nblocks by one, but if the cached
506  * value isn't as expected, just invalidate it so the next call asks the
507  * kernel.
508  */
509  if (reln->smgr_cached_nblocks[forknum] == blocknum)
510  reln->smgr_cached_nblocks[forknum] = blocknum + 1;
511  else
512  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
513 }
#define InvalidBlockNumber
Definition: block.h:33
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.c:52

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

Referenced by _bt_blwritepage(), _hash_alloc_buckets(), end_heap_rewrite(), gist_indexsortbuild(), gist_indexsortbuild_flush_ready_pages(), raw_heap_insert(), RelationCopyStorage(), and RelationCopyStorageUsingBuffer().

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 721 of file smgr.c.

722 {
723  smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
724 }

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

Referenced by _bt_load(), end_heap_rewrite(), gist_indexsortbuild(), and RelationCopyStorage().

◆ smgrinit()

void smgrinit ( void  )

Definition at line 115 of file smgr.c.

116 {
117  int i;
118 
119  for (i = 0; i < NSmgr; i++)
120  {
121  if (smgrsw[i].smgr_init)
122  smgrsw[i].smgr_init();
123  }
124 
125  /* register the shutdown proc */
127 }
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:305
static const int NSmgr
Definition: smgr.c:92
static void smgrshutdown(int code, Datum arg)
Definition: smgr.c:133
void(* smgr_init)(void)
Definition: smgr.c:43

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

Referenced by BaseInit().

◆ smgrnblocks()

BlockNumber smgrnblocks ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 609 of file smgr.c.

610 {
611  BlockNumber result;
612 
613  /* Check and return if we get the cached value for the number of blocks. */
614  result = smgrnblocks_cached(reln, forknum);
615  if (result != InvalidBlockNumber)
616  return result;
617 
618  result = smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
619 
620  reln->smgr_cached_nblocks[forknum] = result;
621 
622  return result;
623 }
uint32 BlockNumber
Definition: block.h:31
BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:633
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:64

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(), 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 633 of file smgr.c.

634 {
635  /*
636  * For now, we only use cached values in recovery due to lack of a shared
637  * invalidation mechanism for changes in file size.
638  */
639  if (InRecovery && reln->smgr_cached_nblocks[forknum] != InvalidBlockNumber)
640  return reln->smgr_cached_nblocks[forknum];
641 
642  return InvalidBlockNumber;
643 }
bool InRecovery
Definition: xlogutils.c:53

References InRecovery, InvalidBlockNumber, and SMgrRelationData::smgr_cached_nblocks.

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

◆ smgropen()

SMgrRelation smgropen ( RelFileLocator  rlocator,
BackendId  backend 
)

Definition at line 150 of file smgr.c.

151 {
152  RelFileLocatorBackend brlocator;
153  SMgrRelation reln;
154  bool found;
155 
156  if (SMgrRelationHash == NULL)
157  {
158  /* First time through: initialize the hash table */
159  HASHCTL ctl;
160 
161  ctl.keysize = sizeof(RelFileLocatorBackend);
162  ctl.entrysize = sizeof(SMgrRelationData);
163  SMgrRelationHash = hash_create("smgr relation table", 400,
164  &ctl, HASH_ELEM | HASH_BLOBS);
166  }
167 
168  /* Look up or create an entry */
169  brlocator.locator = rlocator;
170  brlocator.backend = backend;
172  &brlocator,
173  HASH_ENTER, &found);
174 
175  /* Initialize it if not present before */
176  if (!found)
177  {
178  /* hash_search already filled in the lookup key */
179  reln->smgr_owner = NULL;
181  for (int i = 0; i <= MAX_FORKNUM; ++i)
183  reln->smgr_which = 0; /* we only have md.c at present */
184 
185  /* implementation-specific initialization */
186  smgrsw[reln->smgr_which].smgr_open(reln);
187 
188  /* it has no owner yet */
190  }
191 
192  return reln;
193 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
@ 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
struct RelFileLocatorBackend RelFileLocatorBackend
struct SMgrRelationData SMgrRelationData
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
RelFileLocator locator
BlockNumber smgr_targblock
Definition: smgr.h:53
void(* smgr_open)(SMgrRelation reln)
Definition: smgr.c:45

References RelFileLocatorBackend::backend, dlist_init(), dlist_push_tail(), HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), i, InvalidBlockNumber, HASHCTL::keysize, RelFileLocatorBackend::locator, MAX_FORKNUM, SMgrRelationData::node, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_open, SMgrRelationData::smgr_owner, SMgrRelationData::smgr_targblock, SMgrRelationData::smgr_which, SMgrRelationHash, smgrsw, and unowned_relns.

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

◆ smgrprefetch()

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

Definition at line 548 of file smgr.c.

549 {
550  return smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum);
551 }
bool(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:56

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

Referenced by PrefetchLocalBuffer(), and PrefetchSharedBuffer().

◆ smgrread()

void smgrread ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
void *  buffer 
)

Definition at line 562 of file smgr.c.

564 {
565  smgrsw[reln->smgr_which].smgr_read(reln, forknum, blocknum, buffer);
566 }
void(* smgr_read)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: smgr.c:58

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

Referenced by pg_prewarm(), ReadBuffer_common(), and RelationCopyStorage().

◆ smgrrelease()

void smgrrelease ( SMgrRelation  reln)

Definition at line 292 of file smgr.c.

293 {
294  for (ForkNumber forknum = 0; forknum <= MAX_FORKNUM; forknum++)
295  {
296  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
297  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
298  }
300 }

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

Referenced by smgrreleaseall().

◆ smgrreleaseall()

void smgrreleaseall ( void  )

Definition at line 308 of file smgr.c.

309 {
310  HASH_SEQ_STATUS status;
311  SMgrRelation reln;
312 
313  /* Nothing to do if hashtable not set up */
314  if (SMgrRelationHash == NULL)
315  return;
316 
318 
319  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
320  smgrrelease(reln);
321 }
void smgrrelease(SMgrRelation reln)
Definition: smgr.c:292

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

Referenced by ProcessBarrierSmgrRelease().

◆ smgrsetowner()

void smgrsetowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 202 of file smgr.c.

203 {
204  /* We don't support "disowning" an SMgrRelation here, use smgrclearowner */
205  Assert(owner != NULL);
206 
207  /*
208  * First, unhook any old owner. (Normally there shouldn't be any, but it
209  * seems possible that this can happen during swap_relation_files()
210  * depending on the order of processing. It's ok to close the old
211  * relcache entry early in that case.)
212  *
213  * If there isn't an old owner, then the reln should be in the unowned
214  * list, and we need to remove it.
215  */
216  if (reln->smgr_owner)
217  *(reln->smgr_owner) = NULL;
218  else
219  dlist_delete(&reln->node);
220 
221  /* Now establish the ownership relationship. */
222  reln->smgr_owner = owner;
223  *owner = reln;
224 }

References Assert(), dlist_delete(), SMgrRelationData::node, and SMgrRelationData::smgr_owner.

Referenced by RelationGetSmgr().

◆ smgrshutdown()

static void smgrshutdown ( int  code,
Datum  arg 
)
static

Definition at line 133 of file smgr.c.

134 {
135  int i;
136 
137  for (i = 0; i < NSmgr; i++)
138  {
139  if (smgrsw[i].smgr_shutdown)
141  }
142 }
void(* smgr_shutdown)(void)
Definition: smgr.c:44

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 656 of file smgr.c.

657 {
658  int i;
659 
660  /*
661  * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
662  * just drop them without bothering to write the contents.
663  */
664  DropRelationBuffers(reln, forknum, nforks, nblocks);
665 
666  /*
667  * Send a shared-inval message to force other backends to close any smgr
668  * references they may have for this rel. This is useful because they
669  * might have open file pointers to segments that got removed, and/or
670  * smgr_targblock variables pointing past the new rel end. (The inval
671  * message will come back to our backend, too, causing a
672  * probably-unnecessary local smgr flush. But we don't expect that this
673  * is a performance-critical path.) As in the unlink code, we want to be
674  * sure the message is sent before we start changing things on-disk.
675  */
677 
678  /* Do the truncation */
679  for (i = 0; i < nforks; i++)
680  {
681  /* Make the cached size is invalid if we encounter an error. */
682  reln->smgr_cached_nblocks[forknum[i]] = InvalidBlockNumber;
683 
684  smgrsw[reln->smgr_which].smgr_truncate(reln, forknum[i], nblocks[i]);
685 
686  /*
687  * We might as well update the local smgr_cached_nblocks values. The
688  * smgr cache inval message that this function sent will cause other
689  * backends to invalidate their copies of smgr_fsm_nblocks and
690  * smgr_vm_nblocks, and these ones too at the next command boundary.
691  * But these ensure they aren't outright wrong until then.
692  */
693  reln->smgr_cached_nblocks[forknum[i]] = nblocks[i];
694  }
695 }
void DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Definition: bufmgr.c:3597
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:65

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().

◆ smgrwrite()

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

Definition at line 584 of file smgr.c.

586 {
587  smgrsw[reln->smgr_which].smgr_write(reln, forknum, blocknum,
588  buffer, skipFsync);
589 }
void(* smgr_write)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.c:60

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

Referenced by _bt_blwritepage(), FlushBuffer(), FlushRelationBuffers(), GetLocalVictimBuffer(), and gist_indexsortbuild().

◆ smgrwriteback()

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

Definition at line 597 of file smgr.c.

599 {
600  smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
601  nblocks);
602 }
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:62

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

Referenced by IssuePendingWritebacks().

◆ smgrzeroextend()

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

Definition at line 523 of file smgr.c.

525 {
526  smgrsw[reln->smgr_which].smgr_zeroextend(reln, forknum, blocknum,
527  nblocks, skipFsync);
528 
529  /*
530  * Normally we expect this to increase the fork size by nblocks, but if
531  * the cached value isn't as expected, just invalidate it so the next call
532  * asks the kernel.
533  */
534  if (reln->smgr_cached_nblocks[forknum] == blocknum)
535  reln->smgr_cached_nblocks[forknum] = blocknum + nblocks;
536  else
537  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
538 }
void(* smgr_zeroextend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: smgr.c:54

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 92 of file smgr.c.

Referenced by smgrinit(), and smgrshutdown().

◆ SMgrRelationHash

HTAB* SMgrRelationHash = NULL
static

Definition at line 98 of file smgr.c.

Referenced by smgrclose(), smgrcloseall(), smgrcloserellocator(), smgropen(), and smgrreleaseall().

◆ 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_read = mdread,
.smgr_write = mdwrite,
.smgr_writeback = mdwriteback,
.smgr_nblocks = mdnblocks,
.smgr_truncate = mdtruncate,
.smgr_immedsync = mdimmedsync,
}
}
void mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition: md.c:309
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:812
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:173
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:938
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: md.c:192
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:462
void mdinit(void)
Definition: md.c:160
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:693
void mdzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: md.c:527
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:879
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: md.c:743
void mdopen(SMgrRelation reln)
Definition: md.c:682
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:716
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:995
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1097

Definition at line 70 of file smgr.c.

Referenced by smgrclose(), smgrcreate(), smgrdosyncall(), smgrdounlinkall(), smgrexists(), smgrextend(), smgrimmedsync(), smgrinit(), smgrnblocks(), smgropen(), smgrprefetch(), smgrread(), smgrrelease(), smgrshutdown(), smgrtruncate(), smgrwrite(), smgrwriteback(), and smgrzeroextend().

◆ unowned_relns

dlist_head unowned_relns
static

Definition at line 100 of file smgr.c.

Referenced by AtEOXact_SMgr(), smgrclearowner(), and smgropen().