User Tools

Site Tools


ramfs_based_table_space

Tablespace in Memory

Setting up a tablespace

From PostgreSQL documentation:

Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files
representing database objects can be stored. Once created, a tablespace can be referred to by name when creating
database objects.The location must be an existing, empty directory that is owned by the PostgreSQL system user.
All objects subsequently created within the tablespace will be stored in files underneath this directory.
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary
database users to make use of it. To do that, grant them the CREATE privilege on it.

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 databases

To 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 database

We 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
To restore the database schema:
     psql --file $FILENAME -U $ROLE_NAME --dbname postgres --host $HOST

Automating the process

To 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:

Dump database schema:
  pg_dump --create --schema-only --file=/root/memdb.sql --host=localhost -U postgres memdb

Stop postmaster (original init code)

Unmount ramfs:
         umount /mnt/ramfs

When starting the database server:

Mount ramfs:
    if [ ! -d /mnt/ramfs ]; then
        mkdir -p /mnt/ramfs
    fi
    mount -t ramfs none /mnt/ramfs
Recreate PostgreSQL tablespace directory:
    	mkdir /mnt/ramfs/pgdata
    	echo $PGMAJORVERSION > /mnt/ramfs/pgdata/PG_VERSION
    	chown -R postgres:postgres /mnt/ramfs/pgdata
    	chmod -R go-rwx /mnt/ramfs/pgdata
Start postmaster (original init code)

Drop the database because it exists in PostgreSQL system tables only.

         psql --quiet -U postgres --host localhost --command "DROP DATABASE memdb;" 2>/root/psql.log
Restore the database schema:
         psql --quiet --file /root/memdb.sql -U $ROLE_NAME -d postgres --host localhost 2>/root/psql.log

Summary

You have just learned how to set up memory storage for the PostgreSQL database server.

Referance

ramfs_based_table_space.txt · Last modified: 2020/08/10 02:35 (external edit)