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.10 2001/11/05 17:46:23 momjian Exp $
26+ * $Header: /cvsroot/pgsql/contrib/pg_resetxlog/Attic/pg_resetxlog.c,v 1.11 2002/01/10 17:51:52 momjian Exp $
2727 *
2828 *-------------------------------------------------------------------------
2929 */
@@ -709,34 +709,39 @@ PrintControlValues(void)
709709 * Write out the new pg_control file.
710710 */
711711static void
712- RewriteControlFile (void )
712+ RewriteControlFile (TransactionId set_xid )
713713{
714714int fd ;
715715char buffer [BLCKSZ ];/* need not be aligned */
716716
717- /*
718- * Adjust fields as needed to force an empty XLOG starting at the next
719- * available segment.
720- */
721- newXlogId = ControlFile .logId ;
722- newXlogSeg = ControlFile .logSeg ;
723- /* be sure we wrap around correctly at end of a logfile */
724- NextLogSeg (newXlogId ,newXlogSeg );
725-
726- ControlFile .checkPointCopy .redo .xlogid = newXlogId ;
727- ControlFile .checkPointCopy .redo .xrecoff =
728- newXlogSeg * XLogSegSize + SizeOfXLogPHD ;
729- ControlFile .checkPointCopy .undo = ControlFile .checkPointCopy .redo ;
730- ControlFile .checkPointCopy .time = time (NULL );
731-
732- ControlFile .state = DB_SHUTDOWNED ;
733- ControlFile .time = time (NULL );
734- ControlFile .logId = newXlogId ;
735- ControlFile .logSeg = newXlogSeg + 1 ;
736- ControlFile .checkPoint = ControlFile .checkPointCopy .redo ;
737- ControlFile .prevCheckPoint .xlogid = 0 ;
738- ControlFile .prevCheckPoint .xrecoff = 0 ;
739-
717+ if (set_xid == 0 )
718+ {
719+ /*
720+ * Adjust fields as needed to force an empty XLOG starting at the next
721+ * available segment.
722+ */
723+ newXlogId = ControlFile .logId ;
724+ newXlogSeg = ControlFile .logSeg ;
725+ /* be sure we wrap around correctly at end of a logfile */
726+ NextLogSeg (newXlogId ,newXlogSeg );
727+
728+ ControlFile .checkPointCopy .redo .xlogid = newXlogId ;
729+ ControlFile .checkPointCopy .redo .xrecoff =
730+ newXlogSeg * XLogSegSize + SizeOfXLogPHD ;
731+ ControlFile .checkPointCopy .undo = ControlFile .checkPointCopy .redo ;
732+ ControlFile .checkPointCopy .time = time (NULL );
733+
734+ ControlFile .state = DB_SHUTDOWNED ;
735+ ControlFile .time = time (NULL );
736+ ControlFile .logId = newXlogId ;
737+ ControlFile .logSeg = newXlogSeg + 1 ;
738+ ControlFile .checkPoint = ControlFile .checkPointCopy .redo ;
739+ ControlFile .prevCheckPoint .xlogid = 0 ;
740+ ControlFile .prevCheckPoint .xrecoff = 0 ;
741+ }
742+ else
743+ ControlFile .checkPointCopy .nextXid = set_xid ;
744+
740745/* Contents are protected with a CRC */
741746INIT_CRC64 (ControlFile .crc );
742747COMP_CRC64 (ControlFile .crc ,
@@ -926,9 +931,10 @@ WriteEmptyXLOG(void)
926931static void
927932usage (void )
928933{
929- fprintf (stderr ,"Usage: pg_resetxlog [-f] [-n] PGDataDirectory\n\n"
930- " -f\tforce update to be done\n"
931- " -n\tno update, just show extracted pg_control values (for testing)\n" );
934+ fprintf (stderr ,"Usage: pg_resetxlog [-f] [-n] [-x xid] PGDataDirectory\n"
935+ " -f\tforce update to be done\n"
936+ " -n\tno update, just show extracted pg_control values (for testing)\n"
937+ " -x XID\tset XID in pg_control\n" );
932938exit (1 );
933939}
934940
@@ -939,6 +945,7 @@ main(int argc, char **argv)
939945int argn ;
940946bool force = false;
941947bool noupdate = false;
948+ TransactionId set_xid = 0 ;
942949int fd ;
943950char path [MAXPGPATH ];
944951
@@ -950,6 +957,18 @@ main(int argc, char **argv)
950957force = true;
951958else if (strcmp (argv [argn ],"-n" )== 0 )
952959noupdate = true;
960+ else if (strcmp (argv [argn ],"-x" )== 0 )
961+ {
962+ argn ++ ;
963+ if (argn == argc )
964+ usage ();
965+ set_xid = strtoul (argv [argn ],NULL ,0 );
966+ if (set_xid == 0 )
967+ {
968+ fprintf (stderr ,"XID can not be 0." );
969+ exit (1 );
970+ }
971+ }
953972else
954973usage ();
955974}
@@ -992,6 +1011,20 @@ main(int argc, char **argv)
9921011if (!ReadControlFile ())
9931012GuessControlValues ();
9941013
1014+ /*
1015+ * Set XID in pg_control and exit
1016+ */
1017+ if (set_xid )
1018+ {
1019+ if (guessed )
1020+ {
1021+ printf ("\npg_control appears corrupt. Can not update XID.\n" );
1022+ exit (1 );
1023+ }
1024+ RewriteControlFile (set_xid );
1025+ exit (0 );
1026+ }
1027+
9951028/*
9961029 * If we had to guess anything, and -f was not given, just print the
9971030 * guessed values and exit. Also print if -n is given.
@@ -1018,7 +1051,7 @@ main(int argc, char **argv)
10181051/*
10191052 * Else, do the dirty deed.
10201053 */
1021- RewriteControlFile ();
1054+ RewriteControlFile (0 );
10221055KillExistingXLOG ();
10231056WriteEmptyXLOG ();
10241057