On Thu, Feb 25, 2010 at 11:57 AM, Fujii Masao <masao.fu...@gmail.com> wrote:
> On Tue, Feb 23, 2010 at 4:08 PM, Heikki Linnakangas
> <heikki.linnakan...@enterprisedb.com> wrote:
>>> So it's not the TLI of the first record in the file, isn't it?
>>
>> Hmm, or is it the TLI of the last record? Not sure. Anyway, if there's a
>> TLI switch in the current WAL file, curFileTLI doesn't always represent
>> the TLI of the current record.
>
> Hmm. How about using lastPageTLI instead of curFileTLI? lastPageTLI
> would always represent the TLI of the current record.

I attached the revised patch which uses lastPageTLI instead of curFileTLI
as the timeline of the last applied record.

Regards,

-- 
Fujii Masao
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center
*** a/doc/src/sgml/func.sgml
--- b/doc/src/sgml/func.sgml
***************
*** 13199,13204 **** postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
--- 13199,13208 ----
      This is usually the desired behavior for managing transaction log archiving
      behavior, since the preceding file is the last one that currently
      needs to be archived.
+     These functions also accept as a parameter the string that consists of timeline and
+     location, separated by a slash. In this case a transaction log file name is computed
+     by using the given timeline. On the other hand, if timeline is not supplied, the
+     current timeline is used for the computation.
     </para>
  
     <para>
***************
*** 13245,13257 **** postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
          <literal><function>pg_last_xlog_receive_location</function>()</literal>
          </entry>
         <entry><type>text</type></entry>
!        <entry>Get last transaction log location received and synced to disk during
!         streaming recovery. If streaming recovery is still in progress
          this will increase monotonically. If streaming recovery has completed
          then this value will remain static at the value of the last WAL record
          received and synced to disk during that recovery. When the server has
          been started without a streaming recovery then the return value will be
!         InvalidXLogRecPtr (0/0).
         </entry>
        </row>
        <row>
--- 13249,13263 ----
          <literal><function>pg_last_xlog_receive_location</function>()</literal>
          </entry>
         <entry><type>text</type></entry>
!        <entry>Get timeline and location of last transaction log received and synced
!         to disk during streaming recovery. The return string is separated by a slash,
!         the first value indicates the timeline and the other the location.
!         If streaming recovery is still in progress
          this will increase monotonically. If streaming recovery has completed
          then this value will remain static at the value of the last WAL record
          received and synced to disk during that recovery. When the server has
          been started without a streaming recovery then the return value will be
!         <literal>0/0/0</>.
         </entry>
        </row>
        <row>
***************
*** 13259,13270 **** postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
          <literal><function>pg_last_xlog_replay_location</function>()</literal>
          </entry>
         <entry><type>text</type></entry>
!        <entry>Get last transaction log location replayed during recovery.
          If recovery is still in progress this will increase monotonically.
          If recovery has completed then this value will remain static at
          the value of the last WAL record applied during that recovery.
          When the server has been started normally without a recovery
!         then the return value will be InvalidXLogRecPtr (0/0).
         </entry>
        </row>
       </tbody>
--- 13265,13278 ----
          <literal><function>pg_last_xlog_replay_location</function>()</literal>
          </entry>
         <entry><type>text</type></entry>
!        <entry>Get timeline and location of last transaction log replayed during
!         recovery. The return string is separated by a slash, the first value
!         indicates the timeline and the other the location.
          If recovery is still in progress this will increase monotonically.
          If recovery has completed then this value will remain static at
          the value of the last WAL record applied during that recovery.
          When the server has been started normally without a recovery
!         then the return value will be <literal>0/0/0</>.
         </entry>
        </row>
       </tbody>
*** a/src/backend/access/transam/xlog.c
--- b/src/backend/access/transam/xlog.c
***************
*** 392,397 **** typedef struct XLogCtlData
--- 392,399 ----
  	TimestampTz	recoveryLastXTime;
  	/* end+1 of the last record replayed */
  	XLogRecPtr	recoveryLastRecPtr;
+ 	/* tli of last record replayed */
+ 	TimeLineID	recoveryLastTLI;
  
  	slock_t		info_lck;		/* locks shared variables shown above */
  } XLogCtlData;
***************
*** 5776,5785 **** StartupXLOG(void)
  			/* use volatile pointer to prevent code rearrangement */
  			volatile XLogCtlData *xlogctl = XLogCtl;
  
! 			/* initialize shared replayEndRecPtr and recoveryLastRecPtr */
  			SpinLockAcquire(&xlogctl->info_lck);
  			xlogctl->replayEndRecPtr = ReadRecPtr;
  			xlogctl->recoveryLastRecPtr = ReadRecPtr;
  			SpinLockRelease(&xlogctl->info_lck);
  
  			InRedo = true;
--- 5778,5794 ----
  			/* use volatile pointer to prevent code rearrangement */
  			volatile XLogCtlData *xlogctl = XLogCtl;
  
! 			/*
! 			 * initialize shared replayEndRecPtr, recoveryLastRecPtr and
! 			 * recoveryLastTLI. Actually, the latter two variables don't need to
! 			 * be initialized here since they are expected to be updated at least
! 			 * once until read only connections will have read them. But just in
! 			 * case.
! 			 */
  			SpinLockAcquire(&xlogctl->info_lck);
  			xlogctl->replayEndRecPtr = ReadRecPtr;
  			xlogctl->recoveryLastRecPtr = ReadRecPtr;
+ 			xlogctl->recoveryLastTLI = lastPageTLI;
  			SpinLockRelease(&xlogctl->info_lck);
  
  			InRedo = true;
***************
*** 5907,5917 **** StartupXLOG(void)
  				error_context_stack = errcontext.previous;
  
  				/*
! 				 * Update shared recoveryLastRecPtr after this record has been
! 				 * replayed.
  				 */
  				SpinLockAcquire(&xlogctl->info_lck);
  				xlogctl->recoveryLastRecPtr = EndRecPtr;
  				SpinLockRelease(&xlogctl->info_lck);
  
  				LastRec = ReadRecPtr;
--- 5916,5927 ----
  				error_context_stack = errcontext.previous;
  
  				/*
! 				 * Update shared recoveryLastRecPtr and recoveryLastTLI
! 				 * after this record has been replayed.
  				 */
  				SpinLockAcquire(&xlogctl->info_lck);
  				xlogctl->recoveryLastRecPtr = EndRecPtr;
+ 				xlogctl->recoveryLastTLI = lastPageTLI;
  				SpinLockRelease(&xlogctl->info_lck);
  
  				LastRec = ReadRecPtr;
***************
*** 8269,8275 **** pg_current_xlog_insert_location(PG_FUNCTION_ARGS)
  }
  
  /*
!  * Report the last WAL receive location (same format as pg_start_backup etc)
   *
   * This is useful for determining how much of WAL is guaranteed to be received
   * and synced to disk by walreceiver.
--- 8279,8285 ----
  }
  
  /*
!  * Report the last WAL receive tli and location
   *
   * This is useful for determining how much of WAL is guaranteed to be received
   * and synced to disk by walreceiver.
***************
*** 8282,8294 **** pg_last_xlog_receive_location(PG_FUNCTION_ARGS)
  
  	recptr = GetWalRcvWriteRecPtr();
  
! 	snprintf(location, sizeof(location), "%X/%X",
  			 recptr.xlogid, recptr.xrecoff);
  	PG_RETURN_TEXT_P(cstring_to_text(location));
  }
  
  /*
!  * Report the last WAL replay location (same format as pg_start_backup etc)
   *
   * This is useful for determining how much of WAL is visible to read-only
   * connections during recovery.
--- 8292,8305 ----
  
  	recptr = GetWalRcvWriteRecPtr();
  
! 	snprintf(location, sizeof(location), "%X/%X/%X",
! 			 XLogRecPtrIsInvalid(recptr) ? 0 : GetRecoveryTargetTLI(),
  			 recptr.xlogid, recptr.xrecoff);
  	PG_RETURN_TEXT_P(cstring_to_text(location));
  }
  
  /*
!  * Report the last WAL replay tli and location
   *
   * This is useful for determining how much of WAL is visible to read-only
   * connections during recovery.
***************
*** 8298,8312 **** pg_last_xlog_replay_location(PG_FUNCTION_ARGS)
  {
  	/* use volatile pointer to prevent code rearrangement */
  	volatile XLogCtlData *xlogctl = XLogCtl;
  	XLogRecPtr	recptr;
  	char		location[MAXFNAMELEN];
  
  	SpinLockAcquire(&xlogctl->info_lck);
  	recptr = xlogctl->recoveryLastRecPtr;
  	SpinLockRelease(&xlogctl->info_lck);
  
! 	snprintf(location, sizeof(location), "%X/%X",
! 			 recptr.xlogid, recptr.xrecoff);
  	PG_RETURN_TEXT_P(cstring_to_text(location));
  }
  
--- 8309,8325 ----
  {
  	/* use volatile pointer to prevent code rearrangement */
  	volatile XLogCtlData *xlogctl = XLogCtl;
+ 	TimeLineID	tli;
  	XLogRecPtr	recptr;
  	char		location[MAXFNAMELEN];
  
  	SpinLockAcquire(&xlogctl->info_lck);
+ 	tli = xlogctl->recoveryLastTLI;
  	recptr = xlogctl->recoveryLastRecPtr;
  	SpinLockRelease(&xlogctl->info_lck);
  
! 	snprintf(location, sizeof(location), "%X/%X/%X",
! 			 tli, recptr.xlogid, recptr.xrecoff);
  	PG_RETURN_TEXT_P(cstring_to_text(location));
  }
  
***************
*** 8314,8319 **** pg_last_xlog_replay_location(PG_FUNCTION_ARGS)
--- 8327,8336 ----
   * Compute an xlog file name and decimal byte offset given a WAL location,
   * such as is returned by pg_stop_backup() or pg_xlog_switch().
   *
+  * Also use the tli for the computation if it's given with a location,
+  * such as is returned by pg_last_xlog_receive_location() or
+  * pg_last_xlog_replay_location().
+  *
   * Note that a location exactly at a segment boundary is taken to be in
   * the previous segment.  This is usually the right thing, since the
   * expected usage is to determine which xlog file(s) are ready to archive.
***************
*** 8323,8333 **** pg_xlogfile_name_offset(PG_FUNCTION_ARGS)
--- 8340,8352 ----
  {
  	text	   *location = PG_GETARG_TEXT_P(0);
  	char	   *locationstr;
+ 	unsigned int utli;
  	unsigned int uxlogid;
  	unsigned int uxrecoff;
  	uint32		xlogid;
  	uint32		xlogseg;
  	uint32		xrecoff;
+ 	TimeLineID	tli = ThisTimeLineID;
  	XLogRecPtr	locationpoint;
  	char		xlogfilename[MAXFNAMELEN];
  	Datum		values[2];
***************
*** 8341,8347 **** pg_xlogfile_name_offset(PG_FUNCTION_ARGS)
  	 */
  	locationstr = text_to_cstring(location);
  
! 	if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
  				 errmsg("could not parse transaction log location \"%s\"",
--- 8360,8368 ----
  	 */
  	locationstr = text_to_cstring(location);
  
! 	if (sscanf(locationstr, "%X/%X/%X", &utli, &uxlogid, &uxrecoff) == 3)
! 		tli = (TimeLineID) utli;
! 	else if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
  				 errmsg("could not parse transaction log location \"%s\"",
***************
*** 8366,8372 **** pg_xlogfile_name_offset(PG_FUNCTION_ARGS)
  	 * xlogfilename
  	 */
  	XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
! 	XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);
  
  	values[0] = CStringGetTextDatum(xlogfilename);
  	isnull[0] = false;
--- 8387,8393 ----
  	 * xlogfilename
  	 */
  	XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
! 	XLogFileName(xlogfilename, tli, xlogid, xlogseg);
  
  	values[0] = CStringGetTextDatum(xlogfilename);
  	isnull[0] = false;
***************
*** 8392,8413 **** pg_xlogfile_name_offset(PG_FUNCTION_ARGS)
  /*
   * Compute an xlog file name given a WAL location,
   * such as is returned by pg_stop_backup() or pg_xlog_switch().
   */
  Datum
  pg_xlogfile_name(PG_FUNCTION_ARGS)
  {
  	text	   *location = PG_GETARG_TEXT_P(0);
  	char	   *locationstr;
  	unsigned int uxlogid;
  	unsigned int uxrecoff;
  	uint32		xlogid;
  	uint32		xlogseg;
  	XLogRecPtr	locationpoint;
  	char		xlogfilename[MAXFNAMELEN];
  
  	locationstr = text_to_cstring(location);
  
! 	if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
  				 errmsg("could not parse transaction log location \"%s\"",
--- 8413,8442 ----
  /*
   * Compute an xlog file name given a WAL location,
   * such as is returned by pg_stop_backup() or pg_xlog_switch().
+  *
+  * Also use the tli for the computation if it's given with a location,
+  * such as is returned by pg_last_xlog_receive_location() or
+  * pg_last_xlog_replay_location().
   */
  Datum
  pg_xlogfile_name(PG_FUNCTION_ARGS)
  {
  	text	   *location = PG_GETARG_TEXT_P(0);
  	char	   *locationstr;
+ 	unsigned int utli;
  	unsigned int uxlogid;
  	unsigned int uxrecoff;
  	uint32		xlogid;
  	uint32		xlogseg;
+ 	TimeLineID	tli = ThisTimeLineID;
  	XLogRecPtr	locationpoint;
  	char		xlogfilename[MAXFNAMELEN];
  
  	locationstr = text_to_cstring(location);
  
! 	if (sscanf(locationstr, "%X/%X/%X", &utli, &uxlogid, &uxrecoff) == 3)
! 		tli = (TimeLineID) utli;
! 	else if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
  				 errmsg("could not parse transaction log location \"%s\"",
***************
*** 8417,8423 **** pg_xlogfile_name(PG_FUNCTION_ARGS)
  	locationpoint.xrecoff = uxrecoff;
  
  	XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
! 	XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);
  
  	PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
  }
--- 8446,8452 ----
  	locationpoint.xrecoff = uxrecoff;
  
  	XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
! 	XLogFileName(xlogfilename, tli, xlogid, xlogseg);
  
  	PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
  }
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to