LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_restore.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 82.2 % 353 290
Test Date: 2026-07-26 10:15:44 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 66.7 % 213 142

             Branch data     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 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                 :             : #ifdef HAVE_TERMIOS_H
      45                 :             : #include <termios.h>
      46                 :             : #endif
      47                 :             : 
      48                 :             : #include "dumputils.h"
      49                 :             : #include "fe_utils/option_utils.h"
      50                 :             : #include "filter.h"
      51                 :             : #include "getopt_long.h"
      52                 :             : #include "parallel.h"
      53                 :             : #include "pg_backup_utils.h"
      54                 :             : 
      55                 :             : static void usage(const char *progname);
      56                 :             : static void read_restore_filters(const char *filename, RestoreOptions *opts);
      57                 :             : 
      58                 :             : int
      59                 :         101 : main(int argc, char **argv)
      60                 :             : {
      61                 :             :     RestoreOptions *opts;
      62                 :             :     int         c;
      63                 :             :     int         exit_code;
      64                 :         101 :     int         numWorkers = 1;
      65                 :             :     Archive    *AH;
      66                 :             :     char       *inputFileSpec;
      67                 :         101 :     bool        data_only = false;
      68                 :         101 :     bool        schema_only = false;
      69                 :             :     static int  disable_triggers = 0;
      70                 :             :     static int  enable_row_security = 0;
      71                 :             :     static int  if_exists = 0;
      72                 :             :     static int  no_data_for_failed_tables = 0;
      73                 :             :     static int  outputNoTableAm = 0;
      74                 :             :     static int  outputNoTablespaces = 0;
      75                 :             :     static int  use_setsessauth = 0;
      76                 :             :     static int  no_comments = 0;
      77                 :             :     static int  no_data = 0;
      78                 :             :     static int  no_policies = 0;
      79                 :             :     static int  no_publications = 0;
      80                 :             :     static int  no_schema = 0;
      81                 :             :     static int  no_security_labels = 0;
      82                 :             :     static int  no_statistics = 0;
      83                 :             :     static int  no_subscriptions = 0;
      84                 :             :     static int  strict_names = 0;
      85                 :             :     static int  statistics_only = 0;
      86                 :             :     static int  with_statistics = 0;
      87                 :             : 
      88                 :         101 :     struct option cmdopts[] = {
      89                 :             :         {"clean", 0, NULL, 'c'},
      90                 :             :         {"create", 0, NULL, 'C'},
      91                 :             :         {"data-only", 0, NULL, 'a'},
      92                 :             :         {"dbname", 1, NULL, 'd'},
      93                 :             :         {"exit-on-error", 0, NULL, 'e'},
      94                 :             :         {"exclude-schema", 1, NULL, 'N'},
      95                 :             :         {"file", 1, NULL, 'f'},
      96                 :             :         {"format", 1, NULL, 'F'},
      97                 :             :         {"function", 1, NULL, 'P'},
      98                 :             :         {"host", 1, NULL, 'h'},
      99                 :             :         {"index", 1, NULL, 'I'},
     100                 :             :         {"jobs", 1, NULL, 'j'},
     101                 :             :         {"list", 0, NULL, 'l'},
     102                 :             :         {"no-privileges", 0, NULL, 'x'},
     103                 :             :         {"no-acl", 0, NULL, 'x'},
     104                 :             :         {"no-owner", 0, NULL, 'O'},
     105                 :             :         {"no-reconnect", 0, NULL, 'R'},
     106                 :             :         {"port", 1, NULL, 'p'},
     107                 :             :         {"no-password", 0, NULL, 'w'},
     108                 :             :         {"password", 0, NULL, 'W'},
     109                 :             :         {"schema", 1, NULL, 'n'},
     110                 :             :         {"schema-only", 0, NULL, 's'},
     111                 :             :         {"superuser", 1, NULL, 'S'},
     112                 :             :         {"table", 1, NULL, 't'},
     113                 :             :         {"trigger", 1, NULL, 'T'},
     114                 :             :         {"use-list", 1, NULL, 'L'},
     115                 :             :         {"username", 1, NULL, 'U'},
     116                 :             :         {"verbose", 0, NULL, 'v'},
     117                 :             :         {"single-transaction", 0, NULL, '1'},
     118                 :             : 
     119                 :             :         /*
     120                 :             :          * the following options don't have an equivalent short option letter
     121                 :             :          */
     122                 :             :         {"disable-triggers", no_argument, &disable_triggers, 1},
     123                 :             :         {"enable-row-security", no_argument, &enable_row_security, 1},
     124                 :             :         {"if-exists", no_argument, &if_exists, 1},
     125                 :             :         {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
     126                 :             :         {"no-table-access-method", no_argument, &outputNoTableAm, 1},
     127                 :             :         {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
     128                 :             :         {"role", required_argument, NULL, 2},
     129                 :             :         {"section", required_argument, NULL, 3},
     130                 :             :         {"strict-names", no_argument, &strict_names, 1},
     131                 :             :         {"transaction-size", required_argument, NULL, 5},
     132                 :             :         {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
     133                 :             :         {"no-comments", no_argument, &no_comments, 1},
     134                 :             :         {"no-data", no_argument, &no_data, 1},
     135                 :             :         {"no-policies", no_argument, &no_policies, 1},
     136                 :             :         {"no-publications", no_argument, &no_publications, 1},
     137                 :             :         {"no-schema", no_argument, &no_schema, 1},
     138                 :             :         {"no-security-labels", no_argument, &no_security_labels, 1},
     139                 :             :         {"no-subscriptions", no_argument, &no_subscriptions, 1},
     140                 :             :         {"no-statistics", no_argument, &no_statistics, 1},
     141                 :             :         {"statistics", no_argument, &with_statistics, 1},
     142                 :             :         {"statistics-only", no_argument, &statistics_only, 1},
     143                 :             :         {"filter", required_argument, NULL, 4},
     144                 :             :         {"restrict-key", required_argument, NULL, 6},
     145                 :             : 
     146                 :             :         {NULL, 0, NULL, 0}
     147                 :             :     };
     148                 :             : 
     149                 :         101 :     pg_logging_init(argv[0]);
     150                 :         101 :     pg_logging_set_level(PG_LOG_WARNING);
     151                 :         101 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
     152                 :             : 
     153                 :         101 :     init_parallel_dump_utils();
     154                 :             : 
     155                 :         101 :     opts = NewRestoreOptions();
     156                 :             : 
     157                 :         101 :     progname = get_progname(argv[0]);
     158                 :             : 
     159         [ +  + ]:         101 :     if (argc > 1)
     160                 :             :     {
     161   [ +  +  -  + ]:         100 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     162                 :             :         {
     163                 :           1 :             usage(progname);
     164                 :           1 :             exit_nicely(0);
     165                 :             :         }
     166   [ +  +  +  + ]:          99 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     167                 :             :         {
     168                 :          20 :             puts("pg_restore (PostgreSQL) " PG_VERSION);
     169                 :          20 :             exit_nicely(0);
     170                 :             :         }
     171                 :             :     }
     172                 :             : 
     173                 :         477 :     while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
     174         [ +  + ]:         477 :                             cmdopts, NULL)) != -1)
     175                 :             :     {
     176   [ +  +  +  +  :         403 :         switch (c)
          +  +  +  +  +  
          +  -  +  -  -  
          +  -  -  +  -  
          +  -  +  +  +  
          -  -  -  +  +  
          -  -  +  +  -  
                      + ]
     177                 :             :         {
     178                 :           2 :             case 'a':           /* Dump data only */
     179                 :           2 :                 data_only = true;
     180                 :           2 :                 break;
     181                 :          21 :             case 'c':           /* clean (i.e., drop) schema prior to create */
     182                 :          21 :                 opts->dropSchema = 1;
     183                 :          21 :                 break;
     184                 :          34 :             case 'C':
     185                 :          34 :                 opts->createDB = 1;
     186                 :          34 :                 break;
     187                 :          37 :             case 'd':
     188                 :          37 :                 opts->cparams.dbname = pg_strdup(optarg);
     189                 :          37 :                 break;
     190                 :          32 :             case 'e':
     191                 :          32 :                 opts->exit_on_error = true;
     192                 :          32 :                 break;
     193                 :          36 :             case 'f':           /* output file name */
     194                 :          36 :                 opts->filename = pg_strdup(optarg);
     195                 :          36 :                 break;
     196                 :          16 :             case 'F':
     197         [ +  - ]:          16 :                 if (strlen(optarg) != 0)
     198                 :          16 :                     opts->formatName = pg_strdup(optarg);
     199                 :          16 :                 break;
     200                 :          32 :             case 'h':
     201         [ +  - ]:          32 :                 if (strlen(optarg) != 0)
     202                 :          32 :                     opts->cparams.pghost = pg_strdup(optarg);
     203                 :          32 :                 break;
     204                 :             : 
     205                 :           7 :             case 'j':           /* number of restore jobs */
     206         [ +  + ]:           7 :                 if (!option_parse_int(optarg, "-j/--jobs", 1,
     207                 :             :                                       PG_MAX_JOBS,
     208                 :             :                                       &numWorkers))
     209                 :           1 :                     exit(1);
     210                 :           6 :                 break;
     211                 :             : 
     212                 :           4 :             case 'l':           /* Dump the TOC summary */
     213                 :           4 :                 opts->tocSummary = 1;
     214                 :           4 :                 break;
     215                 :             : 
     216                 :           0 :             case 'L':           /* input TOC summary file name */
     217                 :           0 :                 opts->tocFile = pg_strdup(optarg);
     218                 :           0 :                 break;
     219                 :             : 
     220                 :           3 :             case 'n':           /* Dump data for this schema only */
     221                 :           3 :                 simple_string_list_append(&opts->schemaNames, optarg);
     222                 :           3 :                 break;
     223                 :             : 
     224                 :           0 :             case 'N':           /* Do not dump data for this schema */
     225                 :           0 :                 simple_string_list_append(&opts->schemaExcludeNames, optarg);
     226                 :           0 :                 break;
     227                 :             : 
     228                 :           0 :             case 'O':
     229                 :           0 :                 opts->noOwner = 1;
     230                 :           0 :                 break;
     231                 :             : 
     232                 :          42 :             case 'p':
     233         [ +  - ]:          42 :                 if (strlen(optarg) != 0)
     234                 :          42 :                     opts->cparams.pgport = pg_strdup(optarg);
     235                 :          42 :                 break;
     236                 :           0 :             case 'R':
     237                 :             :                 /* no-op, still accepted for backwards compatibility */
     238                 :           0 :                 break;
     239                 :           0 :             case 'P':           /* Function */
     240                 :           0 :                 opts->selTypes = 1;
     241                 :           0 :                 opts->selFunction = 1;
     242                 :           0 :                 simple_string_list_append(&opts->functionNames, optarg);
     243                 :           0 :                 break;
     244                 :           1 :             case 'I':           /* Index */
     245                 :           1 :                 opts->selTypes = 1;
     246                 :           1 :                 opts->selIndex = 1;
     247                 :           1 :                 simple_string_list_append(&opts->indexNames, optarg);
     248                 :           1 :                 break;
     249                 :           0 :             case 'T':           /* Trigger */
     250                 :           0 :                 opts->selTypes = 1;
     251                 :           0 :                 opts->selTrigger = 1;
     252                 :           0 :                 simple_string_list_append(&opts->triggerNames, optarg);
     253                 :           0 :                 break;
     254                 :           1 :             case 's':           /* dump schema only */
     255                 :           1 :                 schema_only = true;
     256                 :           1 :                 break;
     257                 :           0 :             case 'S':           /* Superuser username */
     258         [ #  # ]:           0 :                 if (strlen(optarg) != 0)
     259                 :           0 :                     opts->superuser = pg_strdup(optarg);
     260                 :           0 :                 break;
     261                 :           1 :             case 't':           /* Dump specified table(s) only */
     262                 :           1 :                 opts->selTypes = 1;
     263                 :           1 :                 opts->selTable = 1;
     264                 :           1 :                 simple_string_list_append(&opts->tableNames, optarg);
     265                 :           1 :                 break;
     266                 :             : 
     267                 :          34 :             case 'U':
     268                 :          34 :                 opts->cparams.username = pg_strdup(optarg);
     269                 :          34 :                 break;
     270                 :             : 
     271                 :          37 :             case 'v':           /* verbose */
     272                 :          37 :                 opts->verbose = 1;
     273                 :          37 :                 pg_logging_increase_verbosity();
     274                 :          37 :                 break;
     275                 :             : 
     276                 :           0 :             case 'w':
     277                 :           0 :                 opts->cparams.promptPassword = TRI_NO;
     278                 :           0 :                 break;
     279                 :             : 
     280                 :           0 :             case 'W':
     281                 :           0 :                 opts->cparams.promptPassword = TRI_YES;
     282                 :           0 :                 break;
     283                 :             : 
     284                 :           0 :             case 'x':           /* skip ACL dump */
     285                 :           0 :                 opts->aclsSkip = 1;
     286                 :           0 :                 break;
     287                 :             : 
     288                 :           2 :             case '1':           /* Restore data in a single transaction */
     289                 :           2 :                 opts->single_txn = true;
     290                 :           2 :                 opts->exit_on_error = true;
     291                 :           2 :                 break;
     292                 :             : 
     293                 :          18 :             case 0:
     294                 :             : 
     295                 :             :                 /*
     296                 :             :                  * This covers the long options without a short equivalent.
     297                 :             :                  */
     298                 :          18 :                 break;
     299                 :             : 
     300                 :           0 :             case 2:             /* SET ROLE */
     301                 :           0 :                 opts->use_role = pg_strdup(optarg);
     302                 :           0 :                 break;
     303                 :             : 
     304                 :           0 :             case 3:             /* section */
     305                 :           0 :                 set_dump_section(optarg, &(opts->dumpSections));
     306                 :           0 :                 break;
     307                 :             : 
     308                 :          10 :             case 4:             /* filter */
     309                 :          10 :                 read_restore_filters(optarg, opts);
     310                 :           6 :                 break;
     311                 :             : 
     312                 :          32 :             case 5:             /* transaction-size */
     313         [ -  + ]:          32 :                 if (!option_parse_int(optarg, "--transaction-size",
     314                 :             :                                       1, INT_MAX,
     315                 :             :                                       &opts->txn_size))
     316                 :           0 :                     exit(1);
     317                 :          32 :                 opts->exit_on_error = true;
     318                 :          32 :                 break;
     319                 :             : 
     320                 :           0 :             case 6:
     321                 :           0 :                 opts->restrict_key = pg_strdup(optarg);
     322                 :           0 :                 break;
     323                 :             : 
     324                 :           1 :             default:
     325                 :             :                 /* getopt_long already emitted a complaint */
     326                 :           1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     327                 :           1 :                 exit_nicely(1);
     328                 :             :         }
     329                 :             :     }
     330                 :             : 
     331                 :             :     /* Get file name from command line */
     332         [ +  + ]:          74 :     if (optind < argc)
     333                 :          66 :         inputFileSpec = argv[optind++];
     334                 :             :     else
     335                 :           8 :         inputFileSpec = NULL;
     336                 :             : 
     337                 :             :     /* Complain if any arguments remain */
     338         [ +  + ]:          74 :     if (optind < argc)
     339                 :             :     {
     340                 :           1 :         pg_log_error("too many command-line arguments (first is \"%s\")",
     341                 :             :                      argv[optind]);
     342                 :           1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     343                 :           1 :         exit_nicely(1);
     344                 :             :     }
     345                 :             : 
     346                 :             :     /* Complain if neither -f nor -d was specified (except if dumping TOC) */
     347   [ +  +  +  +  :          73 :     if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
                   +  + ]
     348                 :           1 :         pg_fatal("one of -d/--dbname and -f/--file must be specified");
     349                 :             : 
     350                 :             :     /* Should get at most one of -d and -f, else user is confused */
     351         [ +  + ]:          72 :     if (opts->cparams.dbname)
     352                 :             :     {
     353         [ +  + ]:          37 :         if (opts->filename)
     354                 :             :         {
     355                 :           1 :             pg_log_error("options %s and %s cannot be used together",
     356                 :             :                          "-d/--dbname", "-f/--file");
     357                 :           1 :             pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     358                 :           1 :             exit_nicely(1);
     359                 :             :         }
     360                 :             : 
     361         [ -  + ]:          36 :         if (opts->restrict_key)
     362                 :           0 :             pg_fatal("options %s and %s cannot be used together",
     363                 :             :                      "-d/--dbname", "--restrict-key");
     364                 :             : 
     365                 :          36 :         opts->useDB = 1;
     366                 :             :     }
     367                 :             :     else
     368                 :             :     {
     369                 :             :         /*
     370                 :             :          * If you don't provide a restrict key, one will be appointed for you.
     371                 :             :          */
     372         [ +  - ]:          35 :         if (!opts->restrict_key)
     373                 :          35 :             opts->restrict_key = generate_restrict_key();
     374         [ -  + ]:          35 :         if (!opts->restrict_key)
     375                 :           0 :             pg_fatal("could not generate restrict key");
     376         [ -  + ]:          35 :         if (!valid_restrict_key(opts->restrict_key))
     377                 :           0 :             pg_fatal("invalid restrict key");
     378                 :             :     }
     379                 :             : 
     380                 :             :     /* reject conflicting "-only" options */
     381   [ +  +  +  + ]:          71 :     if (data_only && schema_only)
     382                 :           1 :         pg_fatal("options %s and %s cannot be used together",
     383                 :             :                  "-s/--schema-only", "-a/--data-only");
     384   [ -  +  -  - ]:          70 :     if (schema_only && statistics_only)
     385                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     386                 :             :                  "-s/--schema-only", "--statistics-only");
     387   [ +  +  -  + ]:          70 :     if (data_only && statistics_only)
     388                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     389                 :             :                  "-a/--data-only", "--statistics-only");
     390                 :             : 
     391                 :             :     /* reject conflicting "-only" and "no-" options */
     392   [ +  +  -  + ]:          70 :     if (data_only && no_data)
     393                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     394                 :             :                  "-a/--data-only", "--no-data");
     395   [ -  +  -  - ]:          70 :     if (schema_only && no_schema)
     396                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     397                 :             :                  "-s/--schema-only", "--no-schema");
     398   [ +  +  -  + ]:          70 :     if (statistics_only && no_statistics)
     399                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     400                 :             :                  "--statistics-only", "--no-statistics");
     401                 :             : 
     402                 :             :     /* reject conflicting "no-" options */
     403   [ +  +  -  + ]:          70 :     if (with_statistics && no_statistics)
     404                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     405                 :             :                  "--statistics", "--no-statistics");
     406                 :             : 
     407                 :             :     /* reject conflicting "only-" options */
     408   [ +  +  -  + ]:          70 :     if (data_only && with_statistics)
     409                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     410                 :             :                  "-a/--data-only", "--statistics");
     411   [ -  +  -  - ]:          70 :     if (schema_only && with_statistics)
     412                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     413                 :             :                  "-s/--schema-only", "--statistics");
     414                 :             : 
     415   [ +  +  +  - ]:          70 :     if (data_only && opts->dropSchema)
     416                 :           1 :         pg_fatal("options %s and %s cannot be used together",
     417                 :             :                  "-c/--clean", "-a/--data-only");
     418                 :             : 
     419   [ +  +  -  + ]:          69 :     if (opts->single_txn && opts->txn_size > 0)
     420                 :           0 :         pg_fatal("options %s and %s cannot be used together",
     421                 :             :                  "-1/--single-transaction", "--transaction-size");
     422                 :             : 
     423                 :             :     /*
     424                 :             :      * -C is not compatible with -1, because we can't create a database inside
     425                 :             :      * a transaction block.
     426                 :             :      */
     427   [ +  +  +  + ]:          69 :     if (opts->createDB && opts->single_txn)
     428                 :           1 :         pg_fatal("options %s and %s cannot be used together",
     429                 :             :                  "-C/--create", "-1/--single-transaction");
     430                 :             : 
     431                 :             :     /* Can't do single-txn mode with multiple connections */
     432   [ +  +  +  - ]:          68 :     if (opts->single_txn && numWorkers > 1)
     433                 :           1 :         pg_fatal("cannot specify both --single-transaction and multiple jobs");
     434                 :             : 
     435                 :             :     /*
     436                 :             :      * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
     437                 :             :      * "--schema-only --no-schema", will have already caused an error in one
     438                 :             :      * of the checks above.
     439                 :             :      */
     440   [ +  -  +  +  :          67 :     opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
                   -  + ]
     441   [ +  -  +  - ]:         134 :                       data_only) && !no_data;
     442   [ +  -  +  +  :          67 :     opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
                   -  + ]
     443   [ +  -  +  - ]:         134 :                         schema_only) && !no_schema;
     444   [ +  -  -  + ]:          67 :     opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
     445   [ +  -  -  -  :         134 :                             (statistics_only || with_statistics)) && !no_statistics;
             -  -  +  - ]
     446                 :             : 
     447                 :          67 :     opts->disable_triggers = disable_triggers;
     448                 :          67 :     opts->enable_row_security = enable_row_security;
     449                 :          67 :     opts->noDataForFailedTables = no_data_for_failed_tables;
     450                 :          67 :     opts->noTableAm = outputNoTableAm;
     451                 :          67 :     opts->noTablespace = outputNoTablespaces;
     452                 :          67 :     opts->use_setsessauth = use_setsessauth;
     453                 :          67 :     opts->no_comments = no_comments;
     454                 :          67 :     opts->no_policies = no_policies;
     455                 :          67 :     opts->no_publications = no_publications;
     456                 :          67 :     opts->no_security_labels = no_security_labels;
     457                 :          67 :     opts->no_subscriptions = no_subscriptions;
     458                 :             : 
     459   [ +  +  +  - ]:          67 :     if (if_exists && !opts->dropSchema)
     460                 :           1 :         pg_fatal("option %s requires option %s",
     461                 :             :                  "--if-exists", "-c/--clean");
     462                 :          66 :     opts->if_exists = if_exists;
     463                 :          66 :     opts->strict_names = strict_names;
     464                 :             : 
     465         [ +  + ]:          66 :     if (opts->formatName)
     466                 :             :     {
     467   [ +  -  +  + ]:          32 :         if (pg_strcasecmp(opts->formatName, "c") == 0 ||
     468                 :          16 :             pg_strcasecmp(opts->formatName, "custom") == 0)
     469                 :          13 :             opts->format = archCustom;
     470   [ +  -  +  + ]:           6 :         else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
     471                 :           3 :                  pg_strcasecmp(opts->formatName, "directory") == 0)
     472                 :           1 :             opts->format = archDirectory;
     473   [ +  -  +  + ]:           4 :         else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
     474                 :           2 :                  pg_strcasecmp(opts->formatName, "tar") == 0)
     475                 :           1 :             opts->format = archTar;
     476   [ +  -  -  + ]:           2 :         else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
     477                 :           1 :                  pg_strcasecmp(opts->formatName, "plain") == 0)
     478                 :             :         {
     479                 :             :             /* recognize this for consistency with pg_dump */
     480                 :           0 :             pg_fatal("archive format \"%s\" is not supported; please use psql",
     481                 :             :                      opts->formatName);
     482                 :             :         }
     483                 :             :         else
     484                 :           1 :             pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
     485                 :             :                      opts->formatName);
     486                 :             :     }
     487                 :             : 
     488                 :          65 :     AH = OpenArchive(inputFileSpec, opts->format);
     489                 :             : 
     490                 :          65 :     SetArchiveOptions(AH, NULL, opts);
     491                 :             : 
     492                 :             :     /*
     493                 :             :      * We don't have a connection yet but that doesn't matter. The connection
     494                 :             :      * is initialized to NULL and if we terminate through exit_nicely() while
     495                 :             :      * it's still NULL, the cleanup function will just be a no-op.
     496                 :             :      */
     497                 :          65 :     on_exit_close_archive(AH);
     498                 :             : 
     499                 :             :     /* Let the archiver know how noisy to be */
     500                 :          65 :     AH->verbose = opts->verbose;
     501                 :             : 
     502                 :             :     /*
     503                 :             :      * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
     504                 :             :      */
     505                 :          65 :     AH->exit_on_error = opts->exit_on_error;
     506                 :             : 
     507         [ -  + ]:          65 :     if (opts->tocFile)
     508                 :           0 :         SortTocFromFile(AH);
     509                 :             : 
     510                 :          65 :     AH->numWorkers = numWorkers;
     511                 :             : 
     512         [ +  + ]:          65 :     if (opts->tocSummary)
     513                 :           4 :         PrintTOCSummary(AH);
     514                 :             :     else
     515                 :             :     {
     516                 :          61 :         ProcessArchiveRestoreOptions(AH);
     517                 :          61 :         RestoreArchive(AH);
     518                 :             :     }
     519                 :             : 
     520                 :             :     /* done, print a summary of ignored errors */
     521         [ -  + ]:          65 :     if (AH->n_errors)
     522                 :           0 :         pg_log_warning("errors ignored on restore: %d", AH->n_errors);
     523                 :             : 
     524                 :             :     /* AH may be freed in CloseArchive? */
     525                 :          65 :     exit_code = AH->n_errors ? 1 : 0;
     526                 :             : 
     527                 :          65 :     CloseArchive(AH);
     528                 :             : 
     529                 :          65 :     return exit_code;
     530                 :             : }
     531                 :             : 
     532                 :             : static void
     533                 :           1 : usage(const char *progname)
     534                 :             : {
     535                 :           1 :     printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
     536                 :           1 :     printf(_("Usage:\n"));
     537                 :           1 :     printf(_("  %s [OPTION]... [FILE]\n"), progname);
     538                 :             : 
     539                 :           1 :     printf(_("\nGeneral options:\n"));
     540                 :           1 :     printf(_("  -d, --dbname=NAME        connect to database name\n"));
     541                 :           1 :     printf(_("  -f, --file=FILENAME      output file name (- for stdout)\n"));
     542                 :           1 :     printf(_("  -F, --format=c|d|t       backup file format (should be automatic)\n"));
     543                 :           1 :     printf(_("  -l, --list               print summarized TOC of the archive\n"));
     544                 :           1 :     printf(_("  -v, --verbose            verbose mode\n"));
     545                 :           1 :     printf(_("  -V, --version            output version information, then exit\n"));
     546                 :           1 :     printf(_("  -?, --help               show this help, then exit\n"));
     547                 :             : 
     548                 :           1 :     printf(_("\nOptions controlling the restore:\n"));
     549                 :           1 :     printf(_("  -a, --data-only              restore only the data, no schema\n"));
     550                 :           1 :     printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
     551                 :           1 :     printf(_("  -C, --create                 create the target database\n"));
     552                 :           1 :     printf(_("  -e, --exit-on-error          exit on error, default is to continue\n"));
     553                 :           1 :     printf(_("  -I, --index=NAME             restore named index\n"));
     554                 :           1 :     printf(_("  -j, --jobs=NUM               use this many parallel jobs to restore\n"));
     555                 :           1 :     printf(_("  -L, --use-list=FILENAME      use table of contents from this file for\n"
     556                 :             :              "                               selecting/ordering output\n"));
     557                 :           1 :     printf(_("  -n, --schema=NAME            restore only objects in this schema\n"));
     558                 :           1 :     printf(_("  -N, --exclude-schema=NAME    do not restore objects in this schema\n"));
     559                 :           1 :     printf(_("  -O, --no-owner               skip restoration of object ownership\n"));
     560                 :           1 :     printf(_("  -P, --function=NAME(args)    restore named function\n"));
     561                 :           1 :     printf(_("  -s, --schema-only            restore only the schema, no data\n"));
     562                 :           1 :     printf(_("  -S, --superuser=NAME         superuser user name to use for disabling triggers\n"));
     563                 :           1 :     printf(_("  -t, --table=NAME             restore named relation (table, view, etc.)\n"));
     564                 :           1 :     printf(_("  -T, --trigger=NAME           restore named trigger\n"));
     565                 :           1 :     printf(_("  -x, --no-privileges          skip restoration of access privileges (grant/revoke)\n"));
     566                 :           1 :     printf(_("  -1, --single-transaction     restore as a single transaction\n"));
     567                 :           1 :     printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
     568                 :           1 :     printf(_("  --enable-row-security        enable row security\n"));
     569                 :           1 :     printf(_("  --filter=FILENAME            restore or skip objects based on expressions\n"
     570                 :             :              "                               in FILENAME\n"));
     571                 :           1 :     printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
     572                 :           1 :     printf(_("  --no-comments                do not restore comment commands\n"));
     573                 :           1 :     printf(_("  --no-data                    do not restore data\n"));
     574                 :           1 :     printf(_("  --no-data-for-failed-tables  do not restore data of tables that could not be\n"
     575                 :             :              "                               created\n"));
     576                 :           1 :     printf(_("  --no-policies                do not restore row security policies\n"));
     577                 :           1 :     printf(_("  --no-publications            do not restore publications\n"));
     578                 :           1 :     printf(_("  --no-schema                  do not restore schema\n"));
     579                 :           1 :     printf(_("  --no-security-labels         do not restore security labels\n"));
     580                 :           1 :     printf(_("  --no-statistics              do not restore statistics\n"));
     581                 :           1 :     printf(_("  --no-subscriptions           do not restore subscriptions\n"));
     582                 :           1 :     printf(_("  --no-table-access-method     do not restore table access methods\n"));
     583                 :           1 :     printf(_("  --no-tablespaces             do not restore tablespace assignments\n"));
     584                 :           1 :     printf(_("  --restrict-key=RESTRICT_KEY  use provided string as psql \\restrict key\n"));
     585                 :           1 :     printf(_("  --section=SECTION            restore named section (pre-data, data, or post-data)\n"));
     586                 :           1 :     printf(_("  --statistics                 restore the statistics\n"));
     587                 :           1 :     printf(_("  --statistics-only            restore only the statistics, not schema or data\n"));
     588                 :           1 :     printf(_("  --strict-names               require table and/or schema include patterns to\n"
     589                 :             :              "                               match at least one entity each\n"));
     590                 :           1 :     printf(_("  --transaction-size=N         commit after every N objects\n"));
     591                 :           1 :     printf(_("  --use-set-session-authorization\n"
     592                 :             :              "                               use SET SESSION AUTHORIZATION commands instead of\n"
     593                 :             :              "                               ALTER OWNER commands to set ownership\n"));
     594                 :             : 
     595                 :           1 :     printf(_("\nConnection options:\n"));
     596                 :           1 :     printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
     597                 :           1 :     printf(_("  -p, --port=PORT          database server port number\n"));
     598                 :           1 :     printf(_("  -U, --username=NAME      connect as specified database user\n"));
     599                 :           1 :     printf(_("  -w, --no-password        never prompt for password\n"));
     600                 :           1 :     printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
     601                 :           1 :     printf(_("  --role=ROLENAME          do SET ROLE before restore\n"));
     602                 :             : 
     603                 :           1 :     printf(_("\n"
     604                 :             :              "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
     605                 :             :              "multiple times to select multiple objects.\n"));
     606                 :           1 :     printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
     607                 :           1 :     printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     608                 :           1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     609                 :           1 : }
     610                 :             : 
     611                 :             : /*
     612                 :             :  * read_restore_filters - retrieve object identifier patterns from file
     613                 :             :  *
     614                 :             :  * Parse the specified filter file for include and exclude patterns, and add
     615                 :             :  * them to the relevant lists.  If the filename is "-" then filters will be
     616                 :             :  * read from STDIN rather than a file.
     617                 :             :  */
     618                 :             : static void
     619                 :          10 : read_restore_filters(const char *filename, RestoreOptions *opts)
     620                 :             : {
     621                 :             :     FilterStateData fstate;
     622                 :             :     char       *objname;
     623                 :             :     FilterCommandType comtype;
     624                 :             :     FilterObjectType objtype;
     625                 :             : 
     626                 :          10 :     filter_init(&fstate, filename, exit_nicely);
     627                 :             : 
     628         [ +  + ]:          27 :     while (filter_read_item(&fstate, &objname, &comtype, &objtype))
     629                 :             :     {
     630         [ +  + ]:          11 :         if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
     631                 :             :         {
     632   [ -  +  +  +  :           8 :             switch (objtype)
             +  +  +  - ]
     633                 :             :             {
     634                 :           0 :                 case FILTER_OBJECT_TYPE_NONE:
     635                 :           0 :                     break;
     636                 :           2 :                 case FILTER_OBJECT_TYPE_TABLE_DATA:
     637                 :             :                 case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
     638                 :             :                 case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
     639                 :             :                 case FILTER_OBJECT_TYPE_DATABASE:
     640                 :             :                 case FILTER_OBJECT_TYPE_EXTENSION:
     641                 :             :                 case FILTER_OBJECT_TYPE_FOREIGN_DATA:
     642                 :           2 :                     pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
     643                 :             :                                         "include",
     644                 :             :                                         filter_object_type_name(objtype));
     645                 :           2 :                     exit_nicely(1);
     646                 :             : 
     647                 :           2 :                 case FILTER_OBJECT_TYPE_FUNCTION:
     648                 :           2 :                     opts->selTypes = 1;
     649                 :           2 :                     opts->selFunction = 1;
     650                 :           2 :                     simple_string_list_append(&opts->functionNames, objname);
     651                 :           2 :                     break;
     652                 :           1 :                 case FILTER_OBJECT_TYPE_INDEX:
     653                 :           1 :                     opts->selTypes = 1;
     654                 :           1 :                     opts->selIndex = 1;
     655                 :           1 :                     simple_string_list_append(&opts->indexNames, objname);
     656                 :           1 :                     break;
     657                 :           1 :                 case FILTER_OBJECT_TYPE_SCHEMA:
     658                 :           1 :                     simple_string_list_append(&opts->schemaNames, objname);
     659                 :           1 :                     break;
     660                 :           1 :                 case FILTER_OBJECT_TYPE_TABLE:
     661                 :           1 :                     opts->selTypes = 1;
     662                 :           1 :                     opts->selTable = 1;
     663                 :           1 :                     simple_string_list_append(&opts->tableNames, objname);
     664                 :           1 :                     break;
     665                 :           1 :                 case FILTER_OBJECT_TYPE_TRIGGER:
     666                 :           1 :                     opts->selTypes = 1;
     667                 :           1 :                     opts->selTrigger = 1;
     668                 :           1 :                     simple_string_list_append(&opts->triggerNames, objname);
     669                 :           1 :                     break;
     670                 :             :             }
     671                 :             :         }
     672         [ +  - ]:           3 :         else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
     673                 :             :         {
     674   [ -  +  +  - ]:           3 :             switch (objtype)
     675                 :             :             {
     676                 :           0 :                 case FILTER_OBJECT_TYPE_NONE:
     677                 :           0 :                     break;
     678                 :           2 :                 case FILTER_OBJECT_TYPE_TABLE_DATA:
     679                 :             :                 case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
     680                 :             :                 case FILTER_OBJECT_TYPE_DATABASE:
     681                 :             :                 case FILTER_OBJECT_TYPE_EXTENSION:
     682                 :             :                 case FILTER_OBJECT_TYPE_FOREIGN_DATA:
     683                 :             :                 case FILTER_OBJECT_TYPE_FUNCTION:
     684                 :             :                 case FILTER_OBJECT_TYPE_INDEX:
     685                 :             :                 case FILTER_OBJECT_TYPE_TABLE:
     686                 :             :                 case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
     687                 :             :                 case FILTER_OBJECT_TYPE_TRIGGER:
     688                 :           2 :                     pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
     689                 :             :                                         "exclude",
     690                 :             :                                         filter_object_type_name(objtype));
     691                 :           2 :                     exit_nicely(1);
     692                 :             : 
     693                 :           1 :                 case FILTER_OBJECT_TYPE_SCHEMA:
     694                 :           1 :                     simple_string_list_append(&opts->schemaExcludeNames, objname);
     695                 :           1 :                     break;
     696                 :             :             }
     697                 :             :         }
     698                 :             :         else
     699                 :             :         {
     700                 :             :             Assert(comtype == FILTER_COMMAND_TYPE_NONE);
     701                 :             :             Assert(objtype == FILTER_OBJECT_TYPE_NONE);
     702                 :             :         }
     703                 :             : 
     704         [ -  + ]:           7 :         if (objname)
     705                 :           7 :             free(objname);
     706                 :             :     }
     707                 :             : 
     708                 :           6 :     filter_free(&fstate);
     709                 :           6 : }
        

Generated by: LCOV version 2.0-1