예제 #1
0
파일: server.c 프로젝트: bbbbreno/postgres
void
start_postmaster(ClusterInfo *cluster)
{
	char		cmd[MAXPGPATH];
	PGconn	   *conn;
	bool		exit_hook_registered = false;
	int			pg_ctl_return = 0;

	if (!exit_hook_registered)
	{
#ifdef HAVE_ATEXIT
		atexit(stop_postmaster_atexit);
#else
		on_exit(stop_postmaster_on_exit);
#endif
		exit_hook_registered = true;
	}

	/*
	 * Using autovacuum=off disables cleanup vacuum and analyze, but freeze
	 * vacuums can still happen, so we set autovacuum_freeze_max_age to its
	 * maximum.  We assume all datfrozenxid and relfrozen values are less than
	 * a gap of 2000000000 from the current xid counter, so autovacuum will
	 * not touch them.
	 */
	snprintf(cmd, sizeof(cmd),
			 SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
			 "-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
			 cluster->bindir, log_opts.filename2, cluster->pgdata, cluster->port,
			 (cluster->controldata.cat_ver >=
			  BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
			 "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
			 log_opts.filename2);

	/*
	 * Don't throw an error right away, let connecting throw the error because
	 * it might supply a reason for the failure.
	 */
	pg_ctl_return = exec_prog(false, "%s", cmd);

	/* Check to see if we can connect to the server; if not, report it. */
	if ((conn = get_db_conn(cluster, "template1")) == NULL ||
		PQstatus(conn) != CONNECTION_OK)
	{
		pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
			   PQerrorMessage(conn));
		if (conn)
			PQfinish(conn);
		pg_log(PG_FATAL, "could not connect to %s postmaster started with the command: %s\n",
			   CLUSTER_NAME(cluster), cmd);
	}
	PQfinish(conn);

	/* If the connection didn't fail, fail now */
	if (pg_ctl_return != 0)
		pg_log(PG_FATAL, "pg_ctl failed to start the %s server\n",
			   CLUSTER_NAME(cluster));

	os_info.running_cluster = cluster;
}
예제 #2
0
int PlatDbAccess::exec_trans(const vector<string>& sqls, int & last_insert_id
    , int & affected_rows, unsigned int uin)
{
    int index = get_conn_index(uin);
    ACE_Guard<ACE_Thread_Mutex> guard(db_conn_lock_[index]); 
    // 标识是否在使用 如果不使用了 请注意清理 
	db_conn_flag_[index] = 1; 
    
    ACE_DEBUG((LM_TRACE, "[%D] PlatDbAccess exec_trans sql vec size=%d,conn index=%d\n", sqls.size(), index));

    PlatDbConn* db_conn = get_db_conn(index);
    if (db_conn == NULL)
    {
        ACE_DEBUG((LM_ERROR, "[%D] PlatDbAccess get db conn failed\n"));
		db_conn_flag_[index] = 0;
        return -1;
    }

    int ret = 0;
    ret = db_conn->exec_trans(sqls, last_insert_id, affected_rows);
    if (ret != 0)
    {
        ACE_DEBUG((LM_ERROR, "[%D] PlatDbAccess exec_trans failed,%s\n", db_conn->get_err_msg() ));
        if(ret == -1) // -1 表示服务器连接断开
        {
            ret = db_conn->exec_trans(sqls, last_insert_id, affected_rows);
            if(ret == -1) //重连还失败,清理这个连接对象
            {
                fini(index);
            }
        }
    }
    db_conn_flag_[index] = 0;
    return stat_return(ret);
}
예제 #3
0
파일: server.c 프로젝트: 0x0FFF/postgres
/*
 * connectToServer()
 *
 *	Connects to the desired database on the designated server.
 *	If the connection attempt fails, this function logs an error
 *	message and calls exit() to kill the program.
 */
PGconn *
connectToServer(ClusterInfo *cluster, const char *db_name)
{
	PGconn	   *conn = get_db_conn(cluster, db_name);

	if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
	{
		pg_log(PG_REPORT, "connection to database failed: %s\n",
			   PQerrorMessage(conn));

		if (conn)
			PQfinish(conn);

		printf("Failure, exiting\n");
		exit(1);
	}

	return conn;
}
예제 #4
0
/*
 * connectToServer()
 *
 *	Connects to the desired database on the designated server.
 *	If the connection attempt fails, this function logs an error
 *	message and calls exit() to kill the program.
 */
PGconn *
connectToServer(ClusterInfo *cluster, const char *db_name)
{
	PGconn	   *conn = get_db_conn(cluster, db_name);

	if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
	{
		pg_log(PG_REPORT, "connection to database failed: %s",
			   PQerrorMessage(conn));

		if (conn)
			PQfinish(conn);

		printf(_("Failure, exiting\n"));
		exit(1);
	}

	PQclear(executeQueryOrDie(conn, ALWAYS_SECURE_SEARCH_PATH_SQL));

	return conn;
}
예제 #5
0
//使用的时候连接状态db_conn_flag_ 设置为 1   使用完设置为 0 
//线程和连接绑定的时候不受此变量影响
int PlatDbAccess::exec_update(const char* sql, int& last_insert_id
	, int& affected_rows, unsigned int uin)
{
    int index = get_conn_index(uin);
    ACE_Guard<ACE_Thread_Mutex> guard(db_conn_lock_[index]); 
    // 标识是否在使用 如果不使用了 请注意清理 
	db_conn_flag_[index] = 1;
    
    ACE_DEBUG((LM_TRACE, "[%D] PlatDbAccess exec update sql=%s,conn index=%d\n", sql, index));

    PlatDbConn* db_conn = get_db_conn(index);
    if (db_conn == NULL)
    {
        ACE_DEBUG((LM_ERROR, "[%D] PlatDbAccess get db conn failed\n"));
		db_conn_flag_[index] = 0;
        return -1;
    }

    int ret = 0;
    ret = db_conn->exec_update(sql, last_insert_id, affected_rows);
    if (ret != 0)
    {
        ACE_DEBUG((LM_ERROR, "[%D] PlatDbAccess exec update failed,ret=%d,%s,try again\n"
            , ret, db_conn->get_err_msg() ));
        if(ret == -1) // -1 表示服务器连接断开,尝试重连 try again
        {
            ret = db_conn->exec_update(sql, last_insert_id, affected_rows);
            if(ret == -1) //重连还失败,清理这个连接对象
            {
                fini(index);
            }
        }
    }
    db_conn_flag_[index] = 0;
    return stat_return(ret);
}
예제 #6
0
int PlatDbAccess::exec_multi_query(const char* sql, vector<MYSQL_RES*>& result_set_list
    , unsigned int uin)
{
    int index = get_conn_index(uin);    
    ACE_Guard<ACE_Thread_Mutex> guard(db_conn_lock_[index]); 
    // 标识是否在使用 如果不使用了 请注意清理 
    db_conn_flag_[index] = 1; // 1表示正在使用
    
    ACE_DEBUG(( LM_TRACE, "[%D] PlatDbAccess exec query sql=%s\n", sql ));

    PlatDbConn* db_conn = get_db_conn(index);
    if (db_conn == NULL)
    {
        ACE_DEBUG((LM_ERROR, "[%D] PlatDbAccess get db conn failed\n"));
        db_conn_flag_[index] = 0;
        return -1;
    }

    int ret = 0;    
    ret = db_conn->exec_multi_query(sql, result_set_list);
    if (ret != 0)
    {
        ACE_DEBUG((LM_ERROR, "[%D] PlatDbAccess exec query failed,idx=%d,%s\n"
            , index, db_conn->get_err_msg() ));
        if(ret == -1) // -1 表示服务器连接断开,尝试重连 try again
        {
            ret = db_conn->exec_multi_query(sql, result_set_list);
            if(ret == -1) //重连还失败,清理这个连接对象
            {
                fini(index);
            }
        }
    }
    db_conn_flag_[index] = 0;
    return stat_return(ret);
}
예제 #7
0
파일: server.c 프로젝트: 0x0FFF/postgres
bool
start_postmaster(ClusterInfo *cluster, bool throw_error)
{
	char		cmd[MAXPGPATH * 4 + 1000];
	PGconn	   *conn;
	bool		pg_ctl_return = false;
	char		socket_string[MAXPGPATH + 200];

	static bool exit_hook_registered = false;

	if (!exit_hook_registered)
	{
		atexit(stop_postmaster_atexit);
		exit_hook_registered = true;
	}

	socket_string[0] = '\0';

#ifdef HAVE_UNIX_SOCKETS
	/* prevent TCP/IP connections, restrict socket access */
	strcat(socket_string,
		   " -c listen_addresses='' -c unix_socket_permissions=0700");

	/* Have a sockdir?	Tell the postmaster. */
	if (cluster->sockdir)
		snprintf(socket_string + strlen(socket_string),
				 sizeof(socket_string) - strlen(socket_string),
				 " -c %s='%s'",
				 (GET_MAJOR_VERSION(cluster->major_version) < 903) ?
				 "unix_socket_directory" : "unix_socket_directories",
				 cluster->sockdir);
#endif

	/*
	 * Since PG 9.1, we have used -b to disable autovacuum.  For earlier
	 * releases, setting autovacuum=off disables cleanup vacuum and analyze,
	 * but freeze vacuums can still happen, so we set
	 * autovacuum_freeze_max_age to its maximum.
	 * (autovacuum_multixact_freeze_max_age was introduced after 9.1, so there
	 * is no need to set that.)  We assume all datfrozenxid and relfrozenxid
	 * values are less than a gap of 2000000000 from the current xid counter,
	 * so autovacuum will not touch them.
	 *
	 * Turn off durability requirements to improve object creation speed, and
	 * we only modify the new cluster, so only use it there.  If there is a
	 * crash, the new cluster has to be recreated anyway.  fsync=off is a big
	 * win on ext4.
	 */
	snprintf(cmd, sizeof(cmd),
		  "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d%s%s %s%s\" start",
		  cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
			 (cluster->controldata.cat_ver >=
			  BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? " -b" :
			 " -c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
			 (cluster == &new_cluster) ?
	  " -c synchronous_commit=off -c fsync=off -c full_page_writes=off" : "",
			 cluster->pgopts ? cluster->pgopts : "", socket_string);

	/*
	 * Don't throw an error right away, let connecting throw the error because
	 * it might supply a reason for the failure.
	 */
	pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
	/* pass both file names if they differ */
							  (strcmp(SERVER_LOG_FILE,
									  SERVER_START_LOG_FILE) != 0) ?
							  SERVER_LOG_FILE : NULL,
							  false,
							  "%s", cmd);

	/* Did it fail and we are just testing if the server could be started? */
	if (!pg_ctl_return && !throw_error)
		return false;

	/*
	 * We set this here to make sure atexit() shuts down the server, but only
	 * if we started the server successfully.  We do it before checking for
	 * connectivity in case the server started but there is a connectivity
	 * failure.  If pg_ctl did not return success, we will exit below.
	 *
	 * Pre-9.1 servers do not have PQping(), so we could be leaving the server
	 * running if authentication was misconfigured, so someday we might went
	 * to be more aggressive about doing server shutdowns even if pg_ctl
	 * fails, but now (2013-08-14) it seems prudent to be cautious.  We don't
	 * want to shutdown a server that might have been accidentally started
	 * during the upgrade.
	 */
	if (pg_ctl_return)
		os_info.running_cluster = cluster;

	/*
	 * pg_ctl -w might have failed because the server couldn't be started, or
	 * there might have been a connection problem in _checking_ if the server
	 * has started.  Therefore, even if pg_ctl failed, we continue and test
	 * for connectivity in case we get a connection reason for the failure.
	 */
	if ((conn = get_db_conn(cluster, "template1")) == NULL ||
		PQstatus(conn) != CONNECTION_OK)
	{
		pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
			   PQerrorMessage(conn));
		if (conn)
			PQfinish(conn);
		pg_fatal("could not connect to %s postmaster started with the command:\n"
				 "%s\n",
				 CLUSTER_NAME(cluster), cmd);
	}
	PQfinish(conn);

	/*
	 * If pg_ctl failed, and the connection didn't fail, and throw_error is
	 * enabled, fail now.  This could happen if the server was already
	 * running.
	 */
	if (!pg_ctl_return)
		pg_fatal("pg_ctl failed to start the %s server, or connection failed\n",
				 CLUSTER_NAME(cluster));

	return true;
}
예제 #8
0
파일: server.c 프로젝트: will/postgres
void
start_postmaster(ClusterInfo *cluster)
{
	char		cmd[MAXPGPATH * 4 + 1000];
	PGconn	   *conn;
	bool		exit_hook_registered = false;
	bool		pg_ctl_return = false;
	char		socket_string[MAXPGPATH + 200];

	if (!exit_hook_registered)
	{
		atexit(stop_postmaster_atexit);
		exit_hook_registered = true;
	}

	socket_string[0] = '\0';

#ifdef HAVE_UNIX_SOCKETS
	/* prevent TCP/IP connections, restrict socket access */
	strcat(socket_string,
		   " -c listen_addresses='' -c unix_socket_permissions=0700");

	/* Have a sockdir?  Tell the postmaster. */
	if (cluster->sockdir)
		snprintf(socket_string + strlen(socket_string),
				 sizeof(socket_string) - strlen(socket_string),
				 " -c %s='%s'",
				 (GET_MAJOR_VERSION(cluster->major_version) < 903) ?
				 "unix_socket_directory" : "unix_socket_directories",
				 cluster->sockdir);
#endif

	/*
	 * Using autovacuum=off disables cleanup vacuum and analyze, but freeze
	 * vacuums can still happen, so we set autovacuum_freeze_max_age to its
	 * maximum.  We assume all datfrozenxid and relfrozen values are less than
	 * a gap of 2000000000 from the current xid counter, so autovacuum will
	 * not touch them.
	 */
	snprintf(cmd, sizeof(cmd),
			 "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d %s %s%s\" start",
		  cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
			 (cluster->controldata.cat_ver >=
			  BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
			 "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
			 cluster->pgopts ? cluster->pgopts : "", socket_string);

	/*
	 * Don't throw an error right away, let connecting throw the error because
	 * it might supply a reason for the failure.
	 */
	pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
							  /* pass both file names if they differ */
							  (strcmp(SERVER_LOG_FILE,
									  SERVER_START_LOG_FILE) != 0) ?
							  SERVER_LOG_FILE : NULL,
							  false,
							  "%s", cmd);

	/* Check to see if we can connect to the server; if not, report it. */
	if ((conn = get_db_conn(cluster, "template1")) == NULL ||
		PQstatus(conn) != CONNECTION_OK)
	{
		pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
			   PQerrorMessage(conn));
		if (conn)
			PQfinish(conn);
		pg_log(PG_FATAL, "could not connect to %s postmaster started with the command:\n"
			   "%s\n",
			   CLUSTER_NAME(cluster), cmd);
	}
	PQfinish(conn);

	/* If the connection didn't fail, fail now */
	if (!pg_ctl_return)
		pg_log(PG_FATAL, "pg_ctl failed to start the %s server, or connection failed\n",
			   CLUSTER_NAME(cluster));

	os_info.running_cluster = cluster;
}