PostgreSQL内存泄漏

时间:2009-08-14 22:31:20

标签: c++ postgresql

我正在运行一个用c ++编写的系统,该系统不断地将大量数据插入到我的数据库中,同时查询数据库以获得更新的结果。我的问题是在这个过程中启动的postgres线程不断使用越来越多的内存。我需要知道如何纠正这个问题。以下是一个更简单的程序,用于演示此问题。

#include <iostream>
#include <sstream>

#include <tbb/tbb_thread.h>//intel parallel studio class for parel

#include "libpq-fe.h"
#include "libpq/libpq-fs.h"

class Inserter{
public:
    void operator()(){
        PGconn* conn = PQconnectdb("user=postgres password=1234");
        int i=0;
        while(1){
            std::stringstream insert;
            insert << "INSERT INTO tmp (value) VALUES (" << i%250 << ");";
            PGresult* res=PQexec(conn,insert.str().c_str());
            if (PQresultStatus(res) == PGRES_FATAL_ERROR){
                std::cout << "Error in inserting data:\nError code: " << PQresStatus(PQresultStatus(res)) << "Error Message: " << PQerrorMessage(conn);
                PQclear(res);
                PQfinish(conn);
                return;
            }
            PQclear(res);
            i++;
        }
    }

};
class Queryer{
public:
    void operator()(){
        PGconn* conn = PQconnectdb("user=postgres password=1234");
        int j=0;
        while (1){
            std::stringstream query;
            query << "SELECT * FROM tmp WHERE id>" << j%1000 << ";";
            PGresult* res=PQexec(conn,query.str().c_str());
            if (PQresultStatus(res) == PGRES_FATAL_ERROR){
                std::cout << "Error in searching data:\nError code: " << PQresStatus(PQresultStatus(res)) << "Error Message: " << PQerrorMessage(conn);
                PQclear(res);
                PQfinish(conn);
                return;
            }
            PQclear(res);
            Sleep(10);
            j++;
        }
    }

};

void main(){
    //connect to Database
    PGconn* conn = PQconnectdb("user=postgres password=1234");

    //create table
    std::cout << "Creating table...\n";
    PGresult* res=PQexec(conn,"CREATE TABLE tmp (id SERIAL8 PRIMARY KEY,value INT);");
    if (PQresultStatus(res) == PGRES_FATAL_ERROR){
        std::cout << "Error in Creating table:\nError code: " << PQresStatus(PQresultStatus(res)) << "Error Message: " << PQerrorMessage(conn);
        //PQclear(res);
        //PQfinish(conn);
        //return;
    }
    PQclear(res);
    PQfinish(conn);

    std::cout << "Starting table filling thread...\n";
    //fill table with some data
    Inserter ins;
    tbb::tbb_thread filling(ins);
    Sleep(1000);
    // searching table ... here is where the memory leak is
    std::cout << "Starting table searching thread...\n";
    Queryer que;
    tbb::tbb_thread searching(que);

    while(true)
    {
        tbb::tick_count::interval_t t(1.0);
        tbb::this_tbb_thread::sleep(t);
    }
}

1 个答案:

答案 0 :(得分:1)

也许你需要以某种方式关闭你的联系?