Skip to content

Commit 23dc89d

Browse files
committed
Improve error messages in md.c. When a filesystem operation like open() or
fsync() fails, say "file" rather than "relation" when printing the filename. This makes messages that display block numbers a bit confusing. For example, in message 'could not read block 150000 of file "base/1234/5678.1"', 150000 is the block number from the beginning of the relation, ie. segment 0, not 150000th block within that segment. Per discussion, users aren't usually interested in the exact location within the file, so we can live with that. To ease constructing error messages, add FilePathName(File) function to return the pathname of a virtual fd.
1 parent f4095b4 commit 23dc89d

File tree

3 files changed

+99
-68
lines changed

3 files changed

+99
-68
lines changed

src/backend/storage/file/fd.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.149 2009/06/11 14:49:01 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.150 2009/08/05 18:01:54 heikki Exp $
1111
*
1212
* NOTES:
1313
*
@@ -1319,6 +1319,20 @@ FileTruncate(File file, off_t offset)
13191319
return returnCode;
13201320
}
13211321

1322+
/*
1323+
* Return the pathname associated with an open file.
1324+
*
1325+
* The returned string points to an internal buffer, which is valid until
1326+
* the file is closed.
1327+
*/
1328+
char *
1329+
FilePathName(File file)
1330+
{
1331+
Assert(FileIsValid(file));
1332+
1333+
return VfdCache[file].fileName;
1334+
}
1335+
13221336

13231337
/*
13241338
* Routines that want to use stdio (ie, FILE*) should use AllocateFile

src/backend/storage/smgr/md.c

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/smgr/md.c,v 1.148 2009/06/26 20:29:04 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/smgr/md.c,v 1.149 2009/08/05 18:01:54 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -160,6 +160,8 @@ static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
160160
MdfdVec *seg);
161161
static void register_unlink(RelFileNode rnode);
162162
static MdfdVec *_fdvec_alloc(void);
163+
static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
164+
BlockNumber segno);
163165
static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
164166
BlockNumber segno, int oflags);
165167
static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forkno,
@@ -273,7 +275,7 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
273275
errno = save_errno;
274276
ereport(ERROR,
275277
(errcode_for_file_access(),
276-
errmsg("could not create relation %s: %m", path)));
278+
errmsg("could not create file \"%s\": %m", path)));
277279
}
278280
}
279281

@@ -336,7 +338,16 @@ mdunlink(RelFileNode rnode, ForkNumber forkNum, bool isRedo)
336338
* Delete or truncate the first segment.
337339
*/
338340
if (isRedo || forkNum != MAIN_FORKNUM)
341+
{
339342
ret = unlink(path);
343+
if (ret < 0)
344+
{
345+
if (!isRedo || errno != ENOENT)
346+
ereport(WARNING,
347+
(errcode_for_file_access(),
348+
errmsg("could not remove file \"%s\": %m", path)));
349+
}
350+
}
340351
else
341352
{
342353
/* truncate(2) would be easier here, but Windows hasn't got it */
@@ -354,19 +365,16 @@ mdunlink(RelFileNode rnode, ForkNumber forkNum, bool isRedo)
354365
}
355366
else
356367
ret = -1;
357-
}
358-
if (ret < 0)
359-
{
360-
if (!isRedo || errno != ENOENT)
368+
if (ret < 0 && errno != ENOENT)
361369
ereport(WARNING,
362370
(errcode_for_file_access(),
363-
errmsg("could not remove relation %s: %m", path)));
371+
errmsg("could not truncate file \"%s\": %m", path)));
364372
}
365373

366374
/*
367375
* Delete any additional segments.
368376
*/
369-
else
377+
if (ret >= 0)
370378
{
371379
char *segpath = (char *) palloc(strlen(path) + 12);
372380
BlockNumber segno;
@@ -384,8 +392,7 @@ mdunlink(RelFileNode rnode, ForkNumber forkNum, bool isRedo)
384392
if (errno != ENOENT)
385393
ereport(WARNING,
386394
(errcode_for_file_access(),
387-
errmsg("could not remove segment %u of relation %s: %m",
388-
segno, path)));
395+
errmsg("could not remove file \"%s\": %m", segpath)));
389396
break;
390397
}
391398
}
@@ -429,7 +436,7 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
429436
if (blocknum == InvalidBlockNumber)
430437
ereport(ERROR,
431438
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
432-
errmsg("cannot extend relation %s beyond %u blocks",
439+
errmsg("cannot extend file \"%s\" beyond %u blocks",
433440
relpath(reln->smgr_rnode, forknum),
434441
InvalidBlockNumber)));
435442

@@ -451,23 +458,22 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
451458
if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
452459
ereport(ERROR,
453460
(errcode_for_file_access(),
454-
errmsg("could not seek to block %u of relation %s: %m",
455-
blocknum,
456-
relpath(reln->smgr_rnode, forknum))));
461+
errmsg("could not seek to block %u in file \"%s\": %m",
462+
blocknum, FilePathName(v->mdfd_vfd))));
457463

458464
if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ)) != BLCKSZ)
459465
{
460466
if (nbytes < 0)
461467
ereport(ERROR,
462468
(errcode_for_file_access(),
463-
errmsg("could not extend relation %s: %m",
464-
relpath(reln->smgr_rnode, forknum)),
469+
errmsg("could not extend file \"%s\": %m",
470+
FilePathName(v->mdfd_vfd)),
465471
errhint("Check free disk space.")));
466472
/* short write: complain appropriately */
467473
ereport(ERROR,
468474
(errcode(ERRCODE_DISK_FULL),
469-
errmsg("could not extend relation %s: wrote only %d of %d bytes at block %u",
470-
relpath(reln->smgr_rnode, forknum),
475+
errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
476+
FilePathName(v->mdfd_vfd),
471477
nbytes, BLCKSZ, blocknum),
472478
errhint("Check free disk space.")));
473479
}
@@ -523,7 +529,7 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
523529
}
524530
ereport(ERROR,
525531
(errcode_for_file_access(),
526-
errmsg("could not open relation %s: %m", path)));
532+
errmsg("could not open file \"%s\": %m", path)));
527533
}
528534
}
529535

@@ -612,8 +618,8 @@ mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
612618
if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
613619
ereport(ERROR,
614620
(errcode_for_file_access(),
615-
errmsg("could not seek to block %u of relation %s: %m",
616-
blocknum, relpath(reln->smgr_rnode, forknum))));
621+
errmsg("could not seek to block %u in file \"%s\": %m",
622+
blocknum, FilePathName(v->mdfd_vfd))));
617623

618624
nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ);
619625

@@ -629,8 +635,8 @@ mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
629635
if (nbytes < 0)
630636
ereport(ERROR,
631637
(errcode_for_file_access(),
632-
errmsg("could not read block %u of relation %s: %m",
633-
blocknum, relpath(reln->smgr_rnode, forknum))));
638+
errmsg("could not read block %u in file \"%s\": %m",
639+
blocknum, FilePathName(v->mdfd_vfd))));
634640

635641
/*
636642
* Short read: we are at or past EOF, or we read a partial block at
@@ -645,8 +651,8 @@ mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
645651
else
646652
ereport(ERROR,
647653
(errcode(ERRCODE_DATA_CORRUPTED),
648-
errmsg("could not read block %u of relation %s: read only %d of %d bytes",
649-
blocknum, relpath(reln->smgr_rnode, forknum),
654+
errmsg("could not read block %u in file \"%s\": read only %d of %d bytes",
655+
blocknum, FilePathName(v->mdfd_vfd),
650656
nbytes, BLCKSZ)));
651657
}
652658
}
@@ -685,8 +691,8 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
685691
if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
686692
ereport(ERROR,
687693
(errcode_for_file_access(),
688-
errmsg("could not seek to block %u of relation %s: %m",
689-
blocknum, relpath(reln->smgr_rnode, forknum))));
694+
errmsg("could not seek to block %u in file \"%s\": %m",
695+
blocknum, FilePathName(v->mdfd_vfd))));
690696

691697
nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ);
692698

@@ -702,14 +708,14 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
702708
if (nbytes < 0)
703709
ereport(ERROR,
704710
(errcode_for_file_access(),
705-
errmsg("could not write block %u of relation %s: %m",
706-
blocknum, relpath(reln->smgr_rnode, forknum))));
711+
errmsg("could not write block %u in file \"%s\": %m",
712+
blocknum, FilePathName(v->mdfd_vfd))));
707713
/* short write: complain appropriately */
708714
ereport(ERROR,
709715
(errcode(ERRCODE_DISK_FULL),
710-
errmsg("could not write block %u of relation %s: wrote only %d of %d bytes",
716+
errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes",
711717
blocknum,
712-
relpath(reln->smgr_rnode, forknum),
718+
FilePathName(v->mdfd_vfd),
713719
nbytes, BLCKSZ),
714720
errhint("Check free disk space.")));
715721
}
@@ -776,9 +782,8 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
776782
if (v->mdfd_chain == NULL)
777783
ereport(ERROR,
778784
(errcode_for_file_access(),
779-
errmsg("could not open segment %u of relation %s: %m",
780-
segno,
781-
relpath(reln->smgr_rnode, forknum))));
785+
errmsg("could not open file \"%s\": %m",
786+
_mdfd_segpath(reln, forknum, segno))));
782787
}
783788

784789
v = v->mdfd_chain;
@@ -807,7 +812,7 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
807812
if (InRecovery)
808813
return;
809814
ereport(ERROR,
810-
(errmsg("could not truncate relation %s to %u blocks: it's only %u blocks now",
815+
(errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
811816
relpath(reln->smgr_rnode, forknum),
812817
nblocks, curnblk)));
813818
}
@@ -831,9 +836,9 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
831836
if (FileTruncate(v->mdfd_vfd, 0) < 0)
832837
ereport(ERROR,
833838
(errcode_for_file_access(),
834-
errmsg("could not truncate relation %s to %u blocks: %m",
835-
relpath(reln->smgr_rnode, forknum),
836-
nblocks)));
839+
errmsg("could not truncate file \"%s\": %m",
840+
FilePathName(v->mdfd_vfd))));
841+
837842
if (!isTemp)
838843
register_dirty_segment(reln, forknum, v);
839844
v = v->mdfd_chain;
@@ -856,8 +861,8 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
856861
if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ) < 0)
857862
ereport(ERROR,
858863
(errcode_for_file_access(),
859-
errmsg("could not truncate relation %s to %u blocks: %m",
860-
relpath(reln->smgr_rnode, forknum),
864+
errmsg("could not truncate file \"%s\" to %u blocks: %m",
865+
FilePathName(v->mdfd_vfd),
861866
nblocks)));
862867
if (!isTemp)
863868
register_dirty_segment(reln, forknum, v);
@@ -901,9 +906,8 @@ mdimmedsync(SMgrRelation reln, ForkNumber forknum)
901906
if (FileSync(v->mdfd_vfd) < 0)
902907
ereport(ERROR,
903908
(errcode_for_file_access(),
904-
errmsg("could not fsync segment %u of relation %s: %m",
905-
v->mdfd_segno,
906-
relpath(reln->smgr_rnode, forknum))));
909+
errmsg("could not fsync file \"%s\": %m",
910+
FilePathName(v->mdfd_vfd))));
907911
v = v->mdfd_chain;
908912
}
909913
}
@@ -1070,18 +1074,18 @@ mdsync(void)
10701074
* Don't see one at the moment, but easy to change the test
10711075
* here if so.
10721076
*/
1073-
path = relpath(entry->tag.rnode, entry->tag.forknum);
1077+
path = _mdfd_segpath(reln, entry->tag.forknum,
1078+
entry->tag.segno);
10741079
if (!FILE_POSSIBLY_DELETED(errno) ||
10751080
failures > 0)
10761081
ereport(ERROR,
10771082
(errcode_for_file_access(),
1078-
errmsg("could not fsync segment %u of relation %s: %m",
1079-
entry->tag.segno, path)));
1083+
errmsg("could not fsync file \"%s\": %m", path)));
10801084
else
10811085
ereport(DEBUG1,
10821086
(errcode_for_file_access(),
1083-
errmsg("could not fsync segment %u of relation %s but retrying: %m",
1084-
entry->tag.segno, path)));
1087+
errmsg("could not fsync file \"%s\" but retrying: %m",
1088+
path)));
10851089
pfree(path);
10861090

10871091
/*
@@ -1185,7 +1189,7 @@ mdpostckpt(void)
11851189
if (errno != ENOENT)
11861190
ereport(WARNING,
11871191
(errcode_for_file_access(),
1188-
errmsg("could not remove relation %s: %m", path)));
1192+
errmsg("could not remove file \"%s\": %m", path)));
11891193
}
11901194
pfree(path);
11911195

@@ -1219,9 +1223,8 @@ register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
12191223
if (FileSync(seg->mdfd_vfd) < 0)
12201224
ereport(ERROR,
12211225
(errcode_for_file_access(),
1222-
errmsg("could not fsync segment %u of relation %s: %m",
1223-
seg->mdfd_segno,
1224-
relpath(reln->smgr_rnode, forknum))));
1226+
errmsg("could not fsync file \"%s\": %m",
1227+
FilePathName(seg->mdfd_vfd))));
12251228
}
12261229
}
12271230

@@ -1456,17 +1459,14 @@ _fdvec_alloc(void)
14561459
}
14571460

14581461
/*
1459-
* Open the specified segment of the relation,
1460-
* and make a MdfdVec object for it. Returns NULL on failure.
1462+
* Return the filename for the specified segment of the relation. The
1463+
* returned string is palloc'd.
14611464
*/
1462-
static MdfdVec *
1463-
_mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
1464-
int oflags)
1465+
static char *
1466+
_mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
14651467
{
1466-
MdfdVec *v;
1467-
int fd;
1468-
char *path,
1469-
*fullpath;
1468+
char *path,
1469+
*fullpath;
14701470

14711471
path = relpath(reln->smgr_rnode, forknum);
14721472

@@ -1480,6 +1480,23 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
14801480
else
14811481
fullpath = path;
14821482

1483+
return fullpath;
1484+
}
1485+
1486+
/*
1487+
* Open the specified segment of the relation,
1488+
* and make a MdfdVec object for it. Returns NULL on failure.
1489+
*/
1490+
static MdfdVec *
1491+
_mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
1492+
int oflags)
1493+
{
1494+
MdfdVec *v;
1495+
int fd;
1496+
char *fullpath;
1497+
1498+
fullpath = _mdfd_segpath(reln, forknum, segno);
1499+
14831500
/* open the file */
14841501
fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags, 0600);
14851502

@@ -1566,9 +1583,8 @@ _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
15661583
return NULL;
15671584
ereport(ERROR,
15681585
(errcode_for_file_access(),
1569-
errmsg("could not open segment %u of relation %s (target block %u): %m",
1570-
nextsegno,
1571-
relpath(reln->smgr_rnode, forknum),
1586+
errmsg("could not open file \"%s\" (target block %u): %m",
1587+
_mdfd_segpath(reln, forknum, nextsegno),
15721588
blkno)));
15731589
}
15741590
}
@@ -1589,8 +1605,8 @@ _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
15891605
if (len < 0)
15901606
ereport(ERROR,
15911607
(errcode_for_file_access(),
1592-
errmsg("could not seek to end of segment %u of relation %s: %m",
1593-
seg->mdfd_segno, relpath(reln->smgr_rnode, forknum))));
1608+
errmsg("could not seek to end of file \"%s\": %m",
1609+
FilePathName(seg->mdfd_vfd))));
15941610
/* note that this calculation will ignore any partial block at EOF */
15951611
return (BlockNumber) (len / BLCKSZ);
15961612
}

0 commit comments

Comments
 (0)