当前位置:网站首页>PostgreSQL memory explosion programming summary
PostgreSQL memory explosion programming summary
2022-07-20 15:34:00 【cejutue】
Generally speaking, the surge in memory is due to the unreleased capital , Or create too many capital
Client memory soared The most common usage is not releasing the data received from the server :
The most common reason for the server memory explosion is that too many server resource objects have been created .
Look at the conclusion first. :
1: es = PQgetResult(conn); This res You have to use PQclear(res); Remove , All the time Must have
2: PQsendPrepare If this function is constantly called, the memory of the server will soar , If the same connection executes the same statement , You have to do it on the client sql cache , To prepare the same statement again, you must use PQsendDescribePrepared Instead of PQsendPrepare;
The prototypes of these two functions are as follows :
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
int PQsendDescribePrepared(PGconn *conn, const char *stmt)
The client cache is directly used std::map<string,string> Just save it ,key It can be sql sentence ,value It can be uuid
The general notation is as follows :
m_nCount = -1;
m_pDB = db;
GS_LOCK_IT(m_ptrDB);
Reset();
m_strSQL = sql;
if (m_ptrDB->IsSQLID(sql))
{
m_StrCmdID = m_ptrDB->GetSQLID(sql);
int ascres = PQsendDescribePrepared(m_ptrDB->Handle(), m_StrCmdID);
if (ascres != 1)// success
{
OnPGError(m_ptrDB->Handle());
return false;
}
}
else
{
m_StrCmdID = m_ptrDB->GetSQLID(sql);
int ascres = PQsendPrepare(m_ptrDB->Handle(), m_StrCmdID, sql, m_nParamsCount, NULL);
if (ascres != 1)// success
{
OnPGError(m_ptrDB->Handle());
return false;
}
}
// asynchronous api must get Result clear
PGresult * res = PQgetResult(m_ptrDB->Handle());
while (res)
{
ExecStatusType status = PQresultStatus(res);
PQclear(res);
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK)
{
OnPGError(m_ptrDB->Handle());
return false;
}
res = PQgetResult(m_ptrDB->Handle());
}
return true;
GsString PGDatabase::GetSQLID(const char * strSQL)
{
auto end = m_SqlGUIDMap.find(strSQL);
if (end != m_SqlGUIDMap.end())
return end->second;
auto strID = GsMath::NewGUID();
m_SqlGUIDMap[strSQL] = strID;
return strID;
}
bool PGDatabase::IsSQLID(const char * strSQL)
{
auto end = m_SqlGUIDMap.find(strSQL);
if (end != m_SqlGUIDMap.end())
return true;
return false;
}
3: Connection does not need Use PQfinish Release
To sum up pg The function steps are as follows ( belt send It's all asynchronous API):
- Connect : PQconnectStart
- Get ready SQL sentence : Again SQL The first execution of the statement uses PQsendPrepare, Use after the second time PQsendDescribePrepared
- Clear the result set : Prepared after , asynchronous api must get Result clear PGresult * res = PQgetResult(PGconn); PQclear(res);
- Execute the query : PQsendQueryPrepared
- get data :res = PQgetResult(conn); Get several rows and columns of data PQgetvalue(res, i, j));
- eliminate , Release the connection PQclear(res); res = PQgetResult(conn);PQfinish(conn);
边栏推荐
- Is machine translation finished? Meta open source nllb translation model supports the translation of 200 languages
- 深度详解多链钱包系统开发功能及多币种钱包系统开发原理分析
- 【Mindspore】【读取图数据】无法读取Mindrecord格式图数据
- 【综合笔试题】难度 4/5,字符处理的线段树经典运用
- 图像标注开源小工具-labelImg
- 关于链游系统开发(智能合约上链原理分析说明)丨NFT链游系统开发原理分析及案例
- 磁盘性能与容量
- Example | apicloud AVM framework encapsulates sliding cell components
- && 运算符的使用
- Machine learning (1) environment configuration
猜你喜欢
随机推荐
Gearman 任务调度程序「建议收藏」
How to develop apps with good running experience and high performance with apiccloud
Machine learning Basics (1) photo video display, mouse, control operation
Example | apicloud AVM framework encapsulates sliding cell components
使用Jetty运行项目报错:Form too large或Form too many keys
电商用户行为数据分析
【刷题日记】最长回文子串
Iterm2 installation and use
Grid网格布局常考面试题
MySQL set foreign key constraint set foreign_ KEY_ CHECKS=1
磁盘空间单位GB与GiB是什么区别?
[Golang数据库专题5]Golang语言操作Redis进行增删改查
clickhouse 20. Integration of X and Prometheus + grafana (III)
openresty lua-resty-mlcache多级缓存
【mindspore】【警告原因】进行模型训练的时候报警告
Leetcode | 滑动窗口
SCCM2012R2网络部署重装系统
超详细的MySQL基本操作
Ceres之Powell’s Function
pytorch,筛选出一定范围的值