OSDN Git Service

5034a1dc4d0b1955fe761addb7074c74ff03cbf3
[pg-rex/syncrep.git] / src / backend / storage / smgr / md.c
1 /*-------------------------------------------------------------------------
2  *
3  * md.c
4  *        This code manages relations that reside on magnetic disk.
5  *
6  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/storage/smgr/md.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/file.h>
20
21 #include "catalog/catalog.h"
22 #include "miscadmin.h"
23 #include "portability/instr_time.h"
24 #include "postmaster/bgwriter.h"
25 #include "storage/fd.h"
26 #include "storage/bufmgr.h"
27 #include "storage/relfilenode.h"
28 #include "storage/smgr.h"
29 #include "utils/hsearch.h"
30 #include "utils/memutils.h"
31 #include "pg_trace.h"
32
33
34 /* interval for calling AbsorbFsyncRequests in mdsync */
35 #define FSYNCS_PER_ABSORB               10
36
37 /*
38  * Special values for the segno arg to RememberFsyncRequest.
39  *
40  * Note that CompactBgwriterRequestQueue assumes that it's OK to remove an
41  * fsync request from the queue if an identical, subsequent request is found.
42  * See comments there before making changes here.
43  */
44 #define FORGET_RELATION_FSYNC   (InvalidBlockNumber)
45 #define FORGET_DATABASE_FSYNC   (InvalidBlockNumber-1)
46 #define UNLINK_RELATION_REQUEST (InvalidBlockNumber-2)
47
48 /*
49  * On Windows, we have to interpret EACCES as possibly meaning the same as
50  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
51  * that's what you get.  Ugh.  This code is designed so that we don't
52  * actually believe these cases are okay without further evidence (namely,
53  * a pending fsync request getting revoked ... see mdsync).
54  */
55 #ifndef WIN32
56 #define FILE_POSSIBLY_DELETED(err)      ((err) == ENOENT)
57 #else
58 #define FILE_POSSIBLY_DELETED(err)      ((err) == ENOENT || (err) == EACCES)
59 #endif
60
61 /*
62  *      The magnetic disk storage manager keeps track of open file
63  *      descriptors in its own descriptor pool.  This is done to make it
64  *      easier to support relations that are larger than the operating
65  *      system's file size limit (often 2GBytes).  In order to do that,
66  *      we break relations up into "segment" files that are each shorter than
67  *      the OS file size limit.  The segment size is set by the RELSEG_SIZE
68  *      configuration constant in pg_config.h.
69  *
70  *      On disk, a relation must consist of consecutively numbered segment
71  *      files in the pattern
72  *              -- Zero or more full segments of exactly RELSEG_SIZE blocks each
73  *              -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
74  *              -- Optionally, any number of inactive segments of size 0 blocks.
75  *      The full and partial segments are collectively the "active" segments.
76  *      Inactive segments are those that once contained data but are currently
77  *      not needed because of an mdtruncate() operation.  The reason for leaving
78  *      them present at size zero, rather than unlinking them, is that other
79  *      backends and/or the bgwriter might be holding open file references to
80  *      such segments.  If the relation expands again after mdtruncate(), such
81  *      that a deactivated segment becomes active again, it is important that
82  *      such file references still be valid --- else data might get written
83  *      out to an unlinked old copy of a segment file that will eventually
84  *      disappear.
85  *
86  *      The file descriptor pointer (md_fd field) stored in the SMgrRelation
87  *      cache is, therefore, just the head of a list of MdfdVec objects, one
88  *      per segment.  But note the md_fd pointer can be NULL, indicating
89  *      relation not open.
90  *
91  *      Also note that mdfd_chain == NULL does not necessarily mean the relation
92  *      doesn't have another segment after this one; we may just not have
93  *      opened the next segment yet.  (We could not have "all segments are
94  *      in the chain" as an invariant anyway, since another backend could
95  *      extend the relation when we weren't looking.)  We do not make chain
96  *      entries for inactive segments, however; as soon as we find a partial
97  *      segment, we assume that any subsequent segments are inactive.
98  *
99  *      All MdfdVec objects are palloc'd in the MdCxt memory context.
100  */
101
102 typedef struct _MdfdVec
103 {
104         File            mdfd_vfd;               /* fd number in fd.c's pool */
105         BlockNumber mdfd_segno;         /* segment number, from 0 */
106         struct _MdfdVec *mdfd_chain;    /* next segment, or NULL */
107 } MdfdVec;
108
109 static MemoryContext MdCxt;             /* context for all md.c allocations */
110
111
112 /*
113  * In some contexts (currently, standalone backends and the bgwriter process)
114  * we keep track of pending fsync operations: we need to remember all relation
115  * segments that have been written since the last checkpoint, so that we can
116  * fsync them down to disk before completing the next checkpoint.  This hash
117  * table remembers the pending operations.      We use a hash table mostly as
118  * a convenient way of eliminating duplicate requests.
119  *
120  * We use a similar mechanism to remember no-longer-needed files that can
121  * be deleted after the next checkpoint, but we use a linked list instead of
122  * a hash table, because we don't expect there to be any duplicate requests.
123  *
124  * (Regular backends do not track pending operations locally, but forward
125  * them to the bgwriter.)
126  */
127 typedef struct
128 {
129         RelFileNodeBackend rnode;       /* the targeted relation */
130         ForkNumber      forknum;
131         BlockNumber segno;                      /* which segment */
132 } PendingOperationTag;
133
134 typedef uint16 CycleCtr;                /* can be any convenient integer size */
135
136 typedef struct
137 {
138         PendingOperationTag tag;        /* hash table key (must be first!) */
139         bool            canceled;               /* T => request canceled, not yet removed */
140         CycleCtr        cycle_ctr;              /* mdsync_cycle_ctr when request was made */
141 } PendingOperationEntry;
142
143 typedef struct
144 {
145         RelFileNodeBackend rnode;       /* the dead relation to delete */
146         CycleCtr        cycle_ctr;              /* mdckpt_cycle_ctr when request was made */
147 } PendingUnlinkEntry;
148
149 static HTAB *pendingOpsTable = NULL;
150 static List *pendingUnlinks = NIL;
151
152 static CycleCtr mdsync_cycle_ctr = 0;
153 static CycleCtr mdckpt_cycle_ctr = 0;
154
155
156 typedef enum                                    /* behavior for mdopen & _mdfd_getseg */
157 {
158         EXTENSION_FAIL,                         /* ereport if segment not present */
159         EXTENSION_RETURN_NULL,          /* return NULL if not present */
160         EXTENSION_CREATE                        /* create new segments as needed */
161 } ExtensionBehavior;
162
163 /* local routines */
164 static MdfdVec *mdopen(SMgrRelation reln, ForkNumber forknum,
165            ExtensionBehavior behavior);
166 static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
167                                            MdfdVec *seg);
168 static void register_unlink(RelFileNodeBackend rnode);
169 static MdfdVec *_fdvec_alloc(void);
170 static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
171                           BlockNumber segno);
172 static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
173                           BlockNumber segno, int oflags);
174 static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forkno,
175                          BlockNumber blkno, bool skipFsync, ExtensionBehavior behavior);
176 static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
177                    MdfdVec *seg);
178
179
180 /*
181  *      mdinit() -- Initialize private state for magnetic disk storage manager.
182  */
183 void
184 mdinit(void)
185 {
186         MdCxt = AllocSetContextCreate(TopMemoryContext,
187                                                                   "MdSmgr",
188                                                                   ALLOCSET_DEFAULT_MINSIZE,
189                                                                   ALLOCSET_DEFAULT_INITSIZE,
190                                                                   ALLOCSET_DEFAULT_MAXSIZE);
191
192         /*
193          * Create pending-operations hashtable if we need it.  Currently, we need
194          * it if we are standalone (not under a postmaster) OR if we are a
195          * bootstrap-mode subprocess of a postmaster (that is, a startup or
196          * bgwriter process).
197          */
198         if (!IsUnderPostmaster || IsBootstrapProcessingMode())
199         {
200                 HASHCTL         hash_ctl;
201
202                 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
203                 hash_ctl.keysize = sizeof(PendingOperationTag);
204                 hash_ctl.entrysize = sizeof(PendingOperationEntry);
205                 hash_ctl.hash = tag_hash;
206                 hash_ctl.hcxt = MdCxt;
207                 pendingOpsTable = hash_create("Pending Ops Table",
208                                                                           100L,
209                                                                           &hash_ctl,
210                                                                    HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
211                 pendingUnlinks = NIL;
212         }
213 }
214
215 /*
216  * In archive recovery, we rely on bgwriter to do fsyncs, but we will have
217  * already created the pendingOpsTable during initialization of the startup
218  * process.  Calling this function drops the local pendingOpsTable so that
219  * subsequent requests will be forwarded to bgwriter.
220  */
221 void
222 SetForwardFsyncRequests(void)
223 {
224         /* Perform any pending ops we may have queued up */
225         if (pendingOpsTable)
226                 mdsync();
227         pendingOpsTable = NULL;
228 }
229
230 /*
231  *      mdexists() -- Does the physical file exist?
232  *
233  * Note: this will return true for lingering files, with pending deletions
234  */
235 bool
236 mdexists(SMgrRelation reln, ForkNumber forkNum)
237 {
238         /*
239          * Close it first, to ensure that we notice if the fork has been unlinked
240          * since we opened it.
241          */
242         mdclose(reln, forkNum);
243
244         return (mdopen(reln, forkNum, EXTENSION_RETURN_NULL) != NULL);
245 }
246
247 /*
248  *      mdcreate() -- Create a new relation on magnetic disk.
249  *
250  * If isRedo is true, it's okay for the relation to exist already.
251  */
252 void
253 mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
254 {
255         char       *path;
256         File            fd;
257
258         if (isRedo && reln->md_fd[forkNum] != NULL)
259                 return;                                 /* created and opened already... */
260
261         Assert(reln->md_fd[forkNum] == NULL);
262
263         path = relpath(reln->smgr_rnode, forkNum);
264
265         fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
266
267         if (fd < 0)
268         {
269                 int                     save_errno = errno;
270
271                 /*
272                  * During bootstrap, there are cases where a system relation will be
273                  * accessed (by internal backend processes) before the bootstrap
274                  * script nominally creates it.  Therefore, allow the file to exist
275                  * already, even if isRedo is not set.  (See also mdopen)
276                  */
277                 if (isRedo || IsBootstrapProcessingMode())
278                         fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
279                 if (fd < 0)
280                 {
281                         /* be sure to report the error reported by create, not open */
282                         errno = save_errno;
283                         ereport(ERROR,
284                                         (errcode_for_file_access(),
285                                          errmsg("could not create file \"%s\": %m", path)));
286                 }
287         }
288
289         pfree(path);
290
291         reln->md_fd[forkNum] = _fdvec_alloc();
292
293         reln->md_fd[forkNum]->mdfd_vfd = fd;
294         reln->md_fd[forkNum]->mdfd_segno = 0;
295         reln->md_fd[forkNum]->mdfd_chain = NULL;
296 }
297
298 /*
299  *      mdunlink() -- Unlink a relation.
300  *
301  * Note that we're passed a RelFileNode --- by the time this is called,
302  * there won't be an SMgrRelation hashtable entry anymore.
303  *
304  * Actually, we don't unlink the first segment file of the relation, but
305  * just truncate it to zero length, and record a request to unlink it after
306  * the next checkpoint.  Additional segments can be unlinked immediately,
307  * however.  Leaving the empty file in place prevents that relfilenode
308  * number from being reused.  The scenario this protects us from is:
309  * 1. We delete a relation (and commit, and actually remove its file).
310  * 2. We create a new relation, which by chance gets the same relfilenode as
311  *        the just-deleted one (OIDs must've wrapped around for that to happen).
312  * 3. We crash before another checkpoint occurs.
313  * During replay, we would delete the file and then recreate it, which is fine
314  * if the contents of the file were repopulated by subsequent WAL entries.
315  * But if we didn't WAL-log insertions, but instead relied on fsyncing the
316  * file after populating it (as for instance CLUSTER and CREATE INDEX do),
317  * the contents of the file would be lost forever.      By leaving the empty file
318  * until after the next checkpoint, we prevent reassignment of the relfilenode
319  * number until it's safe, because relfilenode assignment skips over any
320  * existing file.
321  *
322  * If isRedo is true, it's okay for the relation to be already gone.
323  * Also, we should remove the file immediately instead of queuing a request
324  * for later, since during redo there's no possibility of creating a
325  * conflicting relation.
326  *
327  * Note: any failure should be reported as WARNING not ERROR, because
328  * we are usually not in a transaction anymore when this is called.
329  */
330 void
331 mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
332 {
333         char       *path;
334         int                     ret;
335
336         /*
337          * We have to clean out any pending fsync requests for the doomed
338          * relation, else the next mdsync() will fail.
339          */
340         ForgetRelationFsyncRequests(rnode, forkNum);
341
342         path = relpath(rnode, forkNum);
343
344         /*
345          * Delete or truncate the first segment.
346          */
347         if (isRedo || forkNum != MAIN_FORKNUM)
348         {
349                 ret = unlink(path);
350                 if (ret < 0)
351                 {
352                         if (!isRedo || errno != ENOENT)
353                                 ereport(WARNING,
354                                                 (errcode_for_file_access(),
355                                                  errmsg("could not remove file \"%s\": %m", path)));
356                 }
357         }
358         else
359         {
360                 /* truncate(2) would be easier here, but Windows hasn't got it */
361                 int                     fd;
362
363                 fd = BasicOpenFile(path, O_RDWR | PG_BINARY, 0);
364                 if (fd >= 0)
365                 {
366                         int                     save_errno;
367
368                         ret = ftruncate(fd, 0);
369                         save_errno = errno;
370                         close(fd);
371                         errno = save_errno;
372                 }
373                 else
374                         ret = -1;
375                 if (ret < 0 && errno != ENOENT)
376                         ereport(WARNING,
377                                         (errcode_for_file_access(),
378                                          errmsg("could not truncate file \"%s\": %m", path)));
379         }
380
381         /*
382          * Delete any additional segments.
383          */
384         if (ret >= 0)
385         {
386                 char       *segpath = (char *) palloc(strlen(path) + 12);
387                 BlockNumber segno;
388
389                 /*
390                  * Note that because we loop until getting ENOENT, we will correctly
391                  * remove all inactive segments as well as active ones.
392                  */
393                 for (segno = 1;; segno++)
394                 {
395                         sprintf(segpath, "%s.%u", path, segno);
396                         if (unlink(segpath) < 0)
397                         {
398                                 /* ENOENT is expected after the last segment... */
399                                 if (errno != ENOENT)
400                                         ereport(WARNING,
401                                                         (errcode_for_file_access(),
402                                            errmsg("could not remove file \"%s\": %m", segpath)));
403                                 break;
404                         }
405                 }
406                 pfree(segpath);
407         }
408
409         pfree(path);
410
411         /* Register request to unlink first segment later */
412         if (!isRedo && forkNum == MAIN_FORKNUM)
413                 register_unlink(rnode);
414 }
415
416 /*
417  *      mdextend() -- Add a block to the specified relation.
418  *
419  *              The semantics are nearly the same as mdwrite(): write at the
420  *              specified position.  However, this is to be used for the case of
421  *              extending a relation (i.e., blocknum is at or beyond the current
422  *              EOF).  Note that we assume writing a block beyond current EOF
423  *              causes intervening file space to become filled with zeroes.
424  */
425 void
426 mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
427                  char *buffer, bool skipFsync)
428 {
429         off_t           seekpos;
430         int                     nbytes;
431         MdfdVec    *v;
432
433         /* This assert is too expensive to have on normally ... */
434 #ifdef CHECK_WRITE_VS_EXTEND
435         Assert(blocknum >= mdnblocks(reln, forknum));
436 #endif
437
438         /*
439          * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
440          * more --- we mustn't create a block whose number actually is
441          * InvalidBlockNumber.
442          */
443         if (blocknum == InvalidBlockNumber)
444                 ereport(ERROR,
445                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
446                                  errmsg("cannot extend file \"%s\" beyond %u blocks",
447                                                 relpath(reln->smgr_rnode, forknum),
448                                                 InvalidBlockNumber)));
449
450         v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
451
452         seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE));
453
454         Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
455
456         /*
457          * Note: because caller usually obtained blocknum by calling mdnblocks,
458          * which did a seek(SEEK_END), this seek is often redundant and will be
459          * optimized away by fd.c.      It's not redundant, however, if there is a
460          * partial page at the end of the file. In that case we want to try to
461          * overwrite the partial page with a full page.  It's also not redundant
462          * if bufmgr.c had to dump another buffer of the same file to make room
463          * for the new page's buffer.
464          */
465         if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
466                 ereport(ERROR,
467                                 (errcode_for_file_access(),
468                                  errmsg("could not seek to block %u in file \"%s\": %m",
469                                                 blocknum, FilePathName(v->mdfd_vfd))));
470
471         if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ)) != BLCKSZ)
472         {
473                 if (nbytes < 0)
474                         ereport(ERROR,
475                                         (errcode_for_file_access(),
476                                          errmsg("could not extend file \"%s\": %m",
477                                                         FilePathName(v->mdfd_vfd)),
478                                          errhint("Check free disk space.")));
479                 /* short write: complain appropriately */
480                 ereport(ERROR,
481                                 (errcode(ERRCODE_DISK_FULL),
482                                  errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
483                                                 FilePathName(v->mdfd_vfd),
484                                                 nbytes, BLCKSZ, blocknum),
485                                  errhint("Check free disk space.")));
486         }
487
488         if (!skipFsync && !SmgrIsTemp(reln))
489                 register_dirty_segment(reln, forknum, v);
490
491         Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
492 }
493
494 /*
495  *      mdopen() -- Open the specified relation.
496  *
497  * Note we only open the first segment, when there are multiple segments.
498  *
499  * If first segment is not present, either ereport or return NULL according
500  * to "behavior".  We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
501  * EXTENSION_CREATE means it's OK to extend an existing relation, not to
502  * invent one out of whole cloth.
503  */
504 static MdfdVec *
505 mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
506 {
507         MdfdVec    *mdfd;
508         char       *path;
509         File            fd;
510
511         /* No work if already open */
512         if (reln->md_fd[forknum])
513                 return reln->md_fd[forknum];
514
515         path = relpath(reln->smgr_rnode, forknum);
516
517         fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
518
519         if (fd < 0)
520         {
521                 /*
522                  * During bootstrap, there are cases where a system relation will be
523                  * accessed (by internal backend processes) before the bootstrap
524                  * script nominally creates it.  Therefore, accept mdopen() as a
525                  * substitute for mdcreate() in bootstrap mode only. (See mdcreate)
526                  */
527                 if (IsBootstrapProcessingMode())
528                         fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
529                 if (fd < 0)
530                 {
531                         if (behavior == EXTENSION_RETURN_NULL &&
532                                 FILE_POSSIBLY_DELETED(errno))
533                         {
534                                 pfree(path);
535                                 return NULL;
536                         }
537                         ereport(ERROR,
538                                         (errcode_for_file_access(),
539                                          errmsg("could not open file \"%s\": %m", path)));
540                 }
541         }
542
543         pfree(path);
544
545         reln->md_fd[forknum] = mdfd = _fdvec_alloc();
546
547         mdfd->mdfd_vfd = fd;
548         mdfd->mdfd_segno = 0;
549         mdfd->mdfd_chain = NULL;
550         Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
551
552         return mdfd;
553 }
554
555 /*
556  *      mdclose() -- Close the specified relation, if it isn't closed already.
557  */
558 void
559 mdclose(SMgrRelation reln, ForkNumber forknum)
560 {
561         MdfdVec    *v = reln->md_fd[forknum];
562
563         /* No work if already closed */
564         if (v == NULL)
565                 return;
566
567         reln->md_fd[forknum] = NULL;    /* prevent dangling pointer after error */
568
569         while (v != NULL)
570         {
571                 MdfdVec    *ov = v;
572
573                 /* if not closed already */
574                 if (v->mdfd_vfd >= 0)
575                         FileClose(v->mdfd_vfd);
576                 /* Now free vector */
577                 v = v->mdfd_chain;
578                 pfree(ov);
579         }
580 }
581
582 /*
583  *      mdprefetch() -- Initiate asynchronous read of the specified block of a relation
584  */
585 void
586 mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
587 {
588 #ifdef USE_PREFETCH
589         off_t           seekpos;
590         MdfdVec    *v;
591
592         v = _mdfd_getseg(reln, forknum, blocknum, false, EXTENSION_FAIL);
593
594         seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE));
595
596         Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
597
598         (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ);
599 #endif   /* USE_PREFETCH */
600 }
601
602
603 /*
604  *      mdread() -- Read the specified block from a relation.
605  */
606 void
607 mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
608            char *buffer)
609 {
610         off_t           seekpos;
611         int                     nbytes;
612         MdfdVec    *v;
613
614         TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
615                                                                                 reln->smgr_rnode.node.spcNode,
616                                                                                 reln->smgr_rnode.node.dbNode,
617                                                                                 reln->smgr_rnode.node.relNode,
618                                                                                 reln->smgr_rnode.backend);
619
620         v = _mdfd_getseg(reln, forknum, blocknum, false, EXTENSION_FAIL);
621
622         seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE));
623
624         Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
625
626         if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
627                 ereport(ERROR,
628                                 (errcode_for_file_access(),
629                                  errmsg("could not seek to block %u in file \"%s\": %m",
630                                                 blocknum, FilePathName(v->mdfd_vfd))));
631
632         nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ);
633
634         TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
635                                                                            reln->smgr_rnode.node.spcNode,
636                                                                            reln->smgr_rnode.node.dbNode,
637                                                                            reln->smgr_rnode.node.relNode,
638                                                                            reln->smgr_rnode.backend,
639                                                                            nbytes,
640                                                                            BLCKSZ);
641
642         if (nbytes != BLCKSZ)
643         {
644                 if (nbytes < 0)
645                         ereport(ERROR,
646                                         (errcode_for_file_access(),
647                                          errmsg("could not read block %u in file \"%s\": %m",
648                                                         blocknum, FilePathName(v->mdfd_vfd))));
649
650                 /*
651                  * Short read: we are at or past EOF, or we read a partial block at
652                  * EOF.  Normally this is an error; upper levels should never try to
653                  * read a nonexistent block.  However, if zero_damaged_pages is ON or
654                  * we are InRecovery, we should instead return zeroes without
655                  * complaining.  This allows, for example, the case of trying to
656                  * update a block that was later truncated away.
657                  */
658                 if (zero_damaged_pages || InRecovery)
659                         MemSet(buffer, 0, BLCKSZ);
660                 else
661                         ereport(ERROR,
662                                         (errcode(ERRCODE_DATA_CORRUPTED),
663                                          errmsg("could not read block %u in file \"%s\": read only %d of %d bytes",
664                                                         blocknum, FilePathName(v->mdfd_vfd),
665                                                         nbytes, BLCKSZ)));
666         }
667 }
668
669 /*
670  *      mdwrite() -- Write the supplied block at the appropriate location.
671  *
672  *              This is to be used only for updating already-existing blocks of a
673  *              relation (ie, those before the current EOF).  To extend a relation,
674  *              use mdextend().
675  */
676 void
677 mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
678                 char *buffer, bool skipFsync)
679 {
680         off_t           seekpos;
681         int                     nbytes;
682         MdfdVec    *v;
683
684         /* This assert is too expensive to have on normally ... */
685 #ifdef CHECK_WRITE_VS_EXTEND
686         Assert(blocknum < mdnblocks(reln, forknum));
687 #endif
688
689         TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
690                                                                                  reln->smgr_rnode.node.spcNode,
691                                                                                  reln->smgr_rnode.node.dbNode,
692                                                                                  reln->smgr_rnode.node.relNode,
693                                                                                  reln->smgr_rnode.backend);
694
695         v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_FAIL);
696
697         seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE));
698
699         Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
700
701         if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
702                 ereport(ERROR,
703                                 (errcode_for_file_access(),
704                                  errmsg("could not seek to block %u in file \"%s\": %m",
705                                                 blocknum, FilePathName(v->mdfd_vfd))));
706
707         nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ);
708
709         TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
710                                                                                 reln->smgr_rnode.node.spcNode,
711                                                                                 reln->smgr_rnode.node.dbNode,
712                                                                                 reln->smgr_rnode.node.relNode,
713                                                                                 reln->smgr_rnode.backend,
714                                                                                 nbytes,
715                                                                                 BLCKSZ);
716
717         if (nbytes != BLCKSZ)
718         {
719                 if (nbytes < 0)
720                         ereport(ERROR,
721                                         (errcode_for_file_access(),
722                                          errmsg("could not write block %u in file \"%s\": %m",
723                                                         blocknum, FilePathName(v->mdfd_vfd))));
724                 /* short write: complain appropriately */
725                 ereport(ERROR,
726                                 (errcode(ERRCODE_DISK_FULL),
727                                  errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes",
728                                                 blocknum,
729                                                 FilePathName(v->mdfd_vfd),
730                                                 nbytes, BLCKSZ),
731                                  errhint("Check free disk space.")));
732         }
733
734         if (!skipFsync && !SmgrIsTemp(reln))
735                 register_dirty_segment(reln, forknum, v);
736 }
737
738 /*
739  *      mdnblocks() -- Get the number of blocks stored in a relation.
740  *
741  *              Important side effect: all active segments of the relation are opened
742  *              and added to the mdfd_chain list.  If this routine has not been
743  *              called, then only segments up to the last one actually touched
744  *              are present in the chain.
745  */
746 BlockNumber
747 mdnblocks(SMgrRelation reln, ForkNumber forknum)
748 {
749         MdfdVec    *v = mdopen(reln, forknum, EXTENSION_FAIL);
750         BlockNumber nblocks;
751         BlockNumber segno = 0;
752
753         /*
754          * Skip through any segments that aren't the last one, to avoid redundant
755          * seeks on them.  We have previously verified that these segments are
756          * exactly RELSEG_SIZE long, and it's useless to recheck that each time.
757          *
758          * NOTE: this assumption could only be wrong if another backend has
759          * truncated the relation.      We rely on higher code levels to handle that
760          * scenario by closing and re-opening the md fd, which is handled via
761          * relcache flush.      (Since the bgwriter doesn't participate in relcache
762          * flush, it could have segment chain entries for inactive segments;
763          * that's OK because the bgwriter never needs to compute relation size.)
764          */
765         while (v->mdfd_chain != NULL)
766         {
767                 segno++;
768                 v = v->mdfd_chain;
769         }
770
771         for (;;)
772         {
773                 nblocks = _mdnblocks(reln, forknum, v);
774                 if (nblocks > ((BlockNumber) RELSEG_SIZE))
775                         elog(FATAL, "segment too big");
776                 if (nblocks < ((BlockNumber) RELSEG_SIZE))
777                         return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
778
779                 /*
780                  * If segment is exactly RELSEG_SIZE, advance to next one.
781                  */
782                 segno++;
783
784                 if (v->mdfd_chain == NULL)
785                 {
786                         /*
787                          * Because we pass O_CREAT, we will create the next segment (with
788                          * zero length) immediately, if the last segment is of length
789                          * RELSEG_SIZE.  While perhaps not strictly necessary, this keeps
790                          * the logic simple.
791                          */
792                         v->mdfd_chain = _mdfd_openseg(reln, forknum, segno, O_CREAT);
793                         if (v->mdfd_chain == NULL)
794                                 ereport(ERROR,
795                                                 (errcode_for_file_access(),
796                                                  errmsg("could not open file \"%s\": %m",
797                                                                 _mdfd_segpath(reln, forknum, segno))));
798                 }
799
800                 v = v->mdfd_chain;
801         }
802 }
803
804 /*
805  *      mdtruncate() -- Truncate relation to specified number of blocks.
806  */
807 void
808 mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
809 {
810         MdfdVec    *v;
811         BlockNumber curnblk;
812         BlockNumber priorblocks;
813
814         /*
815          * NOTE: mdnblocks makes sure we have opened all active segments, so that
816          * truncation loop will get them all!
817          */
818         curnblk = mdnblocks(reln, forknum);
819         if (nblocks > curnblk)
820         {
821                 /* Bogus request ... but no complaint if InRecovery */
822                 if (InRecovery)
823                         return;
824                 ereport(ERROR,
825                                 (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
826                                                 relpath(reln->smgr_rnode, forknum),
827                                                 nblocks, curnblk)));
828         }
829         if (nblocks == curnblk)
830                 return;                                 /* no work */
831
832         v = mdopen(reln, forknum, EXTENSION_FAIL);
833
834         priorblocks = 0;
835         while (v != NULL)
836         {
837                 MdfdVec    *ov = v;
838
839                 if (priorblocks > nblocks)
840                 {
841                         /*
842                          * This segment is no longer active (and has already been unlinked
843                          * from the mdfd_chain). We truncate the file, but do not delete
844                          * it, for reasons explained in the header comments.
845                          */
846                         if (FileTruncate(v->mdfd_vfd, 0) < 0)
847                                 ereport(ERROR,
848                                                 (errcode_for_file_access(),
849                                                  errmsg("could not truncate file \"%s\": %m",
850                                                                 FilePathName(v->mdfd_vfd))));
851
852                         if (!SmgrIsTemp(reln))
853                                 register_dirty_segment(reln, forknum, v);
854                         v = v->mdfd_chain;
855                         Assert(ov != reln->md_fd[forknum]); /* we never drop the 1st
856                                                                                                  * segment */
857                         pfree(ov);
858                 }
859                 else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
860                 {
861                         /*
862                          * This is the last segment we want to keep. Truncate the file to
863                          * the right length, and clear chain link that points to any
864                          * remaining segments (which we shall zap). NOTE: if nblocks is
865                          * exactly a multiple K of RELSEG_SIZE, we will truncate the K+1st
866                          * segment to 0 length but keep it. This adheres to the invariant
867                          * given in the header comments.
868                          */
869                         BlockNumber lastsegblocks = nblocks - priorblocks;
870
871                         if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ) < 0)
872                                 ereport(ERROR,
873                                                 (errcode_for_file_access(),
874                                         errmsg("could not truncate file \"%s\" to %u blocks: %m",
875                                                    FilePathName(v->mdfd_vfd),
876                                                    nblocks)));
877                         if (!SmgrIsTemp(reln))
878                                 register_dirty_segment(reln, forknum, v);
879                         v = v->mdfd_chain;
880                         ov->mdfd_chain = NULL;
881                 }
882                 else
883                 {
884                         /*
885                          * We still need this segment and 0 or more blocks beyond it, so
886                          * nothing to do here.
887                          */
888                         v = v->mdfd_chain;
889                 }
890                 priorblocks += RELSEG_SIZE;
891         }
892 }
893
894 /*
895  *      mdimmedsync() -- Immediately sync a relation to stable storage.
896  *
897  * Note that only writes already issued are synced; this routine knows
898  * nothing of dirty buffers that may exist inside the buffer manager.
899  */
900 void
901 mdimmedsync(SMgrRelation reln, ForkNumber forknum)
902 {
903         MdfdVec    *v;
904
905         /*
906          * NOTE: mdnblocks makes sure we have opened all active segments, so that
907          * fsync loop will get them all!
908          */
909         mdnblocks(reln, forknum);
910
911         v = mdopen(reln, forknum, EXTENSION_FAIL);
912
913         while (v != NULL)
914         {
915                 if (FileSync(v->mdfd_vfd) < 0)
916                         ereport(ERROR,
917                                         (errcode_for_file_access(),
918                                          errmsg("could not fsync file \"%s\": %m",
919                                                         FilePathName(v->mdfd_vfd))));
920                 v = v->mdfd_chain;
921         }
922 }
923
924 /*
925  *      mdsync() -- Sync previous writes to stable storage.
926  */
927 void
928 mdsync(void)
929 {
930         static bool mdsync_in_progress = false;
931
932         HASH_SEQ_STATUS hstat;
933         PendingOperationEntry *entry;
934         int                     absorb_counter;
935
936         /* Statistics on sync times */
937         int                     processed = 0;
938         instr_time      sync_start,
939                                 sync_end,
940                                 sync_diff;
941         uint64          elapsed;
942         uint64          longest = 0;
943         uint64          total_elapsed = 0;
944
945         /*
946          * This is only called during checkpoints, and checkpoints should only
947          * occur in processes that have created a pendingOpsTable.
948          */
949         if (!pendingOpsTable)
950                 elog(ERROR, "cannot sync without a pendingOpsTable");
951
952         /*
953          * If we are in the bgwriter, the sync had better include all fsync
954          * requests that were queued by backends up to this point.      The tightest
955          * race condition that could occur is that a buffer that must be written
956          * and fsync'd for the checkpoint could have been dumped by a backend just
957          * before it was visited by BufferSync().  We know the backend will have
958          * queued an fsync request before clearing the buffer's dirtybit, so we
959          * are safe as long as we do an Absorb after completing BufferSync().
960          */
961         AbsorbFsyncRequests();
962
963         /*
964          * To avoid excess fsync'ing (in the worst case, maybe a never-terminating
965          * checkpoint), we want to ignore fsync requests that are entered into the
966          * hashtable after this point --- they should be processed next time,
967          * instead.  We use mdsync_cycle_ctr to tell old entries apart from new
968          * ones: new ones will have cycle_ctr equal to the incremented value of
969          * mdsync_cycle_ctr.
970          *
971          * In normal circumstances, all entries present in the table at this point
972          * will have cycle_ctr exactly equal to the current (about to be old)
973          * value of mdsync_cycle_ctr.  However, if we fail partway through the
974          * fsync'ing loop, then older values of cycle_ctr might remain when we
975          * come back here to try again.  Repeated checkpoint failures would
976          * eventually wrap the counter around to the point where an old entry
977          * might appear new, causing us to skip it, possibly allowing a checkpoint
978          * to succeed that should not have.  To forestall wraparound, any time the
979          * previous mdsync() failed to complete, run through the table and
980          * forcibly set cycle_ctr = mdsync_cycle_ctr.
981          *
982          * Think not to merge this loop with the main loop, as the problem is
983          * exactly that that loop may fail before having visited all the entries.
984          * From a performance point of view it doesn't matter anyway, as this path
985          * will never be taken in a system that's functioning normally.
986          */
987         if (mdsync_in_progress)
988         {
989                 /* prior try failed, so update any stale cycle_ctr values */
990                 hash_seq_init(&hstat, pendingOpsTable);
991                 while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
992                 {
993                         entry->cycle_ctr = mdsync_cycle_ctr;
994                 }
995         }
996
997         /* Advance counter so that new hashtable entries are distinguishable */
998         mdsync_cycle_ctr++;
999
1000         /* Set flag to detect failure if we don't reach the end of the loop */
1001         mdsync_in_progress = true;
1002
1003         /* Now scan the hashtable for fsync requests to process */
1004         absorb_counter = FSYNCS_PER_ABSORB;
1005         hash_seq_init(&hstat, pendingOpsTable);
1006         while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
1007         {
1008                 /*
1009                  * If the entry is new then don't process it this time.  Note that
1010                  * "continue" bypasses the hash-remove call at the bottom of the loop.
1011                  */
1012                 if (entry->cycle_ctr == mdsync_cycle_ctr)
1013                         continue;
1014
1015                 /* Else assert we haven't missed it */
1016                 Assert((CycleCtr) (entry->cycle_ctr + 1) == mdsync_cycle_ctr);
1017
1018                 /*
1019                  * If fsync is off then we don't have to bother opening the file at
1020                  * all.  (We delay checking until this point so that changing fsync on
1021                  * the fly behaves sensibly.)  Also, if the entry is marked canceled,
1022                  * fall through to delete it.
1023                  */
1024                 if (enableFsync && !entry->canceled)
1025                 {
1026                         int                     failures;
1027
1028                         /*
1029                          * If in bgwriter, we want to absorb pending requests every so
1030                          * often to prevent overflow of the fsync request queue.  It is
1031                          * unspecified whether newly-added entries will be visited by
1032                          * hash_seq_search, but we don't care since we don't need to
1033                          * process them anyway.
1034                          */
1035                         if (--absorb_counter <= 0)
1036                         {
1037                                 AbsorbFsyncRequests();
1038                                 absorb_counter = FSYNCS_PER_ABSORB;
1039                         }
1040
1041                         /*
1042                          * The fsync table could contain requests to fsync segments that
1043                          * have been deleted (unlinked) by the time we get to them. Rather
1044                          * than just hoping an ENOENT (or EACCES on Windows) error can be
1045                          * ignored, what we do on error is absorb pending requests and
1046                          * then retry.  Since mdunlink() queues a "revoke" message before
1047                          * actually unlinking, the fsync request is guaranteed to be
1048                          * marked canceled after the absorb if it really was this case.
1049                          * DROP DATABASE likewise has to tell us to forget fsync requests
1050                          * before it starts deletions.
1051                          */
1052                         for (failures = 0;; failures++)         /* loop exits at "break" */
1053                         {
1054                                 SMgrRelation reln;
1055                                 MdfdVec    *seg;
1056                                 char       *path;
1057
1058                                 /*
1059                                  * Find or create an smgr hash entry for this relation. This
1060                                  * may seem a bit unclean -- md calling smgr?  But it's really
1061                                  * the best solution.  It ensures that the open file reference
1062                                  * isn't permanently leaked if we get an error here. (You may
1063                                  * say "but an unreferenced SMgrRelation is still a leak!" Not
1064                                  * really, because the only case in which a checkpoint is done
1065                                  * by a process that isn't about to shut down is in the
1066                                  * bgwriter, and it will periodically do smgrcloseall(). This
1067                                  * fact justifies our not closing the reln in the success path
1068                                  * either, which is a good thing since in non-bgwriter cases
1069                                  * we couldn't safely do that.)  Furthermore, in many cases
1070                                  * the relation will have been dirtied through this same smgr
1071                                  * relation, and so we can save a file open/close cycle.
1072                                  */
1073                                 reln = smgropen(entry->tag.rnode.node,
1074                                                                 entry->tag.rnode.backend);
1075
1076                                 /*
1077                                  * It is possible that the relation has been dropped or
1078                                  * truncated since the fsync request was entered.  Therefore,
1079                                  * allow ENOENT, but only if we didn't fail already on this
1080                                  * file.  This applies both during _mdfd_getseg() and during
1081                                  * FileSync, since fd.c might have closed the file behind our
1082                                  * back.
1083                                  */
1084                                 seg = _mdfd_getseg(reln, entry->tag.forknum,
1085                                                           entry->tag.segno * ((BlockNumber) RELSEG_SIZE),
1086                                                                    false, EXTENSION_RETURN_NULL);
1087
1088                                 if (log_checkpoints)
1089                                         INSTR_TIME_SET_CURRENT(sync_start);
1090                                 else
1091                                         INSTR_TIME_SET_ZERO(sync_start);
1092
1093                                 if (seg != NULL &&
1094                                         FileSync(seg->mdfd_vfd) >= 0)
1095                                 {
1096                                         if (log_checkpoints && (!INSTR_TIME_IS_ZERO(sync_start)))
1097                                         {
1098                                                 INSTR_TIME_SET_CURRENT(sync_end);
1099                                                 sync_diff = sync_end;
1100                                                 INSTR_TIME_SUBTRACT(sync_diff, sync_start);
1101                                                 elapsed = INSTR_TIME_GET_MICROSEC(sync_diff);
1102                                                 if (elapsed > longest)
1103                                                         longest = elapsed;
1104                                                 total_elapsed += elapsed;
1105                                                 processed++;
1106                                                 elog(DEBUG1, "checkpoint sync: number=%d file=%s time=%.3f msec",
1107                                                          processed, FilePathName(seg->mdfd_vfd), (double) elapsed / 1000);
1108                                         }
1109
1110                                         break;          /* success; break out of retry loop */
1111                                 }
1112
1113                                 /*
1114                                  * XXX is there any point in allowing more than one retry?
1115                                  * Don't see one at the moment, but easy to change the test
1116                                  * here if so.
1117                                  */
1118                                 path = _mdfd_segpath(reln, entry->tag.forknum,
1119                                                                          entry->tag.segno);
1120                                 if (!FILE_POSSIBLY_DELETED(errno) ||
1121                                         failures > 0)
1122                                         ereport(ERROR,
1123                                                         (errcode_for_file_access(),
1124                                                    errmsg("could not fsync file \"%s\": %m", path)));
1125                                 else
1126                                         ereport(DEBUG1,
1127                                                         (errcode_for_file_access(),
1128                                            errmsg("could not fsync file \"%s\" but retrying: %m",
1129                                                           path)));
1130                                 pfree(path);
1131
1132                                 /*
1133                                  * Absorb incoming requests and check to see if canceled.
1134                                  */
1135                                 AbsorbFsyncRequests();
1136                                 absorb_counter = FSYNCS_PER_ABSORB;             /* might as well... */
1137
1138                                 if (entry->canceled)
1139                                         break;
1140                         }                                       /* end retry loop */
1141                 }
1142
1143                 /*
1144                  * If we get here, either we fsync'd successfully, or we don't have to
1145                  * because enableFsync is off, or the entry is (now) marked canceled.
1146                  * Okay to delete it.
1147                  */
1148                 if (hash_search(pendingOpsTable, &entry->tag,
1149                                                 HASH_REMOVE, NULL) == NULL)
1150                         elog(ERROR, "pendingOpsTable corrupted");
1151         }                                                       /* end loop over hashtable entries */
1152
1153         /* Return sync performance metrics for report at checkpoint end */
1154         CheckpointStats.ckpt_sync_rels = processed;
1155         CheckpointStats.ckpt_longest_sync = longest;
1156         CheckpointStats.ckpt_agg_sync_time = total_elapsed;
1157
1158         /* Flag successful completion of mdsync */
1159         mdsync_in_progress = false;
1160 }
1161
1162 /*
1163  * mdpreckpt() -- Do pre-checkpoint work
1164  *
1165  * To distinguish unlink requests that arrived before this checkpoint
1166  * started from those that arrived during the checkpoint, we use a cycle
1167  * counter similar to the one we use for fsync requests. That cycle
1168  * counter is incremented here.
1169  *
1170  * This must be called *before* the checkpoint REDO point is determined.
1171  * That ensures that we won't delete files too soon.
1172  *
1173  * Note that we can't do anything here that depends on the assumption
1174  * that the checkpoint will be completed.
1175  */
1176 void
1177 mdpreckpt(void)
1178 {
1179         ListCell   *cell;
1180
1181         /*
1182          * In case the prior checkpoint wasn't completed, stamp all entries in the
1183          * list with the current cycle counter.  Anything that's in the list at
1184          * the start of checkpoint can surely be deleted after the checkpoint is
1185          * finished, regardless of when the request was made.
1186          */
1187         foreach(cell, pendingUnlinks)
1188         {
1189                 PendingUnlinkEntry *entry = (PendingUnlinkEntry *) lfirst(cell);
1190
1191                 entry->cycle_ctr = mdckpt_cycle_ctr;
1192         }
1193
1194         /*
1195          * Any unlink requests arriving after this point will be assigned the next
1196          * cycle counter, and won't be unlinked until next checkpoint.
1197          */
1198         mdckpt_cycle_ctr++;
1199 }
1200
1201 /*
1202  * mdpostckpt() -- Do post-checkpoint work
1203  *
1204  * Remove any lingering files that can now be safely removed.
1205  */
1206 void
1207 mdpostckpt(void)
1208 {
1209         while (pendingUnlinks != NIL)
1210         {
1211                 PendingUnlinkEntry *entry = (PendingUnlinkEntry *) linitial(pendingUnlinks);
1212                 char       *path;
1213
1214                 /*
1215                  * New entries are appended to the end, so if the entry is new we've
1216                  * reached the end of old entries.
1217                  */
1218                 if (entry->cycle_ctr == mdckpt_cycle_ctr)
1219                         break;
1220
1221                 /* Else assert we haven't missed it */
1222                 Assert((CycleCtr) (entry->cycle_ctr + 1) == mdckpt_cycle_ctr);
1223
1224                 /* Unlink the file */
1225                 path = relpath(entry->rnode, MAIN_FORKNUM);
1226                 if (unlink(path) < 0)
1227                 {
1228                         /*
1229                          * There's a race condition, when the database is dropped at the
1230                          * same time that we process the pending unlink requests. If the
1231                          * DROP DATABASE deletes the file before we do, we will get ENOENT
1232                          * here. rmtree() also has to ignore ENOENT errors, to deal with
1233                          * the possibility that we delete the file first.
1234                          */
1235                         if (errno != ENOENT)
1236                                 ereport(WARNING,
1237                                                 (errcode_for_file_access(),
1238                                                  errmsg("could not remove file \"%s\": %m", path)));
1239                 }
1240                 pfree(path);
1241
1242                 pendingUnlinks = list_delete_first(pendingUnlinks);
1243                 pfree(entry);
1244         }
1245 }
1246
1247 /*
1248  * register_dirty_segment() -- Mark a relation segment as needing fsync
1249  *
1250  * If there is a local pending-ops table, just make an entry in it for
1251  * mdsync to process later.  Otherwise, try to pass off the fsync request
1252  * to the background writer process.  If that fails, just do the fsync
1253  * locally before returning (we expect this will not happen often enough
1254  * to be a performance problem).
1255  */
1256 static void
1257 register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
1258 {
1259         if (pendingOpsTable)
1260         {
1261                 /* push it into local pending-ops table */
1262                 RememberFsyncRequest(reln->smgr_rnode, forknum, seg->mdfd_segno);
1263         }
1264         else
1265         {
1266                 if (ForwardFsyncRequest(reln->smgr_rnode, forknum, seg->mdfd_segno))
1267                         return;                         /* passed it off successfully */
1268
1269                 ereport(DEBUG1,
1270                                 (errmsg("could not forward fsync request because request queue is full")));
1271
1272                 if (FileSync(seg->mdfd_vfd) < 0)
1273                         ereport(ERROR,
1274                                         (errcode_for_file_access(),
1275                                          errmsg("could not fsync file \"%s\": %m",
1276                                                         FilePathName(seg->mdfd_vfd))));
1277         }
1278 }
1279
1280 /*
1281  * register_unlink() -- Schedule a file to be deleted after next checkpoint
1282  *
1283  * As with register_dirty_segment, this could involve either a local or
1284  * a remote pending-ops table.
1285  */
1286 static void
1287 register_unlink(RelFileNodeBackend rnode)
1288 {
1289         if (pendingOpsTable)
1290         {
1291                 /* push it into local pending-ops table */
1292                 RememberFsyncRequest(rnode, MAIN_FORKNUM, UNLINK_RELATION_REQUEST);
1293         }
1294         else
1295         {
1296                 /*
1297                  * Notify the bgwriter about it.  If we fail to queue the request
1298                  * message, we have to sleep and try again, because we can't simply
1299                  * delete the file now.  Ugly, but hopefully won't happen often.
1300                  *
1301                  * XXX should we just leave the file orphaned instead?
1302                  */
1303                 Assert(IsUnderPostmaster);
1304                 while (!ForwardFsyncRequest(rnode, MAIN_FORKNUM,
1305                                                                         UNLINK_RELATION_REQUEST))
1306                         pg_usleep(10000L);      /* 10 msec seems a good number */
1307         }
1308 }
1309
1310 /*
1311  * RememberFsyncRequest() -- callback from bgwriter side of fsync request
1312  *
1313  * We stuff most fsync requests into the local hash table for execution
1314  * during the bgwriter's next checkpoint.  UNLINK requests go into a
1315  * separate linked list, however, because they get processed separately.
1316  *
1317  * The range of possible segment numbers is way less than the range of
1318  * BlockNumber, so we can reserve high values of segno for special purposes.
1319  * We define three:
1320  * - FORGET_RELATION_FSYNC means to cancel pending fsyncs for a relation
1321  * - FORGET_DATABASE_FSYNC means to cancel pending fsyncs for a whole database
1322  * - UNLINK_RELATION_REQUEST is a request to delete the file after the next
1323  *       checkpoint.
1324  *
1325  * (Handling the FORGET_* requests is a tad slow because the hash table has
1326  * to be searched linearly, but it doesn't seem worth rethinking the table
1327  * structure for them.)
1328  */
1329 void
1330 RememberFsyncRequest(RelFileNodeBackend rnode, ForkNumber forknum,
1331                                          BlockNumber segno)
1332 {
1333         Assert(pendingOpsTable);
1334
1335         if (segno == FORGET_RELATION_FSYNC)
1336         {
1337                 /* Remove any pending requests for the entire relation */
1338                 HASH_SEQ_STATUS hstat;
1339                 PendingOperationEntry *entry;
1340
1341                 hash_seq_init(&hstat, pendingOpsTable);
1342                 while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
1343                 {
1344                         if (RelFileNodeBackendEquals(entry->tag.rnode, rnode) &&
1345                                 entry->tag.forknum == forknum)
1346                         {
1347                                 /* Okay, cancel this entry */
1348                                 entry->canceled = true;
1349                         }
1350                 }
1351         }
1352         else if (segno == FORGET_DATABASE_FSYNC)
1353         {
1354                 /* Remove any pending requests for the entire database */
1355                 HASH_SEQ_STATUS hstat;
1356                 PendingOperationEntry *entry;
1357                 ListCell   *cell,
1358                                    *prev,
1359                                    *next;
1360
1361                 /* Remove fsync requests */
1362                 hash_seq_init(&hstat, pendingOpsTable);
1363                 while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
1364                 {
1365                         if (entry->tag.rnode.node.dbNode == rnode.node.dbNode)
1366                         {
1367                                 /* Okay, cancel this entry */
1368                                 entry->canceled = true;
1369                         }
1370                 }
1371
1372                 /* Remove unlink requests */
1373                 prev = NULL;
1374                 for (cell = list_head(pendingUnlinks); cell; cell = next)
1375                 {
1376                         PendingUnlinkEntry *entry = (PendingUnlinkEntry *) lfirst(cell);
1377
1378                         next = lnext(cell);
1379                         if (entry->rnode.node.dbNode == rnode.node.dbNode)
1380                         {
1381                                 pendingUnlinks = list_delete_cell(pendingUnlinks, cell, prev);
1382                                 pfree(entry);
1383                         }
1384                         else
1385                                 prev = cell;
1386                 }
1387         }
1388         else if (segno == UNLINK_RELATION_REQUEST)
1389         {
1390                 /* Unlink request: put it in the linked list */
1391                 MemoryContext oldcxt = MemoryContextSwitchTo(MdCxt);
1392                 PendingUnlinkEntry *entry;
1393
1394                 entry = palloc(sizeof(PendingUnlinkEntry));
1395                 entry->rnode = rnode;
1396                 entry->cycle_ctr = mdckpt_cycle_ctr;
1397
1398                 pendingUnlinks = lappend(pendingUnlinks, entry);
1399
1400                 MemoryContextSwitchTo(oldcxt);
1401         }
1402         else
1403         {
1404                 /* Normal case: enter a request to fsync this segment */
1405                 PendingOperationTag key;
1406                 PendingOperationEntry *entry;
1407                 bool            found;
1408
1409                 /* ensure any pad bytes in the hash key are zeroed */
1410                 MemSet(&key, 0, sizeof(key));
1411                 key.rnode = rnode;
1412                 key.forknum = forknum;
1413                 key.segno = segno;
1414
1415                 entry = (PendingOperationEntry *) hash_search(pendingOpsTable,
1416                                                                                                           &key,
1417                                                                                                           HASH_ENTER,
1418                                                                                                           &found);
1419                 /* if new or previously canceled entry, initialize it */
1420                 if (!found || entry->canceled)
1421                 {
1422                         entry->canceled = false;
1423                         entry->cycle_ctr = mdsync_cycle_ctr;
1424                 }
1425
1426                 /*
1427                  * NB: it's intentional that we don't change cycle_ctr if the entry
1428                  * already exists.      The fsync request must be treated as old, even
1429                  * though the new request will be satisfied too by any subsequent
1430                  * fsync.
1431                  *
1432                  * However, if the entry is present but is marked canceled, we should
1433                  * act just as though it wasn't there.  The only case where this could
1434                  * happen would be if a file had been deleted, we received but did not
1435                  * yet act on the cancel request, and the same relfilenode was then
1436                  * assigned to a new file.      We mustn't lose the new request, but it
1437                  * should be considered new not old.
1438                  */
1439         }
1440 }
1441
1442 /*
1443  * ForgetRelationFsyncRequests -- forget any fsyncs for a rel
1444  */
1445 void
1446 ForgetRelationFsyncRequests(RelFileNodeBackend rnode, ForkNumber forknum)
1447 {
1448         if (pendingOpsTable)
1449         {
1450                 /* standalone backend or startup process: fsync state is local */
1451                 RememberFsyncRequest(rnode, forknum, FORGET_RELATION_FSYNC);
1452         }
1453         else if (IsUnderPostmaster)
1454         {
1455                 /*
1456                  * Notify the bgwriter about it.  If we fail to queue the revoke
1457                  * message, we have to sleep and try again ... ugly, but hopefully
1458                  * won't happen often.
1459                  *
1460                  * XXX should we CHECK_FOR_INTERRUPTS in this loop?  Escaping with an
1461                  * error would leave the no-longer-used file still present on disk,
1462                  * which would be bad, so I'm inclined to assume that the bgwriter
1463                  * will always empty the queue soon.
1464                  */
1465                 while (!ForwardFsyncRequest(rnode, forknum, FORGET_RELATION_FSYNC))
1466                         pg_usleep(10000L);      /* 10 msec seems a good number */
1467
1468                 /*
1469                  * Note we don't wait for the bgwriter to actually absorb the revoke
1470                  * message; see mdsync() for the implications.
1471                  */
1472         }
1473 }
1474
1475 /*
1476  * ForgetDatabaseFsyncRequests -- forget any fsyncs and unlinks for a DB
1477  */
1478 void
1479 ForgetDatabaseFsyncRequests(Oid dbid)
1480 {
1481         RelFileNodeBackend rnode;
1482
1483         rnode.node.dbNode = dbid;
1484         rnode.node.spcNode = 0;
1485         rnode.node.relNode = 0;
1486         rnode.backend = InvalidBackendId;
1487
1488         if (pendingOpsTable)
1489         {
1490                 /* standalone backend or startup process: fsync state is local */
1491                 RememberFsyncRequest(rnode, InvalidForkNumber, FORGET_DATABASE_FSYNC);
1492         }
1493         else if (IsUnderPostmaster)
1494         {
1495                 /* see notes in ForgetRelationFsyncRequests */
1496                 while (!ForwardFsyncRequest(rnode, InvalidForkNumber,
1497                                                                         FORGET_DATABASE_FSYNC))
1498                         pg_usleep(10000L);      /* 10 msec seems a good number */
1499         }
1500 }
1501
1502
1503 /*
1504  *      _fdvec_alloc() -- Make a MdfdVec object.
1505  */
1506 static MdfdVec *
1507 _fdvec_alloc(void)
1508 {
1509         return (MdfdVec *) MemoryContextAlloc(MdCxt, sizeof(MdfdVec));
1510 }
1511
1512 /*
1513  * Return the filename for the specified segment of the relation. The
1514  * returned string is palloc'd.
1515  */
1516 static char *
1517 _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
1518 {
1519         char       *path,
1520                            *fullpath;
1521
1522         path = relpath(reln->smgr_rnode, forknum);
1523
1524         if (segno > 0)
1525         {
1526                 /* be sure we have enough space for the '.segno' */
1527                 fullpath = (char *) palloc(strlen(path) + 12);
1528                 sprintf(fullpath, "%s.%u", path, segno);
1529                 pfree(path);
1530         }
1531         else
1532                 fullpath = path;
1533
1534         return fullpath;
1535 }
1536
1537 /*
1538  * Open the specified segment of the relation,
1539  * and make a MdfdVec object for it.  Returns NULL on failure.
1540  */
1541 static MdfdVec *
1542 _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
1543                           int oflags)
1544 {
1545         MdfdVec    *v;
1546         int                     fd;
1547         char       *fullpath;
1548
1549         fullpath = _mdfd_segpath(reln, forknum, segno);
1550
1551         /* open the file */
1552         fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags, 0600);
1553
1554         pfree(fullpath);
1555
1556         if (fd < 0)
1557                 return NULL;
1558
1559         /* allocate an mdfdvec entry for it */
1560         v = _fdvec_alloc();
1561
1562         /* fill the entry */
1563         v->mdfd_vfd = fd;
1564         v->mdfd_segno = segno;
1565         v->mdfd_chain = NULL;
1566         Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
1567
1568         /* all done */
1569         return v;
1570 }
1571
1572 /*
1573  *      _mdfd_getseg() -- Find the segment of the relation holding the
1574  *              specified block.
1575  *
1576  * If the segment doesn't exist, we ereport, return NULL, or create the
1577  * segment, according to "behavior".  Note: skipFsync is only used in the
1578  * EXTENSION_CREATE case.
1579  */
1580 static MdfdVec *
1581 _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
1582                          bool skipFsync, ExtensionBehavior behavior)
1583 {
1584         MdfdVec    *v = mdopen(reln, forknum, behavior);
1585         BlockNumber targetseg;
1586         BlockNumber nextsegno;
1587
1588         if (!v)
1589                 return NULL;                    /* only possible if EXTENSION_RETURN_NULL */
1590
1591         targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
1592         for (nextsegno = 1; nextsegno <= targetseg; nextsegno++)
1593         {
1594                 Assert(nextsegno == v->mdfd_segno + 1);
1595
1596                 if (v->mdfd_chain == NULL)
1597                 {
1598                         /*
1599                          * Normally we will create new segments only if authorized by the
1600                          * caller (i.e., we are doing mdextend()).      But when doing WAL
1601                          * recovery, create segments anyway; this allows cases such as
1602                          * replaying WAL data that has a write into a high-numbered
1603                          * segment of a relation that was later deleted.  We want to go
1604                          * ahead and create the segments so we can finish out the replay.
1605                          *
1606                          * We have to maintain the invariant that segments before the last
1607                          * active segment are of size RELSEG_SIZE; therefore, pad them out
1608                          * with zeroes if needed.  (This only matters if caller is
1609                          * extending the relation discontiguously, but that can happen in
1610                          * hash indexes.)
1611                          */
1612                         if (behavior == EXTENSION_CREATE || InRecovery)
1613                         {
1614                                 if (_mdnblocks(reln, forknum, v) < RELSEG_SIZE)
1615                                 {
1616                                         char       *zerobuf = palloc0(BLCKSZ);
1617
1618                                         mdextend(reln, forknum,
1619                                                          nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
1620                                                          zerobuf, skipFsync);
1621                                         pfree(zerobuf);
1622                                 }
1623                                 v->mdfd_chain = _mdfd_openseg(reln, forknum, +nextsegno, O_CREAT);
1624                         }
1625                         else
1626                         {
1627                                 /* We won't create segment if not existent */
1628                                 v->mdfd_chain = _mdfd_openseg(reln, forknum, nextsegno, 0);
1629                         }
1630                         if (v->mdfd_chain == NULL)
1631                         {
1632                                 if (behavior == EXTENSION_RETURN_NULL &&
1633                                         FILE_POSSIBLY_DELETED(errno))
1634                                         return NULL;
1635                                 ereport(ERROR,
1636                                                 (errcode_for_file_access(),
1637                                    errmsg("could not open file \"%s\" (target block %u): %m",
1638                                                   _mdfd_segpath(reln, forknum, nextsegno),
1639                                                   blkno)));
1640                         }
1641                 }
1642                 v = v->mdfd_chain;
1643         }
1644         return v;
1645 }
1646
1647 /*
1648  * Get number of blocks present in a single disk file
1649  */
1650 static BlockNumber
1651 _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
1652 {
1653         off_t           len;
1654
1655         len = FileSeek(seg->mdfd_vfd, 0L, SEEK_END);
1656         if (len < 0)
1657                 ereport(ERROR,
1658                                 (errcode_for_file_access(),
1659                                  errmsg("could not seek to end of file \"%s\": %m",
1660                                                 FilePathName(seg->mdfd_vfd))));
1661         /* note that this calculation will ignore any partial block at EOF */
1662         return (BlockNumber) (len / BLCKSZ);
1663 }