How to do indexing, partition, and migration in Postgres 10

Revision as of 21:40, 3 August 2018 by Jizhou (talk | contribs)
Jump to navigation Jump to search

This tutorial shows how to do data migration, partition, and indexing in Postgres 10.

1. Change data directory

The dataset is quite large, and it will take up about 220GB of hard disk once loaded and indexed into Postgres. It is better to move the database storage to another large disk instead of the default (root) one. A Solid State Disk (SSD) is preferred for faster disk access.

(1) Check current Postgres data directory.

sudo -i 
su - postgres
SHOW data_directory;

(1 row)

(2) Quit all Postgres Processes and terminate Postgres service.

\q     # quit from psql
sudo systemctl stop postgresql-10            # stop postgres
sudo systemctl status postgresql-10         # check its status

(3) Create new folder in destination directory

cd /ssd/disk1             
mkdir psql_10_data                                       # create a new folder
chown postgres psql_10_data                     # Postgres requires exclusive ownership and access to the data directory. 
chmod 700 psql_10_data                             # Change read, write, and execute authority of this folder. No group or world access to this folder. Required by Postgres.

(4) Copy current data and configure files to new folder

cp -R /var/lib/pgsql/10/data/* /ssd/disk1/psql_10_data/

(5) Point to the new data location

vim /var/lib/pgsql/10/data/postgresql.conf       # Edit the configuration file in old folder.

data_directory = '/ssd/disk1/psql_10_data'              # use data in another directory

(6) Restart PostgreSQL service

sudo systemctl start postgresql-10

Trouble shooting, you may encounter the following error message:

● postgresql-10.service - PostgreSQL 10 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2018-07-24 13:04:11 PDT; 38s ago
  Process: 23952 ExecStart=/usr/pgsql-10/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
  Process: 23946 ExecStartPre=/usr/pgsql-10/bin/postgresql-10-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 23952 (code=exited, status=1/FAILURE)

Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og systemd[1]: Starting PostgreSQL 10 database server...
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og postmaster[23952]: 2018-07-24 13:04:11.944 PDT [23952] LOG:
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og postmaster[23952]: 2018-07-24 13:04:11.944 PDT [23952] HINT:  Is...y.
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og postmaster[23952]: 2018-07-24 13:04:11.944 PDT [23952] LOG:
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og systemd[1]: postgresql-10.service: main process exited, code=ex...URE
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og systemd[1]: Failed to start PostgreSQL 10 database server.
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og systemd[1]: Unit postgresql-10.service entered failed state.
Jul 24 13:04:11 yod.cluster.ucsf.bkslab.og systemd[1]: postgresql-10.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

Job for postgresql-10.service failed because the control process exited with error code. See "systemctl status postgresql-10.service" and "journalctl -xe" for details.

journalctl -xe               # check the error message
There is a ******.lock file block access to the PostgreSQL service. Change the ownership or access roles of this file to solve this problem.

(7) Restart psql and check data directory again.

postgres=# SHOW data_directory;

(1 row)

2. Create Partition Tables and Load Data into Tables

In Postgres, a table can be partitioned by certain attributes. Partition speedup search, because only need to focus on a subset of data. We can modify a partition (child) table without effecting the whole table. An automate script is /var/lib/pgsql/script/

(1) Create partition table (parent). Partition on trache_name, a natural way to partition the dataset.

    smiles varchar(256) NOT NULL, 
    zinc_id varchar(128) NOT NULL, 
    inchikey varchar(128), 
    mwt REAL NOT NULL, 
    logp REAL NOT NULL, 
    reactive REAL NOT NULL, 
    purchase INT NOT NULL, 
    tranche_name varchar(32) NOT NULL, 
    feature text) PARTITION BY LIST(tranche_name);

(2) Create partition table(child).

CREATE TABLE IF NOT EXISTS $tablename PARTITION OF smile_partition FOR VALUES IN ('$prefix');   # $tablename is like smile_partition_aaaa, smile_partition_ebca; '$prefix' is like 'EEBD', 'AAAA'

(3) Load data to the table.

copy smile_partition from '$i' with CSV HEADER Delimiter AS E'\t';   # copy to the partition (parent) table. Data will be automatically load to corresponding child table. Don't copy data to partition (child) table.

(4) Check created tables.



List of relations
 Schema |         Name         | Type  |  Owner   
 public | smile_partition      | table | postgres
 public | smile_partition_aaaa | table | postgres
 public | smile_partition_aaab | table | postgres
 public | smile_partition_aaad | table | postgres
 public | smile_partition_aaae | table | postgres
 public | smile_partition_aaaf | table | postgres
 public | smile_partition_aaba | table | postgres
 public | smile_partition_kkgf | table | postgres
 public | smile_partition_kkia | table | postgres
 public | smile_partition_kkib | table | postgres
 public | smile_partition_kkie | table | postgres
 public | smile_partition_kkif | table | postgres
(3601 rows)