Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions | Variables

streamutil.h File Reference

#include "libpq-fe.h"
Include dependency graph for streamutil.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define disconnect_and_exit(code)

Functions

PGconnGetConnection (void)

Variables

const char * progname
char * connection_string
char * dbhost
char * dbuser
char * dbport
int dbgetpassword
PGconnconn

Define Documentation

#define disconnect_and_exit (   code  ) 

Function Documentation

PGconn* GetConnection ( void   ) 

Definition at line 35 of file streamutil.c.

References _, CONNECTION_BAD, CONNECTION_OK, connection_string, dbgetpassword, dbhost, dbpassword, dbport, dbuser, free, i, _PQconninfoOption::keyword, NULL, pg_malloc0(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconninfoFree(), PQconninfoParse(), PQerrorMessage(), PQfinish(), PQparameterStatus(), PQstatus(), progname, simple_prompt(), _PQconninfoOption::val, and values.

{
    PGconn     *tmpconn;
    int         argcount = 7;   /* dbname, replication, fallback_app_name,
                                 * host, user, port, password */
    int         i;
    const char **keywords;
    const char **values;
    char       *password = NULL;
    const char *tmpparam;
    PQconninfoOption *conn_opts = NULL;
    PQconninfoOption *conn_opt;
    char       *err_msg = NULL;

    /*
     * Merge the connection info inputs given in form of connection string,
     * options and default values (dbname=replication, replication=true,
     * etc.)
     */
    i = 0;
    if (connection_string)
    {
        conn_opts = PQconninfoParse(connection_string, &err_msg);
        if (conn_opts == NULL)
        {
            fprintf(stderr, "%s: %s\n", progname, err_msg);
            return NULL;
        }

        for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
        {
            if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
                argcount++;
        }

        keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
        values = pg_malloc0((argcount + 1) * sizeof(*values));

        for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
        {
            if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
            {
                keywords[i] = conn_opt->keyword;
                values[i] = conn_opt->val;
                i++;
            }
        }
    }
    else
    {
        keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
        values = pg_malloc0((argcount + 1) * sizeof(*values));
    }

    keywords[i] = "dbname";
    values[i] = "replication";
    i++;
    keywords[i] = "replication";
    values[i] = "true";
    i++;
    keywords[i] = "fallback_application_name";
    values[i] = progname;
    i++;

    if (dbhost)
    {
        keywords[i] = "host";
        values[i] = dbhost;
        i++;
    }
    if (dbuser)
    {
        keywords[i] = "user";
        values[i] = dbuser;
        i++;
    }
    if (dbport)
    {
        keywords[i] = "port";
        values[i] = dbport;
        i++;
    }

    while (true)
    {
        if (password)
            free(password);

        if (dbpassword)
        {
            /*
             * We've saved a password when a previous connection succeeded,
             * meaning this is the call for a second session to the same
             * database, so just forcibly reuse that password.
             */
            keywords[i] = "password";
            values[i] = dbpassword;
            dbgetpassword = -1; /* Don't try again if this fails */
        }
        else if (dbgetpassword == 1)
        {
            password = simple_prompt(_("Password: "), 100, false);
            keywords[i] = "password";
            values[i] = password;
        }

        tmpconn = PQconnectdbParams(keywords, values, true);

        /*
         * If there is too little memory even to allocate the PGconn object
         * and PQconnectdbParams returns NULL, we call exit(1) directly.
         */
        if (!tmpconn)
        {
            fprintf(stderr, _("%s: could not connect to server\n"),
                    progname);
            exit(1);
        }

        if (PQstatus(tmpconn) == CONNECTION_BAD &&
            PQconnectionNeedsPassword(tmpconn) &&
            dbgetpassword != -1)
        {
            dbgetpassword = 1;  /* ask for password next time */
            PQfinish(tmpconn);
            continue;
        }

        if (PQstatus(tmpconn) != CONNECTION_OK)
        {
            fprintf(stderr, _("%s: could not connect to server: %s\n"),
                    progname, PQerrorMessage(tmpconn));
            PQfinish(tmpconn);
            free(values);
            free(keywords);
            if (conn_opts)
                PQconninfoFree(conn_opts);
            return NULL;
        }

        /* Connection ok! */
        free(values);
        free(keywords);
        if (conn_opts)
            PQconninfoFree(conn_opts);

        /*
         * Ensure we have the same value of integer timestamps as the server
         * we are connecting to.
         */
        tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
        if (!tmpparam)
        {
            fprintf(stderr,
                    _("%s: could not determine server setting for integer_datetimes\n"),
                    progname);
            PQfinish(tmpconn);
            exit(1);
        }

#ifdef HAVE_INT64_TIMESTAMP
        if (strcmp(tmpparam, "on") != 0)
#else
        if (strcmp(tmpparam, "off") != 0)
#endif
        {
            fprintf(stderr,
             _("%s: integer_datetimes compile flag does not match server\n"),
                    progname);
            PQfinish(tmpconn);
            exit(1);
        }

        /* Store the password for next run */
        if (password)
            dbpassword = password;
        return tmpconn;
    }
}


Variable Documentation

Definition at line 27 of file streamutil.c.

Referenced by BaseBackup(), check_for_isn_and_int8_passing_mismatch(), check_for_prepared_transactions(), check_for_reg_data_type_usage(), check_is_super_user(), check_loadable_libraries(), cluster_all_databases(), cluster_one_database(), connect_pg_server(), connectDatabase(), connectMaintenanceDatabase(), connectToServer(), create_cursor(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_fetch(), dblink_get_notify(), dblink_is_busy(), dblink_open(), dblink_record_internal(), dblink_send_query(), doConnect(), dumpBlobs(), dumpDatabase(), dumpTableData_copy(), estimate_path_cost_size(), ExecuteSqlCommand(), fetch_more_data(), get_db_infos(), get_loadable_libraries(), get_pg_database_relfilenode(), get_rel_infos(), get_tablespace_paths(), install_support_functions_in_new_db(), main(), new_9_0_populate_pg_largeobject_metadata(), old_8_3_check_for_name_data_type_usage(), old_8_3_check_for_tsquery_usage(), old_8_3_check_ltree_usage(), old_8_3_create_sequence_script(), old_8_3_invalidate_bpchar_pattern_ops_indexes(), old_8_3_invalidate_hash_gin_indexes(), old_8_3_rebuild_tsvector_tables(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), PQconnectdb(), PQconnectdbParams(), PQconnectStart(), PQconnectStartParams(), PQping(), PQpingParams(), PQsetdbLogin(), reindex_all_databases(), reindex_one_database(), reindex_system_catalogs(), run_permutation(), set_frozenxids(), set_locale_and_encoding(), setup_connection(), sql_conn(), start_postmaster(), StreamLog(), try_complete_step(), uninstall_support_functions_from_new_cluster(), vacuum_all_databases(), vacuum_one_database(), and vacuumlo().

Definition at line 21 of file streamutil.c.

Referenced by GetConnection(), and main().

Definition at line 25 of file streamutil.c.

Referenced by GetConnection(), and main().

char* dbhost

Definition at line 22 of file streamutil.c.

Referenced by GetConnection(), and main().

char* dbport

Definition at line 24 of file streamutil.c.

Referenced by GetConnection(), and main().

char* dbuser

Definition at line 23 of file streamutil.c.

Referenced by GetConnection(), and main().

const char* progname

Definition at line 35 of file pg_archivecleanup.c.