博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Postgresql内存表
阅读量:2395 次
发布时间:2019-05-10

本文共 3308 字,大约阅读时间需要 11 分钟。

目前Postgresql9.X以前版本里并没有什么内存表创建,不像mysql一样,有相关的创建语句。目前网上更多的是采用系统的方式。

也就是创建一个目录,然后加载到内存区里,然后让Postgresql的表空间映射到此目录即可。

特将相关的命令转发过来:

mount -t ramfs swap /tmp/ram
chown -R postgres /tmp/ram
CREATE TABLESPACE ramtablespace LOCATION '/tmp/ram';
CREATE TABLE ramtable(id serial primary key USING INDEX TABLESPACE ramtablespace) TABLESPACE ramtablespace;

如果加载到内存里的话,就不能随便删除。

加载命令是:

 mount -t ramfs none /RAM1/ -o maxsize=10000

首先要创建一个目录,这里是RAM1目录
删除时,如图:

多次运行mount命令会覆盖之前的内容。
关于ramfs的特点如下:

好处:内存,当然有着极高的读写速度,操作时通用文件系统接口

应用场景:对文件系统读写频繁,文件不大的地方!

坏处:一旦down机,需要重新运行挂载命令,信息将不存在

使用心得:
常规操作很快(insert/update/delete),但是有时候就将无法正常启动,报错信息如下:
[root@chenyi3 16422]# /etc/init.d/postgresql-9.1 start
Starting PostgreSQL 9.1: 
waiting for server to start........ stopped waiting
pg_ctl: could not start server
Examine the log output.
PostgreSQL 9.1 did not start in a timely fashion, please see /opt/PostgreSQL/9.1/data/pg_log/startup.log for details
[root@chenyi3 16422]# 
日志里的内容如下:
[root@chenyi3 pg_log]# cat postgresql-2011-12-01_154553.log
2011-12-01 15:45:53 CST LOG:  database system was interrupted while in recovery at 2011-12-01 15:41:33 CST
2011-12-01 15:45:53 CST HINT:  This probably means that some data is corrupted and you will have to use the last backup for recovery.
2011-12-01 15:45:53 CST LOG:  database system was not properly shut down; automatic recovery in progress
2011-12-01 15:45:53 CST LOG:  consistent recovery state reached at 0/2650890
2011-12-01 15:45:53 CST LOG:  redo starts at 0/2650890
2011-12-01 15:45:53 CST FATAL:  tablespace 16447 is not empty
2011-12-01 15:45:53 CST CONTEXT:  xlog redo drop ts: 16447
2011-12-01 15:45:53 CST LOG:  startup process (PID 17895) exited with exit code 1
2011-12-01 15:45:53 CST LOG:  aborting startup due to startup process failure

解决方式:

重新运行挂载命令: mount -t ramfs none /RAM1/ -o maxsize=10000

另一种方式是tmpfs的方式

命令如下:

mount tmpfs /RAM2/ -t tmpfs -o size=10M

不管是使用ramfs还是tmpfs,必须明白的是,一旦系统重启(这里是指机器重启,并不是postresql应用重启),它们中的内容将会丢失。所以那些东西可以放在内存文件系统中得根据系统的具体情况而定。

目前还需要找些其他的替代方式。
目前有一种第三方的工具pgfincore(用于9.1以前的版本) ,就是用来避免这种失效问题。目前我还没用过这个东东,接着会体验下这个工具。

目前在9.1以后的版本(包含这个版本),已经有了新特性UNLOGGED出来(可以提高写的速度),如下:

UNLOGGED

If specified, the table is created as an unlogged table. Data written to unlogged tables is not written to the write-ahead log (see ), which makes them considerably faster than ordinary tables. However, they are not crash-safe: an unlogged table is automatically truncated after a crash or unclean shutdown. The contents of an unlogged table are also not replicated to standby servers. Any indexes created on an unlogged table are automatically unlogged as well; however, unlogged  are currently not supported and cannot be created on an unlogged table.

UNLOGGED特性是在创建表的时候使用。语法如下:

CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [  { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]    | table_constraint    | LIKE parent_table [ like_option ... ] }    [, ... ]] )[ INHERITS ( parent_table [, ... ] ) ][ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ][ TABLESPACE tablespace ]
详细文档参考网址:

接着我在9.1的版本上体验下这个新特性,后面会给出测试结果。

该特性用来不错,正常关闭系统和启动,表里面的内容不会删除,但是一旦是非正常关闭(如kill掉),那么表里面的内容会清空(表还是存在的),不过此特性体现在写速度还不错,不过还是不是真正意义上的内存表的机制,

转载地址:http://vpzob.baihongyu.com/

你可能感兴趣的文章
Spring的认识
查看>>
maven项目出现如下错误,求指点;CoreException: Could not calculate build plan:
查看>>
理解Paxos算法的证明过程
查看>>
详解 JVM Garbage First(G1) 垃圾收集器
查看>>
Java 8 函数式编程入门之Lambda
查看>>
用高阶函数轻松实现Java对象的深度遍历
查看>>
WindowsApi+Easyx图形库的透明时钟
查看>>
Eclipse LUNA配置TomCat(非j2ee版本)
查看>>
树莓派安装mysql-srver报错 404 not found!
查看>>
Ubuntu 14.04LTS 下安装.net框架
查看>>
Eclipse 配置Groovy语言环境 && Java工程运行Groovy
查看>>
人工智能术语表
查看>>
Tensorflow Python API 翻译(sparse_ops)
查看>>
Tensorflow Python API 翻译(math_ops)(第一部分)
查看>>
Tensorflow Python API 翻译(math_ops)(第二部分)
查看>>
课程---程序员炒股,如何计算股票投资组合的风险和收益
查看>>
人工智能资料库:第3辑(20170107)
查看>>
人工智能资料库:第22辑(20170131)
查看>>
人工智能资料库:第23辑(20170201)
查看>>
MongoDB-初体验
查看>>