PostgreSQL Source Code  git master
md.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * md.c
4  * This code manages relations that reside on magnetic disk.
5  *
6  * Or at least, that was what the Berkeley folk had in mind when they named
7  * this file. In reality, what this code provides is an interface from
8  * the smgr API to Unix-like filesystem APIs, so it will work with any type
9  * of device for which the operating system provides filesystem support.
10  * It doesn't matter whether the bits are on spinning rust or some other
11  * storage technology.
12  *
13  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  *
17  * IDENTIFICATION
18  * src/backend/storage/smgr/md.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 #include "postgres.h"
23 
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/file.h>
27 
28 #include "access/xlog.h"
29 #include "access/xlogutils.h"
30 #include "commands/tablespace.h"
31 #include "miscadmin.h"
32 #include "pg_trace.h"
33 #include "pgstat.h"
34 #include "postmaster/bgwriter.h"
35 #include "storage/bufmgr.h"
36 #include "storage/fd.h"
37 #include "storage/md.h"
38 #include "storage/relfilenode.h"
39 #include "storage/smgr.h"
40 #include "storage/sync.h"
41 #include "utils/hsearch.h"
42 #include "utils/memutils.h"
43 
44 /*
45  * The magnetic disk storage manager keeps track of open file
46  * descriptors in its own descriptor pool. This is done to make it
47  * easier to support relations that are larger than the operating
48  * system's file size limit (often 2GBytes). In order to do that,
49  * we break relations up into "segment" files that are each shorter than
50  * the OS file size limit. The segment size is set by the RELSEG_SIZE
51  * configuration constant in pg_config.h.
52  *
53  * On disk, a relation must consist of consecutively numbered segment
54  * files in the pattern
55  * -- Zero or more full segments of exactly RELSEG_SIZE blocks each
56  * -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
57  * -- Optionally, any number of inactive segments of size 0 blocks.
58  * The full and partial segments are collectively the "active" segments.
59  * Inactive segments are those that once contained data but are currently
60  * not needed because of an mdtruncate() operation. The reason for leaving
61  * them present at size zero, rather than unlinking them, is that other
62  * backends and/or the checkpointer might be holding open file references to
63  * such segments. If the relation expands again after mdtruncate(), such
64  * that a deactivated segment becomes active again, it is important that
65  * such file references still be valid --- else data might get written
66  * out to an unlinked old copy of a segment file that will eventually
67  * disappear.
68  *
69  * File descriptors are stored in the per-fork md_seg_fds arrays inside
70  * SMgrRelation. The length of these arrays is stored in md_num_open_segs.
71  * Note that a fork's md_num_open_segs having a specific value does not
72  * necessarily mean the relation doesn't have additional segments; we may
73  * just not have opened the next segment yet. (We could not have "all
74  * segments are in the array" as an invariant anyway, since another backend
75  * could extend the relation while we aren't looking.) We do not have
76  * entries for inactive segments, however; as soon as we find a partial
77  * segment, we assume that any subsequent segments are inactive.
78  *
79  * The entire MdfdVec array is palloc'd in the MdCxt memory context.
80  */
81 
82 typedef struct _MdfdVec
83 {
84  File mdfd_vfd; /* fd number in fd.c's pool */
85  BlockNumber mdfd_segno; /* segment number, from 0 */
86 } MdfdVec;
87 
88 static MemoryContext MdCxt; /* context for all MdfdVec objects */
89 
90 
91 /* Populate a file tag describing an md.c segment file. */
92 #define INIT_MD_FILETAG(a,xx_rnode,xx_forknum,xx_segno) \
93 ( \
94  memset(&(a), 0, sizeof(FileTag)), \
95  (a).handler = SYNC_HANDLER_MD, \
96  (a).rnode = (xx_rnode), \
97  (a).forknum = (xx_forknum), \
98  (a).segno = (xx_segno) \
99 )
100 
101 
102 /*** behavior for mdopen & _mdfd_getseg ***/
103 /* ereport if segment not present */
104 #define EXTENSION_FAIL (1 << 0)
105 /* return NULL if segment not present */
106 #define EXTENSION_RETURN_NULL (1 << 1)
107 /* create new segments as needed */
108 #define EXTENSION_CREATE (1 << 2)
109 /* create new segments if needed during recovery */
110 #define EXTENSION_CREATE_RECOVERY (1 << 3)
111 /*
112  * Allow opening segments which are preceded by segments smaller than
113  * RELSEG_SIZE, e.g. inactive segments (see above). Note that this breaks
114  * mdnblocks() and related functionality henceforth - which currently is ok,
115  * because this is only required in the checkpointer which never uses
116  * mdnblocks().
117  */
118 #define EXTENSION_DONT_CHECK_SIZE (1 << 4)
119 
120 
121 /* local routines */
122 static void mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum,
123  bool isRedo);
124 static MdfdVec *mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior);
125 static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
126  MdfdVec *seg);
127 static void register_unlink_segment(RelFileNodeBackend rnode, ForkNumber forknum,
128  BlockNumber segno);
129 static void register_forget_request(RelFileNodeBackend rnode, ForkNumber forknum,
130  BlockNumber segno);
131 static void _fdvec_resize(SMgrRelation reln,
132  ForkNumber forknum,
133  int nseg);
134 static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
135  BlockNumber segno);
136 static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
137  BlockNumber segno, int oflags);
138 static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forkno,
139  BlockNumber blkno, bool skipFsync, int behavior);
140 static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
141  MdfdVec *seg);
142 
143 
144 /*
145  * mdinit() -- Initialize private state for magnetic disk storage manager.
146  */
147 void
148 mdinit(void)
149 {
151  "MdSmgr",
153 }
154 
155 /*
156  * mdexists() -- Does the physical file exist?
157  *
158  * Note: this will return true for lingering files, with pending deletions
159  */
160 bool
162 {
163  /*
164  * Close it first, to ensure that we notice if the fork has been unlinked
165  * since we opened it.
166  */
167  mdclose(reln, forkNum);
168 
169  return (mdopenfork(reln, forkNum, EXTENSION_RETURN_NULL) != NULL);
170 }
171 
172 /*
173  * mdcreate() -- Create a new relation on magnetic disk.
174  *
175  * If isRedo is true, it's okay for the relation to exist already.
176  */
177 void
178 mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
179 {
180  MdfdVec *mdfd;
181  char *path;
182  File fd;
183 
184  if (isRedo && reln->md_num_open_segs[forkNum] > 0)
185  return; /* created and opened already... */
186 
187  Assert(reln->md_num_open_segs[forkNum] == 0);
188 
189  /*
190  * We may be using the target table space for the first time in this
191  * database, so create a per-database subdirectory if needed.
192  *
193  * XXX this is a fairly ugly violation of module layering, but this seems
194  * to be the best place to put the check. Maybe TablespaceCreateDbspace
195  * should be here and not in commands/tablespace.c? But that would imply
196  * importing a lot of stuff that smgr.c oughtn't know, either.
197  */
199  reln->smgr_rnode.node.dbNode,
200  isRedo);
201 
202  path = relpath(reln->smgr_rnode, forkNum);
203 
204  fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
205 
206  if (fd < 0)
207  {
208  int save_errno = errno;
209 
210  if (isRedo)
211  fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
212  if (fd < 0)
213  {
214  /* be sure to report the error reported by create, not open */
215  errno = save_errno;
216  ereport(ERROR,
218  errmsg("could not create file \"%s\": %m", path)));
219  }
220  }
221 
222  pfree(path);
223 
224  _fdvec_resize(reln, forkNum, 1);
225  mdfd = &reln->md_seg_fds[forkNum][0];
226  mdfd->mdfd_vfd = fd;
227  mdfd->mdfd_segno = 0;
228 }
229 
230 /*
231  * mdunlink() -- Unlink a relation.
232  *
233  * Note that we're passed a RelFileNodeBackend --- by the time this is called,
234  * there won't be an SMgrRelation hashtable entry anymore.
235  *
236  * forkNum can be a fork number to delete a specific fork, or InvalidForkNumber
237  * to delete all forks.
238  *
239  * For regular relations, we don't unlink the first segment file of the rel,
240  * but just truncate it to zero length, and record a request to unlink it after
241  * the next checkpoint. Additional segments can be unlinked immediately,
242  * however. Leaving the empty file in place prevents that relfilenode
243  * number from being reused. The scenario this protects us from is:
244  * 1. We delete a relation (and commit, and actually remove its file).
245  * 2. We create a new relation, which by chance gets the same relfilenode as
246  * the just-deleted one (OIDs must've wrapped around for that to happen).
247  * 3. We crash before another checkpoint occurs.
248  * During replay, we would delete the file and then recreate it, which is fine
249  * if the contents of the file were repopulated by subsequent WAL entries.
250  * But if we didn't WAL-log insertions, but instead relied on fsyncing the
251  * file after populating it (as we do at wal_level=minimal), the contents of
252  * the file would be lost forever. By leaving the empty file until after the
253  * next checkpoint, we prevent reassignment of the relfilenode number until
254  * it's safe, because relfilenode assignment skips over any existing file.
255  *
256  * We do not need to go through this dance for temp relations, though, because
257  * we never make WAL entries for temp rels, and so a temp rel poses no threat
258  * to the health of a regular rel that has taken over its relfilenode number.
259  * The fact that temp rels and regular rels have different file naming
260  * patterns provides additional safety.
261  *
262  * All the above applies only to the relation's main fork; other forks can
263  * just be removed immediately, since they are not needed to prevent the
264  * relfilenode number from being recycled. Also, we do not carefully
265  * track whether other forks have been created or not, but just attempt to
266  * unlink them unconditionally; so we should never complain about ENOENT.
267  *
268  * If isRedo is true, it's unsurprising for the relation to be already gone.
269  * Also, we should remove the file immediately instead of queuing a request
270  * for later, since during redo there's no possibility of creating a
271  * conflicting relation.
272  *
273  * Note: any failure should be reported as WARNING not ERROR, because
274  * we are usually not in a transaction anymore when this is called.
275  */
276 void
277 mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
278 {
279  /* Now do the per-fork work */
280  if (forkNum == InvalidForkNumber)
281  {
282  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
283  mdunlinkfork(rnode, forkNum, isRedo);
284  }
285  else
286  mdunlinkfork(rnode, forkNum, isRedo);
287 }
288 
289 /*
290  * Truncate a file to release disk space.
291  */
292 static int
293 do_truncate(const char *path)
294 {
295  int save_errno;
296  int ret;
297 
298  ret = pg_truncate(path, 0);
299 
300  /* Log a warning here to avoid repetition in callers. */
301  if (ret < 0 && errno != ENOENT)
302  {
303  save_errno = errno;
306  errmsg("could not truncate file \"%s\": %m", path)));
307  errno = save_errno;
308  }
309 
310  return ret;
311 }
312 
313 static void
314 mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
315 {
316  char *path;
317  int ret;
318 
319  path = relpath(rnode, forkNum);
320 
321  /*
322  * Delete or truncate the first segment.
323  */
324  if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
325  {
326  if (!RelFileNodeBackendIsTemp(rnode))
327  {
328  /* Prevent other backends' fds from holding on to the disk space */
329  ret = do_truncate(path);
330 
331  /* Forget any pending sync requests for the first segment */
332  register_forget_request(rnode, forkNum, 0 /* first seg */ );
333  }
334  else
335  ret = 0;
336 
337  /* Next unlink the file, unless it was already found to be missing */
338  if (ret == 0 || errno != ENOENT)
339  {
340  ret = unlink(path);
341  if (ret < 0 && errno != ENOENT)
344  errmsg("could not remove file \"%s\": %m", path)));
345  }
346  }
347  else
348  {
349  /* Prevent other backends' fds from holding on to the disk space */
350  ret = do_truncate(path);
351 
352  /* Register request to unlink first segment later */
353  register_unlink_segment(rnode, forkNum, 0 /* first seg */ );
354  }
355 
356  /*
357  * Delete any additional segments.
358  */
359  if (ret >= 0)
360  {
361  char *segpath = (char *) palloc(strlen(path) + 12);
362  BlockNumber segno;
363 
364  /*
365  * Note that because we loop until getting ENOENT, we will correctly
366  * remove all inactive segments as well as active ones.
367  */
368  for (segno = 1;; segno++)
369  {
370  sprintf(segpath, "%s.%u", path, segno);
371 
372  if (!RelFileNodeBackendIsTemp(rnode))
373  {
374  /*
375  * Prevent other backends' fds from holding on to the disk
376  * space.
377  */
378  if (do_truncate(segpath) < 0 && errno == ENOENT)
379  break;
380 
381  /*
382  * Forget any pending sync requests for this segment before we
383  * try to unlink.
384  */
385  register_forget_request(rnode, forkNum, segno);
386  }
387 
388  if (unlink(segpath) < 0)
389  {
390  /* ENOENT is expected after the last segment... */
391  if (errno != ENOENT)
394  errmsg("could not remove file \"%s\": %m", segpath)));
395  break;
396  }
397  }
398  pfree(segpath);
399  }
400 
401  pfree(path);
402 }
403 
404 /*
405  * mdextend() -- Add a block to the specified relation.
406  *
407  * The semantics are nearly the same as mdwrite(): write at the
408  * specified position. However, this is to be used for the case of
409  * extending a relation (i.e., blocknum is at or beyond the current
410  * EOF). Note that we assume writing a block beyond current EOF
411  * causes intervening file space to become filled with zeroes.
412  */
413 void
414 mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
415  char *buffer, bool skipFsync)
416 {
417  off_t seekpos;
418  int nbytes;
419  MdfdVec *v;
420 
421  /* This assert is too expensive to have on normally ... */
422 #ifdef CHECK_WRITE_VS_EXTEND
423  Assert(blocknum >= mdnblocks(reln, forknum));
424 #endif
425 
426  /*
427  * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
428  * more --- we mustn't create a block whose number actually is
429  * InvalidBlockNumber. (Note that this failure should be unreachable
430  * because of upstream checks in bufmgr.c.)
431  */
432  if (blocknum == InvalidBlockNumber)
433  ereport(ERROR,
434  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
435  errmsg("cannot extend file \"%s\" beyond %u blocks",
436  relpath(reln->smgr_rnode, forknum),
438 
439  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
440 
441  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
442 
443  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
444 
445  if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
446  {
447  if (nbytes < 0)
448  ereport(ERROR,
450  errmsg("could not extend file \"%s\": %m",
451  FilePathName(v->mdfd_vfd)),
452  errhint("Check free disk space.")));
453  /* short write: complain appropriately */
454  ereport(ERROR,
455  (errcode(ERRCODE_DISK_FULL),
456  errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
458  nbytes, BLCKSZ, blocknum),
459  errhint("Check free disk space.")));
460  }
461 
462  if (!skipFsync && !SmgrIsTemp(reln))
463  register_dirty_segment(reln, forknum, v);
464 
465  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
466 }
467 
468 /*
469  * mdopenfork() -- Open one fork of the specified relation.
470  *
471  * Note we only open the first segment, when there are multiple segments.
472  *
473  * If first segment is not present, either ereport or return NULL according
474  * to "behavior". We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
475  * EXTENSION_CREATE means it's OK to extend an existing relation, not to
476  * invent one out of whole cloth.
477  */
478 static MdfdVec *
479 mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
480 {
481  MdfdVec *mdfd;
482  char *path;
483  File fd;
484 
485  /* No work if already open */
486  if (reln->md_num_open_segs[forknum] > 0)
487  return &reln->md_seg_fds[forknum][0];
488 
489  path = relpath(reln->smgr_rnode, forknum);
490 
491  fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
492 
493  if (fd < 0)
494  {
495  if ((behavior & EXTENSION_RETURN_NULL) &&
496  FILE_POSSIBLY_DELETED(errno))
497  {
498  pfree(path);
499  return NULL;
500  }
501  ereport(ERROR,
503  errmsg("could not open file \"%s\": %m", path)));
504  }
505 
506  pfree(path);
507 
508  _fdvec_resize(reln, forknum, 1);
509  mdfd = &reln->md_seg_fds[forknum][0];
510  mdfd->mdfd_vfd = fd;
511  mdfd->mdfd_segno = 0;
512 
513  Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
514 
515  return mdfd;
516 }
517 
518 /*
519  * mdopen() -- Initialize newly-opened relation.
520  */
521 void
523 {
524  /* mark it not open */
525  for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
526  reln->md_num_open_segs[forknum] = 0;
527 }
528 
529 /*
530  * mdclose() -- Close the specified relation, if it isn't closed already.
531  */
532 void
534 {
535  int nopensegs = reln->md_num_open_segs[forknum];
536 
537  /* No work if already closed */
538  if (nopensegs == 0)
539  return;
540 
541  /* close segments starting from the end */
542  while (nopensegs > 0)
543  {
544  MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
545 
546  FileClose(v->mdfd_vfd);
547  _fdvec_resize(reln, forknum, nopensegs - 1);
548  nopensegs--;
549  }
550 }
551 
552 /*
553  * mdprefetch() -- Initiate asynchronous read of the specified block of a relation
554  */
555 bool
557 {
558 #ifdef USE_PREFETCH
559  off_t seekpos;
560  MdfdVec *v;
561 
562  v = _mdfd_getseg(reln, forknum, blocknum, false,
564  if (v == NULL)
565  return false;
566 
567  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
568 
569  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
570 
571  (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ, WAIT_EVENT_DATA_FILE_PREFETCH);
572 #endif /* USE_PREFETCH */
573 
574  return true;
575 }
576 
577 /*
578  * mdwriteback() -- Tell the kernel to write pages back to storage.
579  *
580  * This accepts a range of blocks because flushing several pages at once is
581  * considerably more efficient than doing so individually.
582  */
583 void
585  BlockNumber blocknum, BlockNumber nblocks)
586 {
587  /*
588  * Issue flush requests in as few requests as possible; have to split at
589  * segment boundaries though, since those are actually separate files.
590  */
591  while (nblocks > 0)
592  {
593  BlockNumber nflush = nblocks;
594  off_t seekpos;
595  MdfdVec *v;
596  int segnum_start,
597  segnum_end;
598 
599  v = _mdfd_getseg(reln, forknum, blocknum, true /* not used */ ,
601 
602  /*
603  * We might be flushing buffers of already removed relations, that's
604  * ok, just ignore that case.
605  */
606  if (!v)
607  return;
608 
609  /* compute offset inside the current segment */
610  segnum_start = blocknum / RELSEG_SIZE;
611 
612  /* compute number of desired writes within the current segment */
613  segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
614  if (segnum_start != segnum_end)
615  nflush = RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE));
616 
617  Assert(nflush >= 1);
618  Assert(nflush <= nblocks);
619 
620  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
621 
622  FileWriteback(v->mdfd_vfd, seekpos, (off_t) BLCKSZ * nflush, WAIT_EVENT_DATA_FILE_FLUSH);
623 
624  nblocks -= nflush;
625  blocknum += nflush;
626  }
627 }
628 
629 /*
630  * mdread() -- Read the specified block from a relation.
631  */
632 void
633 mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
634  char *buffer)
635 {
636  off_t seekpos;
637  int nbytes;
638  MdfdVec *v;
639 
640  TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
641  reln->smgr_rnode.node.spcNode,
642  reln->smgr_rnode.node.dbNode,
643  reln->smgr_rnode.node.relNode,
644  reln->smgr_rnode.backend);
645 
646  v = _mdfd_getseg(reln, forknum, blocknum, false,
648 
649  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
650 
651  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
652 
653  nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_READ);
654 
655  TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
656  reln->smgr_rnode.node.spcNode,
657  reln->smgr_rnode.node.dbNode,
658  reln->smgr_rnode.node.relNode,
659  reln->smgr_rnode.backend,
660  nbytes,
661  BLCKSZ);
662 
663  if (nbytes != BLCKSZ)
664  {
665  if (nbytes < 0)
666  ereport(ERROR,
668  errmsg("could not read block %u in file \"%s\": %m",
669  blocknum, FilePathName(v->mdfd_vfd))));
670 
671  /*
672  * Short read: we are at or past EOF, or we read a partial block at
673  * EOF. Normally this is an error; upper levels should never try to
674  * read a nonexistent block. However, if zero_damaged_pages is ON or
675  * we are InRecovery, we should instead return zeroes without
676  * complaining. This allows, for example, the case of trying to
677  * update a block that was later truncated away.
678  */
680  MemSet(buffer, 0, BLCKSZ);
681  else
682  ereport(ERROR,
684  errmsg("could not read block %u in file \"%s\": read only %d of %d bytes",
685  blocknum, FilePathName(v->mdfd_vfd),
686  nbytes, BLCKSZ)));
687  }
688 }
689 
690 /*
691  * mdwrite() -- Write the supplied block at the appropriate location.
692  *
693  * This is to be used only for updating already-existing blocks of a
694  * relation (ie, those before the current EOF). To extend a relation,
695  * use mdextend().
696  */
697 void
698 mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
699  char *buffer, bool skipFsync)
700 {
701  off_t seekpos;
702  int nbytes;
703  MdfdVec *v;
704 
705  /* This assert is too expensive to have on normally ... */
706 #ifdef CHECK_WRITE_VS_EXTEND
707  Assert(blocknum < mdnblocks(reln, forknum));
708 #endif
709 
710  TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
711  reln->smgr_rnode.node.spcNode,
712  reln->smgr_rnode.node.dbNode,
713  reln->smgr_rnode.node.relNode,
714  reln->smgr_rnode.backend);
715 
716  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync,
718 
719  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
720 
721  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
722 
723  nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_WRITE);
724 
725  TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
726  reln->smgr_rnode.node.spcNode,
727  reln->smgr_rnode.node.dbNode,
728  reln->smgr_rnode.node.relNode,
729  reln->smgr_rnode.backend,
730  nbytes,
731  BLCKSZ);
732 
733  if (nbytes != BLCKSZ)
734  {
735  if (nbytes < 0)
736  ereport(ERROR,
738  errmsg("could not write block %u in file \"%s\": %m",
739  blocknum, FilePathName(v->mdfd_vfd))));
740  /* short write: complain appropriately */
741  ereport(ERROR,
742  (errcode(ERRCODE_DISK_FULL),
743  errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes",
744  blocknum,
746  nbytes, BLCKSZ),
747  errhint("Check free disk space.")));
748  }
749 
750  if (!skipFsync && !SmgrIsTemp(reln))
751  register_dirty_segment(reln, forknum, v);
752 }
753 
754 /*
755  * mdnblocks() -- Get the number of blocks stored in a relation.
756  *
757  * Important side effect: all active segments of the relation are opened
758  * and added to the md_seg_fds array. If this routine has not been
759  * called, then only segments up to the last one actually touched
760  * are present in the array.
761  */
764 {
765  MdfdVec *v;
766  BlockNumber nblocks;
767  BlockNumber segno;
768 
769  mdopenfork(reln, forknum, EXTENSION_FAIL);
770 
771  /* mdopen has opened the first segment */
772  Assert(reln->md_num_open_segs[forknum] > 0);
773 
774  /*
775  * Start from the last open segments, to avoid redundant seeks. We have
776  * previously verified that these segments are exactly RELSEG_SIZE long,
777  * and it's useless to recheck that each time.
778  *
779  * NOTE: this assumption could only be wrong if another backend has
780  * truncated the relation. We rely on higher code levels to handle that
781  * scenario by closing and re-opening the md fd, which is handled via
782  * relcache flush. (Since the checkpointer doesn't participate in
783  * relcache flush, it could have segment entries for inactive segments;
784  * that's OK because the checkpointer never needs to compute relation
785  * size.)
786  */
787  segno = reln->md_num_open_segs[forknum] - 1;
788  v = &reln->md_seg_fds[forknum][segno];
789 
790  for (;;)
791  {
792  nblocks = _mdnblocks(reln, forknum, v);
793  if (nblocks > ((BlockNumber) RELSEG_SIZE))
794  elog(FATAL, "segment too big");
795  if (nblocks < ((BlockNumber) RELSEG_SIZE))
796  return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
797 
798  /*
799  * If segment is exactly RELSEG_SIZE, advance to next one.
800  */
801  segno++;
802 
803  /*
804  * We used to pass O_CREAT here, but that has the disadvantage that it
805  * might create a segment which has vanished through some operating
806  * system misadventure. In such a case, creating the segment here
807  * undermines _mdfd_getseg's attempts to notice and report an error
808  * upon access to a missing segment.
809  */
810  v = _mdfd_openseg(reln, forknum, segno, 0);
811  if (v == NULL)
812  return segno * ((BlockNumber) RELSEG_SIZE);
813  }
814 }
815 
816 /*
817  * mdtruncate() -- Truncate relation to specified number of blocks.
818  */
819 void
821 {
822  BlockNumber curnblk;
823  BlockNumber priorblocks;
824  int curopensegs;
825 
826  /*
827  * NOTE: mdnblocks makes sure we have opened all active segments, so that
828  * truncation loop will get them all!
829  */
830  curnblk = mdnblocks(reln, forknum);
831  if (nblocks > curnblk)
832  {
833  /* Bogus request ... but no complaint if InRecovery */
834  if (InRecovery)
835  return;
836  ereport(ERROR,
837  (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
838  relpath(reln->smgr_rnode, forknum),
839  nblocks, curnblk)));
840  }
841  if (nblocks == curnblk)
842  return; /* no work */
843 
844  /*
845  * Truncate segments, starting at the last one. Starting at the end makes
846  * managing the memory for the fd array easier, should there be errors.
847  */
848  curopensegs = reln->md_num_open_segs[forknum];
849  while (curopensegs > 0)
850  {
851  MdfdVec *v;
852 
853  priorblocks = (curopensegs - 1) * RELSEG_SIZE;
854 
855  v = &reln->md_seg_fds[forknum][curopensegs - 1];
856 
857  if (priorblocks > nblocks)
858  {
859  /*
860  * This segment is no longer active. We truncate the file, but do
861  * not delete it, for reasons explained in the header comments.
862  */
864  ereport(ERROR,
866  errmsg("could not truncate file \"%s\": %m",
867  FilePathName(v->mdfd_vfd))));
868 
869  if (!SmgrIsTemp(reln))
870  register_dirty_segment(reln, forknum, v);
871 
872  /* we never drop the 1st segment */
873  Assert(v != &reln->md_seg_fds[forknum][0]);
874 
875  FileClose(v->mdfd_vfd);
876  _fdvec_resize(reln, forknum, curopensegs - 1);
877  }
878  else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
879  {
880  /*
881  * This is the last segment we want to keep. Truncate the file to
882  * the right length. NOTE: if nblocks is exactly a multiple K of
883  * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
884  * keep it. This adheres to the invariant given in the header
885  * comments.
886  */
887  BlockNumber lastsegblocks = nblocks - priorblocks;
888 
889  if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
890  ereport(ERROR,
892  errmsg("could not truncate file \"%s\" to %u blocks: %m",
894  nblocks)));
895  if (!SmgrIsTemp(reln))
896  register_dirty_segment(reln, forknum, v);
897  }
898  else
899  {
900  /*
901  * We still need this segment, so nothing to do for this and any
902  * earlier segment.
903  */
904  break;
905  }
906  curopensegs--;
907  }
908 }
909 
910 /*
911  * mdimmedsync() -- Immediately sync a relation to stable storage.
912  *
913  * Note that only writes already issued are synced; this routine knows
914  * nothing of dirty buffers that may exist inside the buffer manager. We
915  * sync active and inactive segments; smgrDoPendingSyncs() relies on this.
916  * Consider a relation skipping WAL. Suppose a checkpoint syncs blocks of
917  * some segment, then mdtruncate() renders that segment inactive. If we
918  * crash before the next checkpoint syncs the newly-inactive segment, that
919  * segment may survive recovery, reintroducing unwanted data into the table.
920  */
921 void
923 {
924  int segno;
925  int min_inactive_seg;
926 
927  /*
928  * NOTE: mdnblocks makes sure we have opened all active segments, so that
929  * fsync loop will get them all!
930  */
931  mdnblocks(reln, forknum);
932 
933  min_inactive_seg = segno = reln->md_num_open_segs[forknum];
934 
935  /*
936  * Temporarily open inactive segments, then close them after sync. There
937  * may be some inactive segments left opened after fsync() error, but that
938  * is harmless. We don't bother to clean them up and take a risk of
939  * further trouble. The next mdclose() will soon close them.
940  */
941  while (_mdfd_openseg(reln, forknum, segno, 0) != NULL)
942  segno++;
943 
944  while (segno > 0)
945  {
946  MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
947 
951  errmsg("could not fsync file \"%s\": %m",
952  FilePathName(v->mdfd_vfd))));
953 
954  /* Close inactive segments immediately */
955  if (segno > min_inactive_seg)
956  {
957  FileClose(v->mdfd_vfd);
958  _fdvec_resize(reln, forknum, segno - 1);
959  }
960 
961  segno--;
962  }
963 }
964 
965 /*
966  * register_dirty_segment() -- Mark a relation segment as needing fsync
967  *
968  * If there is a local pending-ops table, just make an entry in it for
969  * ProcessSyncRequests to process later. Otherwise, try to pass off the
970  * fsync request to the checkpointer process. If that fails, just do the
971  * fsync locally before returning (we hope this will not happen often
972  * enough to be a performance problem).
973  */
974 static void
976 {
977  FileTag tag;
978 
979  INIT_MD_FILETAG(tag, reln->smgr_rnode.node, forknum, seg->mdfd_segno);
980 
981  /* Temp relations should never be fsync'd */
982  Assert(!SmgrIsTemp(reln));
983 
984  if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false /* retryOnError */ ))
985  {
986  ereport(DEBUG1,
987  (errmsg_internal("could not forward fsync request because request queue is full")));
988 
992  errmsg("could not fsync file \"%s\": %m",
993  FilePathName(seg->mdfd_vfd))));
994  }
995 }
996 
997 /*
998  * register_unlink_segment() -- Schedule a file to be deleted after next checkpoint
999  */
1000 static void
1002  BlockNumber segno)
1003 {
1004  FileTag tag;
1005 
1006  INIT_MD_FILETAG(tag, rnode.node, forknum, segno);
1007 
1008  /* Should never be used with temp relations */
1010 
1011  RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true /* retryOnError */ );
1012 }
1013 
1014 /*
1015  * register_forget_request() -- forget any fsyncs for a relation fork's segment
1016  */
1017 static void
1019  BlockNumber segno)
1020 {
1021  FileTag tag;
1022 
1023  INIT_MD_FILETAG(tag, rnode.node, forknum, segno);
1024 
1025  RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true /* retryOnError */ );
1026 }
1027 
1028 /*
1029  * ForgetDatabaseSyncRequests -- forget any fsyncs and unlinks for a DB
1030  */
1031 void
1033 {
1034  FileTag tag;
1035  RelFileNode rnode;
1036 
1037  rnode.dbNode = dbid;
1038  rnode.spcNode = 0;
1039  rnode.relNode = 0;
1040 
1042 
1043  RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true /* retryOnError */ );
1044 }
1045 
1046 /*
1047  * DropRelationFiles -- drop files of all given relations
1048  */
1049 void
1050 DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo)
1051 {
1052  SMgrRelation *srels;
1053  int i;
1054 
1055  srels = palloc(sizeof(SMgrRelation) * ndelrels);
1056  for (i = 0; i < ndelrels; i++)
1057  {
1058  SMgrRelation srel = smgropen(delrels[i], InvalidBackendId);
1059 
1060  if (isRedo)
1061  {
1062  ForkNumber fork;
1063 
1064  for (fork = 0; fork <= MAX_FORKNUM; fork++)
1065  XLogDropRelation(delrels[i], fork);
1066  }
1067  srels[i] = srel;
1068  }
1069 
1070  smgrdounlinkall(srels, ndelrels, isRedo);
1071 
1072  for (i = 0; i < ndelrels; i++)
1073  smgrclose(srels[i]);
1074  pfree(srels);
1075 }
1076 
1077 
1078 /*
1079  * _fdvec_resize() -- Resize the fork's open segments array
1080  */
1081 static void
1083  ForkNumber forknum,
1084  int nseg)
1085 {
1086  if (nseg == 0)
1087  {
1088  if (reln->md_num_open_segs[forknum] > 0)
1089  {
1090  pfree(reln->md_seg_fds[forknum]);
1091  reln->md_seg_fds[forknum] = NULL;
1092  }
1093  }
1094  else if (reln->md_num_open_segs[forknum] == 0)
1095  {
1096  reln->md_seg_fds[forknum] =
1097  MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
1098  }
1099  else
1100  {
1101  /*
1102  * It doesn't seem worthwhile complicating the code to amortize
1103  * repalloc() calls. Those are far faster than PathNameOpenFile() or
1104  * FileClose(), and the memory context internally will sometimes avoid
1105  * doing an actual reallocation.
1106  */
1107  reln->md_seg_fds[forknum] =
1108  repalloc(reln->md_seg_fds[forknum],
1109  sizeof(MdfdVec) * nseg);
1110  }
1111 
1112  reln->md_num_open_segs[forknum] = nseg;
1113 }
1114 
1115 /*
1116  * Return the filename for the specified segment of the relation. The
1117  * returned string is palloc'd.
1118  */
1119 static char *
1121 {
1122  char *path,
1123  *fullpath;
1124 
1125  path = relpath(reln->smgr_rnode, forknum);
1126 
1127  if (segno > 0)
1128  {
1129  fullpath = psprintf("%s.%u", path, segno);
1130  pfree(path);
1131  }
1132  else
1133  fullpath = path;
1134 
1135  return fullpath;
1136 }
1137 
1138 /*
1139  * Open the specified segment of the relation,
1140  * and make a MdfdVec object for it. Returns NULL on failure.
1141  */
1142 static MdfdVec *
1144  int oflags)
1145 {
1146  MdfdVec *v;
1147  File fd;
1148  char *fullpath;
1149 
1150  fullpath = _mdfd_segpath(reln, forknum, segno);
1151 
1152  /* open the file */
1153  fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags);
1154 
1155  pfree(fullpath);
1156 
1157  if (fd < 0)
1158  return NULL;
1159 
1160  /*
1161  * Segments are always opened in order from lowest to highest, so we must
1162  * be adding a new one at the end.
1163  */
1164  Assert(segno == reln->md_num_open_segs[forknum]);
1165 
1166  _fdvec_resize(reln, forknum, segno + 1);
1167 
1168  /* fill the entry */
1169  v = &reln->md_seg_fds[forknum][segno];
1170  v->mdfd_vfd = fd;
1171  v->mdfd_segno = segno;
1172 
1173  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
1174 
1175  /* all done */
1176  return v;
1177 }
1178 
1179 /*
1180  * _mdfd_getseg() -- Find the segment of the relation holding the
1181  * specified block.
1182  *
1183  * If the segment doesn't exist, we ereport, return NULL, or create the
1184  * segment, according to "behavior". Note: skipFsync is only used in the
1185  * EXTENSION_CREATE case.
1186  */
1187 static MdfdVec *
1189  bool skipFsync, int behavior)
1190 {
1191  MdfdVec *v;
1192  BlockNumber targetseg;
1193  BlockNumber nextsegno;
1194 
1195  /* some way to handle non-existent segments needs to be specified */
1196  Assert(behavior &
1198 
1199  targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
1200 
1201  /* if an existing and opened segment, we're done */
1202  if (targetseg < reln->md_num_open_segs[forknum])
1203  {
1204  v = &reln->md_seg_fds[forknum][targetseg];
1205  return v;
1206  }
1207 
1208  /*
1209  * The target segment is not yet open. Iterate over all the segments
1210  * between the last opened and the target segment. This way missing
1211  * segments either raise an error, or get created (according to
1212  * 'behavior'). Start with either the last opened, or the first segment if
1213  * none was opened before.
1214  */
1215  if (reln->md_num_open_segs[forknum] > 0)
1216  v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
1217  else
1218  {
1219  v = mdopenfork(reln, forknum, behavior);
1220  if (!v)
1221  return NULL; /* if behavior & EXTENSION_RETURN_NULL */
1222  }
1223 
1224  for (nextsegno = reln->md_num_open_segs[forknum];
1225  nextsegno <= targetseg; nextsegno++)
1226  {
1227  BlockNumber nblocks = _mdnblocks(reln, forknum, v);
1228  int flags = 0;
1229 
1230  Assert(nextsegno == v->mdfd_segno + 1);
1231 
1232  if (nblocks > ((BlockNumber) RELSEG_SIZE))
1233  elog(FATAL, "segment too big");
1234 
1235  if ((behavior & EXTENSION_CREATE) ||
1236  (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
1237  {
1238  /*
1239  * Normally we will create new segments only if authorized by the
1240  * caller (i.e., we are doing mdextend()). But when doing WAL
1241  * recovery, create segments anyway; this allows cases such as
1242  * replaying WAL data that has a write into a high-numbered
1243  * segment of a relation that was later deleted. We want to go
1244  * ahead and create the segments so we can finish out the replay.
1245  *
1246  * We have to maintain the invariant that segments before the last
1247  * active segment are of size RELSEG_SIZE; therefore, if
1248  * extending, pad them out with zeroes if needed. (This only
1249  * matters if in recovery, or if the caller is extending the
1250  * relation discontiguously, but that can happen in hash indexes.)
1251  */
1252  if (nblocks < ((BlockNumber) RELSEG_SIZE))
1253  {
1254  char *zerobuf = palloc0(BLCKSZ);
1255 
1256  mdextend(reln, forknum,
1257  nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
1258  zerobuf, skipFsync);
1259  pfree(zerobuf);
1260  }
1261  flags = O_CREAT;
1262  }
1263  else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
1264  nblocks < ((BlockNumber) RELSEG_SIZE))
1265  {
1266  /*
1267  * When not extending (or explicitly including truncated
1268  * segments), only open the next segment if the current one is
1269  * exactly RELSEG_SIZE. If not (this branch), either return NULL
1270  * or fail.
1271  */
1272  if (behavior & EXTENSION_RETURN_NULL)
1273  {
1274  /*
1275  * Some callers discern between reasons for _mdfd_getseg()
1276  * returning NULL based on errno. As there's no failing
1277  * syscall involved in this case, explicitly set errno to
1278  * ENOENT, as that seems the closest interpretation.
1279  */
1280  errno = ENOENT;
1281  return NULL;
1282  }
1283 
1284  ereport(ERROR,
1286  errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1287  _mdfd_segpath(reln, forknum, nextsegno),
1288  blkno, nblocks)));
1289  }
1290 
1291  v = _mdfd_openseg(reln, forknum, nextsegno, flags);
1292 
1293  if (v == NULL)
1294  {
1295  if ((behavior & EXTENSION_RETURN_NULL) &&
1296  FILE_POSSIBLY_DELETED(errno))
1297  return NULL;
1298  ereport(ERROR,
1300  errmsg("could not open file \"%s\" (target block %u): %m",
1301  _mdfd_segpath(reln, forknum, nextsegno),
1302  blkno)));
1303  }
1304  }
1305 
1306  return v;
1307 }
1308 
1309 /*
1310  * Get number of blocks present in a single disk file
1311  */
1312 static BlockNumber
1314 {
1315  off_t len;
1316 
1317  len = FileSize(seg->mdfd_vfd);
1318  if (len < 0)
1319  ereport(ERROR,
1321  errmsg("could not seek to end of file \"%s\": %m",
1322  FilePathName(seg->mdfd_vfd))));
1323  /* note that this calculation will ignore any partial block at EOF */
1324  return (BlockNumber) (len / BLCKSZ);
1325 }
1326 
1327 /*
1328  * Sync a file to disk, given a file tag. Write the path into an output
1329  * buffer so the caller can use it in error messages.
1330  *
1331  * Return 0 on success, -1 on failure, with errno set.
1332  */
1333 int
1334 mdsyncfiletag(const FileTag *ftag, char *path)
1335 {
1337  File file;
1338  bool need_to_close;
1339  int result,
1340  save_errno;
1341 
1342  /* See if we already have the file open, or need to open it. */
1343  if (ftag->segno < reln->md_num_open_segs[ftag->forknum])
1344  {
1345  file = reln->md_seg_fds[ftag->forknum][ftag->segno].mdfd_vfd;
1346  strlcpy(path, FilePathName(file), MAXPGPATH);
1347  need_to_close = false;
1348  }
1349  else
1350  {
1351  char *p;
1352 
1353  p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
1354  strlcpy(path, p, MAXPGPATH);
1355  pfree(p);
1356 
1357  file = PathNameOpenFile(path, O_RDWR | PG_BINARY);
1358  if (file < 0)
1359  return -1;
1360  need_to_close = true;
1361  }
1362 
1363  /* Sync the file. */
1364  result = FileSync(file, WAIT_EVENT_DATA_FILE_SYNC);
1365  save_errno = errno;
1366 
1367  if (need_to_close)
1368  FileClose(file);
1369 
1370  errno = save_errno;
1371  return result;
1372 }
1373 
1374 /*
1375  * Unlink a file, given a file tag. Write the path into an output
1376  * buffer so the caller can use it in error messages.
1377  *
1378  * Return 0 on success, -1 on failure, with errno set.
1379  */
1380 int
1381 mdunlinkfiletag(const FileTag *ftag, char *path)
1382 {
1383  char *p;
1384 
1385  /* Compute the path. */
1386  p = relpathperm(ftag->rnode, MAIN_FORKNUM);
1387  strlcpy(path, p, MAXPGPATH);
1388  pfree(p);
1389 
1390  /* Try to unlink the file. */
1391  return unlink(path);
1392 }
1393 
1394 /*
1395  * Check if a given candidate request matches a given tag, when processing
1396  * a SYNC_FILTER_REQUEST request. This will be called for all pending
1397  * requests to find out whether to forget them.
1398  */
1399 bool
1400 mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
1401 {
1402  /*
1403  * For now we only use filter requests as a way to drop all scheduled
1404  * callbacks relating to a given database, when dropping the database.
1405  * We'll return true for all candidates that have the same database OID as
1406  * the ftag from the SYNC_FILTER_REQUEST request, so they're forgotten.
1407  */
1408  return ftag->rnode.dbNode == candidate->rnode.dbNode;
1409 }
uint32 segno
Definition: sync.h:55
#define relpathperm(rnode, forknum)
Definition: relpath.h:83
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:922
#define AllocSetContextCreate
Definition: memutils.h:173
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
#define DEBUG1
Definition: elog.h:25
static MdfdVec * _mdfd_getseg(SMgrRelation reln, ForkNumber forkno, BlockNumber blkno, bool skipFsync, int behavior)
Definition: md.c:1188
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define EXTENSION_DONT_CHECK_SIZE
Definition: md.c:118
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1564
#define RelFileNodeBackendIsTemp(rnode)
Definition: relfilenode.h:78
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:763
int pg_truncate(const char *path, off_t length)
Definition: fd.c:640
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
BlockNumber mdfd_segno
Definition: md.c:85
struct _MdfdVec MdfdVec
off_t FileSize(File file)
Definition: fd.c:2285
static int do_truncate(const char *path)
Definition: md.c:293
int16 forknum
Definition: sync.h:53
int errcode(int sqlerrcode)
Definition: elog.c:698
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
Definition: smgr.c:384
#define MemSet(start, val, len)
Definition: c.h:1008
RelFileNode rnode
Definition: sync.h:54
uint32 BlockNumber
Definition: block.h:31
#define INIT_MD_FILETAG(a, xx_rnode, xx_forknum, xx_segno)
Definition: md.c:92
unsigned int Oid
Definition: postgres_ext.h:31
void mdinit(void)
Definition: md.c:148
char * FilePathName(File file)
Definition: fd.c:2337
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1271
#define EXTENSION_FAIL
Definition: md.c:104
static void register_forget_request(RelFileNodeBackend rnode, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1018
static MemoryContext MdCxt
Definition: md.c:88
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:414
#define SmgrIsTemp(smgr)
Definition: smgr.h:77
#define sprintf
Definition: port.h:218
void pfree(void *pointer)
Definition: mcxt.c:1169
static MdfdVec * mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
Definition: md.c:479
#define ERROR
Definition: elog.h:46
#define EXTENSION_RETURN_NULL
Definition: md.c:106
static void register_unlink_segment(RelFileNodeBackend rnode, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1001
#define FATAL
Definition: elog.h:49
#define MAXPGPATH
void DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo)
Definition: md.c:1050
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2264
static void mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
Definition: md.c:314
void mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
Definition: md.c:277
int errcode_for_file_access(void)
Definition: elog.c:721
#define EXTENSION_CREATE_RECOVERY
Definition: md.c:110
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:584
MemoryContext TopMemoryContext
Definition: mcxt.c:48
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:146
ForkNumber
Definition: relpath.h:40
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:47
#define WARNING
Definition: elog.h:40
int mdunlinkfiletag(const FileTag *ftag, char *path)
Definition: md.c:1381
void mdopen(SMgrRelation reln)
Definition: md.c:522
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2166
#define InvalidBackendId
Definition: backendid.h:23
void * palloc0(Size size)
Definition: mcxt.c:1093
int data_sync_elevel(int elevel)
Definition: fd.c:3805
static void _fdvec_resize(SMgrRelation reln, ForkNumber forknum, int nseg)
Definition: md.c:1082
bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type, bool retryOnError)
Definition: sync.c:553
Definition: md.c:82
bool mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
Definition: md.c:1400
static MdfdVec * _mdfd_openseg(SMgrRelation reln, ForkNumber forkno, BlockNumber segno, int oflags)
Definition: md.c:1143
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:820
#define ereport(elevel,...)
Definition: elog.h:157
#define EXTENSION_CREATE
Definition: md.c:108
bool mdexists(SMgrRelation reln, ForkNumber forkNum)
Definition: md.c:161
RelFileNode node
Definition: relfilenode.h:74
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:1032
bool InRecovery
Definition: xlogutils.c:52
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
void FileClose(File file)
Definition: fd.c:1959
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:2059
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:698
#define Assert(condition)
Definition: c.h:804
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:2087
void TablespaceCreateDbspace(Oid spcNode, Oid dbNode, bool isRedo)
Definition: tablespace.c:115
static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:1313
BackendId backend
Definition: relfilenode.h:75
#define InvalidBlockNumber
Definition: block.h:33
#define MAX_FORKNUM
Definition: relpath.h:55
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
int mdsyncfiletag(const FileTag *ftag, char *path)
Definition: md.c:1334
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:533
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:556
void XLogDropRelation(RelFileNode rnode, ForkNumber forknum)
Definition: xlogutils.c:632
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:68
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
#define elog(elevel,...)
Definition: elog.h:232
int i
void mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
Definition: md.c:178
#define relpath(rnode, forknum)
Definition: relpath.h:87
struct _MdfdVec * md_seg_fds[MAX_FORKNUM+1]
Definition: smgr.h:69
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2110
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2302
File mdfd_vfd
Definition: md.c:84
int File
Definition: fd.h:54
Definition: sync.h:50
#define FILE_POSSIBLY_DELETED(err)
Definition: fd.h:77
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:975
static char * _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1120
bool zero_damaged_pages
Definition: bufmgr.c:132
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: md.c:633