PostgreSQL Source Code  git master
smgr.c File Reference
#include "postgres.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 (RelFileNode rnode, 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 smgrcloseall (void)
 
void smgrclosenode (RelFileNodeBackend rnode)
 
void smgrcreate (SMgrRelation reln, ForkNumber forknum, bool isRedo)
 
void smgrdounlink (SMgrRelation reln, bool isRedo)
 
void smgrdounlinkall (SMgrRelation *rels, int nrels, bool isRedo)
 
void smgrdounlinkfork (SMgrRelation reln, ForkNumber forknum, bool isRedo)
 
void smgrextend (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
 
void smgrprefetch (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
 
void smgrread (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
 
void smgrwrite (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
 
void smgrwriteback (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
 
BlockNumber smgrnblocks (SMgrRelation reln, ForkNumber forknum)
 
void smgrtruncate (SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
 
void smgrimmedsync (SMgrRelation reln, ForkNumber forknum)
 
void AtEOXact_SMgr (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

Function Documentation

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 689 of file smgr.c.

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

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

690 {
691  dlist_mutable_iter iter;
692 
693  /*
694  * Zap all unowned SMgrRelations. We rely on smgrclose() to remove each
695  * one from the list.
696  */
698  {
700  iter.cur);
701 
702  Assert(rel->smgr_owner == NULL);
703 
704  smgrclose(rel);
705  }
706 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
dlist_node * cur
Definition: ilist.h:180
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:45
#define Assert(condition)
Definition: c.h:732
static dlist_head unowned_relns
Definition: smgr.c:95

◆ smgrclearowner()

void smgrclearowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 227 of file smgr.c.

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

Referenced by FreeFakeRelcacheEntry().

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:317
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:45
dlist_node node
Definition: smgr.h:74
static dlist_head unowned_relns
Definition: smgr.c:95

◆ smgrclose()

void smgrclose ( SMgrRelation  reln)

Definition at line 256 of file smgr.c.

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

Referenced by AtEOXact_SMgr(), DropRelationFiles(), heapam_relation_copy_data(), heapam_relation_set_new_filenode(), index_copy_data(), RelationSetNewRelfilenode(), smgrcloseall(), smgrclosenode(), and smgrDoPendingDeletes().

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  (void *) &(reln->smgr_rnode),
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 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:44
static HTAB * SMgrRelationHash
Definition: smgr.c:93
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
#define ERROR
Definition: elog.h:43
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
ForkNumber
Definition: relpath.h:40
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:45
int smgr_which
Definition: smgr.h:64
#define MAX_FORKNUM
Definition: relpath.h:55
#define elog(elevel,...)
Definition: elog.h:226
dlist_node node
Definition: smgr.h:74

◆ smgrcloseall()

void smgrcloseall ( void  )

Definition at line 286 of file smgr.c.

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

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

287 {
289  SMgrRelation reln;
290 
291  /* Nothing to do if hashtable not set up */
292  if (SMgrRelationHash == NULL)
293  return;
294 
296 
297  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
298  smgrclose(reln);
299 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
static HTAB * SMgrRelationHash
Definition: smgr.c:93
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1389
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1379
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ smgrclosenode()

void smgrclosenode ( RelFileNodeBackend  rnode)

Definition at line 310 of file smgr.c.

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

Referenced by LocalExecuteInvalidationMessage().

311 {
312  SMgrRelation reln;
313 
314  /* Nothing to do if hashtable not set up */
315  if (SMgrRelationHash == NULL)
316  return;
317 
319  (void *) &rnode,
320  HASH_FIND, NULL);
321  if (reln != NULL)
322  smgrclose(reln);
323 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
SMgrRelationData * SMgrRelation
Definition: smgr.h:77
static HTAB * SMgrRelationHash
Definition: smgr.c:93
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906

◆ smgrcreate()

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

Definition at line 333 of file smgr.c.

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

Referenced by fsm_extend(), heapam_relation_copy_data(), heapam_relation_set_new_filenode(), index_build(), index_copy_data(), RelationCreateStorage(), smgr_redo(), vm_extend(), and XLogReadBufferExtended().

334 {
335  smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
336 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:45
int smgr_which
Definition: smgr.h:64

◆ smgrdounlink()

void smgrdounlink ( SMgrRelation  reln,
bool  isRedo 
)

Definition at line 351 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodesAllBuffers(), InvalidForkNumber, MAX_FORKNUM, f_smgr::smgr_close, SMgrRelationData::smgr_rnode, f_smgr::smgr_unlink, and SMgrRelationData::smgr_which.

352 {
353  RelFileNodeBackend rnode = reln->smgr_rnode;
354  int which = reln->smgr_which;
355  ForkNumber forknum;
356 
357  /* Close the forks at smgr level */
358  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
359  smgrsw[which].smgr_close(reln, forknum);
360 
361  /*
362  * Get rid of any remaining buffers for the relation. bufmgr will just
363  * drop them without bothering to write the contents.
364  */
365  DropRelFileNodesAllBuffers(&rnode, 1);
366 
367  /*
368  * It'd be nice to tell the stats collector to forget it immediately, too.
369  * But we can't because we don't know the OID (and in cases involving
370  * relfilenode swaps, it's not always clear which table OID to forget,
371  * anyway).
372  */
373 
374  /*
375  * Send a shared-inval message to force other backends to close any
376  * dangling smgr references they may have for this rel. We should do this
377  * before starting the actual unlinking, in case we fail partway through
378  * that step. Note that the sinval message will eventually come back to
379  * this backend, too, and thereby provide a backstop that we closed our
380  * own smgr rel.
381  */
382  CacheInvalidateSmgr(rnode);
383 
384  /*
385  * Delete the physical file(s).
386  *
387  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
388  * ERROR, because we've already decided to commit or abort the current
389  * xact.
390  */
391  smgrsw[which].smgr_unlink(rnode, InvalidForkNumber, isRedo);
392 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1369
static const f_smgr smgrsw[]
Definition: smgr.c:66
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
void(* smgr_unlink)(RelFileNodeBackend rnode, ForkNumber forknum, bool isRedo)
Definition: smgr.c:48
ForkNumber
Definition: relpath.h:40
void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes)
Definition: bufmgr.c:2984
int smgr_which
Definition: smgr.h:64
#define MAX_FORKNUM
Definition: relpath.h:55

◆ smgrdounlinkall()

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

Definition at line 408 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodesAllBuffers(), i, MAX_FORKNUM, palloc(), pfree(), f_smgr::smgr_close, SMgrRelationData::smgr_rnode, f_smgr::smgr_unlink, and SMgrRelationData::smgr_which.

Referenced by DropRelationFiles(), and smgrDoPendingDeletes().

409 {
410  int i = 0;
411  RelFileNodeBackend *rnodes;
412  ForkNumber forknum;
413 
414  if (nrels == 0)
415  return;
416 
417  /*
418  * create an array which contains all relations to be dropped, and close
419  * each relation's forks at the smgr level while at it
420  */
421  rnodes = palloc(sizeof(RelFileNodeBackend) * nrels);
422  for (i = 0; i < nrels; i++)
423  {
424  RelFileNodeBackend rnode = rels[i]->smgr_rnode;
425  int which = rels[i]->smgr_which;
426 
427  rnodes[i] = rnode;
428 
429  /* Close the forks at smgr level */
430  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
431  smgrsw[which].smgr_close(rels[i], forknum);
432  }
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  DropRelFileNodesAllBuffers(rnodes, nrels);
439 
440  /*
441  * It'd be nice to tell the stats collector to forget them immediately,
442  * too. But we can't because we don't know the OIDs.
443  */
444 
445  /*
446  * Send a shared-inval message to force other backends to close any
447  * dangling smgr references they may have for these rels. We should do
448  * this before starting the actual unlinking, in case we fail partway
449  * through that step. Note that the sinval messages will eventually come
450  * back to this backend, too, and thereby provide a backstop that we
451  * closed our own smgr rel.
452  */
453  for (i = 0; i < nrels; i++)
454  CacheInvalidateSmgr(rnodes[i]);
455 
456  /*
457  * Delete the physical file(s).
458  *
459  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
460  * ERROR, because we've already decided to commit or abort the current
461  * xact.
462  */
463 
464  for (i = 0; i < nrels; i++)
465  {
466  int which = rels[i]->smgr_which;
467 
468  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
469  smgrsw[which].smgr_unlink(rnodes[i], forknum, isRedo);
470  }
471 
472  pfree(rnodes);
473 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1369
static const f_smgr smgrsw[]
Definition: smgr.c:66
void pfree(void *pointer)
Definition: mcxt.c:1031
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
ForkNumber
Definition: relpath.h:40
void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes)
Definition: bufmgr.c:2984
int smgr_which
Definition: smgr.h:64
#define MAX_FORKNUM
Definition: relpath.h:55
void * palloc(Size size)
Definition: mcxt.c:924
int i

◆ smgrdounlinkfork()

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

Definition at line 486 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodeBuffers(), f_smgr::smgr_close, SMgrRelationData::smgr_rnode, f_smgr::smgr_unlink, and SMgrRelationData::smgr_which.

487 {
488  RelFileNodeBackend rnode = reln->smgr_rnode;
489  int which = reln->smgr_which;
490 
491  /* Close the fork at smgr level */
492  smgrsw[which].smgr_close(reln, forknum);
493 
494  /*
495  * Get rid of any remaining buffers for the fork. bufmgr will just drop
496  * them without bothering to write the contents.
497  */
498  DropRelFileNodeBuffers(rnode, forknum, 0);
499 
500  /*
501  * It'd be nice to tell the stats collector to forget it immediately, too.
502  * But we can't because we don't know the OID (and in cases involving
503  * relfilenode swaps, it's not always clear which table OID to forget,
504  * anyway).
505  */
506 
507  /*
508  * Send a shared-inval message to force other backends to close any
509  * dangling smgr references they may have for this rel. We should do this
510  * before starting the actual unlinking, in case we fail partway through
511  * that step. Note that the sinval message will eventually come back to
512  * this backend, too, and thereby provide a backstop that we closed our
513  * own smgr rel.
514  */
515  CacheInvalidateSmgr(rnode);
516 
517  /*
518  * Delete the physical file(s).
519  *
520  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
521  * ERROR, because we've already decided to commit or abort the current
522  * xact.
523  */
524  smgrsw[which].smgr_unlink(rnode, forknum, isRedo);
525 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1369
static const f_smgr smgrsw[]
Definition: smgr.c:66
void DropRelFileNodeBuffers(RelFileNodeBackend rnode, ForkNumber forkNum, BlockNumber firstDelBlock)
Definition: bufmgr.c:2927
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:44
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
void(* smgr_unlink)(RelFileNodeBackend rnode, ForkNumber forknum, bool isRedo)
Definition: smgr.c:48
int smgr_which
Definition: smgr.h:64

◆ smgrexists()

bool smgrexists ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 247 of file smgr.c.

References f_smgr::smgr_exists, and SMgrRelationData::smgr_which.

Referenced by autoprewarm_database_main(), bt_index_check_internal(), FreeSpaceMapTruncateRel(), fsm_extend(), fsm_readbuf(), heapam_relation_copy_data(), index_build(), index_copy_data(), pg_prewarm(), RelationTruncate(), smgr_redo(), visibilitymap_truncate(), vm_extend(), and vm_readbuf().

248 {
249  return smgrsw[reln->smgr_which].smgr_exists(reln, forknum);
250 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
bool(* smgr_exists)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:47
int smgr_which
Definition: smgr.h:64

◆ smgrextend()

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

Definition at line 537 of file smgr.c.

References f_smgr::smgr_extend, and SMgrRelationData::smgr_which.

Referenced by _bt_blwritepage(), _hash_alloc_buckets(), end_heap_rewrite(), fsm_extend(), raw_heap_insert(), ReadBuffer_common(), RelationCopyStorage(), and vm_extend().

539 {
540  smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
541  buffer, skipFsync);
542 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
int smgr_which
Definition: smgr.h:64
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:50

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 671 of file smgr.c.

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

Referenced by _bt_load(), blbuildempty(), btbuildempty(), heap_sync(), heapam_relation_set_new_filenode(), RelationCopyStorage(), and spgbuildempty().

672 {
673  smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
674 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:63
int smgr_which
Definition: smgr.h:64

◆ smgrinit()

void smgrinit ( void  )

Definition at line 110 of file smgr.c.

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

Referenced by BaseInit().

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

◆ smgrnblocks()

BlockNumber smgrnblocks ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 609 of file smgr.c.

References f_smgr::smgr_nblocks, and SMgrRelationData::smgr_which.

Referenced by FreeSpaceMapTruncateRel(), fsm_extend(), fsm_readbuf(), gistBuildCallback(), ReadBuffer_common(), RelationCopyStorage(), RelationGetNumberOfBlocksInFork(), table_block_relation_size(), visibilitymap_truncate(), vm_extend(), vm_readbuf(), and XLogReadBufferExtended().

610 {
611  return smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
612 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:60
int smgr_which
Definition: smgr.h:64

◆ smgropen()

SMgrRelation smgropen ( RelFileNode  rnode,
BackendId  backend 
)

Definition at line 145 of file smgr.c.

References RelFileNodeBackend::backend, dlist_init(), dlist_push_tail(), HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), InvalidBlockNumber, HASHCTL::keysize, MemSet, RelFileNodeBackend::node, SMgrRelationData::node, SMgrRelationData::smgr_fsm_nblocks, f_smgr::smgr_open, SMgrRelationData::smgr_owner, SMgrRelationData::smgr_targblock, SMgrRelationData::smgr_vm_nblocks, and SMgrRelationData::smgr_which.

Referenced by DropRelationFiles(), FlushBuffer(), heapam_relation_copy_data(), index_copy_data(), IssuePendingWritebacks(), LocalBufferAlloc(), mdsyncfiletag(), ReadBufferWithoutRelcache(), RelationCreateStorage(), smgr_redo(), smgrDoPendingDeletes(), and XLogReadBufferExtended().

146 {
147  RelFileNodeBackend brnode;
148  SMgrRelation reln;
149  bool found;
150 
151  if (SMgrRelationHash == NULL)
152  {
153  /* First time through: initialize the hash table */
154  HASHCTL ctl;
155 
156  MemSet(&ctl, 0, sizeof(ctl));
157  ctl.keysize = sizeof(RelFileNodeBackend);
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  brnode.node = rnode;
166  brnode.backend = backend;
168  (void *) &brnode,
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;
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 }
struct SMgrRelationData SMgrRelationData
BlockNumber smgr_vm_nblocks
Definition: smgr.h:56
#define HASH_ELEM
Definition: hsearch.h:87
SMgrRelationData * SMgrRelation
Definition: smgr.h:77
static const f_smgr smgrsw[]
Definition: smgr.c:66
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
Size entrysize
Definition: hsearch.h:73
static HTAB * SMgrRelationHash
Definition: smgr.c:93
#define MemSet(start, val, len)
Definition: c.h:955
BlockNumber smgr_fsm_nblocks
Definition: smgr.h:55
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
void(* smgr_open)(SMgrRelation reln)
Definition: smgr.c:43
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:45
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
int smgr_which
Definition: smgr.h:64
struct RelFileNodeBackend RelFileNodeBackend
Size keysize
Definition: hsearch.h:72
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
RelFileNode node
Definition: relfilenode.h:74
BlockNumber smgr_targblock
Definition: smgr.h:54
BackendId backend
Definition: relfilenode.h:75
#define InvalidBlockNumber
Definition: block.h:33
dlist_node node
Definition: smgr.h:74
static dlist_head unowned_relns
Definition: smgr.c:95

◆ smgrprefetch()

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

Definition at line 548 of file smgr.c.

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

Referenced by LocalPrefetchBuffer(), and PrefetchBuffer().

549 {
550  smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum);
551 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
void(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:52
int smgr_which
Definition: smgr.h:64

◆ smgrread()

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

Definition at line 562 of file smgr.c.

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

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

564 {
565  smgrsw[reln->smgr_which].smgr_read(reln, forknum, blocknum, buffer);
566 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
void(* smgr_read)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: smgr.c:54
int smgr_which
Definition: smgr.h:64

◆ smgrsetowner()

void smgrsetowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 198 of file smgr.c.

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

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 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:45
#define Assert(condition)
Definition: c.h:732
dlist_node node
Definition: smgr.h:74

◆ smgrshutdown()

static void smgrshutdown ( int  code,
Datum  arg 
)
static

Definition at line 128 of file smgr.c.

References i, NSmgr, and f_smgr::smgr_shutdown.

Referenced by smgrinit().

129 {
130  int i;
131 
132  for (i = 0; i < NSmgr; i++)
133  {
134  if (smgrsw[i].smgr_shutdown)
136  }
137 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
static const int NSmgr
Definition: smgr.c:87
int i
void(* smgr_shutdown)(void)
Definition: smgr.c:42

◆ smgrtruncate()

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

Definition at line 621 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodeBuffers(), SMgrRelationData::smgr_rnode, f_smgr::smgr_truncate, and SMgrRelationData::smgr_which.

Referenced by FreeSpaceMapTruncateRel(), RelationTruncate(), smgr_redo(), and visibilitymap_truncate().

622 {
623  /*
624  * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
625  * just drop them without bothering to write the contents.
626  */
627  DropRelFileNodeBuffers(reln->smgr_rnode, forknum, nblocks);
628 
629  /*
630  * Send a shared-inval message to force other backends to close any smgr
631  * references they may have for this rel. This is useful because they
632  * might have open file pointers to segments that got removed, and/or
633  * smgr_targblock variables pointing past the new rel end. (The inval
634  * message will come back to our backend, too, causing a
635  * probably-unnecessary local smgr flush. But we don't expect that this
636  * is a performance-critical path.) As in the unlink code, we want to be
637  * sure the message is sent before we start changing things on-disk.
638  */
640 
641  /*
642  * Do the truncation.
643  */
644  smgrsw[reln->smgr_which].smgr_truncate(reln, forknum, nblocks);
645 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1369
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:61
static const f_smgr smgrsw[]
Definition: smgr.c:66
void DropRelFileNodeBuffers(RelFileNodeBackend rnode, ForkNumber forkNum, BlockNumber firstDelBlock)
Definition: bufmgr.c:2927
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
int smgr_which
Definition: smgr.h:64

◆ smgrwrite()

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

Definition at line 584 of file smgr.c.

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

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

586 {
587  smgrsw[reln->smgr_which].smgr_write(reln, forknum, blocknum,
588  buffer, skipFsync);
589 }
void(* smgr_write)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:56
static const f_smgr smgrsw[]
Definition: smgr.c:66
int smgr_which
Definition: smgr.h:64

◆ smgrwriteback()

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

Definition at line 597 of file smgr.c.

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

Referenced by IssuePendingWritebacks().

599 {
600  smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
601  nblocks);
602 }
static const f_smgr smgrsw[]
Definition: smgr.c:66
int smgr_which
Definition: smgr.h:64
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:58

Variable Documentation

◆ NSmgr

const int NSmgr = lengthof(smgrsw)
static

Definition at line 87 of file smgr.c.

Referenced by smgrinit(), and smgrshutdown().

◆ SMgrRelationHash

HTAB* SMgrRelationHash = NULL
static

Definition at line 93 of file smgr.c.

◆ 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 mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:891
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:739
void mdinit(void)
Definition: md.c:148
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:388
void mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
Definition: md.c:278
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:560
void mdopen(SMgrRelation reln)
Definition: md.c:495
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:794
bool mdexists(SMgrRelation reln, ForkNumber forkNum)
Definition: md.c:161
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:674
void mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:537
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:506
void mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
Definition: md.c:178
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: md.c:609

Definition at line 66 of file smgr.c.

◆ unowned_relns

dlist_head unowned_relns
static

Definition at line 95 of file smgr.c.