2011-01-23 22:10:15 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_backup_directory.c
|
|
|
|
*
|
|
|
|
* A directory format dump is a directory, which contains a "toc.dat" file
|
|
|
|
* for the TOC, and a separate file for each data entry, named "<oid>.dat".
|
|
|
|
* Large objects (BLOBs) are stored in separate files named "blob_<uid>.dat",
|
|
|
|
* and there's a plain-text TOC file for them called "blobs.toc". If
|
|
|
|
* compression is used, each data file is individually compressed and the
|
|
|
|
* ".gz" suffix is added to the filenames. The TOC files are never
|
|
|
|
* compressed by pg_dump, however they are accepted with the .gz suffix too,
|
|
|
|
* in case the user has manually compressed them with 'gzip'.
|
|
|
|
*
|
|
|
|
* NOTE: This format is identical to the files written in the tar file in
|
|
|
|
* the 'tar' format, except that we don't write the restore.sql file (TODO),
|
|
|
|
* and the tar format doesn't support compression. Please keep the formats in
|
|
|
|
* sync.
|
|
|
|
*
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
2011-01-23 22:10:15 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
* Portions Copyright (c) 2000, Philip Warner
|
|
|
|
*
|
|
|
|
* Rights are granted to use this software in any way so long
|
|
|
|
* as this notice is not removed.
|
|
|
|
*
|
|
|
|
* The author is not responsible for loss or damages that may
|
2019-05-23 03:17:41 +02:00
|
|
|
* result from its use.
|
2011-01-23 22:10:15 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/bin/pg_dump/pg_backup_directory.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2014-10-14 20:00:55 +02:00
|
|
|
#include "postgres_fe.h"
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
#include "compress_io.h"
|
2013-03-24 16:27:20 +01:00
|
|
|
#include "parallel.h"
|
2014-10-14 20:00:55 +02:00
|
|
|
#include "pg_backup_utils.h"
|
2017-03-22 15:00:30 +01:00
|
|
|
#include "common/file_utils.h"
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2011-01-23 22:44:07 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Our archive location. This is basically what the user specified as his
|
|
|
|
* backup file but of course here it is a directory.
|
|
|
|
*/
|
2011-04-10 17:42:00 +02:00
|
|
|
char *directory;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
cfp *dataFH; /* currently open data file */
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
cfp *blobsTocFH; /* file handle for blobs.toc */
|
2013-03-24 16:27:20 +01:00
|
|
|
ParallelState *pstate; /* for parallel backup / restore */
|
2011-01-23 22:10:15 +01:00
|
|
|
} lclContext;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *filename; /* filename excluding the directory (basename) */
|
|
|
|
} lclTocEntry;
|
|
|
|
|
|
|
|
/* prototypes for private functions */
|
|
|
|
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static void _StartData(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static void _EndData(ArchiveHandle *AH, TocEntry *te);
|
2014-05-06 02:27:16 +02:00
|
|
|
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
|
2011-01-23 22:10:15 +01:00
|
|
|
static int _WriteByte(ArchiveHandle *AH, const int i);
|
|
|
|
static int _ReadByte(ArchiveHandle *);
|
2014-05-06 02:27:16 +02:00
|
|
|
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
|
|
|
|
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
|
2016-01-13 23:48:33 +01:00
|
|
|
static void _CloseArchive(ArchiveHandle *AH);
|
2013-03-24 16:27:20 +01:00
|
|
|
static void _ReopenArchive(ArchiveHandle *AH);
|
2016-01-13 23:48:33 +01:00
|
|
|
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
|
|
|
|
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
|
|
|
|
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
|
|
|
|
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);
|
2016-01-13 23:48:33 +01:00
|
|
|
static void _LoadBlobs(ArchiveHandle *AH);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
Improve parallel scheduling logic in pg_dump/pg_restore.
Previously, the way this worked was that a parallel pg_dump would
re-order the TABLE_DATA items in the dump's TOC into decreasing size
order, and separately re-order (some of) the INDEX items into decreasing
size order. Then pg_dump would dump the items in that order. Later,
parallel pg_restore just followed the TOC order. This method had lots
of deficiencies:
* TOC ordering randomly differed between parallel and non-parallel
dumps, and was hard to predict in the former case, causing problems
for building stable pg_dump test cases.
* Parallel restore only followed a well-chosen order if the dump had
been done in parallel; in particular, this never happened for restore
from custom-format dumps.
* The best order for restore isn't necessarily the same as for dump,
and it's not really static either because of locking considerations.
* TABLE_DATA and INDEX items aren't the only things that might take a lot
of work during restore. Scheduling was particularly stupid for the BLOBS
item, which might require lots of work during dump as well as restore,
but was left to the end in either case.
This patch removes the logic that changed the TOC order, fixing the
test instability problem. Instead, we sort the parallelizable items
just before processing them during a parallel dump. Independently
of that, parallel restore prioritizes the ready-to-execute tasks
based on the size of the underlying table. In the case of dependent
tasks such as index, constraint, or foreign key creation, the largest
relevant table is used as the metric for estimating the task length.
(This is pretty crude, but it should be enough to avoid the case we
want to avoid, which is ending the run with just a few large tasks
such that we can't make use of all N workers.)
Patch by me, responding to a complaint from Peter Eisentraut,
who also reviewed the patch.
Discussion: https://postgr.es/m/5137fe12-d0a2-4971-61b6-eb4e7e8875f8@2ndquadrant.com
2018-09-14 23:31:51 +02:00
|
|
|
static void _PrepParallelRestore(ArchiveHandle *AH);
|
2013-03-24 16:27:20 +01:00
|
|
|
static void _Clone(ArchiveHandle *AH);
|
|
|
|
static void _DeClone(ArchiveHandle *AH);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
static int _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static int _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te);
|
2013-03-24 16:27:20 +01:00
|
|
|
|
|
|
|
static void setFilePath(ArchiveHandle *AH, char *buf,
|
2019-05-22 19:04:48 +02:00
|
|
|
const char *relativeFilename);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Init routine required by ALL formats. This is a global routine
|
|
|
|
* and should be declared in pg_backup_archiver.h
|
|
|
|
*
|
|
|
|
* Its task is to create any extra archive context (using AH->formatData),
|
|
|
|
* and to initialize the supported function pointers.
|
|
|
|
*
|
|
|
|
* It should also prepare whatever its input source is for reading/writing,
|
|
|
|
* and in the case of a read mode connection, it should load the Header & TOC.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
InitArchiveFmt_Directory(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
lclContext *ctx;
|
|
|
|
|
|
|
|
/* Assuming static functions, this can be copied for each format. */
|
|
|
|
AH->ArchiveEntryPtr = _ArchiveEntry;
|
|
|
|
AH->StartDataPtr = _StartData;
|
|
|
|
AH->WriteDataPtr = _WriteData;
|
|
|
|
AH->EndDataPtr = _EndData;
|
|
|
|
AH->WriteBytePtr = _WriteByte;
|
|
|
|
AH->ReadBytePtr = _ReadByte;
|
|
|
|
AH->WriteBufPtr = _WriteBuf;
|
|
|
|
AH->ReadBufPtr = _ReadBuf;
|
|
|
|
AH->ClosePtr = _CloseArchive;
|
2013-03-24 16:27:20 +01:00
|
|
|
AH->ReopenPtr = _ReopenArchive;
|
2011-01-23 22:10:15 +01:00
|
|
|
AH->PrintTocDataPtr = _PrintTocData;
|
|
|
|
AH->ReadExtraTocPtr = _ReadExtraToc;
|
|
|
|
AH->WriteExtraTocPtr = _WriteExtraToc;
|
|
|
|
AH->PrintExtraTocPtr = _PrintExtraToc;
|
|
|
|
|
|
|
|
AH->StartBlobsPtr = _StartBlobs;
|
|
|
|
AH->StartBlobPtr = _StartBlob;
|
|
|
|
AH->EndBlobPtr = _EndBlob;
|
|
|
|
AH->EndBlobsPtr = _EndBlobs;
|
|
|
|
|
Improve parallel scheduling logic in pg_dump/pg_restore.
Previously, the way this worked was that a parallel pg_dump would
re-order the TABLE_DATA items in the dump's TOC into decreasing size
order, and separately re-order (some of) the INDEX items into decreasing
size order. Then pg_dump would dump the items in that order. Later,
parallel pg_restore just followed the TOC order. This method had lots
of deficiencies:
* TOC ordering randomly differed between parallel and non-parallel
dumps, and was hard to predict in the former case, causing problems
for building stable pg_dump test cases.
* Parallel restore only followed a well-chosen order if the dump had
been done in parallel; in particular, this never happened for restore
from custom-format dumps.
* The best order for restore isn't necessarily the same as for dump,
and it's not really static either because of locking considerations.
* TABLE_DATA and INDEX items aren't the only things that might take a lot
of work during restore. Scheduling was particularly stupid for the BLOBS
item, which might require lots of work during dump as well as restore,
but was left to the end in either case.
This patch removes the logic that changed the TOC order, fixing the
test instability problem. Instead, we sort the parallelizable items
just before processing them during a parallel dump. Independently
of that, parallel restore prioritizes the ready-to-execute tasks
based on the size of the underlying table. In the case of dependent
tasks such as index, constraint, or foreign key creation, the largest
relevant table is used as the metric for estimating the task length.
(This is pretty crude, but it should be enough to avoid the case we
want to avoid, which is ending the run with just a few large tasks
such that we can't make use of all N workers.)
Patch by me, responding to a complaint from Peter Eisentraut,
who also reviewed the patch.
Discussion: https://postgr.es/m/5137fe12-d0a2-4971-61b6-eb4e7e8875f8@2ndquadrant.com
2018-09-14 23:31:51 +02:00
|
|
|
AH->PrepParallelRestorePtr = _PrepParallelRestore;
|
2013-03-24 16:27:20 +01:00
|
|
|
AH->ClonePtr = _Clone;
|
|
|
|
AH->DeClonePtr = _DeClone;
|
|
|
|
|
|
|
|
AH->WorkerJobRestorePtr = _WorkerJobRestoreDirectory;
|
|
|
|
AH->WorkerJobDumpPtr = _WorkerJobDumpDirectory;
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
/* Set up our private context */
|
2012-10-02 21:35:10 +02:00
|
|
|
ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
|
2011-01-23 22:10:15 +01:00
|
|
|
AH->formatData = (void *) ctx;
|
|
|
|
|
|
|
|
ctx->dataFH = NULL;
|
|
|
|
ctx->blobsTocFH = NULL;
|
|
|
|
|
|
|
|
/* Initialize LO buffering */
|
|
|
|
AH->lo_buf_size = LOBBUFSIZE;
|
2011-11-25 21:40:51 +01:00
|
|
|
AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now open the TOC file
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!AH->fSpec || strcmp(AH->fSpec, "") == 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
|
|
|
fatal("no output directory specified");
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
ctx->directory = AH->fSpec;
|
|
|
|
|
|
|
|
if (AH->mode == archModeWrite)
|
|
|
|
{
|
2013-03-24 16:27:20 +01:00
|
|
|
struct stat st;
|
|
|
|
bool is_empty = false;
|
|
|
|
|
|
|
|
/* we accept an empty existing directory */
|
|
|
|
if (stat(ctx->directory, &st) == 0 && S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
DIR *dir = opendir(ctx->directory);
|
|
|
|
|
|
|
|
if (dir)
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
|
|
|
|
is_empty = true;
|
2014-03-21 18:45:11 +01:00
|
|
|
while (errno = 0, (d = readdir(dir)))
|
2013-03-24 16:27:20 +01:00
|
|
|
{
|
|
|
|
if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0)
|
|
|
|
{
|
|
|
|
is_empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-21 18:45:11 +01:00
|
|
|
|
|
|
|
if (errno)
|
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
|
|
|
fatal("could not read directory \"%s\": %m",
|
|
|
|
ctx->directory);
|
2014-03-21 18:45:11 +01:00
|
|
|
|
|
|
|
if (closedir(dir))
|
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
|
|
|
fatal("could not close directory \"%s\": %m",
|
|
|
|
ctx->directory);
|
2013-03-24 16:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_empty && mkdir(ctx->directory, 0700) < 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
|
|
|
fatal("could not create directory \"%s\": %m",
|
|
|
|
ctx->directory);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* Read Mode */
|
2013-03-24 16:27:20 +01:00
|
|
|
char fname[MAXPGPATH];
|
2011-01-23 22:10:15 +01:00
|
|
|
cfp *tocFH;
|
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
setFilePath(AH, fname, "toc.dat");
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
tocFH = cfopen_read(fname, PG_BINARY_R);
|
|
|
|
if (tocFH == NULL)
|
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
|
|
|
fatal("could not open input file \"%s\": %m", fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
ctx->dataFH = tocFH;
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* The TOC of a directory format dump shares the format code of the
|
|
|
|
* tar format.
|
2011-01-23 22:10:15 +01:00
|
|
|
*/
|
|
|
|
AH->format = archTar;
|
|
|
|
ReadHead(AH);
|
|
|
|
AH->format = archDirectory;
|
|
|
|
ReadToc(AH);
|
|
|
|
|
|
|
|
/* Nothing else in the file, so close it again... */
|
|
|
|
if (cfclose(tocFH) != 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
|
|
|
fatal("could not close TOC file: %m");
|
2011-01-23 22:10:15 +01:00
|
|
|
ctx->dataFH = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the Archiver when the dumper creates a new TOC entry.
|
|
|
|
*
|
|
|
|
* We determine the filename for this entry.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclTocEntry *tctx;
|
|
|
|
char fn[MAXPGPATH];
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2012-10-02 21:35:10 +02:00
|
|
|
tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
|
Improve parallel scheduling logic in pg_dump/pg_restore.
Previously, the way this worked was that a parallel pg_dump would
re-order the TABLE_DATA items in the dump's TOC into decreasing size
order, and separately re-order (some of) the INDEX items into decreasing
size order. Then pg_dump would dump the items in that order. Later,
parallel pg_restore just followed the TOC order. This method had lots
of deficiencies:
* TOC ordering randomly differed between parallel and non-parallel
dumps, and was hard to predict in the former case, causing problems
for building stable pg_dump test cases.
* Parallel restore only followed a well-chosen order if the dump had
been done in parallel; in particular, this never happened for restore
from custom-format dumps.
* The best order for restore isn't necessarily the same as for dump,
and it's not really static either because of locking considerations.
* TABLE_DATA and INDEX items aren't the only things that might take a lot
of work during restore. Scheduling was particularly stupid for the BLOBS
item, which might require lots of work during dump as well as restore,
but was left to the end in either case.
This patch removes the logic that changed the TOC order, fixing the
test instability problem. Instead, we sort the parallelizable items
just before processing them during a parallel dump. Independently
of that, parallel restore prioritizes the ready-to-execute tasks
based on the size of the underlying table. In the case of dependent
tasks such as index, constraint, or foreign key creation, the largest
relevant table is used as the metric for estimating the task length.
(This is pretty crude, but it should be enough to avoid the case we
want to avoid, which is ending the run with just a few large tasks
such that we can't make use of all N workers.)
Patch by me, responding to a complaint from Peter Eisentraut,
who also reviewed the patch.
Discussion: https://postgr.es/m/5137fe12-d0a2-4971-61b6-eb4e7e8875f8@2ndquadrant.com
2018-09-14 23:31:51 +02:00
|
|
|
if (strcmp(te->desc, "BLOBS") == 0)
|
|
|
|
tctx->filename = pg_strdup("blobs.toc");
|
|
|
|
else if (te->dataDumper)
|
2011-01-23 22:10:15 +01:00
|
|
|
{
|
|
|
|
snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
|
2011-11-25 21:40:51 +01:00
|
|
|
tctx->filename = pg_strdup(fn);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
tctx->filename = NULL;
|
|
|
|
|
|
|
|
te->formatData = (void *) tctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the Archiver to save any extra format-related TOC entry
|
|
|
|
* data.
|
|
|
|
*
|
|
|
|
* Use the Archiver routines to write data - they are non-endian, and
|
|
|
|
* maintain other important file information.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A dumpable object has set tctx->filename, any other object has not.
|
|
|
|
* (see _ArchiveEntry).
|
|
|
|
*/
|
|
|
|
if (tctx->filename)
|
|
|
|
WriteStr(AH, tctx->filename);
|
|
|
|
else
|
|
|
|
WriteStr(AH, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the Archiver to read any extra format-related TOC data.
|
|
|
|
*
|
|
|
|
* Needs to match the order defined in _WriteExtraToc, and should also
|
|
|
|
* use the Archiver input routines.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
|
|
|
|
|
|
|
|
if (tctx == NULL)
|
|
|
|
{
|
2012-10-02 21:35:10 +02:00
|
|
|
tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
|
2011-01-23 22:10:15 +01:00
|
|
|
te->formatData = (void *) tctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
tctx->filename = ReadStr(AH);
|
|
|
|
if (strlen(tctx->filename) == 0)
|
|
|
|
{
|
|
|
|
free(tctx->filename);
|
|
|
|
tctx->filename = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the Archiver when restoring an archive to output a comment
|
|
|
|
* that includes useful information about the TOC entry.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
|
|
|
|
|
|
|
|
if (AH->public.verbose && tctx->filename)
|
|
|
|
ahprintf(AH, "-- File: %s\n", tctx->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when saving TABLE DATA (not schema). This routine
|
|
|
|
* should save whatever format-specific information is needed to read
|
|
|
|
* the archive back.
|
|
|
|
*
|
|
|
|
* It is called just prior to the dumper's 'DataDumper' routine being called.
|
|
|
|
*
|
|
|
|
* We create the data file for writing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_StartData(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
2013-03-24 16:27:20 +01:00
|
|
|
char fname[MAXPGPATH];
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
setFilePath(AH, fname, tctx->filename);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
|
|
|
|
if (ctx->dataFH == NULL)
|
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
|
|
|
fatal("could not open output file \"%s\": %m", fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by archiver when dumper calls WriteData. This routine is
|
|
|
|
* called for both BLOB and TABLE data; it is the responsibility of
|
|
|
|
* the format to manage each kind of data using StartBlob/StartData.
|
|
|
|
*
|
|
|
|
* It should only be called from within a DataDumper routine.
|
|
|
|
*
|
|
|
|
* We write the data to the open data file.
|
|
|
|
*/
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2011-01-23 22:10:15 +01:00
|
|
|
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
|
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
|
|
|
fatal("could not write to output file: %s",
|
2019-05-22 18:55:34 +02:00
|
|
|
get_cfp_error(ctx->dataFH));
|
2017-08-03 00:26:26 +02:00
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
return;
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when a dumper's 'DataDumper' routine has
|
|
|
|
* finished.
|
|
|
|
*
|
|
|
|
* We close the data file.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_EndData(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/* Close the file */
|
|
|
|
cfclose(ctx->dataFH);
|
|
|
|
|
|
|
|
ctx->dataFH = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print data for a given file (can be a BLOB as well)
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-13 23:48:33 +01:00
|
|
|
_PrintFileData(ArchiveHandle *AH, char *filename)
|
2011-01-23 22:10:15 +01:00
|
|
|
{
|
|
|
|
size_t cnt;
|
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
|
|
|
cfp *cfp;
|
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return;
|
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
cfp = cfopen_read(filename, PG_BINARY_R);
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
if (!cfp)
|
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
|
|
|
fatal("could not open input file \"%s\": %m", filename);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2011-11-25 21:40:51 +01:00
|
|
|
buf = pg_malloc(ZLIB_OUT_SIZE);
|
2011-01-23 22:10:15 +01:00
|
|
|
buflen = ZLIB_OUT_SIZE;
|
|
|
|
|
|
|
|
while ((cnt = cfread(buf, buflen, cfp)))
|
2016-05-29 19:18:48 +02:00
|
|
|
{
|
2011-01-23 22:10:15 +01:00
|
|
|
ahwrite(buf, 1, cnt, AH);
|
2016-05-29 19:18:48 +02:00
|
|
|
}
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
free(buf);
|
2014-05-06 18:12:18 +02:00
|
|
|
if (cfclose(cfp) !=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
|
|
|
fatal("could not close data file: %m");
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print data for a given TOC entry
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-13 23:48:33 +01:00
|
|
|
_PrintTocData(ArchiveHandle *AH, TocEntry *te)
|
2011-01-23 22:10:15 +01:00
|
|
|
{
|
|
|
|
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
|
|
|
|
|
|
|
|
if (!tctx->filename)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (strcmp(te->desc, "BLOBS") == 0)
|
2016-01-13 23:48:33 +01:00
|
|
|
_LoadBlobs(AH);
|
2011-01-23 22:10:15 +01:00
|
|
|
else
|
|
|
|
{
|
2013-03-24 16:27:20 +01:00
|
|
|
char fname[MAXPGPATH];
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
setFilePath(AH, fname, tctx->filename);
|
2016-01-13 23:48:33 +01:00
|
|
|
_PrintFileData(AH, fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-13 23:48:33 +01:00
|
|
|
_LoadBlobs(ArchiveHandle *AH)
|
2011-01-23 22:10:15 +01:00
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
Oid oid;
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
2013-03-24 16:27:20 +01:00
|
|
|
char fname[MAXPGPATH];
|
2011-04-10 17:42:00 +02:00
|
|
|
char line[MAXPGPATH];
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
StartRestoreBlobs(AH);
|
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
setFilePath(AH, fname, "blobs.toc");
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
ctx->blobsTocFH = cfopen_read(fname, PG_BINARY_R);
|
|
|
|
|
|
|
|
if (ctx->blobsTocFH == NULL)
|
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
|
|
|
fatal("could not open large object TOC file \"%s\" for input: %m",
|
|
|
|
fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/* Read the blobs TOC file line-by-line, and process each blob */
|
|
|
|
while ((cfgets(ctx->blobsTocFH, line, MAXPGPATH)) != NULL)
|
|
|
|
{
|
|
|
|
char fname[MAXPGPATH];
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
|
2014-02-15 17:50:56 +01:00
|
|
|
/* Can't overflow because line and fname are the same length. */
|
2011-01-23 22:10:15 +01:00
|
|
|
if (sscanf(line, "%u %s\n", &oid, fname) != 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
|
|
|
fatal("invalid line in large object TOC file \"%s\": \"%s\"",
|
2019-05-22 18:55:34 +02:00
|
|
|
fname, line);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2016-01-13 23:48:33 +01:00
|
|
|
StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
|
2011-01-23 22:10:15 +01:00
|
|
|
snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, fname);
|
2016-01-13 23:48:33 +01:00
|
|
|
_PrintFileData(AH, path);
|
2011-01-23 22:10:15 +01:00
|
|
|
EndRestoreBlob(AH, oid);
|
|
|
|
}
|
|
|
|
if (!cfeof(ctx->blobsTocFH))
|
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
|
|
|
fatal("error reading large object TOC file \"%s\"",
|
2019-05-22 18:55:34 +02:00
|
|
|
fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
if (cfclose(ctx->blobsTocFH) != 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
|
|
|
fatal("could not close large object TOC file \"%s\": %m",
|
|
|
|
fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
ctx->blobsTocFH = NULL;
|
|
|
|
|
|
|
|
EndRestoreBlobs(AH);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a byte of data to the archive.
|
|
|
|
* Called by the archiver to do integer & byte output to the archive.
|
|
|
|
* These routines are only used to read & write the headers & TOC.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
_WriteByte(ArchiveHandle *AH, const int i)
|
|
|
|
{
|
|
|
|
unsigned char c = (unsigned char) i;
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
|
|
|
if (cfwrite(&c, 1, ctx->dataFH) != 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
|
|
|
fatal("could not write to output file: %s",
|
2019-05-22 18:55:34 +02:00
|
|
|
get_cfp_error(ctx->dataFH));
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a byte of data from the archive.
|
|
|
|
* Called by the archiver to read bytes & integers from the archive.
|
|
|
|
* These routines are only used to read & write headers & TOC.
|
|
|
|
* EOF should be treated as a fatal error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
_ReadByte(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
return cfgetc(ctx->dataFH);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a buffer of data to the archive.
|
|
|
|
* Called by the archiver to write a block of bytes to the TOC or a data file.
|
|
|
|
*/
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2011-01-23 22:10:15 +01:00
|
|
|
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
if (cfwrite(buf, len, ctx->dataFH) != len)
|
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
|
|
|
fatal("could not write to output file: %s",
|
2019-05-22 18:55:34 +02:00
|
|
|
get_cfp_error(ctx->dataFH));
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
return;
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a block of bytes from the archive.
|
|
|
|
*
|
|
|
|
* Called by the archiver to read a block of bytes from the archive
|
|
|
|
*/
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2011-01-23 22:10:15 +01:00
|
|
|
_ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* If there was an I/O error, we already exited in cfread(), so here we
|
|
|
|
* exit on short reads.
|
2014-05-06 02:27:16 +02:00
|
|
|
*/
|
|
|
|
if (cfread(buf, len, ctx->dataFH) != len)
|
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
|
|
|
fatal("could not read from input file: end of file");
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
return;
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the archive.
|
|
|
|
*
|
|
|
|
* When writing the archive, this is the routine that actually starts
|
|
|
|
* the process of saving it to files. No data should be written prior
|
|
|
|
* to this point, since the user could sort the TOC after creating it.
|
|
|
|
*
|
|
|
|
* If an archive is to be written, this routine must call:
|
|
|
|
* WriteHead to save the archive header
|
|
|
|
* WriteToc to save the TOC entries
|
|
|
|
* WriteDataChunks to save all DATA & BLOBs.
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-13 23:48:33 +01:00
|
|
|
_CloseArchive(ArchiveHandle *AH)
|
2011-01-23 22:10:15 +01:00
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
if (AH->mode == archModeWrite)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
cfp *tocFH;
|
2013-03-24 16:27:20 +01:00
|
|
|
char fname[MAXPGPATH];
|
|
|
|
|
|
|
|
setFilePath(AH, fname, "toc.dat");
|
|
|
|
|
|
|
|
/* this will actually fork the processes for a parallel backup */
|
2016-01-13 23:48:33 +01:00
|
|
|
ctx->pstate = ParallelBackupStart(AH);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/* The TOC is always created uncompressed */
|
|
|
|
tocFH = cfopen_write(fname, PG_BINARY_W, 0);
|
|
|
|
if (tocFH == NULL)
|
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
|
|
|
fatal("could not open output file \"%s\": %m", fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
ctx->dataFH = tocFH;
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
/*
|
|
|
|
* Write 'tar' in the format field of the toc.dat file. The directory
|
|
|
|
* is compatible with 'tar', so there's no point having a different
|
|
|
|
* format code for it.
|
|
|
|
*/
|
|
|
|
AH->format = archTar;
|
|
|
|
WriteHead(AH);
|
|
|
|
AH->format = archDirectory;
|
|
|
|
WriteToc(AH);
|
|
|
|
if (cfclose(tocFH) != 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
|
|
|
fatal("could not close TOC file: %m");
|
2016-01-13 23:48:33 +01:00
|
|
|
WriteDataChunks(AH, ctx->pstate);
|
2013-03-24 16:27:20 +01:00
|
|
|
|
|
|
|
ParallelBackupEnd(AH, ctx->pstate);
|
2017-03-22 15:00:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In directory mode, there is no need to sync all the entries
|
|
|
|
* individually. Just recurse once through all the files generated.
|
|
|
|
*/
|
|
|
|
if (AH->dosync)
|
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
|
|
|
fsync_dir_recurse(ctx->directory);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
AH->FH = NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
/*
|
|
|
|
* Reopen the archive's file handle.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_ReopenArchive(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Our TOC is in memory, our data files are opened by each child anyway as
|
|
|
|
* they are separate. We support reopening the archive by just doing
|
|
|
|
* nothing.
|
|
|
|
*/
|
|
|
|
}
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* BLOB support
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when starting to save all BLOB DATA (not schema).
|
|
|
|
* It is called just prior to the dumper's DataDumper routine.
|
|
|
|
*
|
2011-04-10 17:42:00 +02:00
|
|
|
* We open the large object TOC file here, so that we can append a line to
|
2011-01-23 22:10:15 +01:00
|
|
|
* it for each blob.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_StartBlobs(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
2013-03-24 16:27:20 +01:00
|
|
|
char fname[MAXPGPATH];
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
setFilePath(AH, fname, "blobs.toc");
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/* The blob TOC file is never compressed */
|
|
|
|
ctx->blobsTocFH = cfopen_write(fname, "ab", 0);
|
|
|
|
if (ctx->blobsTocFH == NULL)
|
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
|
|
|
fatal("could not open output file \"%s\": %m", fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when we're about to start dumping a blob.
|
|
|
|
*
|
|
|
|
* We create a file to write the blob to.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
char fname[MAXPGPATH];
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
|
|
|
|
|
|
|
|
ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
|
|
|
|
|
|
|
|
if (ctx->dataFH == NULL)
|
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
|
|
|
fatal("could not open output file \"%s\": %m", fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when the dumper is finished writing a blob.
|
|
|
|
*
|
|
|
|
* We close the blob file and write an entry to the blob TOC file for it.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
char buf[50];
|
|
|
|
int len;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
/* Close the BLOB data file itself */
|
|
|
|
cfclose(ctx->dataFH);
|
|
|
|
ctx->dataFH = NULL;
|
|
|
|
|
|
|
|
/* register the blob in blobs.toc */
|
|
|
|
len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
|
|
|
|
if (cfwrite(buf, len, ctx->blobsTocFH) != len)
|
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
|
|
|
fatal("could not write to blobs TOC file");
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when finishing saving all BLOB DATA.
|
|
|
|
*
|
|
|
|
* We close the blobs TOC file.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_EndBlobs(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
|
|
|
cfclose(ctx->blobsTocFH);
|
|
|
|
ctx->blobsTocFH = NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
/*
|
|
|
|
* Gets a relative file name and prepends the output directory, writing the
|
|
|
|
* result to buf. The caller needs to make sure that buf is MAXPGPATH bytes
|
|
|
|
* big. Can't use a static char[MAXPGPATH] inside the function because we run
|
|
|
|
* multithreaded on Windows.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
|
2011-01-23 22:10:15 +01:00
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
char *dname;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
dname = ctx->directory;
|
|
|
|
|
|
|
|
if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
|
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
|
|
|
fatal("file name too long: \"%s\"", dname);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
strcpy(buf, dname);
|
|
|
|
strcat(buf, "/");
|
|
|
|
strcat(buf, relativeFilename);
|
2013-03-24 16:27:20 +01:00
|
|
|
}
|
|
|
|
|
Improve parallel scheduling logic in pg_dump/pg_restore.
Previously, the way this worked was that a parallel pg_dump would
re-order the TABLE_DATA items in the dump's TOC into decreasing size
order, and separately re-order (some of) the INDEX items into decreasing
size order. Then pg_dump would dump the items in that order. Later,
parallel pg_restore just followed the TOC order. This method had lots
of deficiencies:
* TOC ordering randomly differed between parallel and non-parallel
dumps, and was hard to predict in the former case, causing problems
for building stable pg_dump test cases.
* Parallel restore only followed a well-chosen order if the dump had
been done in parallel; in particular, this never happened for restore
from custom-format dumps.
* The best order for restore isn't necessarily the same as for dump,
and it's not really static either because of locking considerations.
* TABLE_DATA and INDEX items aren't the only things that might take a lot
of work during restore. Scheduling was particularly stupid for the BLOBS
item, which might require lots of work during dump as well as restore,
but was left to the end in either case.
This patch removes the logic that changed the TOC order, fixing the
test instability problem. Instead, we sort the parallelizable items
just before processing them during a parallel dump. Independently
of that, parallel restore prioritizes the ready-to-execute tasks
based on the size of the underlying table. In the case of dependent
tasks such as index, constraint, or foreign key creation, the largest
relevant table is used as the metric for estimating the task length.
(This is pretty crude, but it should be enough to avoid the case we
want to avoid, which is ending the run with just a few large tasks
such that we can't make use of all N workers.)
Patch by me, responding to a complaint from Peter Eisentraut,
who also reviewed the patch.
Discussion: https://postgr.es/m/5137fe12-d0a2-4971-61b6-eb4e7e8875f8@2ndquadrant.com
2018-09-14 23:31:51 +02:00
|
|
|
/*
|
|
|
|
* Prepare for parallel restore.
|
|
|
|
*
|
|
|
|
* The main thing that needs to happen here is to fill in TABLE DATA and BLOBS
|
|
|
|
* TOC entries' dataLength fields with appropriate values to guide the
|
|
|
|
* ordering of restore jobs. The source of said data is format-dependent,
|
|
|
|
* as is the exact meaning of the values.
|
|
|
|
*
|
|
|
|
* A format module might also choose to do other setup here.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_PrepParallelRestore(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
TocEntry *te;
|
|
|
|
|
|
|
|
for (te = AH->toc->next; te != AH->toc; te = te->next)
|
|
|
|
{
|
|
|
|
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
|
|
|
|
char fname[MAXPGPATH];
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A dumpable object has set tctx->filename, any other object has not.
|
|
|
|
* (see _ArchiveEntry).
|
|
|
|
*/
|
|
|
|
if (tctx->filename == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We may ignore items not due to be restored */
|
|
|
|
if ((te->reqs & REQ_DATA) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stat the file and, if successful, put its size in dataLength. When
|
|
|
|
* using compression, the physical file size might not be a very good
|
|
|
|
* guide to the amount of work involved in restoring the file, but we
|
|
|
|
* only need an approximate indicator of that.
|
|
|
|
*/
|
|
|
|
setFilePath(AH, fname, tctx->filename);
|
|
|
|
|
|
|
|
if (stat(fname, &st) == 0)
|
|
|
|
te->dataLength = st.st_size;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It might be compressed */
|
|
|
|
strlcat(fname, ".gz", sizeof(fname));
|
|
|
|
if (stat(fname, &st) == 0)
|
|
|
|
te->dataLength = st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is the BLOBS entry, what we stat'd was blobs.toc, which
|
|
|
|
* most likely is a lot smaller than the actual blob data. We don't
|
|
|
|
* have a cheap way to estimate how much smaller, but fortunately it
|
|
|
|
* doesn't matter too much as long as we get the blobs processed
|
|
|
|
* reasonably early. Arbitrarily scale up by a factor of 1K.
|
|
|
|
*/
|
|
|
|
if (strcmp(te->desc, "BLOBS") == 0)
|
|
|
|
te->dataLength *= 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
/*
|
|
|
|
* Clone format-specific fields during parallel restoration.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_Clone(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
|
|
|
AH->formatData = (lclContext *) pg_malloc(sizeof(lclContext));
|
|
|
|
memcpy(AH->formatData, ctx, sizeof(lclContext));
|
|
|
|
ctx = (lclContext *) AH->formatData;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: we do not make a local lo_buf because we expect at most one BLOBS
|
|
|
|
* entry per archive, so no parallelism is possible. Likewise,
|
|
|
|
* TOC-entry-local state isn't an issue because any one TOC entry is
|
|
|
|
* touched by just one worker child.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We also don't copy the ParallelState pointer (pstate), only the master
|
|
|
|
* process ever writes to it.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_DeClone(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
lclContext *ctx = (lclContext *) AH->formatData;
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
* This function is executed in the child of a parallel backup for a
|
|
|
|
* directory-format archive and dumps the actual data for one TOC entry.
|
2013-03-24 16:27:20 +01:00
|
|
|
*/
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
static int
|
2016-01-13 23:48:33 +01:00
|
|
|
_WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te)
|
2013-03-24 16:27:20 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This function returns void. We either fail and die horribly or
|
|
|
|
* succeed... A failure will be detected by the parent when the child dies
|
|
|
|
* unexpectedly.
|
|
|
|
*/
|
2016-01-13 23:48:33 +01:00
|
|
|
WriteDataChunksForTocEntry(AH, te);
|
2013-03-24 16:27:20 +01:00
|
|
|
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
return 0;
|
2013-03-24 16:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
* This function is executed in the child of a parallel restore from a
|
|
|
|
* directory-format archive and restores the actual data for one TOC entry.
|
2013-03-24 16:27:20 +01:00
|
|
|
*/
|
|
|
|
static int
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
_WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te)
|
2013-03-24 16:27:20 +01:00
|
|
|
{
|
Rationalize parallel dump/restore's handling of worker cmd/status messages.
The existing APIs for creating and parsing command and status messages are
rather messy; for example, archive-format modules have to provide code
for constructing command messages, which is entirely pointless since
the code to read them is hard-wired in WaitForCommands() and hence
no format-specific variation is actually possible. But there's little
foreseeable reason to need format-specific variation anyway.
The situation for status messages is no better; at least those are both
constructed and parsed by format-specific code, but said code is quite
redundant since there's no actual need for format-specific variation.
To add insult to injury, the first API involves returning pointers to
static buffers, which is bad, while the second involves returning pointers
to malloc'd strings, which is safer but randomly inconsistent.
Hence, get rid of the MasterStartParallelItem and MasterEndParallelItem
APIs, and instead write centralized functions that construct and parse
command and status messages. If we ever do need more flexibility, these
functions can be the standard implementations of format-specific
callback methods, but that's a long way off if it ever happens.
Tom Lane, reviewed by Kevin Grittner
Discussion: <17340.1464465717@sss.pgh.pa.us>
2016-09-27 19:56:04 +02:00
|
|
|
return parallel_restore(AH, te);
|
2013-03-24 16:27:20 +01:00
|
|
|
}
|