mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-22 01:53:22 +02:00
This program relies on rm_desc backend routines and the xlogreader infrastructure to emit human-readable rendering of WAL records. Author: Andres Freund, with many reworks by Álvaro Reviewed (in a much earlier version) by Peter Eisentraut
95 lines
2.1 KiB
C
95 lines
2.1 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* compat.c
|
|
* Reimplementations of various backend functions.
|
|
*
|
|
* Portions Copyright (c) 2012, PostgreSQL Global Development Group
|
|
*
|
|
* IDENTIFICATION
|
|
* contrib/pg_xlogdump/compat.c
|
|
*
|
|
* This file contains client-side implementations for various backend
|
|
* functions that the rm_desc functions in *desc.c files rely on.
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
/* ugly hack, same as in e.g pg_controldata */
|
|
#define FRONTEND 1
|
|
#include "postgres.h"
|
|
|
|
#include <time.h>
|
|
|
|
#include "utils/datetime.h"
|
|
#include "lib/stringinfo.h"
|
|
|
|
/* copied from timestamp.c */
|
|
pg_time_t
|
|
timestamptz_to_time_t(TimestampTz t)
|
|
{
|
|
pg_time_t result;
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
result = (pg_time_t) (t / USECS_PER_SEC +
|
|
((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
|
|
#else
|
|
result = (pg_time_t) (t +
|
|
((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Stopgap implementation of timestamptz_to_str that doesn't depend on backend
|
|
* infrastructure.
|
|
*
|
|
* XXX: The backend timestamp infrastructure should instead be split out and
|
|
* moved into src/common.
|
|
*/
|
|
const char *
|
|
timestamptz_to_str(TimestampTz dt)
|
|
{
|
|
static char buf[MAXDATELEN + 1];
|
|
static char ts[MAXDATELEN + 1];
|
|
static char zone[MAXDATELEN + 1];
|
|
pg_time_t result = timestamptz_to_time_t(dt);
|
|
struct tm *ltime = localtime(&result);
|
|
|
|
strftime(ts, sizeof(zone), "%Y-%m-%d %H:%M:%S", ltime);
|
|
strftime(zone, sizeof(zone), "%Z", ltime);
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
sprintf(buf, "%s.%06d %s", ts, (int)(dt % USECS_PER_SEC), zone);
|
|
#else
|
|
sprintf(buf, "%s.%.6f %s", ts, fabs(dt - floor(dt)), zone);
|
|
#endif
|
|
|
|
return buf;
|
|
}
|
|
|
|
/*
|
|
* Provide a hacked up compat layer for StringInfos so xlog desc functions can
|
|
* be linked/called.
|
|
*/
|
|
void
|
|
appendStringInfo(StringInfo str, const char *fmt, ...)
|
|
{
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
vprintf(fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void
|
|
appendStringInfoString(StringInfo str, const char *string)
|
|
{
|
|
appendStringInfo(str, "%s", string);
|
|
}
|
|
|
|
void
|
|
appendStringInfoChar(StringInfo str, char ch)
|
|
{
|
|
appendStringInfo(str, "%c", ch);
|
|
}
|