23
23
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
24
24
* Portions Copyright (c) 1994, Regents of the University of California
25
25
*
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 $
27
27
*
28
28
*-------------------------------------------------------------------------
29
29
*/
@@ -709,34 +709,39 @@ PrintControlValues(void)
709
709
* Write out the new pg_control file.
710
710
*/
711
711
static void
712
- RewriteControlFile (void )
712
+ RewriteControlFile (TransactionId set_xid )
713
713
{
714
714
int fd ;
715
715
char buffer [BLCKSZ ];/* need not be aligned */
716
716
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
+
740
745
/* Contents are protected with a CRC */
741
746
INIT_CRC64 (ControlFile .crc );
742
747
COMP_CRC64 (ControlFile .crc ,
@@ -926,9 +931,10 @@ WriteEmptyXLOG(void)
926
931
static void
927
932
usage (void )
928
933
{
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" );
932
938
exit (1 );
933
939
}
934
940
@@ -939,6 +945,7 @@ main(int argc, char **argv)
939
945
int argn ;
940
946
bool force = false;
941
947
bool noupdate = false;
948
+ TransactionId set_xid = 0 ;
942
949
int fd ;
943
950
char path [MAXPGPATH ];
944
951
@@ -950,6 +957,18 @@ main(int argc, char **argv)
950
957
force = true;
951
958
else if (strcmp (argv [argn ],"-n" )== 0 )
952
959
noupdate = 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
+ }
953
972
else
954
973
usage ();
955
974
}
@@ -992,6 +1011,20 @@ main(int argc, char **argv)
992
1011
if (!ReadControlFile ())
993
1012
GuessControlValues ();
994
1013
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
+
995
1028
/*
996
1029
* If we had to guess anything, and -f was not given, just print the
997
1030
* guessed values and exit. Also print if -n is given.
@@ -1018,7 +1051,7 @@ main(int argc, char **argv)
1018
1051
/*
1019
1052
* Else, do the dirty deed.
1020
1053
*/
1021
- RewriteControlFile ();
1054
+ RewriteControlFile (0 );
1022
1055
KillExistingXLOG ();
1023
1056
WriteEmptyXLOG ();
1024
1057