2000-07-21 13:43:26 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_backup_null.c
|
|
|
|
*
|
|
|
|
* Implementation of an archive that is never saved; it is used by
|
2005-06-21 22:45:44 +02:00
|
|
|
* pg_dump to output a plain text SQL script instead of saving
|
2000-07-21 13:43:26 +02:00
|
|
|
* a real archive.
|
|
|
|
*
|
|
|
|
* See the headers to pg_restore for more details.
|
|
|
|
*
|
|
|
|
* 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.
|
2000-07-21 13:43:26 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/pg_dump/pg_backup_null.c
|
2001-01-12 05:32:07 +01:00
|
|
|
*
|
2000-07-21 13:43:26 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2014-10-14 20:00:55 +02:00
|
|
|
#include "postgres_fe.h"
|
2000-07-21 13:43:26 +02:00
|
|
|
|
2016-03-24 20:55:44 +01:00
|
|
|
#include "fe_utils/string_utils.h"
|
2005-06-21 22:45:44 +02:00
|
|
|
#include "libpq/libpq-fs.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "pg_backup_archiver.h"
|
|
|
|
#include "pg_backup_utils.h"
|
2005-06-21 22:45:44 +02:00
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
|
|
|
|
static void _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen);
|
2000-07-21 13:43:26 +02:00
|
|
|
static void _EndData(ArchiveHandle *AH, TocEntry *te);
|
|
|
|
static int _WriteByte(ArchiveHandle *AH, const int i);
|
2014-05-06 02:27:16 +02:00
|
|
|
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
|
2016-01-13 23:48:33 +01:00
|
|
|
static void _CloseArchive(ArchiveHandle *AH);
|
|
|
|
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te);
|
2005-06-21 22:45:44 +02:00
|
|
|
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);
|
|
|
|
|
2000-07-21 13:43:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializer
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
InitArchiveFmt_Null(ArchiveHandle *AH)
|
|
|
|
{
|
|
|
|
/* Assuming static functions, this can be copied for each format. */
|
|
|
|
AH->WriteDataPtr = _WriteData;
|
|
|
|
AH->EndDataPtr = _EndData;
|
|
|
|
AH->WriteBytePtr = _WriteByte;
|
|
|
|
AH->WriteBufPtr = _WriteBuf;
|
|
|
|
AH->ClosePtr = _CloseArchive;
|
2009-02-02 21:07:37 +01:00
|
|
|
AH->ReopenPtr = NULL;
|
2000-07-21 13:43:26 +02:00
|
|
|
AH->PrintTocDataPtr = _PrintTocData;
|
|
|
|
|
2005-06-21 22:45:44 +02:00
|
|
|
AH->StartBlobsPtr = _StartBlobs;
|
|
|
|
AH->StartBlobPtr = _StartBlob;
|
|
|
|
AH->EndBlobPtr = _EndBlob;
|
|
|
|
AH->EndBlobsPtr = _EndBlobs;
|
2009-02-02 21:07:37 +01:00
|
|
|
AH->ClonePtr = NULL;
|
|
|
|
AH->DeClonePtr = NULL;
|
2005-06-21 22:45:44 +02:00
|
|
|
|
|
|
|
/* Initialize LO buffering */
|
|
|
|
AH->lo_buf_size = LOBBUFSIZE;
|
2011-11-25 21:40:51 +01:00
|
|
|
AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
|
2005-06-21 22:45:44 +02:00
|
|
|
|
2000-07-21 13:43:26 +02:00
|
|
|
/*
|
|
|
|
* Now prevent reading...
|
|
|
|
*/
|
|
|
|
if (AH->mode == archModeRead)
|
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("this format cannot be read");
|
2000-07-21 13:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* - Start a new TOC entry
|
|
|
|
*/
|
|
|
|
|
2005-06-21 22:45:44 +02:00
|
|
|
/*
|
2000-07-21 13:43:26 +02:00
|
|
|
* Called by dumper via archiver from within a data dump routine
|
|
|
|
*/
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2002-08-20 19:54:45 +02:00
|
|
|
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
|
2000-07-21 13:43:26 +02:00
|
|
|
{
|
2014-05-06 02:27:16 +02:00
|
|
|
/* Just send it to output, ahwrite() already errors on failure */
|
2000-07-21 13:43:26 +02:00
|
|
|
ahwrite(data, 1, dLen, AH);
|
|
|
|
}
|
|
|
|
|
2005-06-21 22:45:44 +02:00
|
|
|
/*
|
|
|
|
* Called by dumper via archiver from within a data dump routine
|
|
|
|
* We substitute this for _WriteData while emitting a BLOB
|
|
|
|
*/
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2005-06-21 22:45:44 +02:00
|
|
|
_WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen)
|
|
|
|
{
|
|
|
|
if (dLen > 0)
|
|
|
|
{
|
2009-08-04 23:56:09 +02:00
|
|
|
PQExpBuffer buf = createPQExpBuffer();
|
2005-06-21 22:45:44 +02:00
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
appendByteaLiteralAHX(buf,
|
|
|
|
(const unsigned char *) data,
|
|
|
|
dLen,
|
|
|
|
AH);
|
2005-06-21 22:45:44 +02:00
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
|
2005-06-21 22:45:44 +02:00
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
destroyPQExpBuffer(buf);
|
2005-06-21 22:45:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-21 13:43:26 +02:00
|
|
|
static void
|
|
|
|
_EndData(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
ahprintf(AH, "\n\n");
|
|
|
|
}
|
|
|
|
|
2005-06-21 22:45:44 +02:00
|
|
|
/*
|
|
|
|
* Called by the archiver when starting to save all BLOB DATA (not schema).
|
|
|
|
* This routine should save whatever format-specific information is needed
|
|
|
|
* to read the BLOBs back into memory.
|
|
|
|
*
|
|
|
|
* It is called just prior to the dumper's DataDumper routine.
|
|
|
|
*
|
|
|
|
* Optional, but strongly recommended.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_StartBlobs(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
ahprintf(AH, "BEGIN;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when the dumper calls StartBlob.
|
|
|
|
*
|
|
|
|
* Mandatory.
|
|
|
|
*
|
|
|
|
* Must save the passed OID for retrieval at restore-time.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
|
|
|
|
{
|
2010-02-18 02:29:10 +01:00
|
|
|
bool old_blob_style = (AH->version < K_VERS_1_12);
|
|
|
|
|
2005-06-21 22:45:44 +02:00
|
|
|
if (oid == 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("invalid OID for large object");
|
2005-06-21 22:45:44 +02:00
|
|
|
|
2010-02-18 02:29:10 +01:00
|
|
|
/* With an old archive we must do drop and create logic here */
|
2016-01-13 23:48:33 +01:00
|
|
|
if (old_blob_style && AH->public.ropt->dropSchema)
|
2009-12-14 01:39:11 +01:00
|
|
|
DropBlobIfExists(AH, oid);
|
2009-07-21 23:46:10 +02:00
|
|
|
|
2010-02-18 02:29:10 +01:00
|
|
|
if (old_blob_style)
|
|
|
|
ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
|
|
|
|
oid, INV_WRITE);
|
|
|
|
else
|
|
|
|
ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
|
|
|
|
oid, INV_WRITE);
|
2005-06-21 22:45:44 +02:00
|
|
|
|
|
|
|
AH->WriteDataPtr = _WriteBlobData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when the dumper calls EndBlob.
|
|
|
|
*
|
|
|
|
* Optional.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
|
|
|
|
{
|
|
|
|
AH->WriteDataPtr = _WriteData;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2009-07-21 23:46:10 +02:00
|
|
|
ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
|
2005-06-21 22:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the archiver when finishing saving all BLOB DATA.
|
|
|
|
*
|
|
|
|
* Optional.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_EndBlobs(ArchiveHandle *AH, TocEntry *te)
|
|
|
|
{
|
|
|
|
ahprintf(AH, "COMMIT;\n\n");
|
|
|
|
}
|
|
|
|
|
2000-07-21 13:43:26 +02:00
|
|
|
/*------
|
|
|
|
* Called as part of a RestoreArchive call; for the NULL archive, this
|
|
|
|
* just sends the data for a given TOC entry to the output.
|
|
|
|
*------
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-13 23:48:33 +01:00
|
|
|
_PrintTocData(ArchiveHandle *AH, TocEntry *te)
|
2000-07-21 13:43:26 +02:00
|
|
|
{
|
2001-03-25 00:11:14 +01:00
|
|
|
if (te->dataDumper)
|
2000-07-21 13:43:26 +02:00
|
|
|
{
|
|
|
|
AH->currToc = te;
|
2005-06-21 22:45:44 +02:00
|
|
|
|
|
|
|
if (strcmp(te->desc, "BLOBS") == 0)
|
|
|
|
_StartBlobs(AH, te);
|
|
|
|
|
2017-09-07 18:06:23 +02:00
|
|
|
te->dataDumper((Archive *) AH, te->dataDumperArg);
|
2005-06-21 22:45:44 +02:00
|
|
|
|
|
|
|
if (strcmp(te->desc, "BLOBS") == 0)
|
|
|
|
_EndBlobs(AH, te);
|
|
|
|
|
2000-07-21 13:43:26 +02:00
|
|
|
AH->currToc = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_WriteByte(ArchiveHandle *AH, const int i)
|
|
|
|
{
|
|
|
|
/* Don't do anything */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2002-08-20 19:54:45 +02:00
|
|
|
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
|
2000-07-21 13:43:26 +02:00
|
|
|
{
|
|
|
|
/* Don't do anything */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-13 23:48:33 +01:00
|
|
|
_CloseArchive(ArchiveHandle *AH)
|
2000-07-21 13:43:26 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|