Closed timzaak closed 2 years ago
https://pgtune.leopard.in.ua/ https://github.com/jfcoz/postgresqltuner postgresql配置 优化工具。 pgbench 性能测试工具。
https://github.com/Vonng/pigsty 集群/监控方案
PostgreSQL 秒杀场景优化 pg_try_advisory_xact_lock 的使用 用PostgreSQL支持含有更新,删除,插入的实时流式计算 实时流式计算 stream,View
按照优化顺序来搞。
前7条实施起来,没太多难度云厂商会封一些参数,需要工单特殊处理。
unlogged tables 这个要有个前提,就是整个表都丢了也没问题,也不能基于WAL做主从备份。
batch insert 这一步都要用的话,还是最好切架构吧。这已经不是数据库的事情,需要搞多机处理、并行落库。
在腾讯云上简单测试了下,1C2G PG 15,启用1~5条, 1.5+w/s的插入速度,cpu 80%,不设置2、4条,1+w/s插入速度,cpu 100%+。 根据腾讯云公布的测试结果,简单估算一下,4C8G PG 15, 能做到10w+/s的插入速度。
create table test_insert(
id int,
sn text not null,
data jsonb not null,
time timestamp without time zone
);
create index test_insert_time on test_insert using brin(time);
create index test_insert_id on test_insert using brin(id);
-- 测试脚本
\set number random(1,1000000)
insert into test_insert(id,sn,data,time) values (:number, :sn, :json_data, current_timestamp);
# 4C8G Ubuntu 22
sudo apt-get update
sudo apt-get install postgresql-client
sudo apt-get install postgresql-contrib
pgbench -f insert_test.sql -r -c 100 -j 20 -M prepared -n -T 600 -p 5432 -h $host -U $user -d $database --define "sn='3rdskwefoik'" --define "json_data={\"a\":1,\"b\":12390123}"
TimescaleDB,Kafka + ELT + Doris/TiDB/Clickhouse/HBase 至于 InfluxDB 之流的时序数据库,针对特定场景优化做到极致,反而到常规场景下就很难受,不是极端适配场景,还是不要轻易采用。
PG 常用运维指令
-- 创建新Database + 用户授权
create database db;
create user db_user with password 'db_password';
alter database db owner to db_user;
锁
pg-locks表 锁类型
查看当前链接服务进程pid
查看当前锁
状态统计
统计收集器
常见问题
slowCount pg Cluster
Kafka Connector
kafka-sink-pg-json 未测试呢,先记着