Alvaro Herrera wrote:
> Here's the complete patch in case anyone is wondering.
I found out that with just a little bit of extra header hacking, the
whole thing ends up cleaner -- for instance, with the attached patch,
pg_backup_archiver.h is no longer needed by pg_backup_db.h. I also
tweaked things so that no .h file includes postgres_fe.h, but instead
every .c file includes it before including anything else, as is already
customary for postgres.h in backend code.
The main changes herein are:
* some routines in pg_backup_db.c/h had an argument of type
ArchiveHandle. I made them take Archive instead, and cast internally.
This is already done for some other routines.
* also in pg_backup_db.c/h, EndDBCopyMode() had an argument of type
TocEntry, and then it only uses te->tag for an error message. If I
instead pass the te->tag I can remove the TocEntry, and there is no more
need for pg_backup_archiver.h in pg_backup_db.h.
* I moved exit_horribly() from parallel.h to pg_backup_utils.h, where
prototypes for other exit routines such as exit_nicely() are already
located. (The implementation of exit_horribly() is in parallel.c, but
the prototype looks misplaced in parallel.h.)
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c
index 17e9574..8bfc604 100644
--- a/src/bin/pg_dump/common.c
+++ b/src/bin/pg_dump/common.c
@@ -13,8 +13,11 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
+
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
+#include "pg_dump.h"
#include <ctype.h>
diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c
index 2e2a447..2c568cd 100644
--- a/src/bin/pg_dump/compress_io.c
+++ b/src/bin/pg_dump/compress_io.c
@@ -51,10 +51,11 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
#include "compress_io.h"
-#include "pg_backup_utils.h"
#include "parallel.h"
+#include "pg_backup_utils.h"
/*----------------------
* Compressor API
diff --git a/src/bin/pg_dump/compress_io.h b/src/bin/pg_dump/compress_io.h
index 713c78b..5a21450 100644
--- a/src/bin/pg_dump/compress_io.h
+++ b/src/bin/pg_dump/compress_io.h
@@ -15,7 +15,6 @@
#ifndef __COMPRESS_IO__
#define __COMPRESS_IO__
-#include "postgres_fe.h"
#include "pg_backup_archiver.h"
/* Initial buffer sizes used in zlib compression. */
diff --git a/src/bin/pg_dump/dumputils.h b/src/bin/pg_dump/dumputils.h
index 06763ed..688e9ca 100644
--- a/src/bin/pg_dump/dumputils.h
+++ b/src/bin/pg_dump/dumputils.h
@@ -12,7 +12,6 @@
*
*-------------------------------------------------------------------------
*/
-
#ifndef DUMPUTILS_H
#define DUMPUTILS_H
diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c
index ceed115..3b8d584 100644
--- a/src/bin/pg_dump/parallel.c
+++ b/src/bin/pg_dump/parallel.c
@@ -18,8 +18,8 @@
#include "postgres_fe.h"
-#include "pg_backup_utils.h"
#include "parallel.h"
+#include "pg_backup_utils.h"
#ifndef WIN32
#include <sys/types.h>
diff --git a/src/bin/pg_dump/parallel.h b/src/bin/pg_dump/parallel.h
index 744c76b..dd3546f 100644
--- a/src/bin/pg_dump/parallel.h
+++ b/src/bin/pg_dump/parallel.h
@@ -86,8 +86,4 @@ extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate);
extern void checkAborting(ArchiveHandle *AH);
-extern void
-exit_horribly(const char *modulename, const char *fmt,...)
-__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3), noreturn));
-
#endif /* PG_DUMP_PARALLEL_H */
diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
index 37fdd8c..c2ebcd4 100644
--- a/src/bin/pg_dump/pg_backup.h
+++ b/src/bin/pg_dump/pg_backup.h
@@ -23,10 +23,7 @@
#ifndef PG_BACKUP_H
#define PG_BACKUP_H
-#include "postgres_fe.h"
-
#include "dumputils.h"
-
#include "libpq-fe.h"
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 71ac6e5..95cb6e8 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -19,10 +19,12 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
+#include "parallel.h"
+#include "pg_backup_archiver.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
-#include "parallel.h"
#include <ctype.h>
#include <fcntl.h>
@@ -424,7 +426,7 @@ RestoreArchive(Archive *AHX)
if (ropt->single_txn)
{
if (AH->connection)
- StartTransaction(AH);
+ StartTransaction(AHX);
else
ahprintf(AH, "BEGIN;\n\n");
}
@@ -642,7 +644,7 @@ RestoreArchive(Archive *AHX)
if (ropt->single_txn)
{
if (AH->connection)
- CommitTransaction(AH);
+ CommitTransaction(AHX);
else
ahprintf(AH, "COMMIT;\n\n");
}
@@ -823,7 +825,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
* Parallel restore is always talking directly to a
* server, so no need to see if we should issue BEGIN.
*/
- StartTransaction(AH);
+ StartTransaction(&AH->public);
/*
* If the server version is >= 8.4, make sure we issue
@@ -854,12 +856,12 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
*/
if (AH->outputKind == OUTPUT_COPYDATA &&
RestoringToDB(AH))
- EndDBCopyMode(AH, te);
+ EndDBCopyMode(&AH->public, te->tag);
AH->outputKind = OUTPUT_SQLCMDS;
/* close out the transaction started above */
if (is_parallel && te->created)
- CommitTransaction(AH);
+ CommitTransaction(&AH->public);
_enableTriggersIfNecessary(AH, te, ropt);
}
@@ -1155,7 +1157,7 @@ StartRestoreBlobs(ArchiveHandle *AH)
if (!AH->ropt->single_txn)
{
if (AH->connection)
- StartTransaction(AH);
+ StartTransaction(&AH->public);
else
ahprintf(AH, "BEGIN;\n\n");
}
@@ -1172,7 +1174,7 @@ EndRestoreBlobs(ArchiveHandle *AH)
if (!AH->ropt->single_txn)
{
if (AH->connection)
- CommitTransaction(AH);
+ CommitTransaction(&AH->public);
else
ahprintf(AH, "COMMIT;\n\n");
}
@@ -1629,7 +1631,7 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
* connected then send it to the DB.
*/
if (RestoringToDB(AH))
- bytes_written = ExecuteSqlCommandBuf(AH, (const char *) ptr, size * nmemb);
+ bytes_written = ExecuteSqlCommandBuf(&AH->public, (const char *) ptr, size * nmemb);
else
bytes_written = fwrite(ptr, size, nmemb, AH->OF) * size;
}
diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_backup_archiver.h
index 59c7721..144027c 100644
--- a/src/bin/pg_dump/pg_backup_archiver.h
+++ b/src/bin/pg_dump/pg_backup_archiver.h
@@ -21,15 +21,12 @@
*
*-------------------------------------------------------------------------
*/
-
#ifndef __PG_BACKUP_ARCHIVE__
#define __PG_BACKUP_ARCHIVE__
-#include "postgres_fe.h"
#include <time.h>
-#include "pg_dump.h"
#include "pg_backup.h"
#include "libpq-fe.h"
diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c
index 6f29430..ee05380 100644
--- a/src/bin/pg_dump/pg_backup_custom.c
+++ b/src/bin/pg_dump/pg_backup_custom.c
@@ -23,6 +23,7 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
#include "compress_io.h"
#include "parallel.h"
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c
index 85bf92c..07313f4 100644
--- a/src/bin/pg_dump/pg_backup_db.c
+++ b/src/bin/pg_dump/pg_backup_db.c
@@ -9,11 +9,12 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
+#include "dumputils.h"
+#include "pg_backup_archiver.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
-#include "dumputils.h"
-#include "parallel.h"
#include <unistd.h>
#include <ctype.h>
@@ -500,8 +501,10 @@ ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
* Implement ahwrite() for direct-to-DB restore
*/
int
-ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen)
+ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
{
+ ArchiveHandle *AH = (ArchiveHandle *) AHX;
+
if (AH->outputKind == OUTPUT_COPYDATA)
{
/*
@@ -553,8 +556,10 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen)
* Terminate a COPY operation during direct-to-DB restore
*/
void
-EndDBCopyMode(ArchiveHandle *AH, TocEntry *te)
+EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
{
+ ArchiveHandle *AH = (ArchiveHandle *) AHX;
+
if (AH->pgCopyIn)
{
PGresult *res;
@@ -567,7 +572,7 @@ EndDBCopyMode(ArchiveHandle *AH, TocEntry *te)
res = PQgetResult(AH->connection);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
- te->tag, PQerrorMessage(AH->connection));
+ tocEntryTag, PQerrorMessage(AH->connection));
PQclear(res);
AH->pgCopyIn = false;
@@ -575,14 +580,18 @@ EndDBCopyMode(ArchiveHandle *AH, TocEntry *te)
}
void
-StartTransaction(ArchiveHandle *AH)
+StartTransaction(Archive *AHX)
{
+ ArchiveHandle *AH = (ArchiveHandle *) AHX;
+
ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
}
void
-CommitTransaction(ArchiveHandle *AH)
+CommitTransaction(Archive *AHX)
{
+ ArchiveHandle *AH = (ArchiveHandle *) AHX;
+
ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
}
diff --git a/src/bin/pg_dump/pg_backup_db.h b/src/bin/pg_dump/pg_backup_db.h
index 26eaa4e..6408f14 100644
--- a/src/bin/pg_dump/pg_backup_db.h
+++ b/src/bin/pg_dump/pg_backup_db.h
@@ -8,17 +8,18 @@
#ifndef PG_BACKUP_DB_H
#define PG_BACKUP_DB_H
-#include "pg_backup_archiver.h"
+#include "pg_backup.h"
-extern int ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen);
+
+extern int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen);
extern void ExecuteSqlStatement(Archive *AHX, const char *query);
extern PGresult *ExecuteSqlQuery(Archive *AHX, const char *query,
ExecStatusType status);
-extern void EndDBCopyMode(ArchiveHandle *AH, TocEntry *te);
+extern void EndDBCopyMode(Archive *AHX, const char *tocEntryTag);
-extern void StartTransaction(ArchiveHandle *AH);
-extern void CommitTransaction(ArchiveHandle *AH);
+extern void StartTransaction(Archive *AHX);
+extern void CommitTransaction(Archive *AHX);
#endif
diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_backup_directory.c
index 01b0e97..4c8cd40 100644
--- a/src/bin/pg_dump/pg_backup_directory.c
+++ b/src/bin/pg_dump/pg_backup_directory.c
@@ -32,10 +32,11 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
#include "compress_io.h"
-#include "pg_backup_utils.h"
#include "parallel.h"
+#include "pg_backup_utils.h"
#include <dirent.h>
#include <sys/stat.h>
diff --git a/src/bin/pg_dump/pg_backup_null.c b/src/bin/pg_dump/pg_backup_null.c
index 430ad41..716cb18 100644
--- a/src/bin/pg_dump/pg_backup_null.c
+++ b/src/bin/pg_dump/pg_backup_null.c
@@ -21,12 +21,10 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
-#include "parallel.h"
-
-#include <unistd.h> /* for dup */
#include "libpq/libpq-fs.h"
diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c
index 1c5056c..f48d369 100644
--- a/src/bin/pg_dump/pg_backup_tar.c
+++ b/src/bin/pg_dump/pg_backup_tar.c
@@ -27,12 +27,11 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
-#include "pg_backup.h"
#include "pg_backup_archiver.h"
#include "pg_backup_tar.h"
#include "pg_backup_utils.h"
-#include "parallel.h"
#include "pgtar.h"
#include <sys/stat.h>
diff --git a/src/bin/pg_dump/pg_backup_utils.c b/src/bin/pg_dump/pg_backup_utils.c
index a9a0396..0d7c6db 100644
--- a/src/bin/pg_dump/pg_backup_utils.c
+++ b/src/bin/pg_dump/pg_backup_utils.c
@@ -14,7 +14,6 @@
#include "postgres_fe.h"
#include "pg_backup_utils.h"
-#include "parallel.h"
/* Globals exported by this file */
const char *progname = NULL;
diff --git a/src/bin/pg_dump/pg_backup_utils.h b/src/bin/pg_dump/pg_backup_utils.h
index 9af6d6f..22903a4 100644
--- a/src/bin/pg_dump/pg_backup_utils.h
+++ b/src/bin/pg_dump/pg_backup_utils.h
@@ -37,4 +37,8 @@ __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
extern void on_exit_nicely(on_exit_nicely_callback function, void *arg);
extern void exit_nicely(int code) __attribute__((noreturn));
+extern void
+exit_horribly(const char *modulename, const char *fmt,...)
+__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3), noreturn));
+
#endif /* PG_BACKUP_UTILS_H */
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 53a1254..66b1407 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -29,7 +29,6 @@
*
*-------------------------------------------------------------------------
*/
-
#include "postgres_fe.h"
#include <unistd.h>
@@ -49,7 +48,6 @@
#include "catalog/pg_cast.h"
#include "catalog/pg_class.h"
#include "catalog/pg_default_acl.h"
-#include "catalog/pg_event_trigger.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "catalog/pg_proc.h"
@@ -57,11 +55,11 @@
#include "catalog/pg_type.h"
#include "libpq/libpq-fs.h"
-#include "pg_backup_archiver.h"
-#include "pg_backup_db.h"
-#include "pg_backup_utils.h"
#include "dumputils.h"
#include "parallel.h"
+#include "pg_backup_db.h"
+#include "pg_backup_utils.h"
+#include "pg_dump.h"
typedef struct
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index ba25004..10ae87a 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -14,8 +14,6 @@
#ifndef PG_DUMP_H
#define PG_DUMP_H
-#include "postgres_fe.h"
-
#include "pg_backup.h"
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index 90aedee..030bccc 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -13,9 +13,11 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
+
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
-#include "parallel.h"
+#include "pg_dump.h"
/* translator: this is a module name */
static const char *modulename = gettext_noop("sorter");
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index f954b3c..9c6e533 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -38,12 +38,13 @@
*
*-------------------------------------------------------------------------
*/
+#include "postgres_fe.h"
+
+#include "getopt_long.h"
-#include "pg_backup_archiver.h"
-#include "pg_backup_utils.h"
#include "dumputils.h"
#include "parallel.h"
-#include "getopt_long.h"
+#include "pg_backup_utils.h"
#include <ctype.h>
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers