LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_restore.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 176 219 80.4 %
Date: 2019-11-21 14:06:36 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          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 "getopt_long.h"
      50             : #include "parallel.h"
      51             : #include "pg_backup_utils.h"
      52             : 
      53             : static void usage(const char *progname);
      54             : 
      55             : int
      56          62 : main(int argc, char **argv)
      57             : {
      58             :     RestoreOptions *opts;
      59             :     int         c;
      60             :     int         exit_code;
      61          62 :     int         numWorkers = 1;
      62             :     Archive    *AH;
      63             :     char       *inputFileSpec;
      64             :     static int  disable_triggers = 0;
      65             :     static int  enable_row_security = 0;
      66             :     static int  if_exists = 0;
      67             :     static int  no_data_for_failed_tables = 0;
      68             :     static int  outputNoTablespaces = 0;
      69             :     static int  use_setsessauth = 0;
      70             :     static int  no_comments = 0;
      71             :     static int  no_publications = 0;
      72             :     static int  no_security_labels = 0;
      73             :     static int  no_subscriptions = 0;
      74             :     static int  strict_names = 0;
      75             : 
      76          62 :     struct option cmdopts[] = {
      77             :         {"clean", 0, NULL, 'c'},
      78             :         {"create", 0, NULL, 'C'},
      79             :         {"data-only", 0, NULL, 'a'},
      80             :         {"dbname", 1, NULL, 'd'},
      81             :         {"exit-on-error", 0, NULL, 'e'},
      82             :         {"exclude-schema", 1, NULL, 'N'},
      83             :         {"file", 1, NULL, 'f'},
      84             :         {"format", 1, NULL, 'F'},
      85             :         {"function", 1, NULL, 'P'},
      86             :         {"host", 1, NULL, 'h'},
      87             :         {"index", 1, NULL, 'I'},
      88             :         {"jobs", 1, NULL, 'j'},
      89             :         {"list", 0, NULL, 'l'},
      90             :         {"no-privileges", 0, NULL, 'x'},
      91             :         {"no-acl", 0, NULL, 'x'},
      92             :         {"no-owner", 0, NULL, 'O'},
      93             :         {"no-reconnect", 0, NULL, 'R'},
      94             :         {"port", 1, NULL, 'p'},
      95             :         {"no-password", 0, NULL, 'w'},
      96             :         {"password", 0, NULL, 'W'},
      97             :         {"schema", 1, NULL, 'n'},
      98             :         {"schema-only", 0, NULL, 's'},
      99             :         {"superuser", 1, NULL, 'S'},
     100             :         {"table", 1, NULL, 't'},
     101             :         {"trigger", 1, NULL, 'T'},
     102             :         {"use-list", 1, NULL, 'L'},
     103             :         {"username", 1, NULL, 'U'},
     104             :         {"verbose", 0, NULL, 'v'},
     105             :         {"single-transaction", 0, NULL, '1'},
     106             : 
     107             :         /*
     108             :          * the following options don't have an equivalent short option letter
     109             :          */
     110             :         {"disable-triggers", no_argument, &disable_triggers, 1},
     111             :         {"enable-row-security", no_argument, &enable_row_security, 1},
     112             :         {"if-exists", no_argument, &if_exists, 1},
     113             :         {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
     114             :         {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
     115             :         {"role", required_argument, NULL, 2},
     116             :         {"section", required_argument, NULL, 3},
     117             :         {"strict-names", no_argument, &strict_names, 1},
     118             :         {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
     119             :         {"no-comments", no_argument, &no_comments, 1},
     120             :         {"no-publications", no_argument, &no_publications, 1},
     121             :         {"no-security-labels", no_argument, &no_security_labels, 1},
     122             :         {"no-subscriptions", no_argument, &no_subscriptions, 1},
     123             : 
     124             :         {NULL, 0, NULL, 0}
     125             :     };
     126             : 
     127          62 :     pg_logging_init(argv[0]);
     128          62 :     pg_logging_set_level(PG_LOG_WARNING);
     129          62 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
     130             : 
     131          62 :     init_parallel_dump_utils();
     132             : 
     133          62 :     opts = NewRestoreOptions();
     134             : 
     135          62 :     progname = get_progname(argv[0]);
     136             : 
     137          62 :     if (argc > 1)
     138             :     {
     139          60 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     140             :         {
     141           2 :             usage(progname);
     142           2 :             exit_nicely(0);
     143             :         }
     144          58 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     145             :         {
     146           2 :             puts("pg_restore (PostgreSQL) " PG_VERSION);
     147           2 :             exit_nicely(0);
     148             :         }
     149             :     }
     150             : 
     151         292 :     while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
     152             :                             cmdopts, NULL)) != -1)
     153             :     {
     154         178 :         switch (c)
     155             :         {
     156             :             case 'a':           /* Dump data only */
     157           4 :                 opts->dataOnly = 1;
     158           4 :                 break;
     159             :             case 'c':           /* clean (i.e., drop) schema prior to create */
     160           6 :                 opts->dropSchema = 1;
     161           6 :                 break;
     162             :             case 'C':
     163          16 :                 opts->createDB = 1;
     164          16 :                 break;
     165             :             case 'd':
     166          18 :                 opts->dbname = pg_strdup(optarg);
     167          18 :                 break;
     168             :             case 'e':
     169          12 :                 opts->exit_on_error = true;
     170          12 :                 break;
     171             :             case 'f':           /* output file name */
     172          36 :                 opts->filename = pg_strdup(optarg);
     173          36 :                 break;
     174             :             case 'F':
     175          10 :                 if (strlen(optarg) != 0)
     176          10 :                     opts->formatName = pg_strdup(optarg);
     177          10 :                 break;
     178             :             case 'h':
     179          12 :                 if (strlen(optarg) != 0)
     180          12 :                     opts->pghost = pg_strdup(optarg);
     181          12 :                 break;
     182             : 
     183             :             case 'j':           /* number of restore jobs */
     184           8 :                 numWorkers = atoi(optarg);
     185           8 :                 break;
     186             : 
     187             :             case 'l':           /* Dump the TOC summary */
     188           0 :                 opts->tocSummary = 1;
     189           0 :                 break;
     190             : 
     191             :             case 'L':           /* input TOC summary file name */
     192           0 :                 opts->tocFile = pg_strdup(optarg);
     193           0 :                 break;
     194             : 
     195             :             case 'n':           /* Dump data for this schema only */
     196           0 :                 simple_string_list_append(&opts->schemaNames, optarg);
     197           0 :                 break;
     198             : 
     199             :             case 'N':           /* Do not dump data for this schema */
     200           0 :                 simple_string_list_append(&opts->schemaExcludeNames, optarg);
     201           0 :                 break;
     202             : 
     203             :             case 'O':
     204           0 :                 opts->noOwner = 1;
     205           0 :                 break;
     206             : 
     207             :             case 'p':
     208          12 :                 if (strlen(optarg) != 0)
     209          12 :                     opts->pgport = pg_strdup(optarg);
     210          12 :                 break;
     211             :             case 'R':
     212             :                 /* no-op, still accepted for backwards compatibility */
     213           0 :                 break;
     214             :             case 'P':           /* Function */
     215           0 :                 opts->selTypes = 1;
     216           0 :                 opts->selFunction = 1;
     217           0 :                 simple_string_list_append(&opts->functionNames, optarg);
     218           0 :                 break;
     219             :             case 'I':           /* Index */
     220           0 :                 opts->selTypes = 1;
     221           0 :                 opts->selIndex = 1;
     222           0 :                 simple_string_list_append(&opts->indexNames, optarg);
     223           0 :                 break;
     224             :             case 'T':           /* Trigger */
     225           0 :                 opts->selTypes = 1;
     226           0 :                 opts->selTrigger = 1;
     227           0 :                 simple_string_list_append(&opts->triggerNames, optarg);
     228           0 :                 break;
     229             :             case 's':           /* dump schema only */
     230           2 :                 opts->schemaOnly = 1;
     231           2 :                 break;
     232             :             case 'S':           /* Superuser username */
     233           0 :                 if (strlen(optarg) != 0)
     234           0 :                     opts->superuser = pg_strdup(optarg);
     235           0 :                 break;
     236             :             case 't':           /* Dump specified table(s) only */
     237           0 :                 opts->selTypes = 1;
     238           0 :                 opts->selTable = 1;
     239           0 :                 simple_string_list_append(&opts->tableNames, optarg);
     240           0 :                 break;
     241             : 
     242             :             case 'U':
     243          16 :                 opts->username = pg_strdup(optarg);
     244          16 :                 break;
     245             : 
     246             :             case 'v':           /* verbose */
     247          18 :                 opts->verbose = 1;
     248          18 :                 pg_logging_set_level(PG_LOG_INFO);
     249          18 :                 break;
     250             : 
     251             :             case 'w':
     252           0 :                 opts->promptPassword = TRI_NO;
     253           0 :                 break;
     254             : 
     255             :             case 'W':
     256           0 :                 opts->promptPassword = TRI_YES;
     257           0 :                 break;
     258             : 
     259             :             case 'x':           /* skip ACL dump */
     260           0 :                 opts->aclsSkip = 1;
     261           0 :                 break;
     262             : 
     263             :             case '1':           /* Restore data in a single transaction */
     264           4 :                 opts->single_txn = true;
     265           4 :                 opts->exit_on_error = true;
     266           4 :                 break;
     267             : 
     268             :             case 0:
     269             : 
     270             :                 /*
     271             :                  * This covers the long options without a short equivalent.
     272             :                  */
     273           2 :                 break;
     274             : 
     275             :             case 2:             /* SET ROLE */
     276           0 :                 opts->use_role = pg_strdup(optarg);
     277           0 :                 break;
     278             : 
     279             :             case 3:             /* section */
     280           0 :                 set_dump_section(optarg, &(opts->dumpSections));
     281           0 :                 break;
     282             : 
     283             :             default:
     284           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     285           2 :                 exit_nicely(1);
     286             :         }
     287             :     }
     288             : 
     289             :     /* Get file name from command line */
     290          56 :     if (optind < argc)
     291          38 :         inputFileSpec = argv[optind++];
     292             :     else
     293          18 :         inputFileSpec = NULL;
     294             : 
     295             :     /* Complain if any arguments remain */
     296          56 :     if (optind < argc)
     297             :     {
     298           2 :         pg_log_error("too many command-line arguments (first is \"%s\")",
     299             :                      argv[optind]);
     300           2 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
     301             :                 progname);
     302           2 :         exit_nicely(1);
     303             :     }
     304             : 
     305             :     /* Complain if neither -f nor -d was specified (except if dumping TOC) */
     306          54 :     if (!opts->dbname && !opts->filename && !opts->tocSummary)
     307             :     {
     308           2 :         pg_log_error("one of -d/--dbname and -f/--file must be specified");
     309           2 :         exit_nicely(1);
     310             :     }
     311             : 
     312             :     /* Should get at most one of -d and -f, else user is confused */
     313          52 :     if (opts->dbname)
     314             :     {
     315          18 :         if (opts->filename)
     316             :         {
     317           2 :             pg_log_error("options -d/--dbname and -f/--file cannot be used together");
     318           2 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
     319             :                     progname);
     320           2 :             exit_nicely(1);
     321             :         }
     322          16 :         opts->useDB = 1;
     323             :     }
     324             : 
     325          50 :     if (opts->dataOnly && opts->schemaOnly)
     326             :     {
     327           2 :         pg_log_error("options -s/--schema-only and -a/--data-only cannot be used together");
     328           2 :         exit_nicely(1);
     329             :     }
     330             : 
     331          48 :     if (opts->dataOnly && opts->dropSchema)
     332             :     {
     333           2 :         pg_log_error("options -c/--clean and -a/--data-only cannot be used together");
     334           2 :         exit_nicely(1);
     335             :     }
     336             : 
     337             :     /*
     338             :      * -C is not compatible with -1, because we can't create a database inside
     339             :      * a transaction block.
     340             :      */
     341          46 :     if (opts->createDB && opts->single_txn)
     342             :     {
     343           2 :         pg_log_error("options -C/--create and -1/--single-transaction cannot be used together");
     344           2 :         exit_nicely(1);
     345             :     }
     346             : 
     347          44 :     if (numWorkers <= 0)
     348             :     {
     349           2 :         pg_log_error("invalid number of parallel jobs");
     350           2 :         exit(1);
     351             :     }
     352             : 
     353             :     /* See comments in pg_dump.c */
     354             : #ifdef WIN32
     355             :     if (numWorkers > MAXIMUM_WAIT_OBJECTS)
     356             :     {
     357             :         pg_log_error("maximum number of parallel jobs is %d",
     358             :                      MAXIMUM_WAIT_OBJECTS);
     359             :         exit(1);
     360             :     }
     361             : #endif
     362             : 
     363             :     /* Can't do single-txn mode with multiple connections */
     364          42 :     if (opts->single_txn && numWorkers > 1)
     365             :     {
     366           2 :         pg_log_error("cannot specify both --single-transaction and multiple jobs");
     367           2 :         exit_nicely(1);
     368             :     }
     369             : 
     370          40 :     opts->disable_triggers = disable_triggers;
     371          40 :     opts->enable_row_security = enable_row_security;
     372          40 :     opts->noDataForFailedTables = no_data_for_failed_tables;
     373          40 :     opts->noTablespace = outputNoTablespaces;
     374          40 :     opts->use_setsessauth = use_setsessauth;
     375          40 :     opts->no_comments = no_comments;
     376          40 :     opts->no_publications = no_publications;
     377          40 :     opts->no_security_labels = no_security_labels;
     378          40 :     opts->no_subscriptions = no_subscriptions;
     379             : 
     380          40 :     if (if_exists && !opts->dropSchema)
     381             :     {
     382           2 :         pg_log_error("option --if-exists requires option -c/--clean");
     383           2 :         exit_nicely(1);
     384             :     }
     385          38 :     opts->if_exists = if_exists;
     386          38 :     opts->strict_names = strict_names;
     387             : 
     388          38 :     if (opts->formatName)
     389             :     {
     390          10 :         switch (opts->formatName[0])
     391             :         {
     392             :             case 'c':
     393             :             case 'C':
     394           4 :                 opts->format = archCustom;
     395           4 :                 break;
     396             : 
     397             :             case 'd':
     398             :             case 'D':
     399           2 :                 opts->format = archDirectory;
     400           2 :                 break;
     401             : 
     402             :             case 't':
     403             :             case 'T':
     404           2 :                 opts->format = archTar;
     405           2 :                 break;
     406             : 
     407             :             default:
     408           2 :                 pg_log_error("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
     409             :                              opts->formatName);
     410           2 :                 exit_nicely(1);
     411             :         }
     412             :     }
     413             : 
     414          36 :     AH = OpenArchive(inputFileSpec, opts->format);
     415             : 
     416          36 :     SetArchiveOptions(AH, NULL, opts);
     417             : 
     418             :     /*
     419             :      * We don't have a connection yet but that doesn't matter. The connection
     420             :      * is initialized to NULL and if we terminate through exit_nicely() while
     421             :      * it's still NULL, the cleanup function will just be a no-op.
     422             :      */
     423          36 :     on_exit_close_archive(AH);
     424             : 
     425             :     /* Let the archiver know how noisy to be */
     426          36 :     AH->verbose = opts->verbose;
     427             : 
     428             :     /*
     429             :      * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
     430             :      */
     431          36 :     AH->exit_on_error = opts->exit_on_error;
     432             : 
     433          36 :     if (opts->tocFile)
     434           0 :         SortTocFromFile(AH);
     435             : 
     436          36 :     AH->numWorkers = numWorkers;
     437             : 
     438          36 :     if (opts->tocSummary)
     439           0 :         PrintTOCSummary(AH);
     440             :     else
     441             :     {
     442          36 :         ProcessArchiveRestoreOptions(AH);
     443          36 :         RestoreArchive(AH);
     444             :     }
     445             : 
     446             :     /* done, print a summary of ignored errors */
     447          36 :     if (AH->n_errors)
     448           0 :         pg_log_warning("errors ignored on restore: %d", AH->n_errors);
     449             : 
     450             :     /* AH may be freed in CloseArchive? */
     451          36 :     exit_code = AH->n_errors ? 1 : 0;
     452             : 
     453          36 :     CloseArchive(AH);
     454             : 
     455          36 :     return exit_code;
     456             : }
     457             : 
     458             : static void
     459           2 : usage(const char *progname)
     460             : {
     461           2 :     printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
     462           2 :     printf(_("Usage:\n"));
     463           2 :     printf(_("  %s [OPTION]... [FILE]\n"), progname);
     464             : 
     465           2 :     printf(_("\nGeneral options:\n"));
     466           2 :     printf(_("  -d, --dbname=NAME        connect to database name\n"));
     467           2 :     printf(_("  -f, --file=FILENAME      output file name (- for stdout)\n"));
     468           2 :     printf(_("  -F, --format=c|d|t       backup file format (should be automatic)\n"));
     469           2 :     printf(_("  -l, --list               print summarized TOC of the archive\n"));
     470           2 :     printf(_("  -v, --verbose            verbose mode\n"));
     471           2 :     printf(_("  -V, --version            output version information, then exit\n"));
     472           2 :     printf(_("  -?, --help               show this help, then exit\n"));
     473             : 
     474           2 :     printf(_("\nOptions controlling the restore:\n"));
     475           2 :     printf(_("  -a, --data-only              restore only the data, no schema\n"));
     476           2 :     printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
     477           2 :     printf(_("  -C, --create                 create the target database\n"));
     478           2 :     printf(_("  -e, --exit-on-error          exit on error, default is to continue\n"));
     479           2 :     printf(_("  -I, --index=NAME             restore named index\n"));
     480           2 :     printf(_("  -j, --jobs=NUM               use this many parallel jobs to restore\n"));
     481           2 :     printf(_("  -L, --use-list=FILENAME      use table of contents from this file for\n"
     482             :              "                               selecting/ordering output\n"));
     483           2 :     printf(_("  -n, --schema=NAME            restore only objects in this schema\n"));
     484           2 :     printf(_("  -N, --exclude-schema=NAME    do not restore objects in this schema\n"));
     485           2 :     printf(_("  -O, --no-owner               skip restoration of object ownership\n"));
     486           2 :     printf(_("  -P, --function=NAME(args)    restore named function\n"));
     487           2 :     printf(_("  -s, --schema-only            restore only the schema, no data\n"));
     488           2 :     printf(_("  -S, --superuser=NAME         superuser user name to use for disabling triggers\n"));
     489           2 :     printf(_("  -t, --table=NAME             restore named relation (table, view, etc.)\n"));
     490           2 :     printf(_("  -T, --trigger=NAME           restore named trigger\n"));
     491           2 :     printf(_("  -x, --no-privileges          skip restoration of access privileges (grant/revoke)\n"));
     492           2 :     printf(_("  -1, --single-transaction     restore as a single transaction\n"));
     493           2 :     printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
     494           2 :     printf(_("  --enable-row-security        enable row security\n"));
     495           2 :     printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
     496           2 :     printf(_("  --no-comments                do not restore comments\n"));
     497           2 :     printf(_("  --no-data-for-failed-tables  do not restore data of tables that could not be\n"
     498             :              "                               created\n"));
     499           2 :     printf(_("  --no-publications            do not restore publications\n"));
     500           2 :     printf(_("  --no-security-labels         do not restore security labels\n"));
     501           2 :     printf(_("  --no-subscriptions           do not restore subscriptions\n"));
     502           2 :     printf(_("  --no-tablespaces             do not restore tablespace assignments\n"));
     503           2 :     printf(_("  --section=SECTION            restore named section (pre-data, data, or post-data)\n"));
     504           2 :     printf(_("  --strict-names               require table and/or schema include patterns to\n"
     505             :              "                               match at least one entity each\n"));
     506           2 :     printf(_("  --use-set-session-authorization\n"
     507             :              "                               use SET SESSION AUTHORIZATION commands instead of\n"
     508             :              "                               ALTER OWNER commands to set ownership\n"));
     509             : 
     510           2 :     printf(_("\nConnection options:\n"));
     511           2 :     printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
     512           2 :     printf(_("  -p, --port=PORT          database server port number\n"));
     513           2 :     printf(_("  -U, --username=NAME      connect as specified database user\n"));
     514           2 :     printf(_("  -w, --no-password        never prompt for password\n"));
     515           2 :     printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
     516           2 :     printf(_("  --role=ROLENAME          do SET ROLE before restore\n"));
     517             : 
     518           2 :     printf(_("\n"
     519             :              "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
     520             :              "multiple times to select multiple objects.\n"));
     521           2 :     printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
     522           2 :     printf(_("Report bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
     523           2 : }

Generated by: LCOV version 1.13