Ejemplo n.º 1
0
/* --------------------------------
 * system_db_connection_exists() - checks and if a connection to the SystemDB exists
 *
 * if not connected, it makes an attempt to connect to the SystemDB. If a connection
 * exists, returns 1, returns 0 otherwise.
 * --------------------------------
 */
static int
system_db_connection_exists(void)
{
	if (!system_db_info->pgconn ||
		(PQstatus(system_db_info->pgconn) != CONNECTION_OK))
	{
		if (system_db_connect())
			return 0;
	}

	return 1;
}
Ejemplo n.º 2
0
/*
 * pool_get_id:
 *    Returns the backend node id from value.
 */
int pool_get_id (DistDefInfo *info, const char *value)
{
    int num;
    PGresult *result;
    int length;

    if (!system_db_info->pgconn ||
            (PQstatus(system_db_info->pgconn) != CONNECTION_OK))
    {
        if (system_db_connect())
            return -1;
    }

    if (info->is_created_prepare == 0)
    {
        if (create_prepared_statement(info) != 0)
            return -1;
    }

    length = strlen(value);
    result = PQexecPrepared(system_db_info->pgconn, info->prepare_name,
                            1, &value, &length, NULL, 0);

    if (!result || PQresultStatus(result) != PGRES_TUPLES_OK ||
            PQgetisnull(result, 0, 0))
    {
        ereport(WARNING,
                (errmsg("error while getting backend id from value, PQexecPrepared failed: \"%s\"", PQerrorMessage(system_db_info->pgconn))));
        return -1;
    }
    else
    {
        char *id;
        id = PQgetvalue(result, 0 ,0);

        if(strlen(id))
        {
            num = atoi(id);
            PQclear(result);

            if(num < NUM_BACKENDS)
            {
                return num;
            } else {
                return -1;
            }
        }
        return -1;
    }
}
Ejemplo n.º 3
0
static
int  get_col_list(DistDefInfo *info)
{
    int i;
    static char sql[1024];
    PGresult *result;

    if (!system_db_info->pgconn ||
            (PQstatus(system_db_info->pgconn) != CONNECTION_OK))
    {
        if (system_db_connect())
            return -1;
    }

    for (i = 0; i < info->col_num; i++)
    {
        snprintf(sql,
                 sizeof(sql),
                 "SELECT col_list[%d],type_list[%d] FROM %s.dist_def where dbname = '%s' and schema_name = '%s' and table_name = '%s'",
                 i + 1,
                 i + 1,
                 pool_config->system_db_schema,
                 info->dbname,info->schema_name,
                 info->table_name);

        result = PQexec(system_db_info->pgconn, sql);

        if (!result || PQresultStatus(result) != PGRES_TUPLES_OK)
        {
            ereport(WARNING,
                    (errmsg("error while getting column list, PQexec failed: \"%s\"", PQerrorMessage(system_db_info->pgconn))));
            return -1;
        }
        else
        {
            info->col_list[i]  = palloc(strlen(PQgetvalue(result,0,0)) + 1);
            info->type_list[i] = palloc(strlen(PQgetvalue(result,0,1)) + 1);

            strcpy(info->col_list[i],PQgetvalue(result,0,0));
            strcpy(info->type_list[i],PQgetvalue(result,0,1));
            if (strcmp(info->col_list[i], info->dist_key_col_name) == 0)
                info->dist_key_col_id = i;
            PQclear(result);
        }
    }
    return 0;
}
Ejemplo n.º 4
0
/*
 * Initialize system DB connection
 */
static void init_system_db_connection(void)
{	
	if (pool_config->parallel_mode || pool_config->enable_query_cache)
	{
		system_db_connect();
		if (PQstatus(system_db_info->pgconn) != CONNECTION_OK)
		{
			pool_error("Could not make persistent libpq system DB connection");
		}

		system_db_info->connection = make_persistent_db_connection(pool_config->system_db_hostname,
																   pool_config->system_db_port,
																   pool_config->system_db_dbname,
																   pool_config->system_db_user,
																   pool_config->system_db_password, false);
		if (system_db_info->connection == NULL)
		{
			pool_error("Could not make persistent system DB connection");
		}
	}
}
Ejemplo n.º 5
0
/* prestogres: create database */
void prestogres_create_database_using_system_db(POOL_CONNECTION *frontend)
{
    static char sql[2048];
    static char db_conninfo[1024];
    PGresult *result;

    if (!system_db_info->pgconn ||
            (PQstatus(system_db_info->pgconn) != CONNECTION_OK))
    {
        if (system_db_connect())
            return;
    }

    /* create database */
    snprintf(sql, sizeof(sql),
             "create database \"%s\"",
             frontend->database);
    result = PQexec(system_db_info->pgconn, sql);

    if (!result /*|| PQresultStatus(result) != PGRES_COMMAND_OK*/)  // TODO error check
    {
        ereport(ERROR,
                (errmsg("error while creating database pg_database = \"%s\": \"%s\"", frontend->database, PQerrorMessage(system_db_info->pgconn))));
    } else
        PQclear(result);

    /* create user */
    snprintf(sql, sizeof(sql),
             "create role \"%s\" with login",
             frontend->username);
    result = PQexec(system_db_info->pgconn, sql);

    if (!result /*|| PQresultStatus(result) != PGRES_COMMAND_OK*/)  // TODO error check
    {
        ereport(ERROR,
                (errmsg("error while creating role pg_user = \"%s\": \"%s\"", frontend->username, PQerrorMessage(system_db_info->pgconn))));
    } else
        PQclear(result);

    /* setup database */
    snprintf(db_conninfo,
             sizeof(db_conninfo),
             "host=\\'%s\\' port=%d dbname=\\'%s\\' user=\\'%s\\' password=\\'%s\\'",
             system_db_info->info->hostname,
             system_db_info->info->port,
             frontend->database,
             system_db_info->info->user,
             system_db_info->info->password);

    snprintf(sql, sizeof(sql),
             "select prestogres_init_database('%s', '%s', E'%s')",
             frontend->username, frontend->database, db_conninfo);

    result = PQexec(system_db_info->pgconn, sql);

    if (!result || PQresultStatus(result) != PGRES_TUPLES_OK)
    {
        ereport(ERROR,
                (errmsg("error while initializing database pg_database = \"%s\" for pg_user = \"%s\": \"%s\"", frontend->database, frontend->username, PQerrorMessage(system_db_info->pgconn))));
    }
    PQclear(result);
}
Ejemplo n.º 6
0
/*
 * pool_memset_system_db_info:
 *    Initializes distribution rules. Distribution rules are stored in
 *    System DB. So we have to execute query, and expand results on
 *    memory.
 */
int pool_memset_system_db_info (SystemDBInfo *info)
{
    int i;
    static char sql[1024],sql2[1024];
    PGresult *result;
    DistDefInfo *dist_info = NULL;
    RepliDefInfo *repli_info = NULL;

    if (!system_db_info->pgconn ||
            (PQstatus(system_db_info->pgconn) != CONNECTION_OK))
    {
        if (system_db_connect())
            return -1;
    }

    /* get distribution rules */
    snprintf(sql,
             sizeof(sql),
             "SELECT dbname, schema_name, table_name,col_name,array_upper(col_list,1),col_list,type_list, dist_def_func FROM %s.dist_def",
             pool_config->system_db_schema);

    result = PQexec(system_db_info->pgconn, sql);
    if (!result || PQresultStatus(result) != PGRES_TUPLES_OK)
    {
        ereport(WARNING,
                (errmsg("error while initializing distribution rules, PQexec failed: \"%s\"", PQerrorMessage(system_db_info->pgconn))));
        return -1;
    }
    else
    {
        info->dist_def_num = PQntuples(result);
        if (PQntuples(result) > 0)
        {
            dist_info = palloc(sizeof(DistDefInfo) * info->dist_def_num);
        }


        info->dist_def_slot = dist_info;

        for (i = 0; i < PQntuples(result); ++i)
        {
            char *t_dbname;
            char *t_schema_name;
            char *t_table_name;
            char *t_dist_key_col_name;
            char *t_dist_def_func;
            int num;
            int len;

            num = atol(PQgetvalue(result, i ,4));
            t_dbname = palloc(strlen(PQgetvalue(result,i,0)) + 1);
            strcpy(t_dbname, PQgetvalue(result,i,0));
            dist_info[i].dbname = t_dbname;

            t_schema_name = palloc(strlen(PQgetvalue(result,i,1)) + 1);
            strcpy(t_schema_name, PQgetvalue(result,i,1));
            dist_info[i].schema_name = t_schema_name;

            t_table_name = palloc(strlen(PQgetvalue(result,i,2)) + 1);
            strcpy(t_table_name, PQgetvalue(result,i,2));
            dist_info[i].table_name = t_table_name;

            t_dist_key_col_name = palloc(strlen(PQgetvalue(result,i,3)) + 1);
            strcpy(t_dist_key_col_name, PQgetvalue(result,i,3));
            dist_info[i].dist_key_col_name = t_dist_key_col_name;

            t_dist_def_func = palloc(strlen(PQgetvalue(result,i,7)) + 1);
            strcpy(t_dist_def_func, PQgetvalue(result,i,7));
            dist_info[i].dist_def_func = t_dist_def_func;

            dist_info[i].col_num = num;

            dist_info[i].col_list = palloc0(num * sizeof(char *));
            dist_info[i].type_list = palloc0(num * sizeof(char *));

            if (get_col_list(&dist_info[i]) < 0)
            {
                ereport(WARNING,
                        (errmsg("error while initializing distribution rules, failed to get column list")));

                PQclear(result);
                pool_close_libpq_connection();
                return -1;
            }

            /* create PREPARE statement */
            len = strlen(t_dbname) + strlen(t_schema_name) +
                  strlen(t_table_name) + strlen("pgpool_");

            dist_info[i].prepare_name = palloc(len + 1);

            snprintf(dist_info[i].prepare_name, len+1, "pgpool_%s%s%s",
                     t_dbname, t_schema_name, t_table_name);
            dist_info[i].prepare_name[len] = '\0';
        }
    }

    PQclear(result);

    /* get replication rules */
    snprintf(sql2,
             sizeof(sql2),
             "SELECT dbname, schema_name, table_name, array_upper(col_list,1),col_list,type_list FROM %s.replicate_def",
             pool_config->system_db_schema);

    result = PQexec(system_db_info->pgconn, sql2);

    if (!result)
    {
        ereport(WARNING,
                (errmsg("error while initializing distribution rules, PQexec failed: \"%s\"", PQerrorMessage(system_db_info->pgconn))));
        return -1;
    }
    else if (PQresultStatus(result) != PGRES_TUPLES_OK)
    {
        info->repli_def_num = 0;
        info->repli_def_slot = NULL;
    }
    else
    {
        info->repli_def_num = PQntuples(result);
        if (PQntuples(result) > 0)
        {
            repli_info = palloc(sizeof(RepliDefInfo) * info->repli_def_num);
        }

        info->repli_def_slot = repli_info;

        for (i = 0; i < PQntuples(result); ++i)
        {
            char *t_dbname;
            char *t_schema_name;
            char *t_table_name;
            int num;
            int len;

            num = atol(PQgetvalue(result, i ,3));
            t_dbname = palloc(strlen(PQgetvalue(result,i,0)) + 1);
            strcpy(t_dbname, PQgetvalue(result,i,0));
            repli_info[i].dbname = t_dbname;

            t_schema_name = palloc(strlen(PQgetvalue(result,i,1)) + 1);
            strcpy(t_schema_name, PQgetvalue(result,i,1));
            repli_info[i].schema_name = t_schema_name;

            t_table_name = palloc(strlen(PQgetvalue(result,i,2)) + 1);
            strcpy(t_table_name, PQgetvalue(result,i,2));
            repli_info[i].table_name = t_table_name;

            repli_info[i].col_num = num;

            repli_info[i].col_list = palloc0(num * sizeof(char *));
            repli_info[i].type_list = palloc0(num * sizeof(char *));

            if (get_col_list2(&repli_info[i]) < 0)
            {
                ereport(WARNING,
                        (errmsg("error while initializing distribution rules, failed to get column list")));
                PQclear(result);
                pool_close_libpq_connection();
                return -1;
            }

            /* create PREPARE statement */
            len = strlen(t_dbname) + strlen(t_schema_name) +
                  strlen(t_table_name) + strlen("pgpool_");

            repli_info[i].prepare_name = palloc(len + 1);
            snprintf(repli_info[i].prepare_name, len+1, "pgpool_%s%s%s",
                     t_dbname, t_schema_name, t_table_name);
            repli_info[i].prepare_name[len] = '\0';
        }
    }

    PQclear(result);

    pool_close_libpq_connection();
    return i;
}