PostgreSQL Source Code  git master
smgr.h File Reference
#include "lib/ilist.h"
#include "storage/block.h"
#include "storage/relfilenode.h"
Include dependency graph for smgr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SMgrRelationData
 

Macros

#define SmgrIsTemp(smgr)   RelFileNodeBackendIsTemp((smgr)->smgr_rnode)
 

Typedefs

typedef struct SMgrRelationData SMgrRelationData
 
typedef SMgrRelationDataSMgrRelation
 

Functions

void smgrinit (void)
 
SMgrRelation smgropen (RelFileNode rnode, BackendId backend)
 
bool smgrexists (SMgrRelation reln, ForkNumber forknum)
 
void smgrsetowner (SMgrRelation *owner, SMgrRelation reln)
 
void smgrclearowner (SMgrRelation *owner, SMgrRelation reln)
 
void smgrclose (SMgrRelation reln)
 
void smgrcloseall (void)
 
void smgrclosenode (RelFileNodeBackend rnode)
 
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, char *buffer, bool skipFsync)
 
bool 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, int nforks, BlockNumber *nblocks)
 
void smgrimmedsync (SMgrRelation reln, ForkNumber forknum)
 
void AtEOXact_SMgr (void)
 

Macro Definition Documentation

◆ SmgrIsTemp

#define SmgrIsTemp (   smgr)    RelFileNodeBackendIsTemp((smgr)->smgr_rnode)

Definition at line 77 of file smgr.h.

Referenced by mdextend(), mdtruncate(), mdwrite(), ReadBuffer_common(), and register_dirty_segment().

Typedef Documentation

◆ SMgrRelation

Definition at line 75 of file smgr.h.

◆ SMgrRelationData

Function Documentation

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 661 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().

662 {
663  dlist_mutable_iter iter;
664 
665  /*
666  * Zap all unowned SMgrRelations. We rely on smgrclose() to remove each
667  * one from the list.
668  */
670  {
672  iter.cur);
673 
674  Assert(rel->smgr_owner == NULL);
675 
676  smgrclose(rel);
677  }
678 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:257
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:745
static dlist_head unowned_relns
Definition: smgr.c:96

◆ smgrclearowner()

void smgrclearowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 228 of file smgr.c.

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

Referenced by FreeFakeRelcacheEntry().

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

◆ smgrclose()

void smgrclose ( SMgrRelation  reln)

Definition at line 257 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().

258 {
259  SMgrRelation *owner;
260  ForkNumber forknum;
261 
262  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
263  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
264 
265  owner = reln->smgr_owner;
266 
267  if (!owner)
268  dlist_delete(&reln->node);
269 
271  (void *) &(reln->smgr_rnode),
272  HASH_REMOVE, NULL) == NULL)
273  elog(ERROR, "SMgrRelation hashtable corrupted");
274 
275  /*
276  * Unhook the owner pointer, if any. We do this last since in the remote
277  * possibility of failure above, the SMgrRelation object will still exist.
278  */
279  if (owner)
280  *owner = NULL;
281 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:45
static HTAB * SMgrRelationHash
Definition: smgr.c:94
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:927
#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:62
#define MAX_FORKNUM
Definition: relpath.h:55
#define elog(elevel,...)
Definition: elog.h:214
dlist_node node
Definition: smgr.h:72

◆ smgrcloseall()

void smgrcloseall ( void  )

Definition at line 287 of file smgr.c.

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

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

288 {
290  SMgrRelation reln;
291 
292  /* Nothing to do if hashtable not set up */
293  if (SMgrRelationHash == NULL)
294  return;
295 
297 
298  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
299  smgrclose(reln);
300 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:257
static HTAB * SMgrRelationHash
Definition: smgr.c:94
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1410
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1400
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ smgrclosenode()

void smgrclosenode ( RelFileNodeBackend  rnode)

Definition at line 311 of file smgr.c.

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

Referenced by LocalExecuteInvalidationMessage().

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

◆ smgrcreate()

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

Definition at line 334 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().

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

◆ smgrdosyncall()

void smgrdosyncall ( SMgrRelation rels,
int  nrels 
)

Definition at line 349 of file smgr.c.

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

Referenced by smgrDoPendingSyncs().

350 {
351  int i = 0;
352  ForkNumber forknum;
353 
354  if (nrels == 0)
355  return;
356 
357  FlushRelationsAllBuffers(rels, nrels);
358 
359  /*
360  * Sync the physical file(s).
361  */
362  for (i = 0; i < nrels; i++)
363  {
364  int which = rels[i]->smgr_which;
365 
366  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
367  {
368  if (smgrsw[which].smgr_exists(rels[i], forknum))
369  smgrsw[which].smgr_immedsync(rels[i], forknum);
370  }
371  }
372 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:64
void FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
Definition: bufmgr.c:3351
ForkNumber
Definition: relpath.h:40
int smgr_which
Definition: smgr.h:62
#define MAX_FORKNUM
Definition: relpath.h:55
int i

◆ smgrdounlinkall()

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

Definition at line 385 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().

386 {
387  int i = 0;
388  RelFileNodeBackend *rnodes;
389  ForkNumber forknum;
390 
391  if (nrels == 0)
392  return;
393 
394  /*
395  * create an array which contains all relations to be dropped, and close
396  * each relation's forks at the smgr level while at it
397  */
398  rnodes = palloc(sizeof(RelFileNodeBackend) * nrels);
399  for (i = 0; i < nrels; i++)
400  {
401  RelFileNodeBackend rnode = rels[i]->smgr_rnode;
402  int which = rels[i]->smgr_which;
403 
404  rnodes[i] = rnode;
405 
406  /* Close the forks at smgr level */
407  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
408  smgrsw[which].smgr_close(rels[i], forknum);
409  }
410 
411  /*
412  * Get rid of any remaining buffers for the relations. bufmgr will just
413  * drop them without bothering to write the contents.
414  */
415  DropRelFileNodesAllBuffers(rnodes, nrels);
416 
417  /*
418  * It'd be nice to tell the stats collector to forget them immediately,
419  * too. But we can't because we don't know the OIDs.
420  */
421 
422  /*
423  * Send a shared-inval message to force other backends to close any
424  * dangling smgr references they may have for these rels. We should do
425  * this before starting the actual unlinking, in case we fail partway
426  * through that step. Note that the sinval messages will eventually come
427  * back to this backend, too, and thereby provide a backstop that we
428  * closed our own smgr rel.
429  */
430  for (i = 0; i < nrels; i++)
431  CacheInvalidateSmgr(rnodes[i]);
432 
433  /*
434  * Delete the physical file(s).
435  *
436  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
437  * ERROR, because we've already decided to commit or abort the current
438  * xact.
439  */
440 
441  for (i = 0; i < nrels; i++)
442  {
443  int which = rels[i]->smgr_which;
444 
445  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
446  smgrsw[which].smgr_unlink(rnodes[i], forknum, isRedo);
447  }
448 
449  pfree(rnodes);
450 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1377
static const f_smgr smgrsw[]
Definition: smgr.c:67
void pfree(void *pointer)
Definition: mcxt.c:1056
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
ForkNumber
Definition: relpath.h:40
void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes)
Definition: bufmgr.c:3051
int smgr_which
Definition: smgr.h:62
#define MAX_FORKNUM
Definition: relpath.h:55
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ smgrexists()

bool smgrexists ( SMgrRelation  reln,
ForkNumber  forknum 
)

◆ smgrextend()

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

Definition at line 463 of file smgr.c.

References InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, 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().

465 {
466  smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
467  buffer, skipFsync);
468 
469  /*
470  * Normally we expect this to increase nblocks by one, but if the cached
471  * value isn't as expected, just invalidate it so the next call asks the
472  * kernel.
473  */
474  if (reln->smgr_cached_nblocks[forknum] == blocknum)
475  reln->smgr_cached_nblocks[forknum] = blocknum + 1;
476  else
477  reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
478 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
int smgr_which
Definition: smgr.h:62
#define InvalidBlockNumber
Definition: block.h:33
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:51

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 643 of file smgr.c.

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

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

644 {
645  smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
646 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:64
int smgr_which
Definition: smgr.h:62

◆ smgrinit()

void smgrinit ( void  )

Definition at line 111 of file smgr.c.

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

Referenced by BaseInit().

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 f_smgr smgrsw[]
Definition: smgr.c:67
static void smgrshutdown(int code, Datum arg)
Definition: smgr.c:129
void(* smgr_init)(void)
Definition: smgr.c:42
static const int NSmgr
Definition: smgr.c:88
int i

◆ smgrnblocks()

BlockNumber smgrnblocks ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 549 of file smgr.c.

References InRecovery, InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_nblocks, and SMgrRelationData::smgr_which.

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

550 {
551  BlockNumber result;
552 
553  /*
554  * For now, we only use cached values in recovery due to lack of a shared
555  * invalidation mechanism for changes in file size.
556  */
557  if (InRecovery && reln->smgr_cached_nblocks[forknum] != InvalidBlockNumber)
558  return reln->smgr_cached_nblocks[forknum];
559 
560  result = smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
561 
562  reln->smgr_cached_nblocks[forknum] = result;
563 
564  return result;
565 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
bool InRecovery
Definition: xlog.c:204
uint32 BlockNumber
Definition: block.h:31
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:61
int smgr_which
Definition: smgr.h:62
#define InvalidBlockNumber
Definition: block.h:33

◆ smgropen()

SMgrRelation smgropen ( RelFileNode  rnode,
BackendId  backend 
)

Definition at line 146 of file smgr.c.

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

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

147 {
148  RelFileNodeBackend brnode;
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  MemSet(&ctl, 0, sizeof(ctl));
158  ctl.keysize = sizeof(RelFileNodeBackend);
159  ctl.entrysize = sizeof(SMgrRelationData);
160  SMgrRelationHash = hash_create("smgr relation table", 400,
161  &ctl, HASH_ELEM | HASH_BLOBS);
163  }
164 
165  /* Look up or create an entry */
166  brnode.node = rnode;
167  brnode.backend = backend;
169  (void *) &brnode,
170  HASH_ENTER, &found);
171 
172  /* Initialize it if not present before */
173  if (!found)
174  {
175  /* hash_search already filled in the lookup key */
176  reln->smgr_owner = NULL;
178  for (int i = 0; i <= MAX_FORKNUM; ++i)
180  reln->smgr_which = 0; /* we only have md.c at present */
181 
182  /* implementation-specific initialization */
183  smgrsw[reln->smgr_which].smgr_open(reln);
184 
185  /* it has no owner yet */
187  }
188 
189  return reln;
190 }
struct SMgrRelationData SMgrRelationData
#define HASH_ELEM
Definition: hsearch.h:87
SMgrRelationData * SMgrRelation
Definition: smgr.h:75
static const f_smgr smgrsw[]
Definition: smgr.c:67
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:94
#define MemSet(start, val, len)
Definition: c.h:978
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:927
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
void(* smgr_open)(SMgrRelation reln)
Definition: smgr.c:44
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:328
int smgr_which
Definition: smgr.h:62
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:53
BackendId backend
Definition: relfilenode.h:75
#define InvalidBlockNumber
Definition: block.h:33
#define MAX_FORKNUM
Definition: relpath.h:55
int i
dlist_node node
Definition: smgr.h:72
static dlist_head unowned_relns
Definition: smgr.c:96

◆ smgrprefetch()

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

Definition at line 488 of file smgr.c.

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

Referenced by PrefetchLocalBuffer(), and PrefetchSharedBuffer().

489 {
490  return smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum);
491 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
bool(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:53
int smgr_which
Definition: smgr.h:62

◆ smgrread()

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

Definition at line 502 of file smgr.c.

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

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

504 {
505  smgrsw[reln->smgr_which].smgr_read(reln, forknum, blocknum, buffer);
506 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
void(* smgr_read)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: smgr.c:55
int smgr_which
Definition: smgr.h:62

◆ smgrsetowner()

void smgrsetowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 199 of file smgr.c.

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

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

◆ smgrtruncate()

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

Definition at line 578 of file smgr.c.

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

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

579 {
580  int i;
581 
582  /*
583  * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
584  * just drop them without bothering to write the contents.
585  */
586  DropRelFileNodeBuffers(reln->smgr_rnode, forknum, nforks, nblocks);
587 
588  /*
589  * Send a shared-inval message to force other backends to close any smgr
590  * references they may have for this rel. This is useful because they
591  * might have open file pointers to segments that got removed, and/or
592  * smgr_targblock variables pointing past the new rel end. (The inval
593  * message will come back to our backend, too, causing a
594  * probably-unnecessary local smgr flush. But we don't expect that this
595  * is a performance-critical path.) As in the unlink code, we want to be
596  * sure the message is sent before we start changing things on-disk.
597  */
599 
600  /* Do the truncation */
601  for (i = 0; i < nforks; i++)
602  {
603  /* Make the cached size is invalid if we encounter an error. */
604  reln->smgr_cached_nblocks[forknum[i]] = InvalidBlockNumber;
605 
606  smgrsw[reln->smgr_which].smgr_truncate(reln, forknum[i], nblocks[i]);
607 
608  /*
609  * We might as well update the local smgr_cached_nblocks values. The
610  * smgr cache inval message that this function sent will cause other
611  * backends to invalidate their copies of smgr_fsm_nblocks and
612  * smgr_vm_nblocks, and these ones too at the next command boundary.
613  * But these ensure they aren't outright wrong until then.
614  */
615  reln->smgr_cached_nblocks[forknum[i]] = nblocks[i];
616  }
617 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1377
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:62
static const f_smgr smgrsw[]
Definition: smgr.c:67
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
int smgr_which
Definition: smgr.h:62
#define InvalidBlockNumber
Definition: block.h:33
void DropRelFileNodeBuffers(RelFileNodeBackend rnode, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Definition: bufmgr.c:2982
int i

◆ smgrwrite()

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

Definition at line 524 of file smgr.c.

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

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

526 {
527  smgrsw[reln->smgr_which].smgr_write(reln, forknum, blocknum,
528  buffer, skipFsync);
529 }
void(* smgr_write)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:57
static const f_smgr smgrsw[]
Definition: smgr.c:67
int smgr_which
Definition: smgr.h:62

◆ smgrwriteback()

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

Definition at line 537 of file smgr.c.

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

Referenced by IssuePendingWritebacks().

539 {
540  smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
541  nblocks);
542 }
static const f_smgr smgrsw[]
Definition: smgr.c:67
int smgr_which
Definition: smgr.h:62
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:59