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,
160160MdfdVec * seg );
161161static void register_unlink (RelFileNode rnode );
162162static MdfdVec * _fdvec_alloc (void );
163+ static char * _mdfd_segpath (SMgrRelation reln ,ForkNumber forknum ,
164+ BlockNumber segno );
163165static MdfdVec * _mdfd_openseg (SMgrRelation reln ,ForkNumber forkno ,
164166BlockNumber segno ,int oflags );
165167static MdfdVec * _mdfd_getseg (SMgrRelation reln ,ForkNumber forkno ,
@@ -273,7 +275,7 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
273275errno = save_errno ;
274276ereport (ERROR ,
275277(errcode_for_file_access (),
276- errmsg ("could not createrelation %s : %m" ,path )));
278+ errmsg ("could not createfile \"%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 */
338340if (isRedo || forkNum != MAIN_FORKNUM )
341+ {
339342ret = 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+ }
340351else
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}
355366else
356367ret = -1 ;
357- }
358- if (ret < 0 )
359- {
360- if (!isRedo || errno != ENOENT )
368+ if (ret < 0 && errno != ENOENT )
361369ereport (WARNING ,
362370(errcode_for_file_access (),
363- errmsg ("could notremove relation %s : %m" ,path )));
371+ errmsg ("could nottruncate file \"%s\" : %m" ,path )));
364372}
365373
366374/*
367375 * Delete any additional segments.
368376 */
369- else
377+ if ( ret >= 0 )
370378{
371379char * segpath = (char * )palloc (strlen (path )+ 12 );
372380BlockNumber segno ;
@@ -384,8 +392,7 @@ mdunlink(RelFileNode rnode, ForkNumber forkNum, bool isRedo)
384392if (errno != ENOENT )
385393ereport (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 )));
389396break ;
390397}
391398}
@@ -429,7 +436,7 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
429436if (blocknum == InvalidBlockNumber )
430437ereport (ERROR ,
431438(errcode (ERRCODE_PROGRAM_LIMIT_EXCEEDED ),
432- errmsg ("cannot extendrelation %s beyond %u blocks" ,
439+ errmsg ("cannot extendfile \"%s\" beyond %u blocks" ,
433440relpath (reln -> smgr_rnode ,forknum ),
434441InvalidBlockNumber )));
435442
@@ -451,23 +458,22 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
451458if (FileSeek (v -> mdfd_vfd ,seekpos ,SEEK_SET )!= seekpos )
452459ereport (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
458464if ((nbytes = FileWrite (v -> mdfd_vfd ,buffer ,BLCKSZ ))!= BLCKSZ )
459465{
460466if (nbytes < 0 )
461467ereport (ERROR ,
462468(errcode_for_file_access (),
463- errmsg ("could not extendrelation %s : %m" ,
464- relpath ( reln -> smgr_rnode , forknum )),
469+ errmsg ("could not extendfile \"%s\" : %m" ,
470+ FilePathName ( v -> mdfd_vfd )),
465471errhint ("Check free disk space." )));
466472/* short write: complain appropriately */
467473ereport (ERROR ,
468474(errcode (ERRCODE_DISK_FULL ),
469- errmsg ("could not extendrelation %s : wrote only %d of %d bytes at block %u" ,
470- relpath ( reln -> smgr_rnode , forknum ),
475+ errmsg ("could not extendfile \"%s\" : wrote only %d of %d bytes at block %u" ,
476+ FilePathName ( v -> mdfd_vfd ),
471477nbytes ,BLCKSZ ,blocknum ),
472478errhint ("Check free disk space." )));
473479}
@@ -523,7 +529,7 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
523529}
524530ereport (ERROR ,
525531(errcode_for_file_access (),
526- errmsg ("could not openrelation %s : %m" ,path )));
532+ errmsg ("could not openfile \"%s\" : %m" ,path )));
527533}
528534}
529535
@@ -612,8 +618,8 @@ mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
612618if (FileSeek (v -> mdfd_vfd ,seekpos ,SEEK_SET )!= seekpos )
613619ereport (ERROR ,
614620(errcode_for_file_access (),
615- errmsg ("could not seek to block %uof relation %s : %m" ,
616- blocknum ,relpath ( reln -> smgr_rnode , forknum ))));
621+ errmsg ("could not seek to block %uin file \"%s\" : %m" ,
622+ blocknum ,FilePathName ( v -> mdfd_vfd ))));
617623
618624nbytes = FileRead (v -> mdfd_vfd ,buffer ,BLCKSZ );
619625
@@ -629,8 +635,8 @@ mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
629635if (nbytes < 0 )
630636ereport (ERROR ,
631637(errcode_for_file_access (),
632- errmsg ("could not read block %uof relation %s : %m" ,
633- blocknum ,relpath ( reln -> smgr_rnode , forknum ))));
638+ errmsg ("could not read block %uin 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,
645651else
646652ereport (ERROR ,
647653(errcode (ERRCODE_DATA_CORRUPTED ),
648- errmsg ("could not read block %uof relation %s : read only %d of %d bytes" ,
649- blocknum ,relpath ( reln -> smgr_rnode , forknum ),
654+ errmsg ("could not read block %uin file \"%s\" : read only %d of %d bytes" ,
655+ blocknum ,FilePathName ( v -> mdfd_vfd ),
650656nbytes ,BLCKSZ )));
651657}
652658}
@@ -685,8 +691,8 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
685691if (FileSeek (v -> mdfd_vfd ,seekpos ,SEEK_SET )!= seekpos )
686692ereport (ERROR ,
687693(errcode_for_file_access (),
688- errmsg ("could not seek to block %uof relation %s : %m" ,
689- blocknum ,relpath ( reln -> smgr_rnode , forknum ))));
694+ errmsg ("could not seek to block %uin file \"%s\" : %m" ,
695+ blocknum ,FilePathName ( v -> mdfd_vfd ))));
690696
691697nbytes = FileWrite (v -> mdfd_vfd ,buffer ,BLCKSZ );
692698
@@ -702,14 +708,14 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
702708if (nbytes < 0 )
703709ereport (ERROR ,
704710(errcode_for_file_access (),
705- errmsg ("could not write block %uof relation %s : %m" ,
706- blocknum ,relpath ( reln -> smgr_rnode , forknum ))));
711+ errmsg ("could not write block %uin file \"%s\" : %m" ,
712+ blocknum ,FilePathName ( v -> mdfd_vfd ))));
707713/* short write: complain appropriately */
708714ereport (ERROR ,
709715(errcode (ERRCODE_DISK_FULL ),
710- errmsg ("could not write block %uof relation %s : wrote only %d of %d bytes" ,
716+ errmsg ("could not write block %uin file \"%s\" : wrote only %d of %d bytes" ,
711717blocknum ,
712- relpath ( reln -> smgr_rnode , forknum ),
718+ FilePathName ( v -> mdfd_vfd ),
713719nbytes ,BLCKSZ ),
714720errhint ("Check free disk space." )));
715721}
@@ -776,9 +782,8 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
776782if (v -> mdfd_chain == NULL )
777783ereport (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
784789v = v -> mdfd_chain ;
@@ -807,7 +812,7 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
807812if (InRecovery )
808813return ;
809814ereport (ERROR ,
810- (errmsg ("could not truncaterelation %s to %u blocks: it's only %u blocks now" ,
815+ (errmsg ("could not truncatefile \"%s\" to %u blocks: it's only %u blocks now" ,
811816relpath (reln -> smgr_rnode ,forknum ),
812817nblocks ,curnblk )));
813818}
@@ -831,9 +836,9 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
831836if (FileTruncate (v -> mdfd_vfd ,0 )< 0 )
832837ereport (ERROR ,
833838(errcode_for_file_access (),
834- errmsg ("could not truncaterelation %s to %u blocks : %m" ,
835- relpath ( reln -> smgr_rnode , forknum ),
836- nblocks )));
839+ errmsg ("could not truncatefile \"%s\" : %m" ,
840+ FilePathName ( v -> mdfd_vfd ))));
841+
837842if (!isTemp )
838843register_dirty_segment (reln ,forknum ,v );
839844v = v -> mdfd_chain ;
@@ -856,8 +861,8 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
856861if (FileTruncate (v -> mdfd_vfd , (off_t )lastsegblocks * BLCKSZ )< 0 )
857862ereport (ERROR ,
858863(errcode_for_file_access (),
859- errmsg ("could not truncaterelation %s to %u blocks: %m" ,
860- relpath ( reln -> smgr_rnode , forknum ),
864+ errmsg ("could not truncatefile \"%s\" to %u blocks: %m" ,
865+ FilePathName ( v -> mdfd_vfd ),
861866nblocks )));
862867if (!isTemp )
863868register_dirty_segment (reln ,forknum ,v );
@@ -901,9 +906,8 @@ mdimmedsync(SMgrRelation reln, ForkNumber forknum)
901906if (FileSync (v -> mdfd_vfd )< 0 )
902907ereport (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 ))));
907911v = 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 );
10741079if (!FILE_POSSIBLY_DELETED (errno )||
10751080failures > 0 )
10761081ereport (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 )));
10801084else
10811085ereport (DEBUG1 ,
10821086(errcode_for_file_access (),
1083- errmsg ("could not fsyncsegment %u of relation %s but retrying: %m" ,
1084- entry -> tag . segno , path )));
1087+ errmsg ("could not fsyncfile \"%s\" but retrying: %m" ,
1088+ path )));
10851089pfree (path );
10861090
10871091/*
@@ -1185,7 +1189,7 @@ mdpostckpt(void)
11851189if (errno != ENOENT )
11861190ereport (WARNING ,
11871191(errcode_for_file_access (),
1188- errmsg ("could not removerelation %s : %m" ,path )));
1192+ errmsg ("could not removefile \"%s\" : %m" ,path )));
11891193}
11901194pfree (path );
11911195
@@ -1219,9 +1223,8 @@ register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
12191223if (FileSync (seg -> mdfd_vfd )< 0 )
12201224ereport (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 thefilename 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
14711471path = relpath (reln -> smgr_rnode ,forknum );
14721472
@@ -1480,6 +1480,23 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
14801480else
14811481fullpath = 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 */
14841501fd = PathNameOpenFile (fullpath ,O_RDWR |PG_BINARY |oflags ,0600 );
14851502
@@ -1566,9 +1583,8 @@ _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
15661583return NULL ;
15671584ereport (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 ),
15721588blkno )));
15731589}
15741590}
@@ -1589,8 +1605,8 @@ _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
15891605if (len < 0 )
15901606ereport (ERROR ,
15911607(errcode_for_file_access (),
1592- errmsg ("could not seek to end ofsegment %u of relation %s : %m" ,
1593- seg -> mdfd_segno , relpath ( reln -> smgr_rnode , forknum ))));
1608+ errmsg ("could not seek to end offile \"%s\" : %m" ,
1609+ FilePathName ( seg -> mdfd_vfd ))));
15941610/* note that this calculation will ignore any partial block at EOF */
15951611return (BlockNumber ) (len /BLCKSZ );
15961612}