LCOV - differential code coverage report
Current view: top level - src/bin/pg_dump - pg_restore.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GIC GNC CBC DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 81.7 % 575 470 44 61 6 215 249 11 28
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 9 9 8 1
Baseline: lcov-20260315-024220-baseline Branches: 70.4 % 287 202 32 5 48 16 2 92 92 20 36
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 15 15 15
(7,30] days: 82.0 % 244 200 44 200
(30,360] days: 72.2 % 18 13 5 13
(360..) days: 81.2 % 298 242 56 6 236
Function coverage date bins:
(7,30] days: 100.0 % 6 6 6
(360..) days: 100.0 % 3 3 2 1
Branch coverage date bins:
(1,7] days: 100.0 % 2 2 2
(7,30] days: 73.8 % 122 90 32 90
(30,360] days: 63.2 % 38 24 14 9 15
(360..) days: 68.8 % 125 86 5 34 7 2 77

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_restore.c
                                  4                 :                :  *  pg_restore is an utility extracting postgres database definitions
                                  5                 :                :  *  from a backup archive created by pg_dump/pg_dumpall using the archiver
                                  6                 :                :  *  interface.
                                  7                 :                :  *
                                  8                 :                :  *  pg_restore will read the backup archive and
                                  9                 :                :  *  dump out a script that reproduces
                                 10                 :                :  *  the schema of the database in terms of
                                 11                 :                :  *        user-defined types
                                 12                 :                :  *        user-defined functions
                                 13                 :                :  *        tables
                                 14                 :                :  *        indexes
                                 15                 :                :  *        aggregates
                                 16                 :                :  *        operators
                                 17                 :                :  *        ACL - grant/revoke
                                 18                 :                :  *
                                 19                 :                :  * the output script is SQL that is understood by PostgreSQL
                                 20                 :                :  *
                                 21                 :                :  * Basic process in a restore operation is:
                                 22                 :                :  *
                                 23                 :                :  *  Open the Archive and read the TOC.
                                 24                 :                :  *  Set flags in TOC entries, and *maybe* reorder them.
                                 25                 :                :  *  Generate script to stdout
                                 26                 :                :  *  Exit
                                 27                 :                :  *
                                 28                 :                :  * Copyright (c) 2000, Philip Warner
                                 29                 :                :  *      Rights are granted to use this software in any way so long
                                 30                 :                :  *      as this notice is not removed.
                                 31                 :                :  *
                                 32                 :                :  *  The author is not responsible for loss or damages that may
                                 33                 :                :  *  result from its use.
                                 34                 :                :  *
                                 35                 :                :  *
                                 36                 :                :  * IDENTIFICATION
                                 37                 :                :  *      src/bin/pg_dump/pg_restore.c
                                 38                 :                :  *
                                 39                 :                :  *-------------------------------------------------------------------------
                                 40                 :                :  */
                                 41                 :                : #include "postgres_fe.h"
                                 42                 :                : 
                                 43                 :                : #include <ctype.h>
                                 44                 :                : #include <sys/stat.h>
                                 45                 :                : #ifdef HAVE_TERMIOS_H
                                 46                 :                : #include <termios.h>
                                 47                 :                : #endif
                                 48                 :                : 
                                 49                 :                : #include "common/string.h"
                                 50                 :                : #include "connectdb.h"
                                 51                 :                : #include "dumputils.h"
                                 52                 :                : #include "fe_utils/option_utils.h"
                                 53                 :                : #include "fe_utils/string_utils.h"
                                 54                 :                : #include "filter.h"
                                 55                 :                : #include "getopt_long.h"
                                 56                 :                : #include "parallel.h"
                                 57                 :                : #include "pg_backup_utils.h"
                                 58                 :                : 
                                 59                 :                : static void usage(const char *progname);
                                 60                 :                : static void read_restore_filters(const char *filename, RestoreOptions *opts);
                                 61                 :                : static bool file_exists_in_directory(const char *dir, const char *filename);
                                 62                 :                : static int  restore_one_database(const char *inputFileSpec, RestoreOptions *opts,
                                 63                 :                :                                  int numWorkers, bool append_data);
                                 64                 :                : static int  restore_global_objects(const char *inputFileSpec, RestoreOptions *opts);
                                 65                 :                : 
                                 66                 :                : static int  restore_all_databases(const char *inputFileSpec,
                                 67                 :                :                                   SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers);
                                 68                 :                : static int  get_dbnames_list_to_restore(PGconn *conn,
                                 69                 :                :                                         SimplePtrList *dbname_oid_list,
                                 70                 :                :                                         SimpleStringList db_exclude_patterns);
                                 71                 :                : static int  get_dbname_oid_list_from_mfile(const char *dumpdirpatharg,
                                 72                 :                :                                            SimplePtrList *dbname_oid_list);
                                 73                 :                : 
                                 74                 :                : /*
                                 75                 :                :  * Stores a database OID and the corresponding name.
                                 76                 :                :  */
                                 77                 :                : typedef struct DbOidName
                                 78                 :                : {
                                 79                 :                :     Oid         oid;
                                 80                 :                :     char        str[FLEXIBLE_ARRAY_MEMBER]; /* null-terminated string here */
                                 81                 :                : } DbOidName;
                                 82                 :                : 
                                 83                 :                : 
                                 84                 :                : int
 9124 bruce@momjian.us           85                 :CBC         134 : main(int argc, char **argv)
                                 86                 :                : {
                                 87                 :                :     RestoreOptions *opts;
                                 88                 :                :     int         c;
 4739 andrew@dunslane.net        89                 :            134 :     int         numWorkers = 1;
                                 90                 :                :     char       *inputFileSpec;
  388 jdavis@postgresql.or       91                 :            134 :     bool        data_only = false;
                                 92                 :            134 :     bool        schema_only = false;
   20 andrew@dunslane.net        93                 :GNC         134 :     int         n_errors = 0;
                                 94                 :            134 :     bool        globals_only = false;
                                 95                 :            134 :     SimpleStringList db_exclude_patterns = {NULL, NULL};
                                 96                 :                :     static int  disable_triggers = 0;
                                 97                 :                :     static int  enable_row_security = 0;
                                 98                 :                :     static int  if_exists = 0;
                                 99                 :                :     static int  no_data_for_failed_tables = 0;
                                100                 :                :     static int  outputNoTableAm = 0;
                                101                 :                :     static int  outputNoTablespaces = 0;
                                102                 :                :     static int  use_setsessauth = 0;
                                103                 :                :     static int  no_comments = 0;
                                104                 :                :     static int  no_data = 0;
                                105                 :                :     static int  no_policies = 0;
                                106                 :                :     static int  no_publications = 0;
                                107                 :                :     static int  no_schema = 0;
                                108                 :                :     static int  no_security_labels = 0;
                                109                 :                :     static int  no_statistics = 0;
                                110                 :                :     static int  no_globals = 0;
                                111                 :                :     static int  no_subscriptions = 0;
                                112                 :                :     static int  strict_names = 0;
                                113                 :                :     static int  statistics_only = 0;
                                114                 :                :     static int  with_statistics = 0;
                                115                 :                : 
 8971 peter_e@gmx.net           116                 :CBC         134 :     struct option cmdopts[] = {
                                117                 :                :         {"clean", 0, NULL, 'c'},
                                118                 :                :         {"create", 0, NULL, 'C'},
                                119                 :                :         {"data-only", 0, NULL, 'a'},
                                120                 :                :         {"globals-only", 0, NULL, 'g'},
                                121                 :                :         {"dbname", 1, NULL, 'd'},
                                122                 :                :         {"exit-on-error", 0, NULL, 'e'},
                                123                 :                :         {"exclude-schema", 1, NULL, 'N'},
                                124                 :                :         {"file", 1, NULL, 'f'},
                                125                 :                :         {"format", 1, NULL, 'F'},
                                126                 :                :         {"function", 1, NULL, 'P'},
                                127                 :                :         {"host", 1, NULL, 'h'},
                                128                 :                :         {"index", 1, NULL, 'I'},
                                129                 :                :         {"jobs", 1, NULL, 'j'},
                                130                 :                :         {"list", 0, NULL, 'l'},
                                131                 :                :         {"no-privileges", 0, NULL, 'x'},
                                132                 :                :         {"no-acl", 0, NULL, 'x'},
                                133                 :                :         {"no-owner", 0, NULL, 'O'},
                                134                 :                :         {"no-reconnect", 0, NULL, 'R'},
                                135                 :                :         {"port", 1, NULL, 'p'},
                                136                 :                :         {"no-password", 0, NULL, 'w'},
                                137                 :                :         {"password", 0, NULL, 'W'},
                                138                 :                :         {"schema", 1, NULL, 'n'},
                                139                 :                :         {"schema-only", 0, NULL, 's'},
                                140                 :                :         {"superuser", 1, NULL, 'S'},
                                141                 :                :         {"table", 1, NULL, 't'},
                                142                 :                :         {"trigger", 1, NULL, 'T'},
                                143                 :                :         {"use-list", 1, NULL, 'L'},
                                144                 :                :         {"username", 1, NULL, 'U'},
                                145                 :                :         {"verbose", 0, NULL, 'v'},
                                146                 :                :         {"single-transaction", 0, NULL, '1'},
                                147                 :                : 
                                148                 :                :         /*
                                149                 :                :          * the following options don't have an equivalent short option letter
                                150                 :                :          */
                                151                 :                :         {"disable-triggers", no_argument, &disable_triggers, 1},
                                152                 :                :         {"enable-row-security", no_argument, &enable_row_security, 1},
                                153                 :                :         {"if-exists", no_argument, &if_exists, 1},
                                154                 :                :         {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
                                155                 :                :         {"no-table-access-method", no_argument, &outputNoTableAm, 1},
                                156                 :                :         {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
                                157                 :                :         {"role", required_argument, NULL, 2},
                                158                 :                :         {"section", required_argument, NULL, 3},
                                159                 :                :         {"strict-names", no_argument, &strict_names, 1},
                                160                 :                :         {"transaction-size", required_argument, NULL, 5},
                                161                 :                :         {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
                                162                 :                :         {"no-comments", no_argument, &no_comments, 1},
                                163                 :                :         {"no-data", no_argument, &no_data, 1},
                                164                 :                :         {"no-policies", no_argument, &no_policies, 1},
                                165                 :                :         {"no-publications", no_argument, &no_publications, 1},
                                166                 :                :         {"no-schema", no_argument, &no_schema, 1},
                                167                 :                :         {"no-security-labels", no_argument, &no_security_labels, 1},
                                168                 :                :         {"no-globals", no_argument, &no_globals, 1},
                                169                 :                :         {"no-subscriptions", no_argument, &no_subscriptions, 1},
                                170                 :                :         {"no-statistics", no_argument, &no_statistics, 1},
                                171                 :                :         {"statistics", no_argument, &with_statistics, 1},
                                172                 :                :         {"statistics-only", no_argument, &statistics_only, 1},
                                173                 :                :         {"filter", required_argument, NULL, 4},
                                174                 :                :         {"restrict-key", required_argument, NULL, 6},
                                175                 :                :         {"exclude-database", required_argument, NULL, 7},
                                176                 :                : 
                                177                 :                :         {NULL, 0, NULL, 0}
                                178                 :                :     };
                                179                 :                : 
 2540 peter@eisentraut.org      180                 :            134 :     pg_logging_init(argv[0]);
 2531                           181                 :            134 :     pg_logging_set_level(PG_LOG_WARNING);
 6303 peter_e@gmx.net           182                 :            134 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
                                183                 :                : 
 6213 andrew@dunslane.net       184                 :            134 :     init_parallel_dump_utils();
                                185                 :                : 
 9368 pjw@rhyme.com.au          186                 :            134 :     opts = NewRestoreOptions();
                                187                 :                : 
 8381 bruce@momjian.us          188                 :            134 :     progname = get_progname(argv[0]);
                                189                 :                : 
 9199 peter_e@gmx.net           190         [ +  + ]:            134 :     if (argc > 1)
                                191                 :                :     {
 9124 bruce@momjian.us          192   [ +  +  -  + ]:            133 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
                                193                 :                :         {
 9199 peter_e@gmx.net           194                 :              1 :             usage(progname);
 5141 rhaas@postgresql.org      195                 :              1 :             exit_nicely(0);
                                196                 :                :         }
 9124 bruce@momjian.us          197   [ +  +  +  + ]:            132 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
                                198                 :                :         {
 9199 peter_e@gmx.net           199                 :             19 :             puts("pg_restore (PostgreSQL) " PG_VERSION);
 5141 rhaas@postgresql.org      200                 :             19 :             exit_nicely(0);
                                201                 :                :         }
                                202                 :                :     }
                                203                 :                : 
   20 andrew@dunslane.net       204                 :GNC         637 :     while ((c = getopt_long(argc, argv, "acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
 8469 peter_e@gmx.net           205         [ +  + ]:CBC         637 :                             cmdopts, NULL)) != -1)
                                206                 :                :     {
 9368 pjw@rhyme.com.au          207   [ +  +  +  +  :            529 :         switch (c)
                                     +  +  +  +  +  
                                     +  +  +  -  -  
                                     -  +  -  -  -  
                                     -  +  -  -  +  
                                     +  -  -  -  +  
                                     +  -  +  +  +  
                                           -  +  + ]
                                208                 :                :         {
                                209                 :              5 :             case 'a':           /* Dump data only */
  475 nathan@postgresql.or      210                 :              5 :                 data_only = true;
 9368 pjw@rhyme.com.au          211                 :              5 :                 break;
 7456 bruce@momjian.us          212                 :             21 :             case 'c':           /* clean (i.e., drop) schema prior to create */
 9368 pjw@rhyme.com.au          213                 :             21 :                 opts->dropSchema = 1;
                                214                 :             21 :                 break;
 9357                           215                 :             59 :             case 'C':
 5783 tgl@sss.pgh.pa.us         216                 :             59 :                 opts->createDB = 1;
 9357 pjw@rhyme.com.au          217                 :             59 :                 break;
 9368                           218                 :             43 :             case 'd':
 1998 tgl@sss.pgh.pa.us         219                 :             43 :                 opts->cparams.dbname = pg_strdup(optarg);
 9368 pjw@rhyme.com.au          220                 :             43 :                 break;
 7877 bruce@momjian.us          221                 :             31 :             case 'e':
                                222                 :             31 :                 opts->exit_on_error = true;
                                223                 :             31 :                 break;
 9368 pjw@rhyme.com.au          224                 :             63 :             case 'f':           /* output file name */
 5224 bruce@momjian.us          225                 :             63 :                 opts->filename = pg_strdup(optarg);
 9368 pjw@rhyme.com.au          226                 :             63 :                 break;
                                227                 :             41 :             case 'F':
 9124 bruce@momjian.us          228         [ +  - ]:             41 :                 if (strlen(optarg) != 0)
 5224                           229                 :             41 :                     opts->formatName = pg_strdup(optarg);
 9368 pjw@rhyme.com.au          230                 :             41 :                 break;
   20 andrew@dunslane.net       231                 :GNC          15 :             case 'g':
                                232                 :                :                 /* restore only global sql commands. */
                                233                 :             15 :                 globals_only = true;
                                234                 :             15 :                 break;
 9368 pjw@rhyme.com.au          235                 :CBC          40 :             case 'h':
                                236         [ +  - ]:             40 :                 if (strlen(optarg) != 0)
 1998 tgl@sss.pgh.pa.us         237                 :             40 :                     opts->cparams.pghost = pg_strdup(optarg);
 9368 pjw@rhyme.com.au          238                 :             40 :                 break;
 6204 peter_e@gmx.net           239                 :              9 :             case 'j':           /* number of restore jobs */
 1695 michael@paquier.xyz       240         [ +  + ]:              9 :                 if (!option_parse_int(optarg, "-j/--jobs", 1,
                                241                 :                :                                       PG_MAX_JOBS,
                                242                 :                :                                       &numWorkers))
                                243                 :              1 :                     exit(1);
 6204 peter_e@gmx.net           244                 :              8 :                 break;
                                245                 :                : 
 9140 pjw@rhyme.com.au          246                 :              6 :             case 'l':           /* Dump the TOC summary */
                                247                 :              6 :                 opts->tocSummary = 1;
                                248                 :              6 :                 break;
                                249                 :                : 
 9140 pjw@rhyme.com.au          250                 :GBC           1 :             case 'L':           /* input TOC summary file name */
 5224 bruce@momjian.us          251                 :              1 :                 opts->tocFile = pg_strdup(optarg);
 9140 pjw@rhyme.com.au          252                 :              1 :                 break;
                                253                 :                : 
 7336 bruce@momjian.us          254                 :UBC           0 :             case 'n':           /* Dump data for this schema only */
 4582 heikki.linnakangas@i      255                 :              0 :                 simple_string_list_append(&opts->schemaNames, optarg);
 7336 bruce@momjian.us          256                 :              0 :                 break;
                                257                 :                : 
 3463 peter_e@gmx.net           258                 :              0 :             case 'N':           /* Do not dump data for this schema */
                                259                 :              0 :                 simple_string_list_append(&opts->schemaExcludeNames, optarg);
                                260                 :              0 :                 break;
                                261                 :                : 
 9368 pjw@rhyme.com.au          262                 :              0 :             case 'O':
 9357                           263                 :              0 :                 opts->noOwner = 1;
 9368                           264                 :              0 :                 break;
                                265                 :                : 
 9368 pjw@rhyme.com.au          266                 :CBC          50 :             case 'p':
                                267         [ +  - ]:             50 :                 if (strlen(optarg) != 0)
 1998 tgl@sss.pgh.pa.us         268                 :             50 :                     opts->cparams.pgport = pg_strdup(optarg);
 9368 pjw@rhyme.com.au          269                 :             50 :                 break;
 9357 pjw@rhyme.com.au          270                 :UBC           0 :             case 'R':
                                271                 :                :                 /* no-op, still accepted for backwards compatibility */
                                272                 :              0 :                 break;
 9124 bruce@momjian.us          273                 :              0 :             case 'P':           /* Function */
 9368 pjw@rhyme.com.au          274                 :              0 :                 opts->selTypes = 1;
                                275                 :              0 :                 opts->selFunction = 1;
 4582 heikki.linnakangas@i      276                 :              0 :                 simple_string_list_append(&opts->functionNames, optarg);
 9368 pjw@rhyme.com.au          277                 :              0 :                 break;
 9124 bruce@momjian.us          278                 :              0 :             case 'I':           /* Index */
 9368 pjw@rhyme.com.au          279                 :              0 :                 opts->selTypes = 1;
                                280                 :              0 :                 opts->selIndex = 1;
 4582 heikki.linnakangas@i      281                 :              0 :                 simple_string_list_append(&opts->indexNames, optarg);
 9368 pjw@rhyme.com.au          282                 :              0 :                 break;
 9124 bruce@momjian.us          283                 :              0 :             case 'T':           /* Trigger */
 9368 pjw@rhyme.com.au          284                 :              0 :                 opts->selTypes = 1;
                                285                 :              0 :                 opts->selTrigger = 1;
 4582 heikki.linnakangas@i      286                 :              0 :                 simple_string_list_append(&opts->triggerNames, optarg);
 9368 pjw@rhyme.com.au          287                 :              0 :                 break;
 9368 pjw@rhyme.com.au          288                 :CBC           3 :             case 's':           /* dump schema only */
  475 nathan@postgresql.or      289                 :              3 :                 schema_only = true;
 9368 pjw@rhyme.com.au          290                 :              3 :                 break;
 9357 pjw@rhyme.com.au          291                 :UBC           0 :             case 'S':           /* Superuser username */
                                292         [ #  # ]:              0 :                 if (strlen(optarg) != 0)
 5224 bruce@momjian.us          293                 :              0 :                     opts->superuser = pg_strdup(optarg);
 9357 pjw@rhyme.com.au          294                 :              0 :                 break;
 3909 tgl@sss.pgh.pa.us         295                 :              0 :             case 't':           /* Dump specified table(s) only */
 9368 pjw@rhyme.com.au          296                 :              0 :                 opts->selTypes = 1;
                                297                 :              0 :                 opts->selTable = 1;
 4805 magnus@hagander.net       298                 :              0 :                 simple_string_list_append(&opts->tableNames, optarg);
 9368 pjw@rhyme.com.au          299                 :              0 :                 break;
                                300                 :                : 
 9068 peter_e@gmx.net           301                 :CBC          32 :             case 'U':
 1998 tgl@sss.pgh.pa.us         302                 :             32 :                 opts->cparams.username = pg_strdup(optarg);
 9368 pjw@rhyme.com.au          303                 :             32 :                 break;
                                304                 :                : 
                                305                 :             35 :             case 'v':           /* verbose */
                                306                 :             35 :                 opts->verbose = 1;
 2005 tgl@sss.pgh.pa.us         307                 :             35 :                 pg_logging_increase_verbosity();
 9368 pjw@rhyme.com.au          308                 :             35 :                 break;
                                309                 :                : 
 6226 peter_e@gmx.net           310                 :UBC           0 :             case 'w':
 1998 tgl@sss.pgh.pa.us         311                 :              0 :                 opts->cparams.promptPassword = TRI_NO;
 6226 peter_e@gmx.net           312                 :              0 :                 break;
                                313                 :                : 
 9068                           314                 :              0 :             case 'W':
 1998 tgl@sss.pgh.pa.us         315                 :              0 :                 opts->cparams.promptPassword = TRI_YES;
 9068 peter_e@gmx.net           316                 :              0 :                 break;
                                317                 :                : 
 9368 pjw@rhyme.com.au          318                 :              0 :             case 'x':           /* skip ACL dump */
                                319                 :              0 :                 opts->aclsSkip = 1;
                                320                 :              0 :                 break;
                                321                 :                : 
 6278 tgl@sss.pgh.pa.us         322                 :CBC           3 :             case '1':           /* Restore data in a single transaction */
                                323                 :              3 :                 opts->single_txn = true;
                                324                 :              3 :                 opts->exit_on_error = true;
                                325                 :              3 :                 break;
                                326                 :                : 
 8971 peter_e@gmx.net           327                 :             26 :             case 0:
                                328                 :                : 
                                329                 :                :                 /*
                                330                 :                :                  * This covers the long options without a short equivalent.
                                331                 :                :                  */
                                332                 :             26 :                 break;
                                333                 :                : 
 6278 tgl@sss.pgh.pa.us         334                 :UBC           0 :             case 2:             /* SET ROLE */
 4902 bruce@momjian.us          335                 :              0 :                 opts->use_role = pg_strdup(optarg);
 7336                           336                 :              0 :                 break;
                                337                 :                : 
 5203 andrew@dunslane.net       338                 :GBC           1 :             case 3:             /* section */
 5038 tgl@sss.pgh.pa.us         339                 :              1 :                 set_dump_section(optarg, &(opts->dumpSections));
 5203 andrew@dunslane.net       340                 :              1 :                 break;
                                341                 :                : 
  713 tgl@sss.pgh.pa.us         342                 :CBC          10 :             case 4:             /* filter */
  837 dgustafsson@postgres      343                 :             10 :                 read_restore_filters(optarg, opts);
                                344                 :              6 :                 break;
                                345                 :                : 
  713 tgl@sss.pgh.pa.us         346                 :             31 :             case 5:             /* transaction-size */
                                347         [ -  + ]:             31 :                 if (!option_parse_int(optarg, "--transaction-size",
                                348                 :                :                                       1, INT_MAX,
                                349                 :                :                                       &opts->txn_size))
  713 tgl@sss.pgh.pa.us         350                 :UBC           0 :                     exit(1);
  713 tgl@sss.pgh.pa.us         351                 :CBC          31 :                 opts->exit_on_error = true;
                                352                 :             31 :                 break;
                                353                 :                : 
  216 nathan@postgresql.or      354                 :UBC           0 :             case 6:
                                355                 :              0 :                 opts->restrict_key = pg_strdup(optarg);
                                356                 :              0 :                 break;
                                357                 :                : 
   20 andrew@dunslane.net       358                 :GNC           3 :             case 7:             /* database patterns to skip */
                                359                 :              3 :                 simple_string_list_append(&db_exclude_patterns, optarg);
                                360                 :              3 :                 break;
                                361                 :                : 
 9368 pjw@rhyme.com.au          362                 :CBC           1 :             default:
                                363                 :                :                 /* getopt_long already emitted a complaint */
 1437 tgl@sss.pgh.pa.us         364                 :              1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 5141 rhaas@postgresql.org      365                 :              1 :                 exit_nicely(1);
                                366                 :                :         }
                                367                 :                :     }
                                368                 :                : 
                                369                 :                :     /* Get file name from command line */
 9124 bruce@momjian.us          370         [ +  + ]:            108 :     if (optind < argc)
 5693 tgl@sss.pgh.pa.us         371                 :             96 :         inputFileSpec = argv[optind++];
                                372                 :                :     else
 8182                           373                 :             12 :         inputFileSpec = NULL;
                                374                 :                : 
                                375                 :                :     /* Complain if any arguments remain */
 5693                           376         [ +  + ]:            108 :     if (optind < argc)
                                377                 :                :     {
 2540 peter@eisentraut.org      378                 :              1 :         pg_log_error("too many command-line arguments (first is \"%s\")",
                                379                 :                :                      argv[optind]);
 1437 tgl@sss.pgh.pa.us         380                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 5141 rhaas@postgresql.org      381                 :              1 :         exit_nicely(1);
                                382                 :                :     }
                                383                 :                : 
                                384                 :                :     /* Complain if neither -f nor -d was specified (except if dumping TOC) */
 1998 tgl@sss.pgh.pa.us         385   [ +  +  +  +  :            107 :     if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
                                              +  + ]
 1437                           386                 :              1 :         pg_fatal("one of -d/--dbname and -f/--file must be specified");
                                387                 :                : 
                                388                 :                :     /* --exclude-database and --globals-only are incompatible */
    6 nathan@postgresql.or      389                 :GNC         106 :     check_mut_excl_opts(db_exclude_patterns.head, "--exclude-database",
                                390                 :                :                         globals_only, "-g/--globals-only");
                                391                 :                : 
                                392                 :                :     /* Should get at most one of -d and -f, else user is confused */
                                393                 :            105 :     check_mut_excl_opts(opts->cparams.dbname, "-d/--dbname",
                                394                 :                :                         opts->filename, "-f/--file");
                                395                 :                : 
                                396                 :                :     /* --dbname and --restrict-key are incompatible */
                                397                 :            104 :     check_mut_excl_opts(opts->cparams.dbname, "-d/--dbname",
                                398                 :                :                         opts->restrict_key, "--restrict-key");
                                399                 :                : 
                                400         [ +  + ]:            104 :     if (opts->cparams.dbname)
 8182 tgl@sss.pgh.pa.us         401                 :CBC          41 :         opts->useDB = 1;
                                402                 :                :     else
                                403                 :                :     {
                                404                 :                :         /*
                                405                 :                :          * If you don't provide a restrict key, one will be appointed for you.
                                406                 :                :          */
  216 nathan@postgresql.or      407         [ +  - ]:             63 :         if (!opts->restrict_key)
                                408                 :             63 :             opts->restrict_key = generate_restrict_key();
                                409         [ -  + ]:             63 :         if (!opts->restrict_key)
  216 nathan@postgresql.or      410                 :UBC           0 :             pg_fatal("could not generate restrict key");
  216 nathan@postgresql.or      411         [ -  + ]:CBC          63 :         if (!valid_restrict_key(opts->restrict_key))
  216 nathan@postgresql.or      412                 :UBC           0 :             pg_fatal("invalid restrict key");
                                413                 :                :     }
                                414                 :                : 
                                415                 :                :     /* *-only options are incompatible with each other */
    6 nathan@postgresql.or      416                 :GNC         104 :     check_mut_excl_opts(data_only, "-a/--data-only",
                                417                 :                :                         globals_only, "-g/--globals-only",
                                418                 :                :                         schema_only, "-s/--schema-only",
                                419                 :                :                         statistics_only, "--statistics-only");
                                420                 :                : 
                                421                 :                :     /* --no-* and *-only for same thing are incompatible */
                                422                 :             97 :     check_mut_excl_opts(data_only, "-a/--data-only",
                                423                 :                :                         no_data, "--no-data");
                                424                 :             97 :     check_mut_excl_opts(globals_only, "-g/--globals-only",
                                425                 :                :                         no_globals, "--no-globals");
                                426                 :             96 :     check_mut_excl_opts(schema_only, "-s/--schema-only",
                                427                 :                :                         no_schema, "--no-schema");
                                428                 :             96 :     check_mut_excl_opts(statistics_only, "--statistics-only",
                                429                 :                :                         no_statistics, "--no-statistics");
                                430                 :                : 
                                431                 :                :     /* --statistics and --no-statistics are incompatible */
                                432                 :             96 :     check_mut_excl_opts(with_statistics, "--statistics",
                                433                 :                :                         no_statistics, "--no-statistics");
                                434                 :                : 
                                435                 :                :     /* --statistics is incompatible with *-only (except --statistics-only) */
                                436                 :             96 :     check_mut_excl_opts(with_statistics, "--statistics",
                                437                 :                :                         data_only, "-a/--data-only",
                                438                 :                :                         globals_only, "-g/--globals-only",
                                439                 :                :                         schema_only, "-s/--schema-only");
                                440                 :                : 
                                441                 :                :     /* --clean and --data-only are incompatible */
                                442                 :             95 :     check_mut_excl_opts(opts->dropSchema, "-c/--clean",
                                443                 :                :                         data_only, "-a/--data-only");
                                444                 :                : 
                                445                 :                :     /*
                                446                 :                :      * --globals-only, --single-transaction, and --transaction-size are
                                447                 :                :      * incompatible.
                                448                 :                :      */
                                449                 :             94 :     check_mut_excl_opts(globals_only, "-g/--globals-only",
                                450                 :                :                         opts->single_txn, "-1/--single-transaction",
                                451                 :                :                         opts->txn_size, "--transaction-size");
                                452                 :                : 
                                453                 :                :     /* --exit-on-error and --globals-only are incompatible */
                                454                 :             92 :     check_mut_excl_opts(opts->exit_on_error, "--exit-on-error",
                                455                 :                :                         globals_only, "-g/--globals-only");
                                456                 :                : 
                                457                 :                :     /*
                                458                 :                :      * -C is not compatible with -1, because we can't create a database inside
                                459                 :                :      * a transaction block.
                                460                 :                :      */
                                461                 :             91 :     check_mut_excl_opts(opts->createDB, "-C/--create",
                                462                 :                :                         opts->single_txn, "-1/--single-transaction");
                                463                 :                : 
                                464                 :                :     /* Can't do single-txn mode with multiple connections */
 4739 andrew@dunslane.net       465   [ +  +  +  - ]:CBC          90 :     if (opts->single_txn && numWorkers > 1)
 1437 tgl@sss.pgh.pa.us         466                 :              1 :         pg_fatal("cannot specify both --single-transaction and multiple jobs");
                                467                 :                : 
                                468                 :                :     /*
                                469                 :                :      * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
                                470                 :                :      * "--schema-only --no-schema", will have already caused an error in one
                                471                 :                :      * of the checks above.
                                472                 :                :      */
  355 jdavis@postgresql.or      473   [ +  -  +  +  :             89 :     opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
                                              -  + ]
  225                           474   [ +  -  +  - ]:            178 :                       data_only) && !no_data;
  355                           475   [ +  +  +  +  :             89 :     opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
                                              -  + ]
  225                           476   [ +  -  +  + ]:            178 :                         schema_only) && !no_schema;
  355                           477   [ +  -  +  + ]:             89 :     opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
                                478   [ +  -  +  -  :            178 :                             (statistics_only || with_statistics)) && !no_statistics;
                                        -  +  +  - ]
                                479                 :                : 
 8710 tgl@sss.pgh.pa.us         480                 :             89 :     opts->disable_triggers = disable_triggers;
 4195 sfrost@snowman.net        481                 :             89 :     opts->enable_row_security = enable_row_security;
 7099 peter_e@gmx.net           482                 :             89 :     opts->noDataForFailedTables = no_data_for_failed_tables;
 1518 michael@paquier.xyz       483                 :             89 :     opts->noTableAm = outputNoTableAm;
 6569 tgl@sss.pgh.pa.us         484                 :             89 :     opts->noTablespace = outputNoTablespaces;
                                485                 :             89 :     opts->use_setsessauth = use_setsessauth;
 2971                           486                 :             89 :     opts->no_comments = no_comments;
  364                           487                 :             89 :     opts->no_policies = no_policies;
 3229 peter_e@gmx.net           488                 :             89 :     opts->no_publications = no_publications;
 5414                           489                 :             89 :     opts->no_security_labels = no_security_labels;
 3232                           490                 :             89 :     opts->no_subscriptions = no_subscriptions;
                                491                 :                : 
 4395 alvherre@alvh.no-ip.      492   [ +  +  +  - ]:             89 :     if (if_exists && !opts->dropSchema)
   97 alvherre@kurilemu.de      493                 :              1 :         pg_fatal("option %s requires option %s",
                                494                 :                :                  "--if-exists", "-c/--clean");
 4395 alvherre@alvh.no-ip.      495                 :             88 :     opts->if_exists = if_exists;
 3835 teodor@sigaev.ru          496                 :             88 :     opts->strict_names = strict_names;
                                497                 :                : 
 9124 bruce@momjian.us          498         [ +  + ]:             88 :     if (opts->formatName)
                                499                 :                :     {
  414 tgl@sss.pgh.pa.us         500   [ +  -  +  + ]:             68 :         if (pg_strcasecmp(opts->formatName, "c") == 0 ||
                                501                 :             34 :             pg_strcasecmp(opts->formatName, "custom") == 0)
                                502                 :             22 :             opts->format = archCustom;
                                503   [ +  -  +  + ]:             24 :         else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
                                504                 :             12 :                  pg_strcasecmp(opts->formatName, "directory") == 0)
                                505                 :              9 :             opts->format = archDirectory;
                                506   [ +  -  +  + ]:              6 :         else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
                                507                 :              3 :                  pg_strcasecmp(opts->formatName, "tar") == 0)
                                508                 :              2 :             opts->format = archTar;
                                509   [ +  -  -  + ]:              2 :         else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
                                510                 :              1 :                  pg_strcasecmp(opts->formatName, "plain") == 0)
                                511                 :                :         {
                                512                 :                :             /* recognize this for consistency with pg_dump */
  414 tgl@sss.pgh.pa.us         513                 :UBC           0 :             pg_fatal("archive format \"%s\" is not supported; please use psql",
                                514                 :                :                      opts->formatName);
                                515                 :                :         }
                                516                 :                :         else
  414 tgl@sss.pgh.pa.us         517                 :CBC           1 :             pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
                                518                 :                :                      opts->formatName);
                                519                 :                :     }
                                520                 :                : 
                                521                 :                :     /*
                                522                 :                :      * If toc.glo file is present, then restore all the databases from
                                523                 :                :      * map.dat, but skip restoring those matching --exclude-database patterns.
                                524                 :                :      */
   20 andrew@dunslane.net       525   [ +  -  +  + ]:GNC         174 :     if (inputFileSpec != NULL &&
                                526                 :             87 :         (file_exists_in_directory(inputFileSpec, "toc.glo")))
                                527                 :             11 :     {
                                528                 :                :         char        global_path[MAXPGPATH];
                                529                 :             21 :         RestoreOptions *tmpopts = pg_malloc0_object(RestoreOptions);
                                530                 :                : 
                                531                 :             21 :         opts->format = archUnknown;
                                532                 :                : 
                                533                 :             21 :         memcpy(tmpopts, opts, sizeof(RestoreOptions));
                                534                 :                : 
                                535                 :                :         /*
                                536                 :                :          * Can only use --list or --use-list options with a single database
                                537                 :                :          * dump.
                                538                 :                :          */
                                539         [ +  + ]:             21 :         if (opts->tocSummary)
                                540                 :              1 :             pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
                                541                 :                :                      "-l/--list");
                                542         [ +  + ]:             20 :         if (opts->tocFile)
                                543                 :              1 :             pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
                                544                 :                :                      "-L/--use-list");
                                545                 :                : 
                                546         [ +  + ]:             19 :         if (opts->strict_names)
                                547                 :              1 :             pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
                                548                 :                :                      "--strict-names");
                                549   [ +  +  +  + ]:             18 :         if (globals_only && opts->dropSchema)
                                550                 :              1 :             pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
                                551                 :                :                      "--clean", "-g/--globals-only");
                                552                 :                : 
                                553                 :                :         /*
                                554                 :                :          * For pg_dumpall archives, --clean implies --if-exists since global
                                555                 :                :          * objects may not exist in the target cluster.
                                556                 :                :          */
                                557   [ +  +  +  - ]:             17 :         if (opts->dropSchema && !opts->if_exists)
                                558                 :                :         {
                                559                 :              1 :             opts->if_exists = 1;
                                560                 :              1 :             pg_log_info("--if-exists is implied by --clean for pg_dumpall archives");
                                561                 :                :         }
                                562                 :                : 
                                563         [ +  + ]:             17 :         if (no_schema)
                                564                 :              1 :             pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
                                565                 :                :                      "--no-schema");
                                566                 :                : 
                                567         [ +  + ]:             16 :         if (data_only)
                                568                 :              1 :             pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
                                569                 :                :                      "-a/--data-only");
                                570                 :                : 
                                571         [ +  + ]:             15 :         if (statistics_only)
                                572                 :              1 :             pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
                                573                 :                :                      "--statistics-only");
                                574                 :                : 
                                575         [ +  + ]:             14 :         if (!(opts->dumpSections & DUMP_PRE_DATA))
                                576                 :              1 :             pg_fatal("option %s cannot exclude %s when restoring a pg_dumpall archive",
                                577                 :                :                      "--section", "--pre-data");
                                578                 :                : 
                                579                 :                :         /*
                                580                 :                :          * To restore from a pg_dumpall archive, -C (create database) option
                                581                 :                :          * must be specified unless we are only restoring globals or we are
                                582                 :                :          * skipping globals.
                                583                 :                :          */
   16                           584   [ +  +  +  +  :             13 :         if (!no_globals && !globals_only && opts->createDB != 1)
                                              +  + ]
                                585                 :                :         {
   20                           586                 :              1 :             pg_log_error("option %s must be specified when restoring an archive created by pg_dumpall",
                                587                 :                :                          "-C/--create");
                                588                 :              1 :             pg_log_error_hint("Try \"%s --help\" for more information.", progname);
                                589                 :              1 :             pg_log_error_hint("Individual databases can be restored using their specific archives.");
                                590                 :              1 :             exit_nicely(1);
                                591                 :                :         }
                                592                 :                : 
                                593                 :                :         /*
                                594                 :                :          * Restore global objects, even if --exclude-database results in zero
                                595                 :                :          * databases to process. If 'globals-only' is set, exit immediately.
                                596                 :                :          */
                                597                 :             12 :         snprintf(global_path, MAXPGPATH, "%s/toc.glo", inputFileSpec);
                                598                 :                : 
   16                           599         [ +  + ]:             12 :         if (!no_globals)
                                600                 :             11 :             n_errors = restore_global_objects(global_path, tmpopts);
                                601                 :                :         else
                                602                 :              1 :             pg_log_info("skipping restore of global objects because %s was specified",
                                603                 :                :                         "--no-globals");
                                604                 :                : 
   20                           605         [ +  + ]:             11 :         if (globals_only)
                                606                 :              1 :             pg_log_info("database restoring skipped because option %s was specified",
                                607                 :                :                         "-g/--globals-only");
                                608                 :                :         else
                                609                 :                :         {
                                610                 :                :             /* Now restore all the databases from map.dat */
                                611                 :             10 :             n_errors = n_errors + restore_all_databases(inputFileSpec, db_exclude_patterns,
                                612                 :                :                                                         opts, numWorkers);
                                613                 :                :         }
                                614                 :                : 
                                615                 :                :         /* Free db pattern list. */
                                616                 :             11 :         simple_string_list_destroy(&db_exclude_patterns);
                                617                 :                :     }
                                618                 :                :     else
                                619                 :                :     {
                                620         [ +  + ]:             66 :         if (db_exclude_patterns.head != NULL)
                                621                 :                :         {
                                622                 :              1 :             simple_string_list_destroy(&db_exclude_patterns);
                                623                 :              1 :             pg_fatal("option %s can be used only when restoring an archive created by pg_dumpall",
                                624                 :                :                      "--exclude-database");
                                625                 :                :         }
                                626                 :                : 
                                627         [ +  + ]:             65 :         if (globals_only)
                                628                 :              1 :             pg_fatal("option %s can be used only when restoring an archive created by pg_dumpall",
                                629                 :                :                      "-g/--globals-only");
                                630                 :                : 
                                631                 :                :         /* Process if toc.glo file does not exist. */
                                632                 :             64 :         n_errors = restore_one_database(inputFileSpec, opts, numWorkers, false);
                                633                 :                :     }
                                634                 :                : 
                                635                 :                :     /* Done, print a summary of ignored errors during restore. */
                                636         [ -  + ]:             75 :     if (n_errors)
                                637                 :                :     {
   20 andrew@dunslane.net       638                 :UNC           0 :         pg_log_warning("errors ignored on restore: %d", n_errors);
                                639                 :              0 :         return 1;
                                640                 :                :     }
                                641                 :                : 
   20 andrew@dunslane.net       642                 :GNC          75 :     return 0;
                                643                 :                : }
                                644                 :                : 
                                645                 :                : /*
                                646                 :                :  * restore_global_objects
                                647                 :                :  *
                                648                 :                :  * This restore all global objects.
                                649                 :                :  */
                                650                 :                : static int
                                651                 :             11 : restore_global_objects(const char *inputFileSpec, RestoreOptions *opts)
                                652                 :                : {
                                653                 :                :     Archive    *AH;
                                654                 :             11 :     int         nerror = 0;
                                655                 :                : 
                                656                 :                :     /* Set format as custom so that toc.glo file can be read. */
                                657                 :             11 :     opts->format = archCustom;
                                658                 :             11 :     opts->txn_size = 0;
                                659                 :                : 
                                660                 :             11 :     AH = OpenArchive(inputFileSpec, opts->format);
                                661                 :                : 
                                662                 :             11 :     SetArchiveOptions(AH, NULL, opts);
                                663                 :                : 
                                664                 :             11 :     on_exit_close_archive(AH);
                                665                 :                : 
                                666                 :                :     /* Let the archiver know how noisy to be */
                                667                 :             11 :     AH->verbose = opts->verbose;
                                668                 :                : 
                                669                 :                :     /* Don't output TOC entry comments when restoring globals */
                                670                 :             11 :     ((ArchiveHandle *) AH)->noTocComments = 1;
                                671                 :                : 
                                672                 :             11 :     AH->exit_on_error = false;
                                673                 :                : 
                                674                 :                :     /* Parallel execution is not supported for global object restoration. */
                                675                 :             11 :     AH->numWorkers = 1;
                                676                 :                : 
                                677                 :             11 :     ProcessArchiveRestoreOptions(AH);
                                678                 :             11 :     RestoreArchive(AH, false);
                                679                 :                : 
                                680                 :             10 :     nerror = AH->n_errors;
                                681                 :                : 
                                682                 :                :     /* AH may be freed in CloseArchive? */
                                683                 :             10 :     CloseArchive(AH);
                                684                 :                : 
                                685                 :             10 :     return nerror;
                                686                 :                : }
                                687                 :                : 
                                688                 :                : /*
                                689                 :                :  * restore_one_database
                                690                 :                :  *
                                691                 :                :  * This will restore one database using toc.dat file.
                                692                 :                :  *
                                693                 :                :  * returns the number of errors while doing restore.
                                694                 :                :  */
                                695                 :                : static int
                                696                 :            141 : restore_one_database(const char *inputFileSpec, RestoreOptions *opts,
                                697                 :                :                      int numWorkers, bool append_data)
                                698                 :                : {
                                699                 :                :     Archive    *AH;
                                700                 :                :     int         n_errors;
                                701                 :                : 
 8182 tgl@sss.pgh.pa.us         702                 :CBC         141 :     AH = OpenArchive(inputFileSpec, opts->format);
                                703                 :                : 
 3714                           704                 :            141 :     SetArchiveOptions(AH, NULL, opts);
                                705                 :                : 
                                706                 :                :     /*
                                707                 :                :      * We don't have a connection yet but that doesn't matter. The connection
                                708                 :                :      * is initialized to NULL and if we terminate through exit_nicely() while
                                709                 :                :      * it's still NULL, the cleanup function will just be a no-op. If we are
                                710                 :                :      * restoring multiple databases, then only update AX handle for cleanup as
                                711                 :                :      * the previous entry was already in the array and we had closed previous
                                712                 :                :      * connection, so we can use the same array slot.
                                713                 :                :      */
   20 andrew@dunslane.net       714         [ +  + ]:GNC         141 :     if (!append_data)
                                715                 :             64 :         on_exit_close_archive(AH);
                                716                 :                :     else
                                717                 :             77 :         replace_on_exit_close_archive(AH);
                                718                 :                : 
                                719                 :                :     /* Let the archiver know how noisy to be */
 9368 pjw@rhyme.com.au          720                 :CBC         141 :     AH->verbose = opts->verbose;
                                721                 :                : 
                                722                 :                :     /*
                                723                 :                :      * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
                                724                 :                :      */
 7877 bruce@momjian.us          725                 :            141 :     AH->exit_on_error = opts->exit_on_error;
                                726                 :                : 
 9124                           727         [ -  + ]:            141 :     if (opts->tocFile)
 3714 tgl@sss.pgh.pa.us         728                 :UBC           0 :         SortTocFromFile(AH);
                                729                 :                : 
 4739 andrew@dunslane.net       730                 :CBC         141 :     AH->numWorkers = numWorkers;
                                731                 :                : 
 9124 bruce@momjian.us          732         [ +  + ]:            141 :     if (opts->tocSummary)
 3714 tgl@sss.pgh.pa.us         733                 :              5 :         PrintTOCSummary(AH);
                                734                 :                :     else
                                735                 :                :     {
                                736                 :            136 :         ProcessArchiveRestoreOptions(AH);
   20 andrew@dunslane.net       737                 :GNC         136 :         RestoreArchive(AH, append_data);
                                738                 :                :     }
                                739                 :                : 
                                740                 :            141 :     n_errors = AH->n_errors;
                                741                 :                : 
                                742                 :                :     /* AH may be freed in CloseArchive? */
 3714 tgl@sss.pgh.pa.us         743                 :CBC         141 :     CloseArchive(AH);
                                744                 :                : 
   20 andrew@dunslane.net       745                 :GNC         141 :     return n_errors;
                                746                 :                : }
                                747                 :                : 
                                748                 :                : static void
 9124 bruce@momjian.us          749                 :CBC           1 : usage(const char *progname)
                                750                 :                : {
   20 andrew@dunslane.net       751                 :GNC           1 :     printf(_("%s restores PostgreSQL databases from archives created by pg_dump or pg_dumpall.\n\n"), progname);
 8600 peter_e@gmx.net           752                 :CBC           1 :     printf(_("Usage:\n"));
 8549                           753                 :              1 :     printf(_("  %s [OPTION]... [FILE]\n"), progname);
                                754                 :                : 
                                755                 :              1 :     printf(_("\nGeneral options:\n"));
 7794 bruce@momjian.us          756                 :              1 :     printf(_("  -d, --dbname=NAME        connect to database name\n"));
 2537 alvherre@alvh.no-ip.      757                 :              1 :     printf(_("  -f, --file=FILENAME      output file name (- for stdout)\n"));
 5530 heikki.linnakangas@i      758                 :              1 :     printf(_("  -F, --format=c|d|t       backup file format (should be automatic)\n"));
 8593 bruce@momjian.us          759                 :              1 :     printf(_("  -l, --list               print summarized TOC of the archive\n"));
 8549 peter_e@gmx.net           760                 :              1 :     printf(_("  -v, --verbose            verbose mode\n"));
 5018                           761                 :              1 :     printf(_("  -V, --version            output version information, then exit\n"));
                                762                 :              1 :     printf(_("  -?, --help               show this help, then exit\n"));
                                763                 :                : 
 7823 bruce@momjian.us          764                 :              1 :     printf(_("\nOptions controlling the restore:\n"));
 5049 peter_e@gmx.net           765                 :              1 :     printf(_("  -a, --data-only              restore only the data, no schema\n"));
                                766                 :              1 :     printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
                                767                 :              1 :     printf(_("  -C, --create                 create the target database\n"));
                                768                 :              1 :     printf(_("  -e, --exit-on-error          exit on error, default is to continue\n"));
   20 andrew@dunslane.net       769                 :GNC           1 :     printf(_("  -g, --globals-only           restore only global objects, no databases\n"));
 4222 peter_e@gmx.net           770                 :CBC           1 :     printf(_("  -I, --index=NAME             restore named index\n"));
 5049                           771                 :              1 :     printf(_("  -j, --jobs=NUM               use this many parallel jobs to restore\n"));
                                772                 :              1 :     printf(_("  -L, --use-list=FILENAME      use table of contents from this file for\n"
                                773                 :                :              "                               selecting/ordering output\n"));
 4222                           774                 :              1 :     printf(_("  -n, --schema=NAME            restore only objects in this schema\n"));
 3463                           775                 :              1 :     printf(_("  -N, --exclude-schema=NAME    do not restore objects in this schema\n"));
 5049                           776                 :              1 :     printf(_("  -O, --no-owner               skip restoration of object ownership\n"));
 4222                           777                 :              1 :     printf(_("  -P, --function=NAME(args)    restore named function\n"));
 5049                           778                 :              1 :     printf(_("  -s, --schema-only            restore only the schema, no data\n"));
                                779                 :              1 :     printf(_("  -S, --superuser=NAME         superuser user name to use for disabling triggers\n"));
 3611                           780                 :              1 :     printf(_("  -t, --table=NAME             restore named relation (table, view, etc.)\n"));
 4222                           781                 :              1 :     printf(_("  -T, --trigger=NAME           restore named trigger\n"));
 5049                           782                 :              1 :     printf(_("  -x, --no-privileges          skip restoration of access privileges (grant/revoke)\n"));
                                783                 :              1 :     printf(_("  -1, --single-transaction     restore as a single transaction\n"));
                                784                 :              1 :     printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
 3833                           785                 :              1 :     printf(_("  --enable-row-security        enable row security\n"));
   20 andrew@dunslane.net       786                 :GNC           1 :     printf(_("  --exclude-database=PATTERN   do not restore the specified database(s)\n"));
  837 dgustafsson@postgres      787                 :CBC           1 :     printf(_("  --filter=FILENAME            restore or skip objects based on expressions\n"
                                788                 :                :              "                               in FILENAME\n"));
 4395 alvherre@alvh.no-ip.      789                 :              1 :     printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
  480 bruce@momjian.us          790                 :              1 :     printf(_("  --no-comments                do not restore comment commands\n"));
  388 jdavis@postgresql.or      791                 :              1 :     printf(_("  --no-data                    do not restore data\n"));
 5049 peter_e@gmx.net           792                 :              1 :     printf(_("  --no-data-for-failed-tables  do not restore data of tables that could not be\n"
                                793                 :                :              "                               created\n"));
  363 tgl@sss.pgh.pa.us         794                 :              1 :     printf(_("  --no-policies                do not restore row security policies\n"));
 3229 peter_e@gmx.net           795                 :              1 :     printf(_("  --no-publications            do not restore publications\n"));
  388 jdavis@postgresql.or      796                 :              1 :     printf(_("  --no-schema                  do not restore schema\n"));
 5049 peter_e@gmx.net           797                 :              1 :     printf(_("  --no-security-labels         do not restore security labels\n"));
  388 jdavis@postgresql.or      798                 :              1 :     printf(_("  --no-statistics              do not restore statistics\n"));
 3232 peter_e@gmx.net           799                 :              1 :     printf(_("  --no-subscriptions           do not restore subscriptions\n"));
   16 andrew@dunslane.net       800                 :GNC           1 :     printf(_("  --no-globals                 do not restore global objects (roles and tablespaces)\n"));
 1518 michael@paquier.xyz       801                 :CBC           1 :     printf(_("  --no-table-access-method     do not restore table access methods\n"));
 5049 peter_e@gmx.net           802                 :              1 :     printf(_("  --no-tablespaces             do not restore tablespace assignments\n"));
  216 nathan@postgresql.or      803                 :              1 :     printf(_("  --restrict-key=RESTRICT_KEY  use provided string as psql \\restrict key\n"));
 4222 peter_e@gmx.net           804                 :              1 :     printf(_("  --section=SECTION            restore named section (pre-data, data, or post-data)\n"));
  225 jdavis@postgresql.or      805                 :              1 :     printf(_("  --statistics                 restore the statistics\n"));
  388                           806                 :              1 :     printf(_("  --statistics-only            restore only the statistics, not schema or data\n"));
 3835 teodor@sigaev.ru          807                 :              1 :     printf(_("  --strict-names               require table and/or schema include patterns to\n"
                                808                 :                :              "                               match at least one entity each\n"));
  713 tgl@sss.pgh.pa.us         809                 :              1 :     printf(_("  --transaction-size=N         commit after every N objects\n"));
 6569                           810                 :              1 :     printf(_("  --use-set-session-authorization\n"
                                811                 :                :              "                               use SET SESSION AUTHORIZATION commands instead of\n"
                                812                 :                :              "                               ALTER OWNER commands to set ownership\n"));
                                813                 :                : 
 8549 peter_e@gmx.net           814                 :              1 :     printf(_("\nConnection options:\n"));
 8313 bruce@momjian.us          815                 :              1 :     printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
 8549 peter_e@gmx.net           816                 :              1 :     printf(_("  -p, --port=PORT          database server port number\n"));
                                817                 :              1 :     printf(_("  -U, --username=NAME      connect as specified database user\n"));
 6226                           818                 :              1 :     printf(_("  -w, --no-password        never prompt for password\n"));
 8549                           819                 :              1 :     printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
 5408                           820                 :              1 :     printf(_("  --role=ROLENAME          do SET ROLE before restore\n"));
                                821                 :                : 
 4222                           822                 :              1 :     printf(_("\n"
                                823                 :                :              "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be\n"
                                824                 :                :              "combined and specified multiple times to select multiple objects.\n"));
 8600                           825                 :              1 :     printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
 2207 peter@eisentraut.org      826                 :              1 :     printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                                827                 :              1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
 9368 pjw@rhyme.com.au          828                 :              1 : }
                                829                 :                : 
                                830                 :                : /*
                                831                 :                :  * read_restore_filters - retrieve object identifier patterns from file
                                832                 :                :  *
                                833                 :                :  * Parse the specified filter file for include and exclude patterns, and add
                                834                 :                :  * them to the relevant lists.  If the filename is "-" then filters will be
                                835                 :                :  * read from STDIN rather than a file.
                                836                 :                :  */
                                837                 :                : static void
  837 dgustafsson@postgres      838                 :             10 : read_restore_filters(const char *filename, RestoreOptions *opts)
                                839                 :                : {
                                840                 :                :     FilterStateData fstate;
                                841                 :                :     char       *objname;
                                842                 :                :     FilterCommandType comtype;
                                843                 :                :     FilterObjectType objtype;
                                844                 :                : 
                                845                 :             10 :     filter_init(&fstate, filename, exit_nicely);
                                846                 :                : 
                                847         [ +  + ]:             27 :     while (filter_read_item(&fstate, &objname, &comtype, &objtype))
                                848                 :                :     {
                                849         [ +  + ]:             11 :         if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
                                850                 :                :         {
                                851   [ -  +  +  +  :              8 :             switch (objtype)
                                        +  +  +  - ]
                                852                 :                :             {
  837 dgustafsson@postgres      853                 :UBC           0 :                 case FILTER_OBJECT_TYPE_NONE:
                                854                 :              0 :                     break;
  837 dgustafsson@postgres      855                 :CBC           2 :                 case FILTER_OBJECT_TYPE_TABLE_DATA:
                                856                 :                :                 case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
                                857                 :                :                 case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
                                858                 :                :                 case FILTER_OBJECT_TYPE_DATABASE:
                                859                 :                :                 case FILTER_OBJECT_TYPE_EXTENSION:
                                860                 :                :                 case FILTER_OBJECT_TYPE_FOREIGN_DATA:
  836                           861                 :              2 :                     pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
                                862                 :                :                                         "include",
                                863                 :                :                                         filter_object_type_name(objtype));
  837                           864                 :              2 :                     exit_nicely(1);
                                865                 :                : 
                                866                 :              2 :                 case FILTER_OBJECT_TYPE_FUNCTION:
                                867                 :              2 :                     opts->selTypes = 1;
                                868                 :              2 :                     opts->selFunction = 1;
                                869                 :              2 :                     simple_string_list_append(&opts->functionNames, objname);
                                870                 :              2 :                     break;
                                871                 :              1 :                 case FILTER_OBJECT_TYPE_INDEX:
                                872                 :              1 :                     opts->selTypes = 1;
                                873                 :              1 :                     opts->selIndex = 1;
                                874                 :              1 :                     simple_string_list_append(&opts->indexNames, objname);
                                875                 :              1 :                     break;
                                876                 :              1 :                 case FILTER_OBJECT_TYPE_SCHEMA:
                                877                 :              1 :                     simple_string_list_append(&opts->schemaNames, objname);
                                878                 :              1 :                     break;
                                879                 :              1 :                 case FILTER_OBJECT_TYPE_TABLE:
                                880                 :              1 :                     opts->selTypes = 1;
                                881                 :              1 :                     opts->selTable = 1;
                                882                 :              1 :                     simple_string_list_append(&opts->tableNames, objname);
                                883                 :              1 :                     break;
                                884                 :              1 :                 case FILTER_OBJECT_TYPE_TRIGGER:
                                885                 :              1 :                     opts->selTypes = 1;
                                886                 :              1 :                     opts->selTrigger = 1;
                                887                 :              1 :                     simple_string_list_append(&opts->triggerNames, objname);
                                888                 :              1 :                     break;
                                889                 :                :             }
                                890                 :                :         }
                                891         [ +  - ]:              3 :         else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
                                892                 :                :         {
                                893   [ -  +  +  - ]:              3 :             switch (objtype)
                                894                 :                :             {
  837 dgustafsson@postgres      895                 :UBC           0 :                 case FILTER_OBJECT_TYPE_NONE:
                                896                 :              0 :                     break;
  837 dgustafsson@postgres      897                 :CBC           2 :                 case FILTER_OBJECT_TYPE_TABLE_DATA:
                                898                 :                :                 case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
                                899                 :                :                 case FILTER_OBJECT_TYPE_DATABASE:
                                900                 :                :                 case FILTER_OBJECT_TYPE_EXTENSION:
                                901                 :                :                 case FILTER_OBJECT_TYPE_FOREIGN_DATA:
                                902                 :                :                 case FILTER_OBJECT_TYPE_FUNCTION:
                                903                 :                :                 case FILTER_OBJECT_TYPE_INDEX:
                                904                 :                :                 case FILTER_OBJECT_TYPE_TABLE:
                                905                 :                :                 case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
                                906                 :                :                 case FILTER_OBJECT_TYPE_TRIGGER:
  836                           907                 :              2 :                     pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
                                908                 :                :                                         "exclude",
                                909                 :                :                                         filter_object_type_name(objtype));
  837                           910                 :              2 :                     exit_nicely(1);
                                911                 :                : 
                                912                 :              1 :                 case FILTER_OBJECT_TYPE_SCHEMA:
                                913                 :              1 :                     simple_string_list_append(&opts->schemaExcludeNames, objname);
                                914                 :              1 :                     break;
                                915                 :                :             }
                                916                 :                :         }
                                917                 :                :         else
                                918                 :                :         {
  837 dgustafsson@postgres      919         [ #  # ]:UBC           0 :             Assert(comtype == FILTER_COMMAND_TYPE_NONE);
                                920         [ #  # ]:              0 :             Assert(objtype == FILTER_OBJECT_TYPE_NONE);
                                921                 :                :         }
                                922                 :                : 
  837 dgustafsson@postgres      923         [ -  + ]:CBC           7 :         if (objname)
                                924                 :              7 :             free(objname);
                                925                 :                :     }
                                926                 :                : 
                                927                 :              6 :     filter_free(&fstate);
                                928                 :              6 : }
                                929                 :                : 
                                930                 :                : /*
                                931                 :                :  * file_exists_in_directory
                                932                 :                :  *
                                933                 :                :  * Returns true if the file exists in the given directory.
                                934                 :                :  */
                                935                 :                : static bool
   20 andrew@dunslane.net       936                 :GNC         243 : file_exists_in_directory(const char *dir, const char *filename)
                                937                 :                : {
                                938                 :                :     struct stat st;
                                939                 :                :     char        buf[MAXPGPATH];
                                940                 :                : 
                                941         [ -  + ]:            243 :     if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH)
   20 andrew@dunslane.net       942                 :UNC           0 :         pg_fatal("directory name too long: \"%s\"", dir);
                                943                 :                : 
   20 andrew@dunslane.net       944   [ +  +  +  - ]:GNC         243 :     return (stat(buf, &st) == 0 && S_ISREG(st.st_mode));
                                945                 :                : }
                                946                 :                : 
                                947                 :                : /*
                                948                 :                :  * get_dbnames_list_to_restore
                                949                 :                :  *
                                950                 :                :  * This will mark for skipping any entries from dbname_oid_list that pattern match an
                                951                 :                :  * entry in the db_exclude_patterns list.
                                952                 :                :  *
                                953                 :                :  * Returns the number of database to be restored.
                                954                 :                :  *
                                955                 :                :  */
                                956                 :                : static int
                                957                 :              1 : get_dbnames_list_to_restore(PGconn *conn,
                                958                 :                :                             SimplePtrList *dbname_oid_list,
                                959                 :                :                             SimpleStringList db_exclude_patterns)
                                960                 :                : {
                                961                 :              1 :     int         count_db = 0;
                                962                 :                :     PQExpBuffer query;
                                963                 :                :     PQExpBuffer db_lit;
                                964                 :                :     PGresult   *res;
                                965                 :                : 
                                966                 :              1 :     query = createPQExpBuffer();
                                967                 :              1 :     db_lit = createPQExpBuffer();
                                968                 :                : 
                                969                 :                :     /*
                                970                 :                :      * Process one by one all dbnames and if specified to skip restoring, then
                                971                 :                :      * remove dbname from list.
                                972                 :                :      */
                                973                 :              1 :     for (SimplePtrListCell *db_cell = dbname_oid_list->head;
                                974         [ +  + ]:              7 :          db_cell; db_cell = db_cell->next)
                                975                 :                :     {
                                976                 :              6 :         DbOidName  *dbidname = (DbOidName *) db_cell->ptr;
                                977                 :              6 :         bool        skip_db_restore = false;
                                978                 :                : 
                                979                 :              6 :         resetPQExpBuffer(query);
                                980                 :              6 :         resetPQExpBuffer(db_lit);
                                981                 :                : 
                                982                 :              6 :         appendStringLiteralConn(db_lit, dbidname->str, conn);
                                983                 :                : 
                                984         [ +  + ]:             11 :         for (SimpleStringListCell *pat_cell = db_exclude_patterns.head; pat_cell; pat_cell = pat_cell->next)
                                985                 :                :         {
                                986                 :                :             /*
                                987                 :                :              * If there is an exact match then we don't need to try a pattern
                                988                 :                :              * match
                                989                 :                :              */
                                990         [ +  + ]:              6 :             if (pg_strcasecmp(dbidname->str, pat_cell->val) == 0)
                                991                 :              1 :                 skip_db_restore = true;
                                992                 :                :             /* Otherwise, try a pattern match if there is a connection */
                                993                 :                :             else
                                994                 :                :             {
                                995                 :                :                 int         dotcnt;
                                996                 :                : 
                                997                 :              5 :                 appendPQExpBufferStr(query, "SELECT 1 ");
                                998                 :              5 :                 processSQLNamePattern(conn, query, pat_cell->val, false,
                                999                 :              5 :                                       false, NULL, db_lit->data,
                               1000                 :                :                                       NULL, NULL, NULL, &dotcnt);
                               1001                 :                : 
                               1002         [ -  + ]:              5 :                 if (dotcnt > 0)
                               1003                 :                :                 {
   20 andrew@dunslane.net      1004                 :UNC           0 :                     pg_log_error("improper qualified name (too many dotted names): %s",
                               1005                 :                :                                  dbidname->str);
                               1006                 :              0 :                     PQfinish(conn);
                               1007                 :              0 :                     exit_nicely(1);
                               1008                 :                :                 }
                               1009                 :                : 
   20 andrew@dunslane.net      1010                 :GNC           5 :                 res = executeQuery(conn, query->data);
                               1011                 :                : 
                               1012         [ -  + ]:              5 :                 if (PQntuples(res))
                               1013                 :                :                 {
   20 andrew@dunslane.net      1014                 :UNC           0 :                     skip_db_restore = true;
                               1015                 :              0 :                     pg_log_info("database name \"%s\" matches --exclude-database pattern \"%s\"", dbidname->str, pat_cell->val);
                               1016                 :                :                 }
                               1017                 :                : 
   20 andrew@dunslane.net      1018                 :GNC           5 :                 PQclear(res);
                               1019                 :              5 :                 resetPQExpBuffer(query);
                               1020                 :                :             }
                               1021                 :                : 
                               1022         [ +  + ]:              6 :             if (skip_db_restore)
                               1023                 :              1 :                 break;
                               1024                 :                :         }
                               1025                 :                : 
                               1026                 :                :         /*
                               1027                 :                :          * Mark db to be skipped or increment the counter of dbs to be
                               1028                 :                :          * restored
                               1029                 :                :          */
                               1030         [ +  + ]:              6 :         if (skip_db_restore)
                               1031                 :                :         {
                               1032                 :              1 :             pg_log_info("excluding database \"%s\"", dbidname->str);
                               1033                 :              1 :             dbidname->oid = InvalidOid;
                               1034                 :                :         }
                               1035                 :                :         else
                               1036                 :              5 :             count_db++;
                               1037                 :                :     }
                               1038                 :                : 
                               1039                 :              1 :     destroyPQExpBuffer(query);
                               1040                 :              1 :     destroyPQExpBuffer(db_lit);
                               1041                 :                : 
                               1042                 :              1 :     return count_db;
                               1043                 :                : }
                               1044                 :                : 
                               1045                 :                : /*
                               1046                 :                :  * get_dbname_oid_list_from_mfile
                               1047                 :                :  *
                               1048                 :                :  * Open map.dat file and read line by line and then prepare a list of database
                               1049                 :                :  * names and corresponding db_oid.
                               1050                 :                :  *
                               1051                 :                :  * Returns, total number of database names in map.dat file.
                               1052                 :                :  */
                               1053                 :                : static int
   11                          1054                 :             10 : get_dbname_oid_list_from_mfile(const char *dumpdirpatharg, SimplePtrList *dbname_oid_list)
                               1055                 :                : {
                               1056                 :                :     StringInfoData linebuf;
                               1057                 :                :     FILE       *pfile;
                               1058                 :                :     char        map_file_path[MAXPGPATH];
   20                          1059                 :             10 :     int         count = 0;
                               1060                 :                :     int         len;
   11                          1061                 :             10 :     char       *dumpdirpath = pstrdup(dumpdirpatharg);
                               1062                 :                : 
                               1063                 :                :     /*
                               1064                 :                :      * If there is no map.dat file in the dump, then return from here as there
                               1065                 :                :      * is no database to restore.
                               1066                 :                :      */
   20                          1067         [ -  + ]:             10 :     if (!file_exists_in_directory(dumpdirpath, "map.dat"))
                               1068                 :                :     {
   20 andrew@dunslane.net      1069                 :UNC           0 :         pg_log_info("database restoring is skipped because file \"%s\" does not exist in directory \"%s\"", "map.dat", dumpdirpath);
                               1070                 :              0 :         return 0;
                               1071                 :                :     }
                               1072                 :                : 
   20 andrew@dunslane.net      1073                 :GNC          10 :     len = strlen(dumpdirpath);
                               1074                 :                : 
                               1075                 :                :     /* Trim slash from directory name. */
                               1076   [ +  -  -  + ]:             10 :     while (len > 1 && dumpdirpath[len - 1] == '/')
                               1077                 :                :     {
   20 andrew@dunslane.net      1078                 :UNC           0 :         dumpdirpath[len - 1] = '\0';
                               1079                 :              0 :         len--;
                               1080                 :                :     }
                               1081                 :                : 
   20 andrew@dunslane.net      1082                 :GNC          10 :     snprintf(map_file_path, MAXPGPATH, "%s/map.dat", dumpdirpath);
                               1083                 :                : 
                               1084                 :                :     /* Open map.dat file. */
                               1085                 :             10 :     pfile = fopen(map_file_path, PG_BINARY_R);
                               1086                 :                : 
                               1087         [ -  + ]:             10 :     if (pfile == NULL)
   20 andrew@dunslane.net      1088                 :UNC           0 :         pg_fatal("could not open file \"%s\": %m", map_file_path);
                               1089                 :                : 
   20 andrew@dunslane.net      1090                 :GNC          10 :     initStringInfo(&linebuf);
                               1091                 :                : 
                               1092                 :                :     /* Append all the dbname/db_oid combinations to the list. */
                               1093         [ +  + ]:            189 :     while (pg_get_line_buf(pfile, &linebuf))
                               1094                 :                :     {
                               1095                 :            179 :         Oid         db_oid = InvalidOid;
                               1096                 :                :         char       *dbname;
                               1097                 :                :         DbOidName  *dbidname;
                               1098                 :                :         int         namelen;
                               1099                 :            179 :         char       *p = linebuf.data;
                               1100                 :                : 
                               1101                 :                :         /* look for the dboid. */
                               1102         [ +  + ]:            489 :         while (isdigit((unsigned char) *p))
                               1103                 :            310 :             p++;
                               1104                 :                : 
                               1105                 :                :         /* ignore lines that don't begin with a digit */
                               1106         [ +  + ]:            179 :         if (p == linebuf.data)
                               1107                 :            101 :             continue;
                               1108                 :                : 
                               1109         [ +  - ]:             78 :         if (*p == ' ')
                               1110                 :                :         {
                               1111                 :             78 :             sscanf(linebuf.data, "%u", &db_oid);
                               1112                 :             78 :             p++;
                               1113                 :                :         }
                               1114                 :                : 
                               1115                 :                :         /* dbname is the rest of the line */
                               1116                 :             78 :         dbname = p;
                               1117                 :             78 :         namelen = strlen(dbname);
                               1118                 :                : 
                               1119                 :                :         /* Strip trailing newline */
                               1120   [ +  -  +  - ]:             78 :         if (namelen > 0 && dbname[namelen - 1] == '\n')
                               1121                 :             78 :             dbname[--namelen] = '\0';
                               1122                 :                : 
                               1123                 :                :         /* Report error and exit if the file has any corrupted data. */
                               1124   [ +  -  -  + ]:             78 :         if (!OidIsValid(db_oid) || namelen < 1)
   20 andrew@dunslane.net      1125                 :UNC           0 :             pg_fatal("invalid entry in file \"%s\" on line %d", map_file_path,
                               1126                 :                :                      count + 1);
                               1127                 :                : 
   20 andrew@dunslane.net      1128                 :GNC          78 :         dbidname = pg_malloc(offsetof(DbOidName, str) + namelen + 1);
                               1129                 :             78 :         dbidname->oid = db_oid;
                               1130                 :             78 :         strlcpy(dbidname->str, dbname, namelen + 1);
                               1131                 :                : 
                               1132                 :             78 :         pg_log_info("found database \"%s\" (OID: %u) in file \"%s\"",
                               1133                 :                :                     dbidname->str, db_oid, map_file_path);
                               1134                 :                : 
                               1135                 :             78 :         simple_ptr_list_append(dbname_oid_list, dbidname);
                               1136                 :             78 :         count++;
                               1137                 :                :     }
                               1138                 :                : 
                               1139                 :                :     /* Close map.dat file. */
                               1140                 :             10 :     fclose(pfile);
                               1141                 :                : 
                               1142                 :             10 :     pfree(linebuf.data);
                               1143                 :                : 
                               1144                 :             10 :     return count;
                               1145                 :                : }
                               1146                 :                : 
                               1147                 :                : /*
                               1148                 :                :  * restore_all_databases
                               1149                 :                :  *
                               1150                 :                :  * This will restore databases those dumps are present in
                               1151                 :                :  * directory based on map.dat file mapping.
                               1152                 :                :  *
                               1153                 :                :  * This will skip restoring for databases that are specified with
                               1154                 :                :  * exclude-database option.
                               1155                 :                :  *
                               1156                 :                :  * returns, number of errors while doing restore.
                               1157                 :                :  */
                               1158                 :                : static int
                               1159                 :             10 : restore_all_databases(const char *inputFileSpec,
                               1160                 :                :                       SimpleStringList db_exclude_patterns, RestoreOptions *opts,
                               1161                 :                :                       int numWorkers)
                               1162                 :                : {
                               1163                 :             10 :     SimplePtrList dbname_oid_list = {NULL, NULL};
                               1164                 :             10 :     int         num_db_restore = 0;
                               1165                 :                :     int         num_total_db;
                               1166                 :             10 :     int         n_errors_total = 0;
                               1167                 :             10 :     char       *connected_db = NULL;
                               1168                 :             10 :     PGconn     *conn = NULL;
                               1169                 :             10 :     RestoreOptions *original_opts = pg_malloc0_object(RestoreOptions);
                               1170                 :             10 :     RestoreOptions *tmpopts = pg_malloc0_object(RestoreOptions);
                               1171                 :                : 
                               1172                 :             10 :     memcpy(original_opts, opts, sizeof(RestoreOptions));
                               1173                 :                : 
                               1174                 :                :     /* Save db name to reuse it for all the database. */
                               1175         [ -  + ]:             10 :     if (opts->cparams.dbname)
   20 andrew@dunslane.net      1176                 :UNC           0 :         connected_db = opts->cparams.dbname;
                               1177                 :                : 
   11 andrew@dunslane.net      1178                 :GNC          10 :     num_total_db = get_dbname_oid_list_from_mfile(inputFileSpec, &dbname_oid_list);
                               1179                 :                : 
   20                          1180                 :             10 :     pg_log_info(ngettext("found %d database name in \"%s\"",
                               1181                 :                :                          "found %d database names in \"%s\"",
                               1182                 :                :                          num_total_db),
                               1183                 :                :                 num_total_db, "map.dat");
                               1184                 :                : 
                               1185                 :                :     /*
                               1186                 :                :      * If exclude-patterns is given, connect to the database to process them.
                               1187                 :                :      */
                               1188         [ +  + ]:             10 :     if (db_exclude_patterns.head != NULL)
                               1189                 :                :     {
                               1190         [ -  + ]:              1 :         if (opts->cparams.dbname)
                               1191                 :                :         {
   20 andrew@dunslane.net      1192                 :UNC           0 :             conn = ConnectDatabase(opts->cparams.dbname, NULL, opts->cparams.pghost,
                               1193                 :              0 :                                    opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
                               1194                 :                :                                    false, progname, NULL, NULL, NULL, NULL);
                               1195                 :                : 
                               1196         [ #  # ]:              0 :             if (!conn)
                               1197                 :              0 :                 pg_fatal("could not connect to database \"%s\"", opts->cparams.dbname);
                               1198                 :                :         }
                               1199                 :                : 
   20 andrew@dunslane.net      1200         [ +  - ]:GNC           1 :         if (!conn)
                               1201                 :                :         {
                               1202                 :              1 :             pg_log_info("trying to connect to database \"%s\"", "postgres");
                               1203                 :                : 
                               1204                 :              1 :             conn = ConnectDatabase("postgres", NULL, opts->cparams.pghost,
                               1205                 :              1 :                                    opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
                               1206                 :                :                                    false, progname, NULL, NULL, NULL, NULL);
                               1207                 :                : 
                               1208                 :                :             /* Try with template1. */
                               1209         [ -  + ]:              1 :             if (!conn)
                               1210                 :                :             {
   20 andrew@dunslane.net      1211                 :UNC           0 :                 pg_log_info("trying to connect to database \"%s\"", "template1");
                               1212                 :                : 
                               1213                 :              0 :                 conn = ConnectDatabase("template1", NULL, opts->cparams.pghost,
                               1214                 :              0 :                                        opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
                               1215                 :                :                                        false, progname, NULL, NULL, NULL, NULL);
                               1216         [ #  # ]:              0 :                 if (!conn)
                               1217                 :                :                 {
                               1218                 :              0 :                     pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
                               1219                 :                :                                  "Please specify an alternative database.");
                               1220                 :              0 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
                               1221                 :              0 :                     exit_nicely(1);
                               1222                 :                :                 }
                               1223                 :                :             }
                               1224                 :                :         }
                               1225                 :                : 
                               1226                 :                :         /* Filter the db list according to the exclude patterns. */
   20 andrew@dunslane.net      1227                 :GNC           1 :         num_db_restore = get_dbnames_list_to_restore(conn, &dbname_oid_list,
                               1228                 :                :                                                      db_exclude_patterns);
                               1229                 :              1 :         PQfinish(conn);
                               1230                 :                :     }
                               1231                 :                :     else
                               1232                 :              9 :         num_db_restore = num_total_db;
                               1233                 :                : 
                               1234                 :                :     /* Exit if no db needs to be restored. */
                               1235         [ -  + ]:             10 :     if (num_db_restore == 0)
                               1236                 :                :     {
   20 andrew@dunslane.net      1237                 :UNC           0 :         pg_log_info(ngettext("no database needs restoring out of %d database",
                               1238                 :                :                              "no database needs restoring out of %d databases", num_total_db),
                               1239                 :                :                     num_total_db);
                               1240                 :              0 :         pg_free(original_opts);
                               1241                 :              0 :         pg_free(tmpopts);
                               1242                 :              0 :         return 0;
                               1243                 :                :     }
                               1244                 :                : 
   20 andrew@dunslane.net      1245                 :GNC          10 :     pg_log_info("need to restore %d databases out of %d databases", num_db_restore, num_total_db);
                               1246                 :                : 
                               1247                 :                :     /*
                               1248                 :                :      * We have a list of databases to restore after processing the
                               1249                 :                :      * exclude-database switch(es).  Now we can restore them one by one.
                               1250                 :                :      */
                               1251                 :             10 :     for (SimplePtrListCell *db_cell = dbname_oid_list.head;
                               1252         [ +  + ]:             88 :          db_cell; db_cell = db_cell->next)
                               1253                 :                :     {
                               1254                 :             78 :         DbOidName  *dbidname = (DbOidName *) db_cell->ptr;
                               1255                 :                :         char        subdirpath[MAXPGPATH];
                               1256                 :                :         char        subdirdbpath[MAXPGPATH];
                               1257                 :                :         char        dbfilename[MAXPGPATH];
                               1258                 :                :         int         n_errors;
                               1259                 :                : 
                               1260                 :                :         /* ignore dbs marked for skipping */
                               1261         [ +  + ]:             78 :         if (dbidname->oid == InvalidOid)
                               1262                 :              1 :             continue;
                               1263                 :                : 
                               1264                 :                :         /*
                               1265                 :                :          * Since pg_backup_archiver.c may modify RestoreOptions during the
                               1266                 :                :          * previous restore, we must provide a fresh copy of the original
                               1267                 :                :          * "opts" for each call to restore_one_database.
                               1268                 :                :          */
                               1269                 :             77 :         memcpy(tmpopts, original_opts, sizeof(RestoreOptions));
                               1270                 :                : 
                               1271                 :                :         /*
                               1272                 :                :          * We need to reset override_dbname so that objects can be restored
                               1273                 :                :          * into an already created database. (used with -d/--dbname option)
                               1274                 :                :          */
                               1275         [ -  + ]:             77 :         if (tmpopts->cparams.override_dbname)
                               1276                 :                :         {
   20 andrew@dunslane.net      1277                 :UNC           0 :             pfree(tmpopts->cparams.override_dbname);
                               1278                 :              0 :             tmpopts->cparams.override_dbname = NULL;
                               1279                 :                :         }
                               1280                 :                : 
   20 andrew@dunslane.net      1281                 :GNC          77 :         snprintf(subdirdbpath, MAXPGPATH, "%s/databases", inputFileSpec);
                               1282                 :                : 
                               1283                 :                :         /*
                               1284                 :                :          * Look for the database dump file/dir. If there is an {oid}.tar or
                               1285                 :                :          * {oid}.dmp file, use it. Otherwise try to use a directory called
                               1286                 :                :          * {oid}
                               1287                 :                :          */
                               1288                 :             77 :         snprintf(dbfilename, MAXPGPATH, "%u.tar", dbidname->oid);
                               1289         [ +  + ]:             77 :         if (file_exists_in_directory(subdirdbpath, dbfilename))
                               1290                 :              8 :             snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.tar", inputFileSpec, dbidname->oid);
                               1291                 :                :         else
                               1292                 :                :         {
                               1293                 :             69 :             snprintf(dbfilename, MAXPGPATH, "%u.dmp", dbidname->oid);
                               1294                 :                : 
                               1295         [ +  + ]:             69 :             if (file_exists_in_directory(subdirdbpath, dbfilename))
                               1296                 :             16 :                 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.dmp", inputFileSpec, dbidname->oid);
                               1297                 :                :             else
                               1298                 :             53 :                 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u", inputFileSpec, dbidname->oid);
                               1299                 :                :         }
                               1300                 :                : 
                               1301                 :             77 :         pg_log_info("restoring database \"%s\"", dbidname->str);
                               1302                 :                : 
                               1303                 :                :         /* If database is already created, then don't set createDB flag. */
                               1304         [ -  + ]:             77 :         if (tmpopts->cparams.dbname)
                               1305                 :                :         {
                               1306                 :                :             PGconn     *test_conn;
                               1307                 :                : 
   20 andrew@dunslane.net      1308                 :UNC           0 :             test_conn = ConnectDatabase(dbidname->str, NULL, tmpopts->cparams.pghost,
                               1309                 :              0 :                                         tmpopts->cparams.pgport, tmpopts->cparams.username, TRI_DEFAULT,
                               1310                 :                :                                         false, progname, NULL, NULL, NULL, NULL);
                               1311         [ #  # ]:              0 :             if (test_conn)
                               1312                 :                :             {
                               1313                 :              0 :                 PQfinish(test_conn);
                               1314                 :                : 
                               1315                 :                :                 /* Use already created database for connection. */
                               1316                 :              0 :                 tmpopts->createDB = 0;
                               1317                 :              0 :                 tmpopts->cparams.dbname = dbidname->str;
                               1318                 :                :             }
                               1319                 :                :             else
                               1320                 :                :             {
   16                          1321         [ #  # ]:              0 :                 if (!tmpopts->createDB)
                               1322                 :                :                 {
                               1323                 :              0 :                     pg_log_info("skipping restore of database \"%s\": database does not exist and %s was not specified",
                               1324                 :                :                                 dbidname->str, "-C/--create");
                               1325                 :              0 :                     continue;
                               1326                 :                :                 }
                               1327                 :                : 
                               1328                 :                :                 /* We'll have to create it */
   20                          1329                 :              0 :                 tmpopts->createDB = 1;
                               1330                 :              0 :                 tmpopts->cparams.dbname = connected_db;
                               1331                 :                :             }
                               1332                 :                :         }
                               1333                 :                : 
                               1334                 :                :         /* Restore the single database. */
   20 andrew@dunslane.net      1335                 :GNC          77 :         n_errors = restore_one_database(subdirpath, tmpopts, numWorkers, true);
                               1336                 :                : 
                               1337                 :             77 :         n_errors_total += n_errors;
                               1338                 :                : 
                               1339                 :                :         /* Print a summary of ignored errors during single database restore. */
                               1340         [ -  + ]:             77 :         if (n_errors)
   20 andrew@dunslane.net      1341                 :UNC           0 :             pg_log_warning("errors ignored on database \"%s\" restore: %d", dbidname->str, n_errors);
                               1342                 :                :     }
                               1343                 :                : 
                               1344                 :                :     /* Log number of processed databases. */
   20 andrew@dunslane.net      1345                 :GNC          10 :     pg_log_info("number of restored databases is %d", num_db_restore);
                               1346                 :                : 
                               1347                 :                :     /* Free dbname and dboid list. */
                               1348                 :             10 :     simple_ptr_list_destroy(&dbname_oid_list);
                               1349                 :                : 
                               1350                 :             10 :     pg_free(original_opts);
                               1351                 :             10 :     pg_free(tmpopts);
                               1352                 :                : 
                               1353                 :             10 :     return n_errors_total;
                               1354                 :                : }
        

Generated by: LCOV version 2.4-beta