当前位置:网站首页>PostgreSQL 内存暴涨编程总结
PostgreSQL 内存暴涨编程总结
2022-07-19 12:35:00 【cejutue】
一般来说内存暴涨都是资原未释放,或者说创建了太多资原
客户端内存暴涨 最常见得用法就是未释放从服务端接收到得数据:
服务端内存暴涨最常见就是创建了太多服务端资原对象.
先看结论:
1: es = PQgetResult(conn); 这个res必须使用PQclear(res);清除掉,任何时候都是 必须得
2: PQsendPrepare这个函数如果不停得调用会造成服务端内存暴涨, 如果同一个连接执行同一个语句, 就必须要在客户端做sql缓存,再次准备同样得语句必须使用 PQsendDescribePrepared代替PQsendPrepare;
此两个函数得函数原型如下:
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
int PQsendDescribePrepared(PGconn *conn, const char *stmt)
客户端缓存直接用std::map<string,string> 存起来即可,key可以是sql语句,value可以是uuid
一般写法如下:
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)//成功
{
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)//成功
{
OnPGError(m_ptrDB->Handle());
return false;
}
}
//异步api必须get结果清除
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: 连接不用需要 使用 PQfinish 释放
总结一下pg得函数步骤如下(带send都是异步API):
- 连接: PQconnectStart
- 准备SQL语句: 同样SQL语句第一次执行使用PQsendPrepare, 第二次后用PQsendDescribePrepared
- 清除结果集: Prepared后, 异步api必须get结果清除 PGresult * res = PQgetResult(PGconn); PQclear(res);
- 执行查询: PQsendQueryPrepared
- 获取数据:res = PQgetResult(conn);获取几行几列数据PQgetvalue(res, i, j));
- 清除,释放连接PQclear(res); res = PQgetResult(conn);PQfinish(conn);
边栏推荐
- Is machine translation finished? Meta open source nllb translation model supports the translation of 200 languages
- Analysis scaffold
- [JSOI2007]重要的城市
- 使用mindspore创建数据集时报错
- pytorch,筛选出一定范围的值
- Rong Tui [jsoi2011] special products
- Zhiyuan admitted plagiarizing the paper, and the relevant responsible person has resigned!
- 面试官:你确定 Redis 是单线程的进程吗?
- 用APICloud如何开发出运行体验良好、高性能的 App
- npm warn config global `--global`, `--local` are deprecated. Use ` --location solution
猜你喜欢
IDEA中如何安装插件和宝贝插件的推荐
[pkusc2018] main fighting ground of provincial selection and special training
Interviewer: are you sure redis is a single threaded process?
Reflection
分布式链路日志minbox-logging使用文档
Introduction to common options of file synchronization tool Rsync and data synchronization through services
Exercice récursif fonctionnel (version Easy)
clickhouse 20. Integration of X and Prometheus + grafana (III)
婚恋交友网站开发社交聊天平台代码分享(三)
省选专练之 [HAOI2009]毛毛虫
随机推荐
【服务器数据恢复】某品牌ProLiant服务器raid瘫痪,数据库文件丢失,数据库文件备份损坏的数据恢复案例
(0711-0717) memorabilia of open source software security this week
Southern Cass 10.1 software installation package download and installation tutorial
省选专练之GCD生成树
kubernetes RBAC
Unity - invincible hero (high energy ahead)
推荐一款支持数据 + 代码生成的开发工具!yyds
reduce函数构建炫耀字体,字体动画。
JS class 并不只是简单的语法糖!
unet和mask rcnn
Flink SQL configures Kafka to chain a topic with multiple partitions, and there is no error. There is no problem with a single partition
GCD spanning tree of provincial selection and special training
deeplabv1和v2
基于 EasyCV 复现 DETR 和 DAB-DETR,Object Query 的正确打开方式
省选专练之棋盘问题
【MindSpore】【模型推理】使用未适配CPU API所训练的模型是否可以在CPU上执行推理
[question brushing diary] longest palindrome substring
web安全入门-icmp测试与防御
不等式学习笔记
最基本的排序