Commit dc7d70ea authored by Joe Conway's avatar Joe Conway

Expose control file data via SQL accessible functions.

Add four new SQL accessible functions: pg_control_system(),
pg_control_checkpoint(), pg_control_recovery(), and pg_control_init()
which expose a subset of the control file data.

Along the way move the code to read and validate the control file to
src/common, where it can be shared by the new backend functions
and the original pg_controldata frontend program.

Patch by me, significant input, testing, and review by Michael Paquier.
parent d34794f7
......@@ -16703,6 +16703,362 @@ SELECT collation for ('foo' COLLATE "de_DE");
</tgroup>
</table>
<para>
The functions shown in <xref linkend="functions-controldata">
print information initialized during <command>initdb</>, such
as the catalog version. They also show information about write-ahead
logging and checkpoint processing. This information is cluster-wide,
and not specific to any one database. They provide most of the same
information, from the same source, as
<xref linkend="APP-PGCONTROLDATA">, although in a form better suited
to <acronym>SQL</acronym> functions.
</para>
<table id="functions-controldata">
<title>Control Data Functions</title>
<tgroup cols="3">
<thead>
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
</thead>
<tbody>
<row>
<entry>
<indexterm><primary>pg_control_checkpoint</primary></indexterm>
<literal><function>pg_control_checkpoint()</function></literal>
</entry>
<entry><type>record</type></entry>
<entry>
Returns information about current checkpoint state.
</entry>
</row>
<row>
<entry>
<indexterm><primary>pg_control_system</primary></indexterm>
<literal><function>pg_control_system()</function></literal>
</entry>
<entry><type>record</type></entry>
<entry>
Returns information about current controldata file state.
</entry>
</row>
<row>
<entry>
<indexterm><primary>pg_control_init</primary></indexterm>
<literal><function>pg_control_init()</function></literal>
</entry>
<entry><type>record</type></entry>
<entry>
Returns information about cluster initialization state.
</entry>
</row>
<row>
<entry>
<indexterm><primary>pg_control_recovery</primary></indexterm>
<literal><function>pg_control_recovery()</function></literal>
</entry>
<entry><type>record</type></entry>
<entry>
Returns information about recovery state.
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
<function>pg_control_checkpoint</> returns a record, shown in
<xref linkend="functions-pg-control-checkpoint">
</para>
<table id="functions-pg-control-checkpoint">
<title><function>pg_control_checkpoint</> Columns</title>
<tgroup cols="2">
<thead>
<row>
<entry>Column Name</entry>
<entry>Data Type</entry>
</row>
</thead>
<tbody>
<row>
<entry>checkpoint_location</entry>
<entry><type>pg_lsn</type></entry>
</row>
<row>
<entry>prior_location</entry>
<entry><type>pg_lsn</type></entry>
</row>
<row>
<entry>redo_location</entry>
<entry><type>pg_lsn</type></entry>
</row>
<row>
<entry>redo_wal_file</entry>
<entry><type>text</type></entry>
</row>
<row>
<entry>timeline_id</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>prev_timeline_id</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>full_page_writes</entry>
<entry><type>boolean</type></entry>
</row>
<row>
<entry>next_xid</entry>
<entry><type>text</type></entry>
</row>
<row>
<entry>next_oid</entry>
<entry><type>oid</type></entry>
</row>
<row>
<entry>next_multixact_id</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>next_multi_offset</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>oldest_xid</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>oldest_xid_dbid</entry>
<entry><type>oid</type></entry>
</row>
<row>
<entry>oldest_active_xid</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>oldest_multi_xid</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>oldest_multi_dbid</entry>
<entry><type>oid</type></entry>
</row>
<row>
<entry>oldest_commit_ts_xid</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>newest_commit_ts_xid</entry>
<entry><type>xid</type></entry>
</row>
<row>
<entry>checkpoint_time</entry>
<entry><type>timestamp with time zone</type></entry>
</row>
</tbody>
</tgroup>
</table>
<para>
<function>pg_control_system</> returns a record, shown in
<xref linkend="functions-pg-control-system">
</para>
<table id="functions-pg-control-system">
<title><function>pg_control_system</> Columns</title>
<tgroup cols="2">
<thead>
<row>
<entry>Column Name</entry>
<entry>Data Type</entry>
</row>
</thead>
<tbody>
<row>
<entry>pg_control_version</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>catalog_version_no</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>system_identifier</entry>
<entry><type>bigint</type></entry>
</row>
<row>
<entry>pg_control_last_modified</entry>
<entry><type>timestamp with time zone</type></entry>
</row>
</tbody>
</tgroup>
</table>
<para>
<function>pg_control_init</> returns a record, shown in
<xref linkend="functions-pg-control-init">
</para>
<table id="functions-pg-control-init">
<title><function>pg_control_init</> Columns</title>
<tgroup cols="2">
<thead>
<row>
<entry>Column Name</entry>
<entry>Data Type</entry>
</row>
</thead>
<tbody>
<row>
<entry>max_data_alignment</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>database_block_size</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>blocks_per_segment</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>wal_block_size</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>bytes_per_wal_segment</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>max_identifier_length</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>max_index_columns</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>max_toast_chunk_size</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>large_object_chunk_size</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>bigint_timestamps</entry>
<entry><type>boolean</type></entry>
</row>
<row>
<entry>float4_pass_by_value</entry>
<entry><type>boolean</type></entry>
</row>
<row>
<entry>float8_pass_by_value</entry>
<entry><type>boolean</type></entry>
</row>
<row>
<entry>data_page_checksum_version</entry>
<entry><type>integer</type></entry>
</row>
</tbody>
</tgroup>
</table>
<para>
<function>pg_control_recovery</> returns a record, shown in
<xref linkend="functions-pg-control-recovery">
</para>
<table id="functions-pg-control-recovery">
<title><function>pg_control_recovery</> Columns</title>
<tgroup cols="2">
<thead>
<row>
<entry>Column Name</entry>
<entry>Data Type</entry>
</row>
</thead>
<tbody>
<row>
<entry>min_recovery_end_location</entry>
<entry><type>pg_lsn</type></entry>
</row>
<row>
<entry>min_recovery_end_timeline</entry>
<entry><type>integer</type></entry>
</row>
<row>
<entry>backup_start_location</entry>
<entry><type>pg_lsn</type></entry>
</row>
<row>
<entry>backup_end_location</entry>
<entry><type>pg_lsn</type></entry>
</row>
<row>
<entry>end_of_backup_record_required</entry>
<entry><type>boolean</type></entry>
</row>
</tbody>
</tgroup>
</table>
</sect1>
<sect1 id="functions-admin">
......
......@@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS)
OBJS = guc.o help_config.o pg_config.o pg_rusage.o \
OBJS = guc.o help_config.o pg_config.o pg_controldata.o pg_rusage.o \
ps_status.o rls.o sampling.o superuser.o timeout.o tzparser.o
# This location might depend on the installation directories. Therefore
......
This diff is collapsed.
This diff is collapsed.
......@@ -36,8 +36,8 @@ override CPPFLAGS += -DVAL_LDFLAGS_EX="\"$(LDFLAGS_EX)\""
override CPPFLAGS += -DVAL_LDFLAGS_SL="\"$(LDFLAGS_SL)\""
override CPPFLAGS += -DVAL_LIBS="\"$(LIBS)\""
OBJS_COMMON = config_info.o exec.o pg_lzcompress.o pgfnames.o psprintf.o \
relpath.o rmtree.o string.o username.o wait_error.o
OBJS_COMMON = config_info.o controldata_utils.o exec.o pg_lzcompress.o \
pgfnames.o psprintf.o relpath.o rmtree.o string.o username.o wait_error.o
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o restricted_token.o
......
/*-------------------------------------------------------------------------
*
* controldata_utils.c
* Common code for control data file output.
*
*
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/common/controldata_utils.c
*
*-------------------------------------------------------------------------
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "port/pg_crc32c.h"
#ifndef FRONTEND
/* NOTE: caller must provide gettext call around the format string */
#define log_error(...) \
elog(ERROR, __VA_ARGS__)
#else
#define log_error(...) \
do { \
char *buf = psprintf(__VA_ARGS__); \
fprintf(stderr, "%s: %s\n", progname, buf); \
exit(2); \
} while (0)
#endif
/*
* get_controlfile(char *DataDir, const char *progname)
*
* Get controlfile values. The caller is responsible
* for pfreeing the result.
*/
ControlFileData *
get_controlfile(char *DataDir, const char *progname)
{
ControlFileData *ControlFile;
int fd;
char ControlFilePath[MAXPGPATH];
pg_crc32c crc;
ControlFile = palloc(sizeof(ControlFileData));
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
if ((fd = open(ControlFilePath, O_RDONLY | PG_BINARY, 0)) == -1)
log_error(_("could not open file \"%s\" for reading: %s"),
ControlFilePath, strerror(errno));
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
log_error(_("could not read file \"%s\": %s"),
ControlFilePath, strerror(errno));
close(fd);
/* Check the CRC. */
INIT_CRC32C(crc);
COMP_CRC32C(crc,
(char *) ControlFile,
offsetof(ControlFileData, crc));
FIN_CRC32C(crc);
if (!EQ_CRC32C(crc, ControlFile->crc))
#ifndef FRONTEND
elog(ERROR, _("calculated CRC checksum does not match value stored in file"));
#else
printf(_("WARNING: Calculated CRC checksum does not match value stored in file.\n"
"Either the file is corrupt, or it has a different layout than this program\n"
"is expecting. The results below are untrustworthy.\n\n"));
#endif
/* Make sure the control file is valid byte order. */
if (ControlFile->pg_control_version % 65536 == 0 &&
ControlFile->pg_control_version / 65536 != 0)
#ifndef FRONTEND
elog(ERROR, _("byte ordering mismatch"));
#else
printf(_("WARNING: possible byte ordering mismatch\n"
"The byte ordering used to store the pg_control file might not match the one\n"
"used by this program. In that case the results below would be incorrect, and\n"
"the PostgreSQL installation would be incompatible with this data directory.\n"));
#endif
return ControlFile;
}
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 201603021
#define CATALOG_VERSION_NO 201603051
#endif
......@@ -5225,6 +5225,19 @@ DESCR("row security for current context active on table by table name");
DATA(insert OID = 3400 ( pg_config PGNSP PGUID 12 1 23 0 0 f f f f t t i r 0 0 2249 "" "{25,25}" "{o,o}" "{name,setting}" _null_ _null_ pg_config _null_ _null_ _null_ ));
DESCR("pg_config binary as a function");
/* pg_controldata related functions */
DATA(insert OID = 3441 ( pg_control_system PGNSP PGUID 12 1 0 0 0 f f f f t f v s 0 0 2249 "" "{23,23,20,1184}" "{o,o,o,o}" "{pg_control_version,catalog_version_no,system_identifier,pg_control_last_modified}" _null_ _null_ pg_control_system _null_ _null_ _null_ ));
DESCR("pg_controldata general state information as a function");
DATA(insert OID = 3442 ( pg_control_checkpoint PGNSP PGUID 12 1 0 0 0 f f f f t f v s 0 0 2249 "" "{3220,3220,3220,25,23,23,16,25,26,28,28,28,26,28,28,26,28,28,1184}" "{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}" "{checkpoint_location,prior_location,redo_location,redo_wal_file,timeline_id,prev_timeline_id,full_page_writes,next_xid,next_oid,next_multixact_id,next_multi_offset,oldest_xid,oldest_xid_dbid,oldest_active_xid,oldest_multi_xid,oldest_multi_dbid,oldest_commit_ts_xid,newest_commit_ts_xid,checkpoint_time}" _null_ _null_ pg_control_checkpoint _null_ _null_ _null_ ));
DESCR("pg_controldata checkpoint state information as a function");
DATA(insert OID = 3443 ( pg_control_recovery PGNSP PGUID 12 1 0 0 0 f f f f t f v s 0 0 2249 "" "{3220,23,3220,3220,16}" "{o,o,o,o,o}" "{min_recovery_end_location,min_recovery_end_timeline,backup_start_location,backup_end_location,end_of_backup_record_required}" _null_ _null_ pg_control_recovery _null_ _null_ _null_ ));
DESCR("pg_controldata recovery state information as a function");
DATA(insert OID = 3444 ( pg_control_init PGNSP PGUID 12 1 0 0 0 f f f f t f v s 0 0 2249 "" "{23,23,23,23,23,23,23,23,23,16,16,16,23}" "{o,o,o,o,o,o,o,o,o,o,o,o,o}" "{max_data_alignment,database_block_size,blocks_per_segment,wal_block_size,bytes_per_wal_segment,max_identifier_length,max_index_columns,max_toast_chunk_size,large_object_chunk_size,bigint_timestamps,float4_pass_by_value,float8_pass_by_value,data_page_checksum_version}" _null_ _null_ pg_control_init _null_ _null_ _null_ ));
DESCR("pg_controldata init state information as a function");
/*
* Symbolic values for provolatile column: these indicate whether the result
* of a function is dependent *only* on the values of its explicit arguments,
......
/*
* controldata_utils.h
* Common code for pg_controldata output
*
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/common/controldata_utils.h
*/
#ifndef COMMON_CONTROLDATA_UTILS_H
#define COMMON_CONTROLDATA_UTILS_H
extern ControlFileData *get_controlfile(char *DataDir, const char *progname);
#endif /* COMMON_CONTROLDATA_UTILS_H */
......@@ -1151,6 +1151,12 @@ extern Datum show_all_file_settings(PG_FUNCTION_ARGS);
/* pg_config.c */
extern Datum pg_config(PG_FUNCTION_ARGS);
/* pg_controldata.c */
extern Datum pg_control_checkpoint(PG_FUNCTION_ARGS);
extern Datum pg_control_system(PG_FUNCTION_ARGS);
extern Datum pg_control_init(PG_FUNCTION_ARGS);
extern Datum pg_control_recovery(PG_FUNCTION_ARGS);
/* rls.c */
extern Datum row_security_active(PG_FUNCTION_ARGS);
extern Datum row_security_active_name(PG_FUNCTION_ARGS);
......
......@@ -106,8 +106,8 @@ sub mkvcbuild
}
our @pgcommonallfiles = qw(
config_info.c exec.c pg_lzcompress.c pgfnames.c psprintf.c
relpath.c rmtree.c string.c username.c wait_error.c);
config_info.c controldata_utils.c exec.c pg_lzcompress.c pgfnames.c
psprintf.c relpath.c rmtree.c string.c username.c wait_error.c);
our @pgcommonfrontendfiles = (
@pgcommonallfiles, qw(fe_memutils.c
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment