PostgreSQL 内存表

背景

在某些场景,要求快速的DML,并且对数据可靠性要求不是非常高。

例如游戏的会话信息,传感器上传的最新数据,运算的中间结果,等等。
screenshot
例如在一个场景中,有非常多的传感器的数据要不断的被更新和查询,可以使用这种方法,每个传感器的ID哈希后分配给对应的会话,这个传感器。

上面的需求,PostgreSQL 临时表都能满足。

但是临时表也存在一定的限制或弊端。

临时表为会话级内存表,跨会话不共享数据和结构,不写REDO。
超过一定大小时会落盘,不是纯内存的。
同时临时表继承了普通表的多版本,但是实际上会话级不需要多版本。
会话断开需要重建临时表结构。

虽然现在的SSD IO能力很强,但是与内存还有一定的插件,同时SSD 擦写次数受限,所以临时表或者普通表难以满足本文开头提到的场景需求。

Oracle 12C 推出列存储内存表,但是它是基于OLAP的应用场景,并不适合本文开头提到的场景。

PostgreSQL 社区也在考虑增加内存表的功能,本文是一个引子,可以看到社区在这方面的努力。

PostgreSQL内存表之路

在postgrespro发表的postgres roadmap中,可以看到,他们正在搞热插拔的存储引擎。

https://wiki.postgresql.org/wiki/Postgres_Professional_roadmap

Pluggable storages

We achieved a significant progress in PostgreSQL extendability: 
  FDWs, custom access methods, generic WAL. And we're not so far from having pluggable storage engines. 
  Concept of API will be presented at PGCon.

We are planning to implement the following types of storage engines:

In-memory row-oriented storage engine with optional support of transactions and optional support of persistency;
Columnar storage engine;
In-memory columnar storage engine;
On-disk row-oriented storage engine with undo-log for better bloat control.

行式内存引擎、列式内存引擎、列存磁盘存储引擎、以及回滚段模式的行式磁盘存储引擎。

目前还没有看到代码方面的输出,但是在postgrespro的项目里有一个与内存表非常类似的项目,会话级变量。

数据保存在内存中,目前支持如下操作
https://github.com/postgrespro/pg_variables

操作看起来是不是有点像redis呢?

Integer variables

FunctionReturns
pgv_set_int(package text, name text, value int)void
pgv_get_int(package text, name text, strict bool default true)int

Text variables

FunctionReturns
pgv_set_text(package text, name text, value text)void
pgv_get_text(package text, name text, strict bool default true)text

Numeric variables

FunctionReturns
pgv_set_numeric(package text, name text, value numeric)void
pgv_get_numeric(package text, name text, strict bool default true)numeric

Timestamp variables

FunctionReturns
pgv_set_timestamp(package text, name text, value timestamp)void
pgv_get_timestamp(package text, name text, strict bool default true)timestamp

Timestamp with timezone variables

FunctionReturns
pgv_set_timestamptz(package text, name text, value timestamptz)void
pgv_get_timestamptz(package text, name text, strict bool default true)timestamptz

Date variables

FunctionReturns
pgv_set_date(package text, name text, value date)void
pgv_get_date(package text, name text, strict bool default true)date

Jsonb variables

FunctionReturns
pgv_set_jsonb(package text, name text, value jsonb)void
pgv_get_jsonb(package text, name text, strict bool default true)jsonb

同样支持集合哦

Records

The following functions are provided by the module to work with collections of record types.

To use pgv_update(), pgv_delete() and pgv_select() functions required package and variable must exists.
Otherwise the error will be raised.
It is necessary to set variable with pgv_insert() function to use these functions.

pgv_update(), pgv_delete() and pgv_select() functions check the variable type.
If the variable type does not record type the error will be raised.

FunctionReturnsDescription
pgv_insert(package text, name text, r record)voidInserts a record to the variable collection. If package and variable do not exists they will be created. The first column of r will be a primary key. If exists a record with the same primary key the error will be raised. If this variable collection has other structure the error will be raised.
pgv_update(package text, name text, r record)booleanUpdates a record with the corresponding primary key (the first column of r is a primary key). Returns true if a record was found. If this variable collection has other structure the error will be raised.
pgv_delete(package text, name text, value anynonarray)booleanDeletes a record with the corresponding primary key (the first column of r is a primary key). Returns true if a record was found.
pgv_select(package text, name text)set of recordReturns the variable collection records.
pgv_select(package text, name text, value anynonarray)recordReturns the record with the corresponding primary key (the first column of r is a primary key).
pgv_select(package text, name text, value anyarray)set of recordReturns the variable collection records with the corresponding primary keys (the first column of r is a primary key).

下面更像redis了

Miscellaneous functions

FunctionReturnsDescription
pgv_exists(package text, name text)boolReturns true if package and variable exists.
pgv_remove(package text, name text)voidRemoves the variable with the corresponding name. Required package and variable must exists, otherwise the error will be raised.
pgv_remove(package text)voidRemoves the package and all package variables with the corresponding name. Required package must exists, otherwise the error will be raised.
pgv_free()voidRemoves all packages and variables.
pgv_list()table(package text, name text)Returns set of records of assigned packages and variables.
pgv_stats()table(package text, used_memory bigint)Returns list of assigned packages and used memory in bytes.

Note that pgv_stats() works only with the PostgreSQL 9.6 and newer.

目前数据仅支持会话级,会话断开则自动释放,期待真正的内存表引擎吧,这只是个引子。

存储逻辑结构

screenshot

术语

package : 包名
name : 变量名
value : 标量类型的值
r : 集合类型的单条记录
pk : 集合类型的主键

测试

安装内存表插件

export PATH=/home/digoal/pgsql9.6/bin:$PATH

git clone https://github.com/postgrespro/pg_variables

cd pg_variables/

make USE_PGXS=1

make USE_PGXS=1 install

make USE_PGXS=1 installcheck

postgres=# create extension pg_variables;
CREATE EXTENSION

标量测试

postgres=# select pgv_set_int('pkg1','k1',100);
 pgv_set_int 
-------------
 
(1 row)

postgres=# select pgv_get_int('pkg1','k1');
 pgv_get_int 
-------------
         100
(1 row)

postgres=# select pgv_set_jsonb('pkg1','k2','{"a":"b", "c":{"hello":"digoal"}}');
 pgv_set_jsonb 
---------------
 
(1 row)

postgres=# select pgv_get_jsonb('pkg1','k2');
            pgv_get_jsonb             
--------------------------------------
 {"a": "b", "c": {"hello": "digoal"}}
(1 row)

更新与自增用法

postgres=# select pgv_set_int(pkg,k, pgv_get_int(pkg,k)+1 ) from (values ('pkg1','k1')) t(pkg,k);
 pgv_set_int 
-------------
 
(1 row)

postgres=# select pgv_get_int('pkg1','k1');
 pgv_get_int 
-------------
         102
(1 row)

postgres=# select pgv_set_int(pkg,k, pgv_get_int(pkg,k)+1 ) from (values ('pkg1','k1')) t(pkg,k);
 pgv_set_int 
-------------
 
(1 row)

postgres=# select pgv_get_int('pkg1','k1');
 pgv_get_int 
-------------
         103
(1 row)

性能,每秒标量更新达到了239万次。

postgres=# select count(*) from (select pgv_set_int('pkg1','k1',id) from generate_series(1,10000000) t(id) ) t;
  count   
----------
 10000000
(1 row)
Time: 4185.179 ms

postgres=# select pgv_get_int('pkg1','k1');
 pgv_get_int 
-------------
    10000000
(1 row)
Time: 0.470 ms

postgres=# select 10000000/4.185;
       ?column?       
----------------------
 2389486.260454002389
(1 row)
Time: 0.869 ms

集合测试

postgres=# select pgv_insert('pkg2', 'k1', row(1::int, 'hello world'::text, current_date::date));
 pgv_insert 
------------
 
(1 row)

postgres=# select * from pgv_select('pkg2', 'k1') as t(c1 int,c2 text,c3 date);
 c1 |     c2      |     c3     
----+-------------+------------
  1 | hello world | 2016-08-18
(1 row)

postgres=# select count(*) from (select pgv_insert('pkg2', 'k1', row(c1,'test'::text,current_date::date)) from generate_series(2,100000) t(c1)) t;
 count 
-------
 99999
(1 row)

postgres=# select * from pgv_select('pkg2', 'k1', array[1,2,3]) as t(c1 int,c2 text,c3 date);
 c1 |     c2      |     c3     
----+-------------+------------
  1 | hello world | 2016-08-18
  2 | test        | 2016-08-18
  3 | test        | 2016-08-18
(3 rows)

内存表和普通表的JOIN

postgres=# select t1.*,t2.* from (select * from pgv_select('pkg2', 'k1') as t(c1 int,c2 text,c3 date)) t1, tbl1 t2 where t1.c1=t2.id and t2.id<10;
 c1 |     c2      |     c3     | id |               info               
----+-------------+------------+----+----------------------------------
  8 | test        | 2016-08-18 |  8 | a8a7e0f849c5895820bbca32d7e798b1
  4 | test        | 2016-08-18 |  4 | f6954fb12336881d590fa7a50dd03916
  9 | test        | 2016-08-18 |  9 | 45ff843fcd5372e525368829f9846def
  5 | test        | 2016-08-18 |  5 | d8afe53f0a7d553716caa9ffaef7ea3d
  7 | test        | 2016-08-18 |  7 | 2b20f485974500d7b3ecb1f4c1d0f975
  2 | test        | 2016-08-18 |  2 | 3d36418926b2e0e2dc7090da17e39451
  6 | test        | 2016-08-18 |  6 | 6923416bbca7634f01f7f79030609f64
  1 | hello world | 2016-08-18 |  1 | 3bb6c833f1b10139edf7e2f2eb4f4a69
  3 | test        | 2016-08-18 |  3 | de5b51374e1db3ccac9c61af75b69a33
(9 rows)

更新与删除内存表的数据

postgres=# select pgv_update('pkg2', 'k1', t) from (select c1,'new val'::text,'2017-01-01'::date from pgv_select('pkg2', 'k1', 1) as tb(c1 int, c2 text, c3 date)) t;
 pgv_update 
------------
 t
(1 row)

Time: 0.665 ms

postgres=# select * from pgv_select('pkg2', 'k1', 1) as tb(c1 int, c2 text, c3 date);
 c1 |   c2    |     c3     
----+---------+------------
  1 | new val | 2017-01-01
(1 row)

Time: 0.518 ms

postgres=# select  pgv_delete('pkg2', 'k1', 1);
 pgv_delete 
------------
 t
(1 row)

Time: 0.440 ms

管理内存对象

postgres=# select * from pgv_exists('pkg1','k1');
 pgv_exists 
------------
 f
(1 row)

Time: 0.491 ms

postgres=# select pgv_list();
 pgv_list  
-----------
 (pkg2,k1)
(1 row)

Time: 0.455 ms

postgres=# select pgv_stats();
    pgv_stats    
-----------------
 (pkg2,16785408)
(1 row)

Time: 0.514 ms

postgres=# select pgv_remove('pkg2','k1');
 pgv_remove 
------------
 
(1 row)

Time: 1.868 ms
postgres=# select pgv_stats();
  pgv_stats   
--------------
 (pkg2,24576)
(1 row)

Time: 0.367 ms
postgres=# select pgv_remove('pkg2');
 pgv_remove 
------------
 
(1 row)

Time: 0.415 ms
postgres=# select pgv_stats();
 pgv_stats 
-----------
(0 rows)

Time: 0.369 ms

数据持久化

postgres=# select count(*) from (select pgv_insert('pkg2', 'k1', row(c1,'test'::text,current_date::date)) from generate_series(2,10000000) t(c1)) t;
  count  
---------
 9999999
(1 row)

在事务中持久化数据  
postgres=# begin;
postgres=# create table tbl as select * from pgv_select('pkg2','k1') as t(c1 int, c2 text, c3 date);
postgres=# end;

postgres=# select count(*) from tbl;
  count  
---------
 9999999
(1 row)

祝大家玩得开心,欢迎随时来 阿里云促膝长谈业务需求 ,恭候光临

阿里云的小伙伴们加油,努力 做好内核与服务,打造最贴地气的云数据库

PostgreSQL 是一个功能强大的开源关系型数据库管理系统,对于大查询的优化通常涉及多个方面,旨在提高性能和资源利用率。以下是几个关键的优化策略: 1. **分区(Partitioning)**:将大分解成更小、更易管理的部分,比如按时间范围、键值等进行分区,这有助于减少I/O操作并加快查询速度。 2. **索引(Indexing)**:创建合适的B树或哈希索引可以帮助快速定位数据,特别是对于经常用于 WHERE 子句中的列。避免全扫描是非常重要的。 3. **批量处理(Batch Processing)**:通过调整 `设置work_mem` 或 `maintenance_work_mem` 参数,控制一次性加载到内存的数据量,减少磁盘 I/O 操作。 4. **查询优化器(Query Planner)**:理解并使用 `EXPLAIN` 命令分析查询计划,找出瓶颈,可能需要调整JOIN顺序或添加合适的连接条件。 5. **材料化视图(Materialized Views)**:预先计算结果并存储起来,当后续查询时可以直接使用,减少了实时计算的负担。 6. **存储过程(Stored Procedures)**:使用存储过程可以封装复杂的业务逻辑,减少网络传输,并且在执行过程中可能有更好的缓存机会。 7. **分区合并(Partition Consolidation)**:定期检查并手动或自动合并分区,以减少的数量和大小。 8. **分片(Sharding)**:在分布式环境下,将数据分布在多个物理服务器上,如果应用支持,可以显著提高读写性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值