1 /*-------------------------------------------------------------------------
4 * Utility routines shared by pg_dump and pg_restore
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/bin/pg_dump/pg_backup_utils.c
12 *-------------------------------------------------------------------------
14 #include "postgres_fe.h"
19 #include "pg_backup_utils.h"
21 /* Globals exported by this file */
22 const char *progname
= NULL
;
24 #define MAX_ON_EXIT_NICELY 20
28 on_exit_nicely_callback function
;
30 } on_exit_nicely_list
[MAX_ON_EXIT_NICELY
];
32 static int on_exit_nicely_index
;
35 * Parse a --section=foo command line argument.
37 * Set or update the bitmask in *dumpSections according to arg.
38 * dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
39 * pg_restore so they can know if this has even been called.
42 set_dump_section(const char *arg
, int *dumpSections
)
44 /* if this is the first call, clear all the bits */
45 if (*dumpSections
== DUMP_UNSECTIONED
)
48 if (strcmp(arg
, "pre-data") == 0)
49 *dumpSections
|= DUMP_PRE_DATA
;
50 else if (strcmp(arg
, "data") == 0)
51 *dumpSections
|= DUMP_DATA
;
52 else if (strcmp(arg
, "post-data") == 0)
53 *dumpSections
|= DUMP_POST_DATA
;
56 pg_log_error("unrecognized section name: \"%s\"", arg
);
57 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
63 /* Register a callback to be run when exit_nicely is invoked. */
65 on_exit_nicely(on_exit_nicely_callback function
, void *arg
)
67 if (on_exit_nicely_index
>= MAX_ON_EXIT_NICELY
)
68 pg_fatal("out of on_exit_nicely slots");
69 on_exit_nicely_list
[on_exit_nicely_index
].function
= function
;
70 on_exit_nicely_list
[on_exit_nicely_index
].arg
= arg
;
71 on_exit_nicely_index
++;
75 * Run accumulated on_exit_nicely callbacks in reverse order and then exit
76 * without printing any message.
78 * If running in a parallel worker thread on Windows, we only exit the thread,
79 * not the whole process.
81 * Note that in parallel operation on Windows, the callback(s) will be run
82 * by each thread since the list state is necessarily shared by all threads;
83 * each callback must contain logic to ensure it does only what's appropriate
84 * for its thread. On Unix, callbacks are also run by each process, but only
85 * for callbacks established before we fork off the child processes. (It'd
86 * be cleaner to reset the list after fork(), and let each child establish
87 * its own callbacks; but then the behavior would be completely inconsistent
88 * between Windows and Unix. For now, just be sure to establish callbacks
89 * before forking to avoid inconsistency.)
96 for (i
= on_exit_nicely_index
- 1; i
>= 0; i
--)
97 on_exit_nicely_list
[i
].function(code
,
98 on_exit_nicely_list
[i
].arg
);
101 if (parallel_init_done
&& GetCurrentThreadId() != mainThreadId
)