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

710 {
711  dlist_mutable_iter iter;
712 
713  /*
714  * Zap all unowned SMgrRelations. We rely on smgrclose() to remove each
715  * one from the list.
716  */
718  {
720  iter.cur);
721 
722  Assert(rel->smgr_owner == NULL);
723 
724  smgrclose(rel);
725  }
726 }
#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:256
static dlist_head unowned_relns
Definition: smgr.c:96
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 733 of file smgr.c.

734 {
735  smgrreleaseall();
736  return true;
737 }
void smgrreleaseall(void)
Definition: smgr.c:303

References smgrreleaseall().

Referenced by ProcessProcSignalBarrier().

◆ smgrclearowner()

void smgrclearowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 227 of file smgr.c.

228 {
229  /* Do nothing if the SMgrRelation object is not owned by the owner */
230  if (reln->smgr_owner != owner)
231  return;
232 
233  /* unset the owner's reference */
234  *owner = NULL;
235 
236  /* unset our reference to the owner */
237  reln->smgr_owner = NULL;
238 
239  /* add to list of unowned relations */
241 }
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 256 of file smgr.c.

257 {
258  SMgrRelation *owner;
259  ForkNumber forknum;
260 
261  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
262  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
263 
264  owner = reln->smgr_owner;
265 
266  if (!owner)
267  dlist_delete(&reln->node);
268 
270  &(reln->smgr_rlocator),
271  HASH_REMOVE, NULL) == NULL)
272  elog(ERROR, "SMgrRelation hashtable corrupted");
273 
274  /*
275  * Unhook the owner pointer, if any. We do this last since in the remote
276  * possibility of failure above, the SMgrRelation object will still exist.
277  */
278  if (owner)
279  *owner = NULL;
280 }
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:94
static const f_smgr smgrsw[]
Definition: smgr.c:67
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:42
int smgr_which
Definition: smgr.h:62
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:45

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

323 {
325  SMgrRelation reln;
326 
327  /* Nothing to do if hashtable not set up */
328  if (SMgrRelationHash == NULL)
329  return;
330 
332 
333  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
334  smgrclose(reln);
335 }
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
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224

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

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

◆ smgrcloserellocator()

void smgrcloserellocator ( RelFileLocatorBackend  rlocator)

Definition at line 346 of file smgr.c.

347 {
348  SMgrRelation reln;
349 
350  /* Nothing to do if hashtable not set up */
351  if (SMgrRelationHash == NULL)
352  return;
353 
355  &rlocator,
356  HASH_FIND, NULL);
357  if (reln != NULL)
358  smgrclose(reln);
359 }
@ 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 369 of file smgr.c.

370 {
371  smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
372 }
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:46

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

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

◆ smgrdosyncall()

void smgrdosyncall ( SMgrRelation rels,
int  nrels 
)

Definition at line 384 of file smgr.c.

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

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

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

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

495 {
496  smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
497  buffer, skipFsync);
498 
499  /*
500  * Normally we expect this to increase nblocks by one, but if the cached
501  * value isn't as expected, just invalidate it so the next call asks the
502  * kernel.
503  */
504  if (reln->smgr_cached_nblocks[forknum] == blocknum)
505  reln->smgr_cached_nblocks[forknum] = blocknum + 1;
506  else
507  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
508 }
#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:51

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(), fsm_extend(), gist_indexsortbuild(), gist_indexsortbuild_flush_ready_pages(), raw_heap_insert(), ReadBuffer_common(), RelationCopyStorage(), RelationCopyStorageUsingBuffer(), and vm_extend().

◆ smgrimmedsync()

◆ smgrinit()

void smgrinit ( void  )

Definition at line 111 of file smgr.c.

112 {
113  int i;
114 
115  for (i = 0; i < NSmgr; i++)
116  {
117  if (smgrsw[i].smgr_init)
118  smgrsw[i].smgr_init();
119  }
120 
121  /* register the shutdown proc */
123 }
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:305
static const int NSmgr
Definition: smgr.c:88
static void smgrshutdown(int code, Datum arg)
Definition: smgr.c:129
void(* smgr_init)(void)
Definition: smgr.c:42

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

580 {
581  BlockNumber result;
582 
583  /* Check and return if we get the cached value for the number of blocks. */
584  result = smgrnblocks_cached(reln, forknum);
585  if (result != InvalidBlockNumber)
586  return result;
587 
588  result = smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
589 
590  reln->smgr_cached_nblocks[forknum] = result;
591 
592  return result;
593 }
uint32 BlockNumber
Definition: block.h:31
BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:603
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:61

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

Referenced by FreeSpaceMapPrepareTruncateRel(), fsm_extend(), fsm_readbuf(), gistBuildCallback(), ReadBuffer_common(), RelationCopyStorage(), RelationCopyStorageUsingBuffer(), RelationGetNumberOfBlocksInFork(), ScanSourceDatabasePgClass(), smgrDoPendingSyncs(), table_block_relation_size(), visibilitymap_prepare_truncate(), vm_extend(), vm_readbuf(), XLogPrefetcherNextBlock(), and XLogReadBufferExtended().

◆ smgrnblocks_cached()

BlockNumber smgrnblocks_cached ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 603 of file smgr.c.

604 {
605  /*
606  * For now, we only use cached values in recovery due to lack of a shared
607  * invalidation mechanism for changes in file size.
608  */
609  if (InRecovery && reln->smgr_cached_nblocks[forknum] != InvalidBlockNumber)
610  return reln->smgr_cached_nblocks[forknum];
611 
612  return InvalidBlockNumber;
613 }
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 146 of file smgr.c.

147 {
148  RelFileLocatorBackend brlocator;
149  SMgrRelation reln;
150  bool found;
151 
152  if (SMgrRelationHash == NULL)
153  {
154  /* First time through: initialize the hash table */
155  HASHCTL ctl;
156 
157  ctl.keysize = sizeof(RelFileLocatorBackend);
158  ctl.entrysize = sizeof(SMgrRelationData);
159  SMgrRelationHash = hash_create("smgr relation table", 400,
160  &ctl, HASH_ELEM | HASH_BLOBS);
162  }
163 
164  /* Look up or create an entry */
165  brlocator.locator = rlocator;
166  brlocator.backend = backend;
168  &brlocator,
169  HASH_ENTER, &found);
170 
171  /* Initialize it if not present before */
172  if (!found)
173  {
174  /* hash_search already filled in the lookup key */
175  reln->smgr_owner = NULL;
177  for (int i = 0; i <= MAX_FORKNUM; ++i)
179  reln->smgr_which = 0; /* we only have md.c at present */
180 
181  /* implementation-specific initialization */
182  smgrsw[reln->smgr_which].smgr_open(reln);
183 
184  /* it has no owner yet */
186  }
187 
188  return reln;
189 }
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:44

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(), heapam_relation_copy_data(), index_copy_data(), IssuePendingWritebacks(), LocalBufferAlloc(), 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 518 of file smgr.c.

519 {
520  return smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum);
521 }
bool(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:53

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

534 {
535  smgrsw[reln->smgr_which].smgr_read(reln, forknum, blocknum, buffer);
536 }
void(* smgr_read)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: smgr.c:55

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

289 {
290  for (ForkNumber forknum = 0; forknum <= MAX_FORKNUM; forknum++)
291  {
292  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
293  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
294  }
295 }

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

Referenced by smgrreleaseall().

◆ smgrreleaseall()

void smgrreleaseall ( void  )

Definition at line 303 of file smgr.c.

304 {
306  SMgrRelation reln;
307 
308  /* Nothing to do if hashtable not set up */
309  if (SMgrRelationHash == NULL)
310  return;
311 
313 
314  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
315  smgrrelease(reln);
316 }
void smgrrelease(SMgrRelation reln)
Definition: smgr.c:288

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

Referenced by ProcessBarrierSmgrRelease().

◆ smgrsetowner()

void smgrsetowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 198 of file smgr.c.

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

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

130 {
131  int i;
132 
133  for (i = 0; i < NSmgr; i++)
134  {
135  if (smgrsw[i].smgr_shutdown)
137  }
138 }
void(* smgr_shutdown)(void)
Definition: smgr.c:43

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

627 {
628  int i;
629 
630  /*
631  * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
632  * just drop them without bothering to write the contents.
633  */
634  DropRelationBuffers(reln, forknum, nforks, nblocks);
635 
636  /*
637  * Send a shared-inval message to force other backends to close any smgr
638  * references they may have for this rel. This is useful because they
639  * might have open file pointers to segments that got removed, and/or
640  * smgr_targblock variables pointing past the new rel end. (The inval
641  * message will come back to our backend, too, causing a
642  * probably-unnecessary local smgr flush. But we don't expect that this
643  * is a performance-critical path.) As in the unlink code, we want to be
644  * sure the message is sent before we start changing things on-disk.
645  */
647 
648  /* Do the truncation */
649  for (i = 0; i < nforks; i++)
650  {
651  /* Make the cached size is invalid if we encounter an error. */
652  reln->smgr_cached_nblocks[forknum[i]] = InvalidBlockNumber;
653 
654  smgrsw[reln->smgr_which].smgr_truncate(reln, forknum[i], nblocks[i]);
655 
656  /*
657  * We might as well update the local smgr_cached_nblocks values. The
658  * smgr cache inval message that this function sent will cause other
659  * backends to invalidate their copies of smgr_fsm_nblocks and
660  * smgr_vm_nblocks, and these ones too at the next command boundary.
661  * But these ensure they aren't outright wrong until then.
662  */
663  reln->smgr_cached_nblocks[forknum[i]] = nblocks[i];
664  }
665 }
void DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Definition: bufmgr.c:3129
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:62

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

556 {
557  smgrsw[reln->smgr_which].smgr_write(reln, forknum, blocknum,
558  buffer, skipFsync);
559 }
void(* smgr_write)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.c:57

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

Referenced by _bt_blwritepage(), blbuildempty(), btbuildempty(), FlushBuffer(), FlushRelationBuffers(), gist_indexsortbuild(), LocalBufferAlloc(), and spgbuildempty().

◆ smgrwriteback()

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

Definition at line 567 of file smgr.c.

569 {
570  smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
571  nblocks);
572 }
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:59

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

Referenced by IssuePendingWritebacks().

Variable Documentation

◆ NSmgr

const int NSmgr = lengthof(smgrsw)
static

Definition at line 88 of file smgr.c.

Referenced by smgrinit(), and smgrshutdown().

◆ SMgrRelationHash

HTAB* SMgrRelationHash = NULL
static

Definition at line 94 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_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:296
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:736
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:163
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:801
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: md.c:182
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:449
void mdinit(void)
Definition: md.c:150
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:568
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:619
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: md.c:671
void mdopen(SMgrRelation reln)
Definition: md.c:557
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:591
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:858
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:960

Definition at line 67 of file smgr.c.

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

◆ unowned_relns

dlist_head unowned_relns
static

Definition at line 96 of file smgr.c.

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