【重新发现PostgreSQL之美】- 7 垂帘听政 异步消息

本文涉及的产品
云数据库 PolarDB MySQL 版,列存表分析加速 8核16GB
PolarSearch,搜索节点 4核8GB
PolarDB Agent Express,2核4GB
简介: 大家好,这里是重新发现PostgreSQL之美 - 7 垂帘听政 异步消息

背景


场景:

  • 重要数据在写入、更新、实时告警或转
  • 流式数据(务车电围栏、刑数据探、股票数据规则、服器运行情况) 实时预警或事件触发
  • 操作(DDL) 异步

规则+ 异步消息的优势:
1
、通过规则过滤掉不需要写入的正常数据, 由于业务正常数据通常占比在99%以上, 从而大幅减轻写入量.
2
、传统的利用定时器查询所有数据去发现问题, 还需要在时间、VALSID等层面去建立索引, 消耗大量存储, 同时索引增加写入RT,性能下降. 规则+异步完全规避这个问题.
3
、可以实时发现并预警或触发其他动作


postgres=# \h create rule  

Command:     CREATE RULE  

Description: define a new rewrite rule  

Syntax:  

CREATE [ OR REPLACE ] RULE name AS ON event  

   TO table_name [ WHERE condition ]  

   DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command ... ) }  

 

where event can be one of:  

 

   SELECT | INSERT | UPDATE | DELETE  

 

URL: https://www.postgresql.org/docs/14/sql-createrule.html  

 

 

postgres=# \h listen  

Command:     LISTEN

Description: listen for a notification  

Syntax:  

LISTEN channel  

 

URL: https://www.postgresql.org/docs/14/sql-listen.html  

 

postgres=# \h notify  

Command:     NOTIFY

Description: generate a notification  

Syntax:  

NOTIFY channel [ , payload ]  

 

URL: https://www.postgresql.org/docs/14/sql-notify.html  

 

postgres=# \df *.*channel*  

                                List of functions  

  Schema  |         Name          | Result data type | Argument data types | Type  

------------+-----------------------+------------------+---------------------+------  

pg_catalog | pg_listening_channels | SETOF text       |                     | func  

(1 row)  

 

postgres=# \df *.*notify*  

                          List of functions  

  Schema  |   Name    | Result data type | Argument data types | Type  

------------+-----------+------------------+---------------------+------  

pg_catalog | pg_notify | void             | text, text          | func  

(1 row)  

例子


机房传感器

create table tbl_sensor_log (  

id serial8 primary key,  

sid int,  

val jsonb,  

crt_time timestamp  

);  

定义规则, 发现异常数据向alert通道发送消息

create or replace rule r1 as on insert  

to tbl_sensor_log  

where coalesce(val['temp']::float4,0) >= 60  

or coalesce(val['cpu_perct']::float4,0) >= 80

or coalesce(val['mem_perct']::float4,0) >= 80

or coalesce(val['io_perct']::float4,0) >= 80

do also  

select pg_notify('alert', format('sensor: %s, ts:%s, val:%s', NEW.sid, NEW.crt_time, NEW.val));  

定义规则(可选), 正常数据不写入

create or replace rule r2 as on insert  

to tbl_sensor_log  

where not (coalesce(val['temp']::float4,0) >= 60  

or coalesce(val['cpu_perct']::float4,0) >= 80

or coalesce(val['mem_perct']::float4,0) >= 80

or coalesce(val['io_perct']::float4,0) >= 80)

do instead NOTHING;  

postgres=# \d+ tbl_sensor_log;  

                                                                Table "public.tbl_sensor_log"

 Column |            Type             | Collation | Nullable |                  Default                   | Storage  | Compression | Stats target | Description  

----------+-----------------------------+-----------+----------+--------------------------------------------+----------+-------------+--------------+-------------  

id      | bigint                     |           | not null | nextval('tbl_sensor_log_id_seq'::regclass) | plain    |             |              |  

sid     | integer                    |           |          |                                            | plain   |             |              |  

val     | jsonb                      |           |          |                                            | extended | pglz        |              |  

crt_time | timestamp without time zone |           |         |                                            | plain    |             |              |  

Indexes:  

   "tbl_sensor_log_pkey" PRIMARY KEY, btree (id)  

Rules:  

   r1 AS

   ON INSERT TO tbl_sensor_log  

  WHERE COALESCE(new.val['temp'::text]::real, 0::real) >= 60::double precision OR COALESCE(new.val['cpu_perct'::text]::real, 0::real) >= 80::double precision OR COALESCE(new.val['mem_perct'::text]::real, 0::real) >= 80::double precision OR COALESCE(new.val['io_perct'::text]::real, 0::real) >= 80::double precision DO  SELECT pg_notify('alert'::text, format('sensor: %s, val:%s'::text, new.sid, new.val)) AS pg_notify  

Access method: heap  

压测

CREATE TYPE sensor_js AS (temp float4, cpu_perct float4, mem_perct float4, io_perct float4);  

 

insert into tbl_sensor_log (sid,val,crt_time)  

values (  

 1,  

row_to_json(row(1,80.1,2,99.11)::sensor_js)::jsonb,  

 now()  

);  

 

 

vi test.sql  

\set sid random(1,1000000)  

\set v1 random(1,61)  

\set v2 random(1,81)  

\set v3 random(1,81)  

\set v4 random(1,81)  

insert into tbl_sensor_log (sid,val,crt_time)  

values (:sid, row_to_json(row(:v1,:v2,:v3,:v4)::sensor_js)::jsonb,now());  

 

 

pgbench -M prepared -n -r -P 1 -f ./test.sql -c 5 -j 5 -T 120  

开启其他会话, 监听alert这个通道的异步消息.

PG 的异步消息为广播模式. 可以在多个会话监听同一个通道, 如果有多个业务希望接收同一类异步消息, 则可以这么做.

listen alter;  

 

Asynchronous notification "alert" with payload "sensor: 459294, val:{"temp": 32, "io_perct": 81, "cpu_perct": 76, "mem_perct": 39}" received from server process with PID 1715.  

Asynchronous notification "alert" with payload "sensor: 788337, val:{"temp": 60, "io_perct": 34, "cpu_perct": 12, "mem_perct": 53}" received from server process with PID 1714.  

Asynchronous notification "alert" with payload "sensor: 421071, val:{"temp": 7, "io_perct": 81, "cpu_perct": 12, "mem_perct": 14}" received from server process with PID 1716.  

Asynchronous notification "alert" with payload "sensor: 523366, val:{"temp": 13, "io_perct": 45, "cpu_perct": 70, "mem_perct": 80}" received from server process with PID 1713.  

Asynchronous notification "alert" with payload "sensor: 94909, val:{"temp": 57, "io_perct": 1, "cpu_perct": 32, "mem_perct": 81}" received from server process with PID 1713.  

Asynchronous notification "alert" with payload "sensor: 13910, val:{"temp": 61, "io_perct": 39, "cpu_perct": 39, "mem_perct": 2}" received from server process with PID 1714.  

Asynchronous notification "alert" with payload "sensor: 252342, val:{"temp": 7, "io_perct": 31, "cpu_perct": 80, "mem_perct": 13}" received from server process with PID 1714.  

Asynchronous notification "alert" with payload "sensor: 222983, val:{"temp": 56, "io_perct": 76, "cpu_perct": 80, "mem_perct": 25}" received from server process with PID 1715.  

Asynchronous notification "alert" with payload "sensor: 913661, val:{"temp": 60, "io_perct": 23, "cpu_perct": 80, "mem_perct": 9}" received from server process with PID 1716.  

压测数据分析:

1、在不开启rule, 写入速度比开启rule, 因为rule里面有CPU运算. 增加了RT.

但是这是纯计算, 没有IO, 内存等开销. 总体效率绝对比定时器后查询快很多.

progress: 1.0 s, 63373.9 tps, lat 0.078 ms stddev 0.066

progress: 2.0 s, 67591.2 tps, lat 0.074 ms stddev 0.044

progress: 3.0 s, 66330.3 tps, lat 0.075 ms stddev 0.039

progress: 4.0 s, 65786.8 tps, lat 0.076 ms stddev 0.038

progress: 5.0 s, 65436.3 tps, lat 0.076 ms stddev 0.043

progress: 6.0 s, 64276.1 tps, lat 0.077 ms stddev 0.042

progress: 7.0 s, 59162.6 tps, lat 0.084 ms stddev 0.045

progress: 8.0 s, 53887.5 tps, lat 0.092 ms stddev 0.048

progress: 1.0 s, 43413.8 tps, lat 0.114 ms stddev 0.084

progress: 2.0 s, 42803.5 tps, lat 0.116 ms stddev 0.040

progress: 3.0 s, 40092.0 tps, lat 0.124 ms stddev 0.176

progress: 4.0 s, 41419.0 tps, lat 0.120 ms stddev 0.046

progress: 5.0 s, 41637.6 tps, lat 0.120 ms stddev 0.040

progress: 6.0 s, 41918.2 tps, lat 0.119 ms stddev 0.040

progress: 7.0 s, 41753.3 tps, lat 0.119 ms stddev 0.038

progress: 8.0 s, 35983.6 tps, lat 0.139 ms stddev 0.042

mac book pro上数据轻松破百万

postgres=# select count(*) from tbl_sensor_log;  

 count  

---------  

2624221

(1 row)  

其他异步消息

202103/20210311_03.md  Postgres Notify for Real Time Dashboards

201807/20180716_01.md  PostgreSQL 异步消息(LISTEN/NOTIFY)缓存多大?》

201807/20180713_03.md  PostgreSQL 流式- 二手商品实时归类(异步消息notify/listen后即焚)

201711/20171111_01.md  PostgreSQL 异步消息- Feed统实时监测与响(电商主动服务) - 钟级到毫秒实现

201710/20171018_03.md  [未完待] PGQ 异步消息列的使用》

201709/20170925_02.md  PostgreSQL 事件触- DDL审计记录+ 异步通知(notify)

201701/20170116_01.md  《从波表到数据小程序之- 数据异步广播(notify/listen)

201111/20111122_01.md  PostgreSQL Notify/Listen Like ESB

201701/20170113_03.md  《从微信小程序数据"小程序" , 鬼知道我经历了什么》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
缓存 关系型数据库 数据库
|
9月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
570 158
|
9月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
9月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1467 152
|
9月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1068 156
|
9月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(中)
使用MYSQL Report分析数据库性能
610 156
|
9月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(上)
最终建议:当前系统是完美的读密集型负载模型,优化重点应放在减少行读取量和提高数据定位效率。通过索引优化、分区策略和内存缓存,预期可降低30%的CPU负载,同时保持100%的缓冲池命中率。建议每百万次查询后刷新统计信息以持续优化
717 161
|
10月前
|
存储 运维 关系型数据库
从MySQL到云数据库,数据库迁移真的有必要吗?
本文探讨了企业在业务增长背景下,是否应从 MySQL 迁移至云数据库的决策问题。分析了 MySQL 的优势与瓶颈,对比了云数据库在存储计算分离、自动化运维、多负载支持等方面的优势,并提出判断迁移必要性的五个关键问题及实施路径,帮助企业理性决策并落地迁移方案。

推荐镜像

更多