2015-03-23 19:47:52 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2020-11-04 11:21:18 +02:00
|
|
|
* libpq_source.c
|
|
|
|
* Functions for fetching files from a remote server via libpq.
|
2015-03-23 19:47:52 +02:00
|
|
|
*
|
2023-01-02 15:00:37 -05:00
|
|
|
* Copyright (c) 2013-2023, PostgreSQL Global Development Group
|
2015-03-23 19:47:52 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
2019-10-23 09:38:53 +05:30
|
|
|
#include "catalog/pg_type_d.h"
|
2020-08-10 09:22:54 -07:00
|
|
|
#include "common/connect.h"
|
2015-03-23 19:47:52 +02:00
|
|
|
#include "datapagemap.h"
|
|
|
|
#include "file_ops.h"
|
|
|
|
#include "filemap.h"
|
2020-11-12 14:52:24 +02:00
|
|
|
#include "lib/stringinfo.h"
|
2019-10-23 09:38:53 +05:30
|
|
|
#include "pg_rewind.h"
|
2017-10-01 15:36:14 -07:00
|
|
|
#include "port/pg_bswap.h"
|
2020-11-04 11:21:18 +02:00
|
|
|
#include "rewind_source.h"
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
/*
|
2020-11-12 14:52:24 +02:00
|
|
|
* Files are fetched MAX_CHUNK_SIZE bytes at a time, and with a
|
|
|
|
* maximum of MAX_CHUNKS_PER_QUERY chunks in a single query.
|
2015-03-23 19:47:52 +02:00
|
|
|
*/
|
2020-11-12 14:52:24 +02:00
|
|
|
#define MAX_CHUNK_SIZE (1024 * 1024)
|
|
|
|
#define MAX_CHUNKS_PER_QUERY 1000
|
|
|
|
|
|
|
|
/* represents a request to fetch a piece of a file from the source */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *path; /* path relative to data directory root */
|
|
|
|
off_t offset;
|
|
|
|
size_t length;
|
|
|
|
} fetch_range_request;
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
rewind_source common; /* common interface functions */
|
|
|
|
|
|
|
|
PGconn *conn;
|
2020-11-12 14:52:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Queue of chunks that have been requested with the queue_fetch_range()
|
|
|
|
* function, but have not been fetched from the remote server yet.
|
|
|
|
*/
|
|
|
|
int num_requests;
|
|
|
|
fetch_range_request request_queue[MAX_CHUNKS_PER_QUERY];
|
|
|
|
|
|
|
|
/* temporary space for process_queued_fetch_requests() */
|
|
|
|
StringInfoData paths;
|
|
|
|
StringInfoData offsets;
|
|
|
|
StringInfoData lengths;
|
2020-11-04 11:21:18 +02:00
|
|
|
} libpq_source;
|
|
|
|
|
|
|
|
static void init_libpq_conn(PGconn *conn);
|
|
|
|
static char *run_simple_query(PGconn *conn, const char *sql);
|
|
|
|
static void run_simple_command(PGconn *conn, const char *sql);
|
2020-11-12 14:52:24 +02:00
|
|
|
static void appendArrayEscapedString(StringInfo buf, const char *str);
|
|
|
|
|
|
|
|
static void process_queued_fetch_requests(libpq_source *src);
|
2020-11-04 11:21:18 +02:00
|
|
|
|
|
|
|
/* public interface functions */
|
|
|
|
static void libpq_traverse_files(rewind_source *source,
|
|
|
|
process_file_callback_t callback);
|
pg_rewind: Fetch small files according to new size.
There's a race condition if a file changes in the source system
after we have collected the file list. If the file becomes larger,
we only fetched up to its original size. That can easily result in
a truncated file. That's not a problem for relation files, files
in pg_xact, etc. because any actions on them will be replayed from
the WAL. However, configuration files are affected.
This commit mitigates the race condition by fetching small files in
whole, even if they have grown. A test is added in which an extra
file copied is concurrently grown with the output of pg_rewind thus
guaranteeing it to have changed in size during the operation. This
is not a full fix: we still believe the original file size for files
larger than 1 MB. That should be enough for configuration files,
and doing more than that would require big changes to the chunking
logic in libpq_source.c.
This mitigates the race condition if the file is modified between
the original scan of files and copying the file, but there's still
a race condition if a file is changed while it's being copied.
That's a much smaller window, though, and pg_basebackup has the
same issue.
This race can be seen with pg_auto_failover, which frequently uses
ALTER SYSTEM, which updates postgresql.auto.conf. Often, pg_rewind
will fail, because the postgresql.auto.conf file changed concurrently
and a partial version of it was copied to the target. The partial
file would fail to parse, preventing the server from starting up.
Author: Heikki Linnakangas
Reviewed-by: Cary Huang
Discussion: https://postgr.es/m/f67feb24-5833-88cb-1020-19a4a2b83ac7%40iki.fi
2022-04-05 14:45:31 +02:00
|
|
|
static void libpq_queue_fetch_file(rewind_source *source, const char *path, size_t len);
|
2020-11-04 11:21:18 +02:00
|
|
|
static void libpq_queue_fetch_range(rewind_source *source, const char *path,
|
|
|
|
off_t off, size_t len);
|
|
|
|
static void libpq_finish_fetch(rewind_source *source);
|
|
|
|
static char *libpq_fetch_file(rewind_source *source, const char *path,
|
|
|
|
size_t *filesize);
|
|
|
|
static XLogRecPtr libpq_get_current_wal_insert_lsn(rewind_source *source);
|
|
|
|
static void libpq_destroy(rewind_source *source);
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
/*
|
|
|
|
* Create a new libpq source.
|
|
|
|
*
|
|
|
|
* The caller has already established the connection, but should not try
|
|
|
|
* to use it while the source is active.
|
|
|
|
*/
|
|
|
|
rewind_source *
|
|
|
|
init_libpq_source(PGconn *conn)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-04 11:21:18 +02:00
|
|
|
libpq_source *src;
|
|
|
|
|
|
|
|
init_libpq_conn(conn);
|
|
|
|
|
|
|
|
src = pg_malloc0(sizeof(libpq_source));
|
|
|
|
|
|
|
|
src->common.traverse_files = libpq_traverse_files;
|
|
|
|
src->common.fetch_file = libpq_fetch_file;
|
pg_rewind: Fetch small files according to new size.
There's a race condition if a file changes in the source system
after we have collected the file list. If the file becomes larger,
we only fetched up to its original size. That can easily result in
a truncated file. That's not a problem for relation files, files
in pg_xact, etc. because any actions on them will be replayed from
the WAL. However, configuration files are affected.
This commit mitigates the race condition by fetching small files in
whole, even if they have grown. A test is added in which an extra
file copied is concurrently grown with the output of pg_rewind thus
guaranteeing it to have changed in size during the operation. This
is not a full fix: we still believe the original file size for files
larger than 1 MB. That should be enough for configuration files,
and doing more than that would require big changes to the chunking
logic in libpq_source.c.
This mitigates the race condition if the file is modified between
the original scan of files and copying the file, but there's still
a race condition if a file is changed while it's being copied.
That's a much smaller window, though, and pg_basebackup has the
same issue.
This race can be seen with pg_auto_failover, which frequently uses
ALTER SYSTEM, which updates postgresql.auto.conf. Often, pg_rewind
will fail, because the postgresql.auto.conf file changed concurrently
and a partial version of it was copied to the target. The partial
file would fail to parse, preventing the server from starting up.
Author: Heikki Linnakangas
Reviewed-by: Cary Huang
Discussion: https://postgr.es/m/f67feb24-5833-88cb-1020-19a4a2b83ac7%40iki.fi
2022-04-05 14:45:31 +02:00
|
|
|
src->common.queue_fetch_file = libpq_queue_fetch_file;
|
2020-11-04 11:21:18 +02:00
|
|
|
src->common.queue_fetch_range = libpq_queue_fetch_range;
|
|
|
|
src->common.finish_fetch = libpq_finish_fetch;
|
|
|
|
src->common.get_current_wal_insert_lsn = libpq_get_current_wal_insert_lsn;
|
|
|
|
src->common.destroy = libpq_destroy;
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
src->conn = conn;
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
initStringInfo(&src->paths);
|
|
|
|
initStringInfo(&src->offsets);
|
|
|
|
initStringInfo(&src->lengths);
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
return &src->common;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a libpq connection for use.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_libpq_conn(PGconn *conn)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
char *str;
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2019-08-28 11:47:35 +09:00
|
|
|
/* disable all types of timeouts */
|
2020-11-04 11:21:18 +02:00
|
|
|
run_simple_command(conn, "SET statement_timeout = 0");
|
|
|
|
run_simple_command(conn, "SET lock_timeout = 0");
|
|
|
|
run_simple_command(conn, "SET idle_in_transaction_session_timeout = 0");
|
2019-08-28 11:47:35 +09:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
/*
|
|
|
|
* we don't intend to do any updates, put the connection in read-only mode
|
|
|
|
* to keep us honest
|
|
|
|
*/
|
|
|
|
run_simple_command(conn, "SET default_transaction_read_only = on");
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
/* secure search_path */
|
Empty search_path in Autovacuum and non-psql/pgbench clients.
This makes the client programs behave as documented regardless of the
connect-time search_path and regardless of user-created objects. Today,
a malicious user with CREATE permission on a search_path schema can take
control of certain of these clients' queries and invoke arbitrary SQL
functions under the client identity, often a superuser. This is
exploitable in the default configuration, where all users have CREATE
privilege on schema "public".
This changes behavior of user-defined code stored in the database, like
pg_index.indexprs and pg_extension_config_dump(). If they reach code
bearing unqualified names, "does not exist" or "no schema has been
selected to create in" errors might appear. Users may fix such errors
by schema-qualifying affected names. After upgrading, consider watching
server logs for these errors.
The --table arguments of src/bin/scripts clients have been lax; for
example, "vacuumdb -Zt pg_am\;CHECKPOINT" performed a checkpoint. That
now fails, but for now, "vacuumdb -Zt 'pg_am(amname);CHECKPOINT'" still
performs a checkpoint.
Back-patch to 9.3 (all supported versions).
Reviewed by Tom Lane, though this fix strategy was not his first choice.
Reported by Arseniy Sharoglazov.
Security: CVE-2018-1058
2018-02-26 07:39:44 -08:00
|
|
|
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
pg_fatal("could not clear search_path: %s",
|
|
|
|
PQresultErrorMessage(res));
|
|
|
|
PQclear(res);
|
|
|
|
|
2015-03-23 19:47:52 +02:00
|
|
|
/*
|
2015-07-12 16:25:51 -04:00
|
|
|
* Also check that full_page_writes is enabled. We can get torn pages if
|
2015-03-23 19:47:52 +02:00
|
|
|
* a page is modified while we read it with pg_read_binary_file(), and we
|
|
|
|
* rely on full page images to fix them.
|
|
|
|
*/
|
2020-11-04 11:21:18 +02:00
|
|
|
str = run_simple_query(conn, "SHOW full_page_writes");
|
2015-03-23 19:47:52 +02:00
|
|
|
if (strcmp(str, "on") != 0)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("full_page_writes must be enabled in the source server");
|
2015-03-23 19:47:52 +02:00
|
|
|
pg_free(str);
|
2020-11-12 14:52:24 +02:00
|
|
|
|
|
|
|
/* Prepare a statement we'll use to fetch files */
|
|
|
|
res = PQprepare(conn, "fetch_chunks_stmt",
|
|
|
|
"SELECT path, begin,\n"
|
|
|
|
" pg_read_binary_file(path, begin, len, true) AS chunk\n"
|
|
|
|
"FROM unnest ($1::text[], $2::int8[], $3::int4[]) as x(path, begin, len)",
|
|
|
|
3, NULL);
|
|
|
|
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
pg_fatal("could not prepare statement to fetch file contents: %s",
|
|
|
|
PQresultErrorMessage(res));
|
|
|
|
PQclear(res);
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-11-04 11:21:18 +02:00
|
|
|
* Run a query that returns a single value.
|
|
|
|
*
|
2015-07-12 16:25:51 -04:00
|
|
|
* The result should be pg_free'd after use.
|
2015-03-23 19:47:52 +02:00
|
|
|
*/
|
|
|
|
static char *
|
2020-11-04 11:21:18 +02:00
|
|
|
run_simple_query(PGconn *conn, const char *sql)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
res = PQexec(conn, sql);
|
|
|
|
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
2020-05-08 20:35:03 +02:00
|
|
|
pg_fatal("error running query (%s) on source server: %s",
|
2015-03-23 19:47:52 +02:00
|
|
|
sql, PQresultErrorMessage(res));
|
|
|
|
|
|
|
|
/* sanity check the result set */
|
|
|
|
if (PQnfields(res) != 1 || PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected result set from query");
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
result = pg_strdup(PQgetvalue(res, 0, 0));
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-28 11:47:35 +09:00
|
|
|
/*
|
2020-11-04 11:21:18 +02:00
|
|
|
* Run a command.
|
|
|
|
*
|
2019-08-28 11:47:35 +09:00
|
|
|
* In the event of a failure, exit immediately.
|
|
|
|
*/
|
|
|
|
static void
|
2020-11-04 11:21:18 +02:00
|
|
|
run_simple_command(PGconn *conn, const char *sql)
|
2019-08-28 11:47:35 +09:00
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
|
|
|
|
res = PQexec(conn, sql);
|
|
|
|
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
pg_fatal("error running query (%s) in source server: %s",
|
|
|
|
sql, PQresultErrorMessage(res));
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
|
2015-03-23 19:47:52 +02:00
|
|
|
/*
|
2020-11-04 11:21:18 +02:00
|
|
|
* Call the pg_current_wal_insert_lsn() function in the remote system.
|
2015-03-23 19:47:52 +02:00
|
|
|
*/
|
2020-11-04 11:21:18 +02:00
|
|
|
static XLogRecPtr
|
|
|
|
libpq_get_current_wal_insert_lsn(rewind_source *source)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-04 11:21:18 +02:00
|
|
|
PGconn *conn = ((libpq_source *) source)->conn;
|
2015-03-23 19:47:52 +02:00
|
|
|
XLogRecPtr result;
|
|
|
|
uint32 hi;
|
|
|
|
uint32 lo;
|
|
|
|
char *val;
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
val = run_simple_query(conn, "SELECT pg_current_wal_insert_lsn()");
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
if (sscanf(val, "%X/%X", &hi, &lo) != 2)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unrecognized result \"%s\" for current WAL insert location", val);
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
result = ((uint64) hi) << 32 | lo;
|
|
|
|
|
2015-07-12 16:25:51 -04:00
|
|
|
pg_free(val);
|
|
|
|
|
2015-03-23 19:47:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a list of all files in the data directory.
|
|
|
|
*/
|
2020-11-04 11:21:18 +02:00
|
|
|
static void
|
|
|
|
libpq_traverse_files(rewind_source *source, process_file_callback_t callback)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-04 11:21:18 +02:00
|
|
|
PGconn *conn = ((libpq_source *) source)->conn;
|
2015-03-23 19:47:52 +02:00
|
|
|
PGresult *res;
|
|
|
|
const char *sql;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a recursive directory listing of the whole data directory.
|
|
|
|
*
|
|
|
|
* The WITH RECURSIVE part does most of the work. The second part gets the
|
|
|
|
* targets of the symlinks in pg_tblspc directory.
|
|
|
|
*
|
|
|
|
* XXX: There is no backend function to get a symbolic link's target in
|
|
|
|
* general, so if the admin has put any custom symbolic links in the data
|
|
|
|
* directory, they won't be copied correctly.
|
|
|
|
*/
|
|
|
|
sql =
|
|
|
|
"WITH RECURSIVE files (path, filename, size, isdir) AS (\n"
|
|
|
|
" SELECT '' AS path, filename, size, isdir FROM\n"
|
2015-06-28 21:35:51 +03:00
|
|
|
" (SELECT pg_ls_dir('.', true, false) AS filename) AS fn,\n"
|
|
|
|
" pg_stat_file(fn.filename, true) AS this\n"
|
2015-03-23 19:47:52 +02:00
|
|
|
" UNION ALL\n"
|
|
|
|
" SELECT parent.path || parent.filename || '/' AS path,\n"
|
|
|
|
" fn, this.size, this.isdir\n"
|
|
|
|
" FROM files AS parent,\n"
|
2015-06-28 21:35:51 +03:00
|
|
|
" pg_ls_dir(parent.path || parent.filename, true, false) AS fn,\n"
|
|
|
|
" pg_stat_file(parent.path || parent.filename || '/' || fn, true) AS this\n"
|
2015-03-23 19:47:52 +02:00
|
|
|
" WHERE parent.isdir = 't'\n"
|
|
|
|
")\n"
|
|
|
|
"SELECT path || filename, size, isdir,\n"
|
|
|
|
" pg_tablespace_location(pg_tablespace.oid) AS link_target\n"
|
|
|
|
"FROM files\n"
|
|
|
|
"LEFT OUTER JOIN pg_tablespace ON files.path = 'pg_tblspc/'\n"
|
|
|
|
" AND oid::text = files.filename\n";
|
|
|
|
res = PQexec(conn, sql);
|
|
|
|
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
2015-06-22 20:40:01 -04:00
|
|
|
pg_fatal("could not fetch file list: %s",
|
2015-03-23 19:47:52 +02:00
|
|
|
PQresultErrorMessage(res));
|
|
|
|
|
|
|
|
/* sanity check the result set */
|
|
|
|
if (PQnfields(res) != 4)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected result set while fetching file list");
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
/* Read result to local variables */
|
|
|
|
for (i = 0; i < PQntuples(res); i++)
|
|
|
|
{
|
2020-07-15 15:17:23 +09:00
|
|
|
char *path;
|
|
|
|
int64 filesize;
|
|
|
|
bool isdir;
|
|
|
|
char *link_target;
|
2015-03-23 19:47:52 +02:00
|
|
|
file_type_t type;
|
|
|
|
|
2020-07-15 15:17:23 +09:00
|
|
|
if (PQgetisnull(res, i, 1))
|
2015-06-28 21:35:51 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The file was removed from the server while the query was
|
|
|
|
* running. Ignore it.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:17:23 +09:00
|
|
|
path = PQgetvalue(res, i, 0);
|
|
|
|
filesize = atol(PQgetvalue(res, i, 1));
|
|
|
|
isdir = (strcmp(PQgetvalue(res, i, 2), "t") == 0);
|
|
|
|
link_target = PQgetvalue(res, i, 3);
|
|
|
|
|
2015-03-23 19:47:52 +02:00
|
|
|
if (link_target[0])
|
|
|
|
type = FILE_TYPE_SYMLINK;
|
|
|
|
else if (isdir)
|
|
|
|
type = FILE_TYPE_DIRECTORY;
|
|
|
|
else
|
|
|
|
type = FILE_TYPE_REGULAR;
|
|
|
|
|
2015-04-15 22:52:00 +03:00
|
|
|
process_source_file(path, type, filesize, link_target);
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
2015-07-12 16:25:51 -04:00
|
|
|
PQclear(res);
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
|
|
|
|
pg_rewind: Fetch small files according to new size.
There's a race condition if a file changes in the source system
after we have collected the file list. If the file becomes larger,
we only fetched up to its original size. That can easily result in
a truncated file. That's not a problem for relation files, files
in pg_xact, etc. because any actions on them will be replayed from
the WAL. However, configuration files are affected.
This commit mitigates the race condition by fetching small files in
whole, even if they have grown. A test is added in which an extra
file copied is concurrently grown with the output of pg_rewind thus
guaranteeing it to have changed in size during the operation. This
is not a full fix: we still believe the original file size for files
larger than 1 MB. That should be enough for configuration files,
and doing more than that would require big changes to the chunking
logic in libpq_source.c.
This mitigates the race condition if the file is modified between
the original scan of files and copying the file, but there's still
a race condition if a file is changed while it's being copied.
That's a much smaller window, though, and pg_basebackup has the
same issue.
This race can be seen with pg_auto_failover, which frequently uses
ALTER SYSTEM, which updates postgresql.auto.conf. Often, pg_rewind
will fail, because the postgresql.auto.conf file changed concurrently
and a partial version of it was copied to the target. The partial
file would fail to parse, preventing the server from starting up.
Author: Heikki Linnakangas
Reviewed-by: Cary Huang
Discussion: https://postgr.es/m/f67feb24-5833-88cb-1020-19a4a2b83ac7%40iki.fi
2022-04-05 14:45:31 +02:00
|
|
|
/*
|
|
|
|
* Queue up a request to fetch a file from remote system.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
libpq_queue_fetch_file(rewind_source *source, const char *path, size_t len)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Truncate the target file immediately, and queue a request to fetch it
|
|
|
|
* from the source. If the file is small, smaller than MAX_CHUNK_SIZE,
|
|
|
|
* request fetching a full-sized chunk anyway, so that if the file has
|
|
|
|
* become larger in the source system, after we scanned the source
|
|
|
|
* directory, we still fetch the whole file. This only works for files up
|
|
|
|
* to MAX_CHUNK_SIZE, but that's good enough for small configuration files
|
|
|
|
* and such that are changed every now and then, but not WAL-logged. For
|
|
|
|
* larger files, we fetch up to the original size.
|
|
|
|
*
|
|
|
|
* Even with that mechanism, there is an inherent race condition if the
|
|
|
|
* file is modified at the same instant that we're copying it, so that we
|
|
|
|
* might copy a torn version of the file with one half from the old
|
|
|
|
* version and another half from the new. But pg_basebackup has the same
|
|
|
|
* problem, and it hasn't been a problem in practice.
|
|
|
|
*
|
|
|
|
* It might seem more natural to truncate the file later, when we receive
|
|
|
|
* it from the source server, but then we'd need to track which
|
|
|
|
* fetch-requests are for a whole file.
|
|
|
|
*/
|
|
|
|
open_target_file(path, true);
|
|
|
|
libpq_queue_fetch_range(source, path, 0, Max(len, MAX_CHUNK_SIZE));
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
/*
|
|
|
|
* Queue up a request to fetch a piece of a file from remote system.
|
2015-03-23 19:47:52 +02:00
|
|
|
*/
|
|
|
|
static void
|
2020-11-04 11:21:18 +02:00
|
|
|
libpq_queue_fetch_range(rewind_source *source, const char *path, off_t off,
|
|
|
|
size_t len)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-04 11:21:18 +02:00
|
|
|
libpq_source *src = (libpq_source *) source;
|
|
|
|
|
|
|
|
/*
|
2020-11-12 14:52:24 +02:00
|
|
|
* Does this request happen to be a continuation of the previous chunk? If
|
|
|
|
* so, merge it with the previous one.
|
|
|
|
*
|
|
|
|
* XXX: We use pointer equality to compare the path. That's good enough
|
|
|
|
* for our purposes; the caller always passes the same pointer for the
|
|
|
|
* same filename. If it didn't, we would fail to merge requests, but it
|
|
|
|
* wouldn't affect correctness.
|
2020-11-04 11:21:18 +02:00
|
|
|
*/
|
2020-11-12 14:52:24 +02:00
|
|
|
if (src->num_requests > 0)
|
2020-11-04 11:21:18 +02:00
|
|
|
{
|
2020-11-12 14:52:24 +02:00
|
|
|
fetch_range_request *prev = &src->request_queue[src->num_requests - 1];
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
if (prev->offset + prev->length == off &&
|
|
|
|
prev->length < MAX_CHUNK_SIZE &&
|
|
|
|
prev->path == path)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Extend the previous request to cover as much of this new
|
|
|
|
* request as possible, without exceeding MAX_CHUNK_SIZE.
|
|
|
|
*/
|
|
|
|
size_t thislen;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
thislen = Min(len, MAX_CHUNK_SIZE - prev->length);
|
|
|
|
prev->length += thislen;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
off += thislen;
|
|
|
|
len -= thislen;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
/*
|
|
|
|
* Fall through to create new requests for any remaining 'len'
|
|
|
|
* that didn't fit in the previous chunk.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
/* Divide the request into pieces of MAX_CHUNK_SIZE bytes each */
|
|
|
|
while (len > 0)
|
2020-11-04 11:21:18 +02:00
|
|
|
{
|
2020-11-12 14:52:24 +02:00
|
|
|
int32 thislen;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
/* if the queue is full, perform all the work queued up so far */
|
|
|
|
if (src->num_requests == MAX_CHUNKS_PER_QUERY)
|
|
|
|
process_queued_fetch_requests(src);
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
thislen = Min(len, MAX_CHUNK_SIZE);
|
|
|
|
src->request_queue[src->num_requests].path = path;
|
|
|
|
src->request_queue[src->num_requests].offset = off;
|
|
|
|
src->request_queue[src->num_requests].length = thislen;
|
|
|
|
src->num_requests++;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
off += thislen;
|
|
|
|
len -= thislen;
|
2020-11-04 11:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-11-12 14:52:24 +02:00
|
|
|
* Fetch all the queued chunks and write them to the target data directory.
|
2020-11-04 11:21:18 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
libpq_finish_fetch(rewind_source *source)
|
|
|
|
{
|
2020-11-12 14:52:24 +02:00
|
|
|
process_queued_fetch_requests((libpq_source *) source);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_queued_fetch_requests(libpq_source *src)
|
|
|
|
{
|
|
|
|
const char *params[3];
|
2015-03-23 19:47:52 +02:00
|
|
|
PGresult *res;
|
2020-11-12 14:52:24 +02:00
|
|
|
int chunkno;
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
if (src->num_requests == 0)
|
|
|
|
return;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
pg_log_debug("getting %d file chunks", src->num_requests);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The prepared statement, 'fetch_chunks_stmt', takes three arrays with
|
|
|
|
* the same length as parameters: paths, offsets and lengths. Construct
|
|
|
|
* the string representations of them.
|
|
|
|
*/
|
|
|
|
resetStringInfo(&src->paths);
|
|
|
|
resetStringInfo(&src->offsets);
|
|
|
|
resetStringInfo(&src->lengths);
|
|
|
|
|
|
|
|
appendStringInfoChar(&src->paths, '{');
|
|
|
|
appendStringInfoChar(&src->offsets, '{');
|
|
|
|
appendStringInfoChar(&src->lengths, '{');
|
|
|
|
for (int i = 0; i < src->num_requests; i++)
|
2020-11-04 11:21:18 +02:00
|
|
|
{
|
2020-11-12 14:52:24 +02:00
|
|
|
fetch_range_request *rq = &src->request_queue[i];
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
appendStringInfoChar(&src->paths, ',');
|
|
|
|
appendStringInfoChar(&src->offsets, ',');
|
|
|
|
appendStringInfoChar(&src->lengths, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
appendArrayEscapedString(&src->paths, rq->path);
|
|
|
|
appendStringInfo(&src->offsets, INT64_FORMAT, (int64) rq->offset);
|
|
|
|
appendStringInfo(&src->lengths, INT64_FORMAT, (int64) rq->length);
|
2020-11-04 11:21:18 +02:00
|
|
|
}
|
2020-11-12 14:52:24 +02:00
|
|
|
appendStringInfoChar(&src->paths, '}');
|
|
|
|
appendStringInfoChar(&src->offsets, '}');
|
|
|
|
appendStringInfoChar(&src->lengths, '}');
|
2020-11-04 11:21:18 +02:00
|
|
|
|
|
|
|
/*
|
2020-11-12 14:52:24 +02:00
|
|
|
* Execute the prepared statement.
|
2020-11-04 11:21:18 +02:00
|
|
|
*/
|
2020-11-12 14:52:24 +02:00
|
|
|
params[0] = src->paths.data;
|
|
|
|
params[1] = src->offsets.data;
|
|
|
|
params[2] = src->lengths.data;
|
2020-11-04 11:21:18 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
if (PQsendQueryPrepared(src->conn, "fetch_chunks_stmt", 3, params, NULL, NULL, 1) != 1)
|
2020-11-04 11:21:18 +02:00
|
|
|
pg_fatal("could not send query: %s", PQerrorMessage(src->conn));
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
if (PQsetSingleRowMode(src->conn) != 1)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("could not set libpq connection to single row mode");
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
/*----
|
|
|
|
* The result set is of format:
|
|
|
|
*
|
|
|
|
* path text -- path in the data directory, e.g "base/1/123"
|
|
|
|
* begin int8 -- offset within the file
|
|
|
|
* chunk bytea -- file content
|
|
|
|
*----
|
|
|
|
*/
|
2020-11-12 14:52:24 +02:00
|
|
|
chunkno = 0;
|
2020-11-04 11:21:18 +02:00
|
|
|
while ((res = PQgetResult(src->conn)) != NULL)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-12 14:52:24 +02:00
|
|
|
fetch_range_request *rq = &src->request_queue[chunkno];
|
2015-03-23 19:47:52 +02:00
|
|
|
char *filename;
|
|
|
|
int filenamelen;
|
pg_rewind: Fix some problems when copying files >2GB.
When incrementally updating a file larger than 2GB, the old code could
either fail outright (if the client asked the server for bytes beyond
the 2GB boundary) or fail to copy all the blocks that had actually
been modified (if the server reported a file size to the client in
excess of 2GB), resulting in data corruption. Generally, such files
won't occur anyway, but they might if using a non-default segment size
or if there the directory contains stray files unrelated to
PostgreSQL. Fix by a more prudent choice of data types.
Even with these improvements, this code still uses a mix of different
types (off_t, size_t, uint64, int64) to represent file sizes and
offsets, not all of which necessarily have the same width or
signedness, so further cleanup might be in order here. However, at
least now they all have the potential to be 64 bits wide on 64-bit
platforms.
Kuntal Ghosh and Michael Paquier, with a tweak by me.
Discussion: http://postgr.es/m/CAGz5QC+8gbkz=Brp0TgoKNqHWTzonbPtPex80U0O6Uh_bevbaA@mail.gmail.com
2017-07-21 14:25:36 -04:00
|
|
|
int64 chunkoff;
|
2015-03-23 19:47:52 +02:00
|
|
|
int chunksize;
|
|
|
|
char *chunk;
|
|
|
|
|
|
|
|
switch (PQresultStatus(res))
|
|
|
|
{
|
|
|
|
case PGRES_SINGLE_TUPLE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PGRES_TUPLES_OK:
|
2015-04-12 22:42:01 +03:00
|
|
|
PQclear(res);
|
2015-03-23 19:47:52 +02:00
|
|
|
continue; /* final zero-row result */
|
|
|
|
|
|
|
|
default:
|
2015-10-01 21:42:00 -04:00
|
|
|
pg_fatal("unexpected result while fetching remote files: %s",
|
2015-03-23 19:47:52 +02:00
|
|
|
PQresultErrorMessage(res));
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
if (chunkno > src->num_requests)
|
|
|
|
pg_fatal("received more data chunks than requested");
|
|
|
|
|
2015-03-23 19:47:52 +02:00
|
|
|
/* sanity check the result set */
|
|
|
|
if (PQnfields(res) != 3 || PQntuples(res) != 1)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected result set size while fetching remote files");
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2017-07-21 12:48:22 -04:00
|
|
|
if (PQftype(res, 0) != TEXTOID ||
|
pg_rewind: Fix some problems when copying files >2GB.
When incrementally updating a file larger than 2GB, the old code could
either fail outright (if the client asked the server for bytes beyond
the 2GB boundary) or fail to copy all the blocks that had actually
been modified (if the server reported a file size to the client in
excess of 2GB), resulting in data corruption. Generally, such files
won't occur anyway, but they might if using a non-default segment size
or if there the directory contains stray files unrelated to
PostgreSQL. Fix by a more prudent choice of data types.
Even with these improvements, this code still uses a mix of different
types (off_t, size_t, uint64, int64) to represent file sizes and
offsets, not all of which necessarily have the same width or
signedness, so further cleanup might be in order here. However, at
least now they all have the potential to be 64 bits wide on 64-bit
platforms.
Kuntal Ghosh and Michael Paquier, with a tweak by me.
Discussion: http://postgr.es/m/CAGz5QC+8gbkz=Brp0TgoKNqHWTzonbPtPex80U0O6Uh_bevbaA@mail.gmail.com
2017-07-21 14:25:36 -04:00
|
|
|
PQftype(res, 1) != INT8OID ||
|
2015-03-23 19:47:52 +02:00
|
|
|
PQftype(res, 2) != BYTEAOID)
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u",
|
2015-03-23 19:47:52 +02:00
|
|
|
PQftype(res, 0), PQftype(res, 1), PQftype(res, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PQfformat(res, 0) != 1 &&
|
|
|
|
PQfformat(res, 1) != 1 &&
|
|
|
|
PQfformat(res, 2) != 1)
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected result format while fetching remote files");
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PQgetisnull(res, 0, 0) ||
|
2015-06-28 21:35:51 +03:00
|
|
|
PQgetisnull(res, 0, 1))
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected null values in result while fetching remote files");
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
|
|
|
|
pg_rewind: Fix some problems when copying files >2GB.
When incrementally updating a file larger than 2GB, the old code could
either fail outright (if the client asked the server for bytes beyond
the 2GB boundary) or fail to copy all the blocks that had actually
been modified (if the server reported a file size to the client in
excess of 2GB), resulting in data corruption. Generally, such files
won't occur anyway, but they might if using a non-default segment size
or if there the directory contains stray files unrelated to
PostgreSQL. Fix by a more prudent choice of data types.
Even with these improvements, this code still uses a mix of different
types (off_t, size_t, uint64, int64) to represent file sizes and
offsets, not all of which necessarily have the same width or
signedness, so further cleanup might be in order here. However, at
least now they all have the potential to be 64 bits wide on 64-bit
platforms.
Kuntal Ghosh and Michael Paquier, with a tweak by me.
Discussion: http://postgr.es/m/CAGz5QC+8gbkz=Brp0TgoKNqHWTzonbPtPex80U0O6Uh_bevbaA@mail.gmail.com
2017-07-21 14:25:36 -04:00
|
|
|
if (PQgetlength(res, 0, 1) != sizeof(int64))
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected result length while fetching remote files");
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
/* Read result set to local variables */
|
pg_rewind: Fix some problems when copying files >2GB.
When incrementally updating a file larger than 2GB, the old code could
either fail outright (if the client asked the server for bytes beyond
the 2GB boundary) or fail to copy all the blocks that had actually
been modified (if the server reported a file size to the client in
excess of 2GB), resulting in data corruption. Generally, such files
won't occur anyway, but they might if using a non-default segment size
or if there the directory contains stray files unrelated to
PostgreSQL. Fix by a more prudent choice of data types.
Even with these improvements, this code still uses a mix of different
types (off_t, size_t, uint64, int64) to represent file sizes and
offsets, not all of which necessarily have the same width or
signedness, so further cleanup might be in order here. However, at
least now they all have the potential to be 64 bits wide on 64-bit
platforms.
Kuntal Ghosh and Michael Paquier, with a tweak by me.
Discussion: http://postgr.es/m/CAGz5QC+8gbkz=Brp0TgoKNqHWTzonbPtPex80U0O6Uh_bevbaA@mail.gmail.com
2017-07-21 14:25:36 -04:00
|
|
|
memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64));
|
2017-10-01 15:36:14 -07:00
|
|
|
chunkoff = pg_ntoh64(chunkoff);
|
2015-03-23 19:47:52 +02:00
|
|
|
chunksize = PQgetlength(res, 0, 2);
|
|
|
|
|
|
|
|
filenamelen = PQgetlength(res, 0, 0);
|
|
|
|
filename = pg_malloc(filenamelen + 1);
|
|
|
|
memcpy(filename, PQgetvalue(res, 0, 0), filenamelen);
|
|
|
|
filename[filenamelen] = '\0';
|
|
|
|
|
|
|
|
chunk = PQgetvalue(res, 0, 2);
|
|
|
|
|
2015-06-28 21:35:51 +03:00
|
|
|
/*
|
2018-03-29 04:00:21 +09:00
|
|
|
* If a file has been deleted on the source, remove it on the target
|
|
|
|
* as well. Note that multiple unlink() calls may happen on the same
|
|
|
|
* file if multiple data chunks are associated with it, hence ignore
|
2020-11-12 14:52:24 +02:00
|
|
|
* unconditionally anything missing.
|
2015-06-28 21:35:51 +03:00
|
|
|
*/
|
|
|
|
if (PQgetisnull(res, 0, 2))
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_debug("received null value for chunk for file \"%s\", file has been deleted",
|
2015-06-28 21:35:51 +03:00
|
|
|
filename);
|
2018-03-29 04:00:21 +09:00
|
|
|
remove_target_file(filename, true);
|
2015-06-28 21:35:51 +03:00
|
|
|
}
|
2020-11-12 14:52:24 +02:00
|
|
|
else
|
|
|
|
{
|
2021-04-19 22:48:13 +02:00
|
|
|
pg_log_debug("received chunk for file \"%s\", offset %lld, size %d",
|
|
|
|
filename, (long long int) chunkoff, chunksize);
|
2020-11-12 14:52:24 +02:00
|
|
|
|
|
|
|
if (strcmp(filename, rq->path) != 0)
|
|
|
|
{
|
|
|
|
pg_fatal("received data for file \"%s\", when requested for \"%s\"",
|
|
|
|
filename, rq->path);
|
|
|
|
}
|
|
|
|
if (chunkoff != rq->offset)
|
2021-04-19 22:48:13 +02:00
|
|
|
pg_fatal("received data at offset %lld of file \"%s\", when requested for offset %lld",
|
|
|
|
(long long int) chunkoff, rq->path, (long long int) rq->offset);
|
2015-06-28 21:35:51 +03:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
/*
|
2021-02-24 16:13:17 +09:00
|
|
|
* We should not receive more data than we requested, or
|
2020-11-12 14:52:24 +02:00
|
|
|
* pg_read_binary_file() messed up. We could receive less,
|
|
|
|
* though, if the file was truncated in the source after we
|
|
|
|
* checked its size. That's OK, there should be a WAL record of
|
|
|
|
* the truncation, which will get replayed when you start the
|
|
|
|
* target system for the first time after pg_rewind has completed.
|
|
|
|
*/
|
|
|
|
if (chunksize > rq->length)
|
|
|
|
pg_fatal("received more than requested for file \"%s\"", rq->path);
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
open_target_file(filename, false);
|
2015-03-23 19:47:52 +02:00
|
|
|
|
2020-11-12 14:52:24 +02:00
|
|
|
write_target_range(chunk, chunkoff, chunksize);
|
|
|
|
}
|
2015-03-29 20:02:14 -04:00
|
|
|
|
|
|
|
pg_free(filename);
|
|
|
|
|
|
|
|
PQclear(res);
|
2020-11-12 14:52:24 +02:00
|
|
|
chunkno++;
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
2020-11-12 14:52:24 +02:00
|
|
|
if (chunkno != src->num_requests)
|
|
|
|
pg_fatal("unexpected number of data chunks received");
|
|
|
|
|
|
|
|
src->num_requests = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Escape a string to be used as element in a text array constant
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
appendArrayEscapedString(StringInfo buf, const char *str)
|
|
|
|
{
|
|
|
|
appendStringInfoCharMacro(buf, '\"');
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
char ch = *str;
|
|
|
|
|
|
|
|
if (ch == '"' || ch == '\\')
|
|
|
|
appendStringInfoCharMacro(buf, '\\');
|
|
|
|
|
|
|
|
appendStringInfoCharMacro(buf, ch);
|
|
|
|
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
appendStringInfoCharMacro(buf, '\"');
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-11-04 11:21:18 +02:00
|
|
|
* Fetch a single file as a malloc'd buffer.
|
2015-03-23 19:47:52 +02:00
|
|
|
*/
|
2020-11-04 11:21:18 +02:00
|
|
|
static char *
|
|
|
|
libpq_fetch_file(rewind_source *source, const char *path, size_t *filesize)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-04 11:21:18 +02:00
|
|
|
PGconn *conn = ((libpq_source *) source)->conn;
|
2015-03-23 19:47:52 +02:00
|
|
|
PGresult *res;
|
|
|
|
char *result;
|
|
|
|
int len;
|
|
|
|
const char *paramValues[1];
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
paramValues[0] = path;
|
2015-03-23 19:47:52 +02:00
|
|
|
res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
|
|
|
|
1, NULL, paramValues, NULL, NULL, 1);
|
|
|
|
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
2015-06-22 20:40:01 -04:00
|
|
|
pg_fatal("could not fetch remote file \"%s\": %s",
|
2020-11-04 11:21:18 +02:00
|
|
|
path, PQresultErrorMessage(res));
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
/* sanity check the result set */
|
|
|
|
if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_fatal("unexpected result set while fetching remote file \"%s\"",
|
2020-11-04 11:21:18 +02:00
|
|
|
path);
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
/* Read result to local variables */
|
|
|
|
len = PQgetlength(res, 0, 0);
|
|
|
|
result = pg_malloc(len + 1);
|
|
|
|
memcpy(result, PQgetvalue(res, 0, 0), len);
|
|
|
|
result[len] = '\0';
|
|
|
|
|
2015-07-27 20:38:44 +03:00
|
|
|
PQclear(res);
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
pg_log_debug("fetched file \"%s\", length %d", path, len);
|
2015-03-23 19:47:52 +02:00
|
|
|
|
|
|
|
if (filesize)
|
|
|
|
*filesize = len;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-11-04 11:21:18 +02:00
|
|
|
* Close a libpq source.
|
2015-03-23 19:47:52 +02:00
|
|
|
*/
|
|
|
|
static void
|
2020-11-04 11:21:18 +02:00
|
|
|
libpq_destroy(rewind_source *source)
|
2015-03-23 19:47:52 +02:00
|
|
|
{
|
2020-11-12 14:52:24 +02:00
|
|
|
libpq_source *src = (libpq_source *) source;
|
|
|
|
|
|
|
|
pfree(src->paths.data);
|
|
|
|
pfree(src->offsets.data);
|
|
|
|
pfree(src->lengths.data);
|
|
|
|
pfree(src);
|
|
|
|
|
2020-11-04 11:21:18 +02:00
|
|
|
/* NOTE: we don't close the connection here, as it was not opened by us. */
|
2015-03-23 19:47:52 +02:00
|
|
|
}
|