2323 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
2424 * Portions Copyright (c) 1994, Regents of the University of California
2525 *
26- * $Header: /cvsroot/pgsql/contrib/pg_resetxlog/Attic/pg_resetxlog.c,v 1.16 2002/01/1106:33:01 momjian Exp $
26+ * $Header: /cvsroot/pgsql/contrib/pg_resetxlog/Attic/pg_resetxlog.c,v 1.17 2002/01/1121:27:13 tgl Exp $
2727 *
2828 *-------------------------------------------------------------------------
2929 */
6363snprintf(path, MAXPGPATH, "%s/%08X%08X",\
6464 XLogDir, log, seg)
6565
66-
6766/******************** end of stuff copied from xlog.c ********************/
6867
6968
@@ -134,7 +133,7 @@ ReadControlFile(void)
134133return true;
135134}
136135
137- fprintf (stderr ,"pg_control exists but has invalid CRC;proceeding with caution.\n" );
136+ fprintf (stderr ,"pg_control exists but has invalid CRC;proceed with caution.\n" );
138137/* We will use the data anyway, but treat it as guessed. */
139138memcpy (& ControlFile ,buffer ,sizeof (ControlFile ));
140139guessed = true;
@@ -218,12 +217,11 @@ GuessControlValues(void)
218217static void
219218PrintControlValues (bool guessed )
220219{
221- printf ("\n %spg_control values:\n\n"
220+ printf ("%spg_control values:\n\n"
222221"pg_control version number: %u\n"
223222"Catalog version number: %u\n"
224223"Current log file id: %u\n"
225224"Next log file segment: %u\n"
226- "Latest checkpoint location: %X/%X\n"
227225"Latest checkpoint's StartUpID: %u\n"
228226"Latest checkpoint's NextXID: %u\n"
229227"Latest checkpoint's NextOID: %u\n"
@@ -237,8 +235,6 @@ PrintControlValues(bool guessed)
237235ControlFile .catalog_version_no ,
238236ControlFile .logId ,
239237ControlFile .logSeg ,
240- ControlFile .checkPoint .xlogid ,
241- ControlFile .checkPoint .xrecoff ,
242238ControlFile .checkPointCopy .ThisStartUpID ,
243239ControlFile .checkPointCopy .nextXid ,
244240ControlFile .checkPointCopy .nextOid ,
@@ -253,7 +249,7 @@ PrintControlValues(bool guessed)
253249 * Write out the new pg_control file.
254250 */
255251static void
256- RewriteControlFile (TransactionId set_xid , XLogRecPtr set_checkpoint )
252+ RewriteControlFile (void )
257253{
258254int fd ;
259255char buffer [BLCKSZ ];/* need not be aligned */
@@ -277,18 +273,10 @@ RewriteControlFile(TransactionId set_xid, XLogRecPtr set_checkpoint)
277273ControlFile .time = time (NULL );
278274ControlFile .logId = newXlogId ;
279275ControlFile .logSeg = newXlogSeg + 1 ;
276+ ControlFile .checkPoint = ControlFile .checkPointCopy .redo ;
280277ControlFile .prevCheckPoint .xlogid = 0 ;
281278ControlFile .prevCheckPoint .xrecoff = 0 ;
282279
283- if (set_xid != 0 )
284- ControlFile .checkPointCopy .nextXid = set_xid ;
285-
286- if (set_checkpoint .xlogid == 0 &&
287- set_checkpoint .xrecoff == 0 )
288- ControlFile .checkPoint = ControlFile .checkPointCopy .redo ;
289- else
290- ControlFile .checkPoint = set_checkpoint ;
291-
292280/* Contents are protected with a CRC */
293281INIT_CRC64 (ControlFile .crc );
294282COMP_CRC64 (ControlFile .crc ,
@@ -478,11 +466,11 @@ WriteEmptyXLOG(void)
478466static void
479467usage (void )
480468{
481- fprintf (stderr ,"Usage: pg_resetxlog [-f] [-n] [-x xid] [ -llog_id offset ] PGDataDirectory\n"
482- " -f\t force update to be done\n"
483- " -n\t no update, just show extracted pg_control values (for testing)\n"
484- " -xXID set XID in pg_control \n"
485- " -llog_id offset set hex checkpoint locationin pg_control \n" );
469+ fprintf (stderr ,"Usage: pg_resetxlog [-f] [-n] [-x xid] [ -lfileid seg ] PGDataDirectory\n"
470+ " -f\t\tforce update to be done\n"
471+ " -n\t\tno update, just show extracted pg_control values (for testing)\n"
472+ " -xxid\tset next transaction ID \n"
473+ " -lfileid seg\tforce minimum WAL starting locationfor new xlog \n" );
486474exit (1 );
487475}
488476
@@ -494,7 +482,8 @@ main(int argc, char **argv)
494482bool force = false;
495483bool noupdate = false;
496484TransactionId set_xid = 0 ;
497- XLogRecPtr set_checkpoint = {0 ,0 };
485+ uint32 minXlogId = 0 ,
486+ minXlogSeg = 0 ;
498487int fd ;
499488char path [MAXPGPATH ];
500489
@@ -514,7 +503,7 @@ main(int argc, char **argv)
514503set_xid = strtoul (argv [argn ],NULL ,0 );
515504if (set_xid == 0 )
516505{
517- fprintf (stderr ,"XID can not be 0." );
506+ fprintf (stderr ,"XID can not be 0.\n " );
518507exit (1 );
519508}
520509}
@@ -523,17 +512,11 @@ main(int argc, char **argv)
523512argn ++ ;
524513if (argn == argc )
525514usage ();
526- set_checkpoint . xlogid = strtoul (argv [argn ],NULL ,16 );
515+ minXlogId = strtoul (argv [argn ],NULL ,0 );
527516argn ++ ;
528517if (argn == argc )
529518usage ();
530- set_checkpoint .xrecoff = strtoul (argv [argn ],NULL ,16 );
531- if (set_checkpoint .xlogid == 0 &&
532- set_checkpoint .xrecoff == 0 )
533- {
534- fprintf (stderr ,"Checkpoint can not be '0 0'." );
535- exit (1 );
536- }
519+ minXlogSeg = strtoul (argv [argn ],NULL ,0 );
537520}
538521else
539522usage ();
@@ -606,8 +589,20 @@ main(int argc, char **argv)
606589
607590/*
608591 * Else, do the dirty deed.
592+ *
593+ * First adjust fields if required by switches.
609594 */
610- RewriteControlFile (set_xid ,set_checkpoint );
595+ if (set_xid != 0 )
596+ ControlFile .checkPointCopy .nextXid = set_xid ;
597+
598+ if (minXlogId > ControlFile .logId ||
599+ (minXlogId == ControlFile .logId && minXlogSeg > ControlFile .logSeg ))
600+ {
601+ ControlFile .logId = minXlogId ;
602+ ControlFile .logSeg = minXlogSeg ;
603+ }
604+
605+ RewriteControlFile ();
611606KillExistingXLOG ();
612607WriteEmptyXLOG ();
613608