
| Tips and tricks: Memory storage on PostgreSQL |
| 摘自: www.redhatmagazine.com 被阅读次数: 143 |
由 yangyi 于 2008-01-19 22:46:53 提供 |
by Alexander TodorovIntroductionPostgreSQL is very sophisticated and powerful database server to use with Red Hat Enterprise Linux. However, many people argue that one of the features it lacks is the memory storage engine of MySQL. This storage engine is ideal for applications that process temporary data that is updated rarely but accessed many times. These applications use databases stored in memory to decrease the number of disc operations. An example of such an application is the web-based BitTorrent tracker. In this tip, you will learn how to set up memory storage for PostgreSQL using standard features provided by the database server itself and the Red Hat Enterprise Linux operating system. Creating memory filesystemFirst we will need a place in memory where all database content will be stored. To create a filesystem in memory use the command:
This will create a filesystem with the following features:
Setting up a tablespaceFrom PostgreSQL documentation:
First create the directory where tablespace will be stored. It must reside on your ramfs filesystem. mkdir /mnt/ramfs/pgdata
chown postgres:postgres /mnt/ramfs/pgdata
chmod go-rwx /mnt/ramfs/pgdata
To create the tablespace and grant permissions on it: CREATE TABLESPACE $TABLESPACE_NAME LOCATION '/mnt/ramfs/pgdata'; GRANT CREATE ON TABLESPACE $TABLESPACE_NAME TO $ROLE_NAME; In addition to setting up entries in internal PostgreSQL tables, a file named “PG_VERSION” will be created under the tablespace directory. Get familliar with it and all other possible files (if) created because we need to restore this structure later. We are defining the tablespace into `template1′ database. Normally all other databases inherit from `template1′. This way we don’t need to redefine it every time and it can be used for all subsequently created objects in the database. It is not good idea for an application to access the database as superuser. Create a new role to use with your application. Warning: Depending on your configuration you may need to tune SELinux policy to allow user `postgres’ access to the tablespace directory. Creating databasesTo create a new database in the tablespace use the command: CREATE DATABASE $DATABASE_NAME WITH TABLESPACE = $TABLESPACE_NAME; All other objects created within this database will be stored in the same tablespace. Tablespaces can be used when creating tables as well. Dumping and restoring the databaseWe need to dump and restore the database structure every time the ramfs filesystem is unmounted. Applications using memory databases should be aware of data loss and responsible for its recreation. To dump the database schema: pg_dump --create --schema-only --file=$FILENAME --host=$HOST -U postgres $DATABASE_NAME psql --file $FILENAME -U $ROLE_NAME --dbname postgres --host $HOST Automating the processTo make this setup work every time when the server is restarted, we need to edit `/etc/init.d/postgresql’. Make sure the items appear in the correct order. When stopping the database server:
When starting the database server:
SummaryYou have just learned how to set up memory storage for the PostgreSQL database server. In addition to all PostgreSQL features, when using memory databases, it does not have the limitations imposed by MySQL. For a complete setup you may tweak your init script as you wish. 原文链接: http://www.redhatmagazine.com/2007/12/12/tip-from-an-rhce-memory-storage-on-postgresql/ |