77 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88 * Portions Copyright (c) 1994, Regents of the University of California
99 *
10- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.185 2005/04/1518:48:10 tgl Exp $
10+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.186 2005/04/1522:19:48 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -1152,6 +1152,9 @@ XLogWrite(XLogwrtRqst WriteRqst)
11521152bool ispartialpage ;
11531153bool use_existent ;
11541154
1155+ /* We should always be inside a critical section here */
1156+ Assert (CritSectionCount > 0 );
1157+
11551158/*
11561159 * Update local LogwrtResult (caller probably did this already,
11571160 * but...)
@@ -1501,6 +1504,11 @@ XLogFlush(XLogRecPtr record)
15011504 * caller must *not* hold the lock at call.
15021505 *
15031506 * Returns FD of opened file.
1507+ *
1508+ * Note: errors here are ERROR not PANIC because we might or might not be
1509+ * inside a critical section (eg, during checkpoint there is no reason to
1510+ * take down the system on failure). They will promote to PANIC if we are
1511+ * in a critical section.
15041512 */
15051513static int
15061514XLogFileInit (uint32 log ,uint32 seg ,
@@ -1528,7 +1536,7 @@ XLogFileInit(uint32 log, uint32 seg,
15281536if (fd < 0 )
15291537{
15301538if (errno != ENOENT )
1531- ereport (PANIC ,
1539+ ereport (ERROR ,
15321540(errcode_for_file_access (),
15331541errmsg ("could not open file \"%s\" (log file %u, segment %u): %m" ,
15341542path ,log ,seg )));
@@ -1551,7 +1559,7 @@ XLogFileInit(uint32 log, uint32 seg,
15511559fd = BasicOpenFile (tmppath ,O_RDWR |O_CREAT |O_EXCL |PG_BINARY ,
15521560S_IRUSR |S_IWUSR );
15531561if (fd < 0 )
1554- ereport (PANIC ,
1562+ ereport (ERROR ,
15551563(errcode_for_file_access (),
15561564errmsg ("could not create file \"%s\": %m" ,tmppath )));
15571565
@@ -1580,19 +1588,19 @@ XLogFileInit(uint32 log, uint32 seg,
15801588/* if write didn't set errno, assume problem is no disk space */
15811589errno = save_errno ?save_errno :ENOSPC ;
15821590
1583- ereport (PANIC ,
1591+ ereport (ERROR ,
15841592(errcode_for_file_access (),
15851593errmsg ("could not write to file \"%s\": %m" ,tmppath )));
15861594}
15871595}
15881596
15891597if (pg_fsync (fd )!= 0 )
1590- ereport (PANIC ,
1598+ ereport (ERROR ,
15911599(errcode_for_file_access (),
15921600errmsg ("could not fsync file \"%s\": %m" ,tmppath )));
15931601
15941602if (close (fd ))
1595- ereport (PANIC ,
1603+ ereport (ERROR ,
15961604(errcode_for_file_access (),
15971605errmsg ("could not close file \"%s\": %m" ,tmppath )));
15981606
@@ -1622,7 +1630,7 @@ XLogFileInit(uint32 log, uint32 seg,
16221630fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |XLOG_SYNC_BIT ,
16231631S_IRUSR |S_IWUSR );
16241632if (fd < 0 )
1625- ereport (PANIC ,
1633+ ereport (ERROR ,
16261634(errcode_for_file_access (),
16271635errmsg ("could not open file \"%s\" (log file %u, segment %u): %m" ,
16281636path ,log ,seg )));
@@ -1659,7 +1667,7 @@ XLogFileCopy(uint32 log, uint32 seg,
16591667XLogFilePath (path ,srcTLI ,srclog ,srcseg );
16601668srcfd = BasicOpenFile (path ,O_RDONLY |PG_BINARY ,0 );
16611669if (srcfd < 0 )
1662- ereport (PANIC ,
1670+ ereport (ERROR ,
16631671(errcode_for_file_access (),
16641672errmsg ("could not open file \"%s\": %m" ,path )));
16651673
@@ -1674,7 +1682,7 @@ XLogFileCopy(uint32 log, uint32 seg,
16741682fd = BasicOpenFile (tmppath ,O_RDWR |O_CREAT |O_EXCL |PG_BINARY ,
16751683S_IRUSR |S_IWUSR );
16761684if (fd < 0 )
1677- ereport (PANIC ,
1685+ ereport (ERROR ,
16781686(errcode_for_file_access (),
16791687errmsg ("could not create file \"%s\": %m" ,tmppath )));
16801688
@@ -1687,11 +1695,11 @@ XLogFileCopy(uint32 log, uint32 seg,
16871695if ((int )read (srcfd ,buffer ,sizeof (buffer ))!= (int )sizeof (buffer ))
16881696{
16891697if (errno != 0 )
1690- ereport (PANIC ,
1698+ ereport (ERROR ,
16911699(errcode_for_file_access (),
16921700errmsg ("could not read file \"%s\": %m" ,path )));
16931701else
1694- ereport (PANIC ,
1702+ ereport (ERROR ,
16951703 (errmsg ("not enough data in file \"%s\"" ,path )));
16961704}
16971705errno = 0 ;
@@ -1707,19 +1715,19 @@ XLogFileCopy(uint32 log, uint32 seg,
17071715/* if write didn't set errno, assume problem is no disk space */
17081716errno = save_errno ?save_errno :ENOSPC ;
17091717
1710- ereport (PANIC ,
1718+ ereport (ERROR ,
17111719(errcode_for_file_access (),
17121720errmsg ("could not write to file \"%s\": %m" ,tmppath )));
17131721}
17141722}
17151723
17161724if (pg_fsync (fd )!= 0 )
1717- ereport (PANIC ,
1725+ ereport (ERROR ,
17181726(errcode_for_file_access (),
17191727errmsg ("could not fsync file \"%s\": %m" ,tmppath )));
17201728
17211729if (close (fd ))
1722- ereport (PANIC ,
1730+ ereport (ERROR ,
17231731(errcode_for_file_access (),
17241732errmsg ("could not close file \"%s\": %m" ,tmppath )));
17251733
@@ -1729,7 +1737,7 @@ XLogFileCopy(uint32 log, uint32 seg,
17291737 * Now move the segment into place with its final name.
17301738 */
17311739if (!InstallXLogFileSegment (& log ,& seg ,tmppath , false,NULL , false))
1732- elog (PANIC ,"InstallXLogFileSegment should not have failed" );
1740+ elog (ERROR ,"InstallXLogFileSegment should not have failed" );
17331741}
17341742
17351743/*
@@ -1806,14 +1814,14 @@ InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
18061814 */
18071815#if HAVE_WORKING_LINK
18081816if (link (tmppath ,path )< 0 )
1809- ereport (PANIC ,
1817+ ereport (ERROR ,
18101818(errcode_for_file_access (),
18111819errmsg ("could not link file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m" ,
18121820tmppath ,path ,* log ,* seg )));
18131821unlink (tmppath );
18141822#else
18151823if (rename (tmppath ,path )< 0 )
1816- ereport (PANIC ,
1824+ ereport (ERROR ,
18171825(errcode_for_file_access (),
18181826errmsg ("could not rename file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m" ,
18191827tmppath ,path ,* log ,* seg )));
@@ -2205,6 +2213,12 @@ MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
22052213 (errmsg ("recycled transaction log file \"%s\"" ,
22062214xlde -> d_name )));
22072215(* nsegsrecycled )++ ;
2216+ /* Needn't recheck that slot on future iterations */
2217+ if (max_advance > 0 )
2218+ {
2219+ NextLogSeg (endlogId ,endlogSeg );
2220+ max_advance -- ;
2221+ }
22082222}
22092223else
22102224{
@@ -2957,7 +2971,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
29572971fd = BasicOpenFile (tmppath ,O_RDWR |O_CREAT |O_EXCL ,
29582972S_IRUSR |S_IWUSR );
29592973if (fd < 0 )
2960- ereport (PANIC ,
2974+ ereport (ERROR ,
29612975(errcode_for_file_access (),
29622976errmsg ("could not create file \"%s\": %m" ,tmppath )));
29632977
@@ -2976,7 +2990,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
29762990if (srcfd < 0 )
29772991{
29782992if (errno != ENOENT )
2979- ereport (FATAL ,
2993+ ereport (ERROR ,
29802994(errcode_for_file_access (),
29812995errmsg ("could not open file \"%s\": %m" ,path )));
29822996/* Not there, so assume parent has no parents */
@@ -2988,7 +3002,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
29883002errno = 0 ;
29893003nbytes = (int )read (srcfd ,buffer ,sizeof (buffer ));
29903004if (nbytes < 0 || errno != 0 )
2991- ereport (PANIC ,
3005+ ereport (ERROR ,
29923006(errcode_for_file_access (),
29933007errmsg ("could not read file \"%s\": %m" ,path )));
29943008if (nbytes == 0 )
@@ -3010,7 +3024,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
30103024 */
30113025errno = save_errno ?save_errno :ENOSPC ;
30123026
3013- ereport (PANIC ,
3027+ ereport (ERROR ,
30143028(errcode_for_file_access (),
30153029errmsg ("could not write to file \"%s\": %m" ,tmppath )));
30163030}
@@ -3048,18 +3062,18 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
30483062/* if write didn't set errno, assume problem is no disk space */
30493063errno = save_errno ?save_errno :ENOSPC ;
30503064
3051- ereport (PANIC ,
3065+ ereport (ERROR ,
30523066(errcode_for_file_access (),
30533067errmsg ("could not write to file \"%s\": %m" ,tmppath )));
30543068}
30553069
30563070if (pg_fsync (fd )!= 0 )
3057- ereport (PANIC ,
3071+ ereport (ERROR ,
30583072(errcode_for_file_access (),
30593073errmsg ("could not fsync file \"%s\": %m" ,tmppath )));
30603074
30613075if (close (fd ))
3062- ereport (PANIC ,
3076+ ereport (ERROR ,
30633077(errcode_for_file_access (),
30643078errmsg ("could not close file \"%s\": %m" ,tmppath )));
30653079
@@ -3076,14 +3090,14 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
30763090 */
30773091#if HAVE_WORKING_LINK
30783092if (link (tmppath ,path )< 0 )
3079- ereport (PANIC ,
3093+ ereport (ERROR ,
30803094(errcode_for_file_access (),
30813095errmsg ("could not link file \"%s\" to \"%s\": %m" ,
30823096tmppath ,path )));
30833097unlink (tmppath );
30843098#else
30853099if (rename (tmppath ,path )< 0 )
3086- ereport (PANIC ,
3100+ ereport (ERROR ,
30873101(errcode_for_file_access (),
30883102errmsg ("could not rename file \"%s\" to \"%s\": %m" ,
30893103tmppath ,path )));