Skip to content

Commit 0eea804

Browse files
committed
pg_dump: Reduce use of global variables
Most pg_dump.c global variables, which were passed down individually to dumping routines, are now grouped as members of the new DumpOptions struct, which is used as a local variable and passed down into routines that need it. This helps future development efforts; in particular it is said to enable a mode in which a parallel pg_dump run can output multiple streams, and have them restored in parallel. Also take the opportunity to clean up the pg_dump header files somewhat, to avoid circularity. Author: Joachim Wieland, revised by Álvaro Herrera Reviewed by Peter Eisentraut
1 parent e0d97d7 commit 0eea804

22 files changed

+762
-679
lines changed

src/bin/pg_dump/common.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
16+
#include "postgres_fe.h"
17+
1618
#include "pg_backup_archiver.h"
1719
#include "pg_backup_utils.h"
20+
#include "pg_dump.h"
1821

1922
#include <ctype.h>
2023

@@ -64,7 +67,7 @@ static DumpableObject **nspinfoindex;
6467

6568
static void flagInhTables(TableInfo *tbinfo, int numTables,
6669
InhInfo *inhinfo, int numInherits);
67-
static void flagInhAttrs(TableInfo *tblinfo, int numTables);
70+
static void flagInhAttrs(DumpOptions *dopt, TableInfo *tblinfo, int numTables);
6871
static DumpableObject **buildIndexArray(void *objArray, int numObjs,
6972
Size objSize);
7073
static int DOCatalogIdCompare(const void *p1, const void *p2);
@@ -78,7 +81,7 @@ static int strInArray(const char *pattern, char **arr, int arr_size);
7881
* Collect information about all potentially dumpable objects
7982
*/
8083
TableInfo *
81-
getSchemaData(Archive *fout, int *numTablesPtr)
84+
getSchemaData(Archive *fout, DumpOptions *dopt, int *numTablesPtr)
8285
{
8386
ExtensionInfo *extinfo;
8487
InhInfo *inhinfo;
@@ -114,19 +117,19 @@ getSchemaData(Archive *fout, int *numTablesPtr)
114117
*/
115118
if (g_verbose)
116119
write_msg(NULL, "reading user-defined tables\n");
117-
tblinfo = getTables(fout, &numTables);
120+
tblinfo = getTables(fout, dopt, &numTables);
118121
tblinfoindex = buildIndexArray(tblinfo, numTables, sizeof(TableInfo));
119122

120123
/* Do this after we've built tblinfoindex */
121124
getOwnedSeqs(fout, tblinfo, numTables);
122125

123126
if (g_verbose)
124127
write_msg(NULL, "reading extensions\n");
125-
extinfo = getExtensions(fout, &numExtensions);
128+
extinfo = getExtensions(fout, dopt, &numExtensions);
126129

127130
if (g_verbose)
128131
write_msg(NULL, "reading user-defined functions\n");
129-
funinfo = getFuncs(fout, &numFuncs);
132+
funinfo = getFuncs(fout, dopt, &numFuncs);
130133
funinfoindex = buildIndexArray(funinfo, numFuncs, sizeof(FuncInfo));
131134

132135
/* this must be after getTables and getFuncs */
@@ -142,7 +145,7 @@ getSchemaData(Archive *fout, int *numTablesPtr)
142145

143146
if (g_verbose)
144147
write_msg(NULL, "reading user-defined aggregate functions\n");
145-
getAggregates(fout, &numAggregates);
148+
getAggregates(fout, dopt, &numAggregates);
146149

147150
if (g_verbose)
148151
write_msg(NULL, "reading user-defined operators\n");
@@ -183,7 +186,7 @@ getSchemaData(Archive *fout, int *numTablesPtr)
183186

184187
if (g_verbose)
185188
write_msg(NULL, "reading default privileges\n");
186-
getDefaultACLs(fout, &numDefaultACLs);
189+
getDefaultACLs(fout, dopt, &numDefaultACLs);
187190

188191
if (g_verbose)
189192
write_msg(NULL, "reading user-defined collations\n");
@@ -213,7 +216,7 @@ getSchemaData(Archive *fout, int *numTablesPtr)
213216
*/
214217
if (g_verbose)
215218
write_msg(NULL, "finding extension members\n");
216-
getExtensionMembership(fout, extinfo, numExtensions);
219+
getExtensionMembership(fout, dopt, extinfo, numExtensions);
217220

218221
/* Link tables to parents, mark parents of target tables interesting */
219222
if (g_verbose)
@@ -222,11 +225,11 @@ getSchemaData(Archive *fout, int *numTablesPtr)
222225

223226
if (g_verbose)
224227
write_msg(NULL, "reading column info for interesting tables\n");
225-
getTableAttrs(fout, tblinfo, numTables);
228+
getTableAttrs(fout, dopt, tblinfo, numTables);
226229

227230
if (g_verbose)
228231
write_msg(NULL, "flagging inherited columns in subtables\n");
229-
flagInhAttrs(tblinfo, numTables);
232+
flagInhAttrs(dopt, tblinfo, numTables);
230233

231234
if (g_verbose)
232235
write_msg(NULL, "reading indexes\n");
@@ -307,7 +310,7 @@ flagInhTables(TableInfo *tblinfo, int numTables,
307310
* modifies tblinfo
308311
*/
309312
static void
310-
flagInhAttrs(TableInfo *tblinfo, int numTables)
313+
flagInhAttrs(DumpOptions *dopt, TableInfo *tblinfo, int numTables)
311314
{
312315
int i,
313316
j,
@@ -384,7 +387,7 @@ flagInhAttrs(TableInfo *tblinfo, int numTables)
384387
attrDef->adef_expr = pg_strdup("NULL");
385388

386389
/* Will column be dumped explicitly? */
387-
if (shouldPrintColumn(tbinfo, j))
390+
if (shouldPrintColumn(dopt, tbinfo, j))
388391
{
389392
attrDef->separate = false;
390393
/* No dependency needed: NULL cannot have dependencies */

src/bin/pg_dump/compress_io.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@
5151
*
5252
*-------------------------------------------------------------------------
5353
*/
54+
#include "postgres_fe.h"
5455

5556
#include "compress_io.h"
56-
#include "pg_backup_utils.h"
5757
#include "parallel.h"
58+
#include "pg_backup_utils.h"
5859

5960
/*----------------------
6061
* Compressor API

src/bin/pg_dump/compress_io.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#ifndef __COMPRESS_IO__
1616
#define __COMPRESS_IO__
1717

18-
#include "postgres_fe.h"
1918
#include "pg_backup_archiver.h"
2019

2120
/* Initial buffer sizes used in zlib compression. */

src/bin/pg_dump/dumputils.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,29 @@
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
15-
1615
#ifndef DUMPUTILS_H
1716
#define DUMPUTILS_H
1817

1918
#include "libpq-fe.h"
2019
#include "pqexpbuffer.h"
2120

21+
/*
22+
* Data structures for simple lists of OIDs and strings. The support for
23+
* these is very primitive compared to the backend's List facilities, but
24+
* it's all we need in pg_dump.
25+
*/
26+
typedef struct SimpleOidListCell
27+
{
28+
struct SimpleOidListCell *next;
29+
Oid val;
30+
} SimpleOidListCell;
31+
32+
typedef struct SimpleOidList
33+
{
34+
SimpleOidListCell *head;
35+
SimpleOidListCell *tail;
36+
} SimpleOidList;
37+
2238
typedef struct SimpleStringListCell
2339
{
2440
struct SimpleStringListCell *next;
@@ -31,6 +47,7 @@ typedef struct SimpleStringList
3147
SimpleStringListCell *tail;
3248
} SimpleStringList;
3349

50+
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
3451

3552
extern int quote_all_identifiers;
3653
extern PQExpBuffer (*getLocalPQExpBuffer) (void);

src/bin/pg_dump/parallel.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
#include "postgres_fe.h"
2020

21-
#include "pg_backup_utils.h"
2221
#include "parallel.h"
22+
#include "pg_backup_utils.h"
2323

2424
#ifndef WIN32
2525
#include <sys/types.h>
@@ -89,11 +89,12 @@ static void WaitForTerminatingWorkers(ParallelState *pstate);
8989
static void sigTermHandler(int signum);
9090
#endif
9191
static void SetupWorker(ArchiveHandle *AH, int pipefd[2], int worker,
92+
DumpOptions *dopt,
9293
RestoreOptions *ropt);
9394
static bool HasEveryWorkerTerminated(ParallelState *pstate);
9495

9596
static void lockTableNoWait(ArchiveHandle *AH, TocEntry *te);
96-
static void WaitForCommands(ArchiveHandle *AH, int pipefd[2]);
97+
static void WaitForCommands(ArchiveHandle *AH, DumpOptions *dopt, int pipefd[2]);
9798
static char *getMessageFromMaster(int pipefd[2]);
9899
static void sendMessageToMaster(int pipefd[2], const char *str);
99100
static int select_loop(int maxFd, fd_set *workerset);
@@ -436,6 +437,7 @@ sigTermHandler(int signum)
436437
*/
437438
static void
438439
SetupWorker(ArchiveHandle *AH, int pipefd[2], int worker,
440+
DumpOptions *dopt,
439441
RestoreOptions *ropt)
440442
{
441443
/*
@@ -445,11 +447,11 @@ SetupWorker(ArchiveHandle *AH, int pipefd[2], int worker,
445447
* properly when we shut down. This happens only that way when it is
446448
* brought down because of an error.
447449
*/
448-
(AH->SetupWorkerPtr) ((Archive *) AH, ropt);
450+
(AH->SetupWorkerPtr) ((Archive *) AH, dopt, ropt);
449451

450452
Assert(AH->connection != NULL);
451453

452-
WaitForCommands(AH, pipefd);
454+
WaitForCommands(AH, dopt, pipefd);
453455

454456
closesocket(pipefd[PIPE_READ]);
455457
closesocket(pipefd[PIPE_WRITE]);
@@ -481,7 +483,7 @@ init_spawned_worker_win32(WorkerInfo *wi)
481483
* of threads while it does a fork() on Unix.
482484
*/
483485
ParallelState *
484-
ParallelBackupStart(ArchiveHandle *AH, RestoreOptions *ropt)
486+
ParallelBackupStart(ArchiveHandle *AH, DumpOptions *dopt, RestoreOptions *ropt)
485487
{
486488
ParallelState *pstate;
487489
int i;
@@ -598,7 +600,7 @@ ParallelBackupStart(ArchiveHandle *AH, RestoreOptions *ropt)
598600
closesocket(pstate->parallelSlot[j].pipeWrite);
599601
}
600602

601-
SetupWorker(pstate->parallelSlot[i].args->AH, pipefd, i, ropt);
603+
SetupWorker(pstate->parallelSlot[i].args->AH, pipefd, i, dopt, ropt);
602604

603605
exit(0);
604606
}
@@ -856,7 +858,7 @@ lockTableNoWait(ArchiveHandle *AH, TocEntry *te)
856858
* exit.
857859
*/
858860
static void
859-
WaitForCommands(ArchiveHandle *AH, int pipefd[2])
861+
WaitForCommands(ArchiveHandle *AH, DumpOptions *dopt, int pipefd[2])
860862
{
861863
char *command;
862864
DumpId dumpId;
@@ -896,7 +898,7 @@ WaitForCommands(ArchiveHandle *AH, int pipefd[2])
896898
* The message we return here has been pg_malloc()ed and we are
897899
* responsible for free()ing it.
898900
*/
899-
str = (AH->WorkerJobDumpPtr) (AH, te);
901+
str = (AH->WorkerJobDumpPtr) (AH, dopt, te);
900902
Assert(AH->connection != NULL);
901903
sendMessageToMaster(pipefd, str);
902904
free(str);

src/bin/pg_dump/parallel.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
#ifndef PG_DUMP_PARALLEL_H
2020
#define PG_DUMP_PARALLEL_H
2121

22-
#include "pg_backup_db.h"
23-
24-
struct _archiveHandle;
25-
struct _tocEntry;
22+
#include "pg_backup_archiver.h"
2623

2724
typedef enum
2825
{
@@ -35,8 +32,8 @@ typedef enum
3532
/* Arguments needed for a worker process */
3633
typedef struct ParallelArgs
3734
{
38-
struct _archiveHandle *AH;
39-
struct _tocEntry *te;
35+
ArchiveHandle *AH;
36+
TocEntry *te;
4037
} ParallelArgs;
4138

4239
/* State for each parallel activity slot */
@@ -74,22 +71,19 @@ extern void init_parallel_dump_utils(void);
7471

7572
extern int GetIdleWorker(ParallelState *pstate);
7673
extern bool IsEveryWorkerIdle(ParallelState *pstate);
77-
extern void ListenToWorkers(struct _archiveHandle * AH, ParallelState *pstate, bool do_wait);
74+
extern void ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait);
7875
extern int ReapWorkerStatus(ParallelState *pstate, int *status);
79-
extern void EnsureIdleWorker(struct _archiveHandle * AH, ParallelState *pstate);
80-
extern void EnsureWorkersFinished(struct _archiveHandle * AH, ParallelState *pstate);
76+
extern void EnsureIdleWorker(ArchiveHandle *AH, ParallelState *pstate);
77+
extern void EnsureWorkersFinished(ArchiveHandle *AH, ParallelState *pstate);
8178

82-
extern ParallelState *ParallelBackupStart(struct _archiveHandle * AH,
79+
extern ParallelState *ParallelBackupStart(ArchiveHandle *AH,
80+
DumpOptions *dopt,
8381
RestoreOptions *ropt);
84-
extern void DispatchJobForTocEntry(struct _archiveHandle * AH,
82+
extern void DispatchJobForTocEntry(ArchiveHandle *AH,
8583
ParallelState *pstate,
86-
struct _tocEntry * te, T_Action act);
87-
extern void ParallelBackupEnd(struct _archiveHandle * AH, ParallelState *pstate);
88-
89-
extern void checkAborting(struct _archiveHandle * AH);
84+
TocEntry *te, T_Action act);
85+
extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate);
9086

91-
extern void
92-
exit_horribly(const char *modulename, const char *fmt,...)
93-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3), noreturn));
87+
extern void checkAborting(ArchiveHandle *AH);
9488

9589
#endif /* PG_DUMP_PARALLEL_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy