LCOV - code coverage report
Current view: top level - contrib/oid2name - oid2name.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 36 238 15.1 %
Date: 2019-07-19 13:07:20 Functions: 3 11 27.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * oid2name, a PostgreSQL app to map OIDs on the filesystem
       3             :  * to table and database names.
       4             :  *
       5             :  * Originally by
       6             :  * B. Palmer, bpalmer@crimelabs.net 1-17-2001
       7             :  *
       8             :  * contrib/oid2name/oid2name.c
       9             :  */
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include "catalog/pg_class_d.h"
      13             : 
      14             : #include "fe_utils/connect.h"
      15             : #include "libpq-fe.h"
      16             : #include "pg_getopt.h"
      17             : #include "getopt_long.h"
      18             : 
      19             : 
      20             : /* an extensible array to keep track of elements to show */
      21             : typedef struct
      22             : {
      23             :     char      **array;
      24             :     int         num;
      25             :     int         alloc;
      26             : } eary;
      27             : 
      28             : /* these are the opts structures for command line params */
      29             : struct options
      30             : {
      31             :     eary       *tables;
      32             :     eary       *oids;
      33             :     eary       *filenodes;
      34             : 
      35             :     bool        quiet;
      36             :     bool        systables;
      37             :     bool        indexes;
      38             :     bool        nodb;
      39             :     bool        extended;
      40             :     bool        tablespaces;
      41             : 
      42             :     char       *dbname;
      43             :     char       *hostname;
      44             :     char       *port;
      45             :     char       *username;
      46             :     const char *progname;
      47             : };
      48             : 
      49             : /* function prototypes */
      50             : static void help(const char *progname);
      51             : void        get_opts(int, char **, struct options *);
      52             : void        add_one_elt(char *eltname, eary *eary);
      53             : char       *get_comma_elts(eary *eary);
      54             : PGconn     *sql_conn(struct options *);
      55             : int         sql_exec(PGconn *, const char *sql, bool quiet);
      56             : void        sql_exec_dumpalldbs(PGconn *, struct options *);
      57             : void        sql_exec_dumpalltables(PGconn *, struct options *);
      58             : void        sql_exec_searchtables(PGconn *, struct options *);
      59             : void        sql_exec_dumpalltbspc(PGconn *, struct options *);
      60             : 
      61             : /* function to parse command line options and check for some usage errors. */
      62             : void
      63           6 : get_opts(int argc, char **argv, struct options *my_opts)
      64             : {
      65             :     static struct option long_options[] = {
      66             :         {"dbname", required_argument, NULL, 'd'},
      67             :         {"host", required_argument, NULL, 'h'},
      68             :         {"host", required_argument, NULL, 'H'}, /* deprecated */
      69             :         {"filenode", required_argument, NULL, 'f'},
      70             :         {"indexes", no_argument, NULL, 'i'},
      71             :         {"oid", required_argument, NULL, 'o'},
      72             :         {"port", required_argument, NULL, 'p'},
      73             :         {"quiet", no_argument, NULL, 'q'},
      74             :         {"tablespaces", no_argument, NULL, 's'},
      75             :         {"system-objects", no_argument, NULL, 'S'},
      76             :         {"table", required_argument, NULL, 't'},
      77             :         {"username", required_argument, NULL, 'U'},
      78             :         {"version", no_argument, NULL, 'V'},
      79             :         {"extended", no_argument, NULL, 'x'},
      80             :         {"help", no_argument, NULL, '?'},
      81             :         {NULL, 0, NULL, 0}
      82             :     };
      83             : 
      84             :     int         c;
      85             :     const char *progname;
      86             :     int         optindex;
      87             : 
      88           6 :     progname = get_progname(argv[0]);
      89             : 
      90             :     /* set the defaults */
      91           6 :     my_opts->quiet = false;
      92           6 :     my_opts->systables = false;
      93           6 :     my_opts->indexes = false;
      94           6 :     my_opts->nodb = false;
      95           6 :     my_opts->extended = false;
      96           6 :     my_opts->tablespaces = false;
      97           6 :     my_opts->dbname = NULL;
      98           6 :     my_opts->hostname = NULL;
      99           6 :     my_opts->port = NULL;
     100           6 :     my_opts->username = NULL;
     101           6 :     my_opts->progname = progname;
     102             : 
     103           6 :     if (argc > 1)
     104             :     {
     105           6 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     106             :         {
     107           2 :             help(progname);
     108           2 :             exit(0);
     109             :         }
     110           4 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     111             :         {
     112           2 :             puts("oid2name (PostgreSQL) " PG_VERSION);
     113           2 :             exit(0);
     114             :         }
     115             :     }
     116             : 
     117             :     /* get opts */
     118           4 :     while ((c = getopt_long(argc, argv, "d:f:h:H:io:p:qsSt:U:x", long_options, &optindex)) != -1)
     119             :     {
     120           2 :         switch (c)
     121             :         {
     122             :                 /* specify the database */
     123             :             case 'd':
     124           0 :                 my_opts->dbname = pg_strdup(optarg);
     125           0 :                 break;
     126             : 
     127             :                 /* specify one filenode to show */
     128             :             case 'f':
     129           0 :                 add_one_elt(optarg, my_opts->filenodes);
     130           0 :                 break;
     131             : 
     132             :                 /* host to connect to */
     133             :             case 'H':           /* deprecated */
     134             :             case 'h':
     135           0 :                 my_opts->hostname = pg_strdup(optarg);
     136           0 :                 break;
     137             : 
     138             :                 /* also display indexes */
     139             :             case 'i':
     140           0 :                 my_opts->indexes = true;
     141           0 :                 break;
     142             : 
     143             :                 /* specify one Oid to show */
     144             :             case 'o':
     145           0 :                 add_one_elt(optarg, my_opts->oids);
     146           0 :                 break;
     147             : 
     148             :                 /* port to connect to on remote host */
     149             :             case 'p':
     150           0 :                 my_opts->port = pg_strdup(optarg);
     151           0 :                 break;
     152             : 
     153             :                 /* don't show headers */
     154             :             case 'q':
     155           0 :                 my_opts->quiet = true;
     156           0 :                 break;
     157             : 
     158             :                 /* dump tablespaces only */
     159             :             case 's':
     160           0 :                 my_opts->tablespaces = true;
     161           0 :                 break;
     162             : 
     163             :                 /* display system tables */
     164             :             case 'S':
     165           0 :                 my_opts->systables = true;
     166           0 :                 break;
     167             : 
     168             :                 /* specify one tablename to show */
     169             :             case 't':
     170           0 :                 add_one_elt(optarg, my_opts->tables);
     171           0 :                 break;
     172             : 
     173             :                 /* username */
     174             :             case 'U':
     175           0 :                 my_opts->username = pg_strdup(optarg);
     176           0 :                 break;
     177             : 
     178             :                 /* display extra columns */
     179             :             case 'x':
     180           0 :                 my_opts->extended = true;
     181           0 :                 break;
     182             : 
     183             :             default:
     184           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     185           2 :                 exit(1);
     186             :         }
     187             :     }
     188           0 : }
     189             : 
     190             : static void
     191           2 : help(const char *progname)
     192             : {
     193           2 :     printf("%s helps examining the file structure used by PostgreSQL.\n\n"
     194             :            "Usage:\n"
     195             :            "  %s [OPTION]...\n"
     196             :            "\nOptions:\n"
     197             :            "  -f, --filenode=FILENODE    show info for table with given file node\n"
     198             :            "  -i, --indexes              show indexes and sequences too\n"
     199             :            "  -o, --oid=OID              show info for table with given OID\n"
     200             :            "  -q, --quiet                quiet (don't show headers)\n"
     201             :            "  -s, --tablespaces          show all tablespaces\n"
     202             :            "  -S, --system-objects       show system objects too\n"
     203             :            "  -t, --table=TABLE          show info for named table\n"
     204             :            "  -V, --version              output version information, then exit\n"
     205             :            "  -x, --extended             extended (show additional columns)\n"
     206             :            "  -?, --help                 show this help, then exit\n"
     207             :            "\nConnection options:\n"
     208             :            "  -d, --dbname=DBNAME        database to connect to\n"
     209             :            "  -h, --host=HOSTNAME        database server host or socket directory\n"
     210             :            "  -H                         same as -h, deprecated option\n"
     211             :            "  -p, --port=PORT            database server port number\n"
     212             :            "  -U, --username=USERNAME    connect as specified database user\n"
     213             :            "\nThe default action is to show all database OIDs.\n\n"
     214             :            "Report bugs to <pgsql-bugs@lists.postgresql.org>.\n",
     215             :            progname, progname);
     216           2 : }
     217             : 
     218             : /*
     219             :  * add_one_elt
     220             :  *
     221             :  * Add one element to a (possibly empty) eary struct.
     222             :  */
     223             : void
     224           0 : add_one_elt(char *eltname, eary *eary)
     225             : {
     226           0 :     if (eary->alloc == 0)
     227             :     {
     228           0 :         eary      ->alloc = 8;
     229           0 :         eary      ->array = (char **) pg_malloc(8 * sizeof(char *));
     230             :     }
     231           0 :     else if (eary->num >= eary->alloc)
     232             :     {
     233           0 :         eary      ->alloc *= 2;
     234           0 :         eary      ->array = (char **) pg_realloc(eary->array,
     235           0 :                                                  eary->alloc * sizeof(char *));
     236             :     }
     237             : 
     238           0 :     eary      ->array[eary->num] = pg_strdup(eltname);
     239           0 :     eary      ->num++;
     240           0 : }
     241             : 
     242             : /*
     243             :  * get_comma_elts
     244             :  *
     245             :  * Return the elements of an eary as a (freshly allocated) single string, in
     246             :  * single quotes, separated by commas and properly escaped for insertion in an
     247             :  * SQL statement.
     248             :  */
     249             : char *
     250           0 : get_comma_elts(eary *eary)
     251             : {
     252             :     char       *ret,
     253             :                *ptr;
     254             :     int         i,
     255           0 :                 length = 0;
     256             : 
     257           0 :     if (eary->num == 0)
     258           0 :         return pg_strdup("");
     259             : 
     260             :     /*
     261             :      * PQescapeString wants 2 * length + 1 bytes of breath space.  Add two
     262             :      * chars per element for the single quotes and one for the comma.
     263             :      */
     264           0 :     for (i = 0; i < eary->num; i++)
     265           0 :         length += strlen(eary->array[i]);
     266             : 
     267           0 :     ret = (char *) pg_malloc(length * 2 + 4 * eary->num);
     268           0 :     ptr = ret;
     269             : 
     270           0 :     for (i = 0; i < eary->num; i++)
     271             :     {
     272           0 :         if (i != 0)
     273           0 :             sprintf(ptr++, ",");
     274           0 :         sprintf(ptr++, "'");
     275           0 :         ptr += PQescapeString(ptr, eary->array[i], strlen(eary->array[i]));
     276           0 :         sprintf(ptr++, "'");
     277             :     }
     278             : 
     279           0 :     return ret;
     280             : }
     281             : 
     282             : /* establish connection with database. */
     283             : PGconn *
     284           0 : sql_conn(struct options *my_opts)
     285             : {
     286             :     PGconn     *conn;
     287           0 :     bool        have_password = false;
     288             :     char        password[100];
     289             :     bool        new_pass;
     290             :     PGresult   *res;
     291             : 
     292             :     /*
     293             :      * Start the connection.  Loop until we have a password if requested by
     294             :      * backend.
     295             :      */
     296             :     do
     297             :     {
     298             : #define PARAMS_ARRAY_SIZE   7
     299             : 
     300             :         const char *keywords[PARAMS_ARRAY_SIZE];
     301             :         const char *values[PARAMS_ARRAY_SIZE];
     302             : 
     303           0 :         keywords[0] = "host";
     304           0 :         values[0] = my_opts->hostname;
     305           0 :         keywords[1] = "port";
     306           0 :         values[1] = my_opts->port;
     307           0 :         keywords[2] = "user";
     308           0 :         values[2] = my_opts->username;
     309           0 :         keywords[3] = "password";
     310           0 :         values[3] = have_password ? password : NULL;
     311           0 :         keywords[4] = "dbname";
     312           0 :         values[4] = my_opts->dbname;
     313           0 :         keywords[5] = "fallback_application_name";
     314           0 :         values[5] = my_opts->progname;
     315           0 :         keywords[6] = NULL;
     316           0 :         values[6] = NULL;
     317             : 
     318           0 :         new_pass = false;
     319           0 :         conn = PQconnectdbParams(keywords, values, true);
     320             : 
     321           0 :         if (!conn)
     322             :         {
     323           0 :             fprintf(stderr, "%s: could not connect to database %s\n",
     324             :                     "oid2name", my_opts->dbname);
     325           0 :             exit(1);
     326             :         }
     327             : 
     328           0 :         if (PQstatus(conn) == CONNECTION_BAD &&
     329           0 :             PQconnectionNeedsPassword(conn) &&
     330           0 :             !have_password)
     331             :         {
     332           0 :             PQfinish(conn);
     333           0 :             simple_prompt("Password: ", password, sizeof(password), false);
     334           0 :             have_password = true;
     335           0 :             new_pass = true;
     336             :         }
     337           0 :     } while (new_pass);
     338             : 
     339             :     /* check to see that the backend connection was successfully made */
     340           0 :     if (PQstatus(conn) == CONNECTION_BAD)
     341             :     {
     342           0 :         fprintf(stderr, "%s: could not connect to database %s: %s",
     343             :                 "oid2name", my_opts->dbname, PQerrorMessage(conn));
     344           0 :         PQfinish(conn);
     345           0 :         exit(1);
     346             :     }
     347             : 
     348           0 :     res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
     349           0 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     350             :     {
     351           0 :         fprintf(stderr, "oid2name: could not clear search_path: %s\n",
     352             :                 PQerrorMessage(conn));
     353           0 :         PQclear(res);
     354           0 :         PQfinish(conn);
     355           0 :         exit(-1);
     356             :     }
     357           0 :     PQclear(res);
     358             : 
     359             :     /* return the conn if good */
     360           0 :     return conn;
     361             : }
     362             : 
     363             : /*
     364             :  * Actual code to make call to the database and print the output data.
     365             :  */
     366             : int
     367           0 : sql_exec(PGconn *conn, const char *todo, bool quiet)
     368             : {
     369             :     PGresult   *res;
     370             : 
     371             :     int         nfields;
     372             :     int         nrows;
     373             :     int         i,
     374             :                 j,
     375             :                 l;
     376             :     int        *length;
     377             :     char       *pad;
     378             : 
     379             :     /* make the call */
     380           0 :     res = PQexec(conn, todo);
     381             : 
     382             :     /* check and deal with errors */
     383           0 :     if (!res || PQresultStatus(res) > 2)
     384             :     {
     385           0 :         fprintf(stderr, "oid2name: query failed: %s\n", PQerrorMessage(conn));
     386           0 :         fprintf(stderr, "oid2name: query was: %s\n", todo);
     387             : 
     388           0 :         PQclear(res);
     389           0 :         PQfinish(conn);
     390           0 :         exit(-1);
     391             :     }
     392             : 
     393             :     /* get the number of fields */
     394           0 :     nrows = PQntuples(res);
     395           0 :     nfields = PQnfields(res);
     396             : 
     397             :     /* for each field, get the needed width */
     398           0 :     length = (int *) pg_malloc(sizeof(int) * nfields);
     399           0 :     for (j = 0; j < nfields; j++)
     400           0 :         length[j] = strlen(PQfname(res, j));
     401             : 
     402           0 :     for (i = 0; i < nrows; i++)
     403             :     {
     404           0 :         for (j = 0; j < nfields; j++)
     405             :         {
     406           0 :             l = strlen(PQgetvalue(res, i, j));
     407           0 :             if (l > length[j])
     408           0 :                 length[j] = strlen(PQgetvalue(res, i, j));
     409             :         }
     410             :     }
     411             : 
     412             :     /* print a header */
     413           0 :     if (!quiet)
     414             :     {
     415           0 :         for (j = 0, l = 0; j < nfields; j++)
     416             :         {
     417           0 :             fprintf(stdout, "%*s", length[j] + 2, PQfname(res, j));
     418           0 :             l += length[j] + 2;
     419             :         }
     420           0 :         fprintf(stdout, "\n");
     421           0 :         pad = (char *) pg_malloc(l + 1);
     422           0 :         MemSet(pad, '-', l);
     423           0 :         pad[l] = '\0';
     424           0 :         fprintf(stdout, "%s\n", pad);
     425           0 :         free(pad);
     426             :     }
     427             : 
     428             :     /* for each row, dump the information */
     429           0 :     for (i = 0; i < nrows; i++)
     430             :     {
     431           0 :         for (j = 0; j < nfields; j++)
     432           0 :             fprintf(stdout, "%*s", length[j] + 2, PQgetvalue(res, i, j));
     433           0 :         fprintf(stdout, "\n");
     434             :     }
     435             : 
     436             :     /* cleanup */
     437           0 :     PQclear(res);
     438           0 :     free(length);
     439             : 
     440           0 :     return 0;
     441             : }
     442             : 
     443             : /*
     444             :  * Dump all databases.  There are no system objects to worry about.
     445             :  */
     446             : void
     447           0 : sql_exec_dumpalldbs(PGconn *conn, struct options *opts)
     448             : {
     449             :     char        todo[1024];
     450             : 
     451             :     /* get the oid and database name from the system pg_database table */
     452           0 :     snprintf(todo, sizeof(todo),
     453             :              "SELECT d.oid AS \"Oid\", datname AS \"Database Name\", "
     454             :              "spcname AS \"Tablespace\" FROM pg_catalog.pg_database d JOIN pg_catalog.pg_tablespace t ON "
     455             :              "(dattablespace = t.oid) ORDER BY 2");
     456             : 
     457           0 :     sql_exec(conn, todo, opts->quiet);
     458           0 : }
     459             : 
     460             : /*
     461             :  * Dump all tables, indexes and sequences in the current database.
     462             :  */
     463             : void
     464           0 : sql_exec_dumpalltables(PGconn *conn, struct options *opts)
     465             : {
     466             :     char        todo[1024];
     467           0 :     char       *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
     468             : 
     469           0 :     snprintf(todo, sizeof(todo),
     470             :              "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s "
     471             :              "FROM pg_catalog.pg_class c "
     472             :              " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
     473             :              " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),"
     474             :              " pg_catalog.pg_tablespace t "
     475             :              "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
     476             :              CppAsString2(RELKIND_MATVIEW) "%s%s) AND "
     477             :              " %s"
     478             :              "     t.oid = CASE"
     479             :              "         WHEN reltablespace <> 0 THEN reltablespace"
     480             :              "         ELSE dattablespace"
     481             :              "     END "
     482             :              "ORDER BY relname",
     483           0 :              opts->extended ? addfields : "",
     484           0 :              opts->indexes ? "," CppAsString2(RELKIND_INDEX) "," CppAsString2(RELKIND_SEQUENCE) : "",
     485           0 :              opts->systables ? "," CppAsString2(RELKIND_TOASTVALUE) : "",
     486           0 :              opts->systables ? "" : "n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname !~ '^pg_toast' AND");
     487             : 
     488           0 :     sql_exec(conn, todo, opts->quiet);
     489           0 : }
     490             : 
     491             : /*
     492             :  * Show oid, filenode, name, schema and tablespace for each of the
     493             :  * given objects in the current database.
     494             :  */
     495             : void
     496           0 : sql_exec_searchtables(PGconn *conn, struct options *opts)
     497             : {
     498             :     char       *todo;
     499             :     char       *qualifiers,
     500             :                *ptr;
     501             :     char       *comma_oids,
     502             :                *comma_filenodes,
     503             :                *comma_tables;
     504           0 :     bool        written = false;
     505           0 :     char       *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
     506             : 
     507             :     /* get tables qualifiers, whether names, filenodes, or OIDs */
     508           0 :     comma_oids = get_comma_elts(opts->oids);
     509           0 :     comma_tables = get_comma_elts(opts->tables);
     510           0 :     comma_filenodes = get_comma_elts(opts->filenodes);
     511             : 
     512             :     /* 80 extra chars for SQL expression */
     513           0 :     qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) +
     514           0 :                                     strlen(comma_filenodes) + 80);
     515           0 :     ptr = qualifiers;
     516             : 
     517           0 :     if (opts->oids->num > 0)
     518             :     {
     519           0 :         ptr += sprintf(ptr, "c.oid IN (%s)", comma_oids);
     520           0 :         written = true;
     521             :     }
     522           0 :     if (opts->filenodes->num > 0)
     523             :     {
     524           0 :         if (written)
     525           0 :             ptr += sprintf(ptr, " OR ");
     526           0 :         ptr += sprintf(ptr, "pg_catalog.pg_relation_filenode(c.oid) IN (%s)", comma_filenodes);
     527           0 :         written = true;
     528             :     }
     529           0 :     if (opts->tables->num > 0)
     530             :     {
     531           0 :         if (written)
     532           0 :             ptr += sprintf(ptr, " OR ");
     533           0 :         sprintf(ptr, "c.relname ~~ ANY (ARRAY[%s])", comma_tables);
     534             :     }
     535           0 :     free(comma_oids);
     536           0 :     free(comma_tables);
     537           0 :     free(comma_filenodes);
     538             : 
     539             :     /* now build the query */
     540           0 :     todo = psprintf(
     541             :                     "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
     542             :                     "FROM pg_catalog.pg_class c\n"
     543             :                     "  LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
     544             :                     "  LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),\n"
     545             :                     "  pg_catalog.pg_tablespace t\n"
     546             :                     "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
     547             :                     CppAsString2(RELKIND_MATVIEW) ","
     548             :                     CppAsString2(RELKIND_INDEX) ","
     549             :                     CppAsString2(RELKIND_SEQUENCE) ","
     550             :                     CppAsString2(RELKIND_TOASTVALUE) ") AND\n"
     551             :                     "      t.oid = CASE\n"
     552             :                     "          WHEN reltablespace <> 0 THEN reltablespace\n"
     553             :                     "          ELSE dattablespace\n"
     554             :                     "      END AND\n"
     555             :                     "  (%s)\n"
     556             :                     "ORDER BY relname\n",
     557           0 :                     opts->extended ? addfields : "",
     558             :                     qualifiers);
     559             : 
     560           0 :     free(qualifiers);
     561             : 
     562           0 :     sql_exec(conn, todo, opts->quiet);
     563           0 : }
     564             : 
     565             : void
     566           0 : sql_exec_dumpalltbspc(PGconn *conn, struct options *opts)
     567             : {
     568             :     char        todo[1024];
     569             : 
     570           0 :     snprintf(todo, sizeof(todo),
     571             :              "SELECT oid AS \"Oid\", spcname as \"Tablespace Name\"\n"
     572             :              "FROM pg_catalog.pg_tablespace");
     573             : 
     574           0 :     sql_exec(conn, todo, opts->quiet);
     575           0 : }
     576             : 
     577             : int
     578           6 : main(int argc, char **argv)
     579             : {
     580             :     struct options *my_opts;
     581             :     PGconn     *pgconn;
     582             : 
     583           6 :     my_opts = (struct options *) pg_malloc(sizeof(struct options));
     584             : 
     585           6 :     my_opts->oids = (eary *) pg_malloc(sizeof(eary));
     586           6 :     my_opts->tables = (eary *) pg_malloc(sizeof(eary));
     587           6 :     my_opts->filenodes = (eary *) pg_malloc(sizeof(eary));
     588             : 
     589           6 :     my_opts->oids->num = my_opts->oids->alloc = 0;
     590           6 :     my_opts->tables->num = my_opts->tables->alloc = 0;
     591           6 :     my_opts->filenodes->num = my_opts->filenodes->alloc = 0;
     592             : 
     593             :     /* parse the opts */
     594           6 :     get_opts(argc, argv, my_opts);
     595             : 
     596           0 :     if (my_opts->dbname == NULL)
     597             :     {
     598           0 :         my_opts->dbname = "postgres";
     599           0 :         my_opts->nodb = true;
     600             :     }
     601           0 :     pgconn = sql_conn(my_opts);
     602             : 
     603             :     /* display only tablespaces */
     604           0 :     if (my_opts->tablespaces)
     605             :     {
     606           0 :         if (!my_opts->quiet)
     607           0 :             printf("All tablespaces:\n");
     608           0 :         sql_exec_dumpalltbspc(pgconn, my_opts);
     609             : 
     610           0 :         PQfinish(pgconn);
     611           0 :         exit(0);
     612             :     }
     613             : 
     614             :     /* display the given elements in the database */
     615           0 :     if (my_opts->oids->num > 0 ||
     616           0 :         my_opts->tables->num > 0 ||
     617           0 :         my_opts->filenodes->num > 0)
     618             :     {
     619           0 :         if (!my_opts->quiet)
     620           0 :             printf("From database \"%s\":\n", my_opts->dbname);
     621           0 :         sql_exec_searchtables(pgconn, my_opts);
     622             : 
     623           0 :         PQfinish(pgconn);
     624           0 :         exit(0);
     625             :     }
     626             : 
     627             :     /* no elements given; dump the given database */
     628           0 :     if (my_opts->dbname && !my_opts->nodb)
     629             :     {
     630           0 :         if (!my_opts->quiet)
     631           0 :             printf("From database \"%s\":\n", my_opts->dbname);
     632           0 :         sql_exec_dumpalltables(pgconn, my_opts);
     633             : 
     634           0 :         PQfinish(pgconn);
     635           0 :         exit(0);
     636             :     }
     637             : 
     638             :     /* no database either; dump all databases */
     639           0 :     if (!my_opts->quiet)
     640           0 :         printf("All databases:\n");
     641           0 :     sql_exec_dumpalldbs(pgconn, my_opts);
     642             : 
     643           0 :     PQfinish(pgconn);
     644           0 :     return 0;
     645             : }

Generated by: LCOV version 1.13