mirror of
https://github.com/postgres/postgres.git
synced 2025-11-09 06:21:09 +03:00
Add pg_dissect_walfile_name()
This function takes in input a WAL segment name and returns a tuple made of the segment sequence number (dependent on the WAL segment size of the cluster) and its timeline, as of a thin SQL wrapper around the existing XLogFromFileName(). This function has multiple usages, like being able to compile a LSN from a file name and an offset, or finding the timeline of a segment without having to do to some maths based on the first eight characters of the segment. Bump catalog version. Author: Bharath Rupireddy Reviewed-by: Nathan Bossart, Kyotaro Horiguchi, Maxim Orlov, Michael Paquier Discussion: https://postgr.es/m/CALj2ACWV=FCddsxcGbVOA=cvPyMr75YCFbSQT6g4KDj=gcJK4g@mail.gmail.com
This commit is contained in:
@@ -432,6 +432,59 @@ pg_walfile_name(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract the sequence number and the timeline ID from given a WAL file
|
||||
* name.
|
||||
*/
|
||||
Datum
|
||||
pg_dissect_walfile_name(PG_FUNCTION_ARGS)
|
||||
{
|
||||
#define PG_DISSECT_WALFILE_NAME_COLS 2
|
||||
char *fname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
||||
char *fname_upper;
|
||||
char *p;
|
||||
TimeLineID tli;
|
||||
XLogSegNo segno;
|
||||
Datum values[PG_DISSECT_WALFILE_NAME_COLS] = {0};
|
||||
bool isnull[PG_DISSECT_WALFILE_NAME_COLS] = {0};
|
||||
TupleDesc tupdesc;
|
||||
HeapTuple tuple;
|
||||
char buf[256];
|
||||
Datum result;
|
||||
|
||||
fname_upper = pstrdup(fname);
|
||||
|
||||
/* Capitalize WAL file name. */
|
||||
for (p = fname_upper; *p; p++)
|
||||
*p = pg_toupper((unsigned char) *p);
|
||||
|
||||
if (!IsXLogFileName(fname_upper))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("invalid WAL file name \"%s\"", fname)));
|
||||
|
||||
XLogFromFileName(fname_upper, &tli, &segno, wal_segment_size);
|
||||
|
||||
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
||||
elog(ERROR, "return type must be a row type");
|
||||
|
||||
/* Convert to numeric. */
|
||||
snprintf(buf, sizeof buf, UINT64_FORMAT, segno);
|
||||
values[0] = DirectFunctionCall3(numeric_in,
|
||||
CStringGetDatum(buf),
|
||||
ObjectIdGetDatum(0),
|
||||
Int32GetDatum(-1));
|
||||
|
||||
values[1] = Int64GetDatum(tli);
|
||||
|
||||
tuple = heap_form_tuple(tupdesc, values, isnull);
|
||||
result = HeapTupleGetDatum(tuple);
|
||||
|
||||
PG_RETURN_DATUM(result);
|
||||
|
||||
#undef PG_DISSECT_WALFILE_NAME_COLS
|
||||
}
|
||||
|
||||
/*
|
||||
* pg_wal_replay_pause - Request to pause recovery
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user