PostgreSQL的臨時表空間,通過參數temp_tablespaces 進行配置,PostgreSQL允許用戶配置多個臨時表空間。
配置多個臨時表空間時,使用逗號隔開。
如果沒有配置temp_tablespaces 參數,臨時表空間對應的是默認的表空間。
PostgreSQL的臨時表空間用來存儲臨時表或臨時表的索引,以及執行SQL時可能產生的臨時文件例如排序,聚合,哈希等。
爲了提高性能,一般建議將臨時表空間放在SSD或者IOPS,以及吞吐量較高的分區中。
例子:
當前未配置temp_tablespaces,使用默認表空間。
postgres=# \l+ postgres
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
----------+----------+----------+---------+-------+-------------------+---------+------------+--------------------------------------------
postgres | postgres | UTF8 | C | C | | 7456 kB | pg_default | default administrative connection database
(1 row)
postgres=# show temp_tablespaces ;
temp_tablespaces
------------------
(1 row)
postgres=# create temp table tmp1 (id int);
CREATE TABLE
postgres=# insert into tmp1 select generate_series(1,1000);
INSERT 0 1000
臨時表放在默認表空間中。
postgres=# select pg_relation_filepath('tmp1');
pg_relation_filepath
----------------------
base/13241/t2_73746
(1 row)
執行一個大的排序,臨時文件放在默認表空間中。
postgres=# select * from generate_series(1,10000000000) order by 1;
查看臨時文件目錄
cd $PGDATA/base/pgsql_tmp
$ ll
-rw------- 1 digoal users 1.0G Mar 26 15:58 pgsql_tmp30315.0
-rw------- 1 digoal users 1.0G Mar 26 15:58 pgsql_tmp30315.1
....
新建一個表空間,並將所有用戶的temp_tablespaces參數設置爲這個新建的表空間。
mkdir /disk1/digoal/tmptbs1
postgres=# create tablespace tmptbs location '/disk1/digoal/tmptbs';
CREATE TABLESPACE
postgres=# alter role all set temp_tablespaces='tmptbs';
ALTER ROLE
重新測試,現在臨時文件都會放到新建的表空間下面。
psql
postgres=# select * from generate_series(1,10000000000) order by 1;
cd /disk1/digoal/tmptbs/PG_9.5_201510051/pgsql_tmp
total 528M
-rw------- 1 digoal users 513M Mar 26 16:05 pgsql_tmp31527.0
postgres=# create temp table t1(id int);
CREATE TABLE
postgres=# insert into t1 select generate_series(1,10000);
INSERT 0 10000
postgres=# select pg_relation_filepath('t1');
pg_relation_filepath
-------------------------------------------------
pg_tblspc/73749/PG_9.5_201510051/13241/t3_73750
(1 row)
下面是Greenplum的臨時表空間,介紹:
關於filespace的使用
Greenplum沒有temp_tablespaces這個參數,那麼臨時文件應該放哪裏呢?
Greenplum將只有filespace的說法,並且臨時文件是全局管理的,也就是說整個GP集羣的臨時文件是放在一個地方(filespace)的。
在Greenplum中不同的用戶不能使用不同的臨時文件目錄。
默認情況下臨時文件是放在默認的表空間下面
臨時文件(例如排序,哈希,產生的work file)
<filespace_directory>/<tablespace_oid>/<database_oid>/pgsql_tmp
臨時表
<filespace_directory>/<tablespace_oid>/<database_oid>/
You can move temporary or transaction files to a specific filespace to improve database performance when
running queries, creating backups, and to store data more sequentially.
The dedicated filespace for temporary and transaction files is tracked in two separate flat files called
gp_temporary_files_filespace and gp_transaction_files_filespace.
These are located in the pg_system directory on each primary and mirror segment, and on master and standby.
You must be a superuser to move temporary or transaction files.
Only the gpfilespace utility can write to this file.
Unless otherwise specified, temporary and transaction files are stored together with all user data.
The default location of temporary files, <filespace_directory>/<tablespace_oid>/<database_oid>/pgsql_tmp
is changed when you use gpfilespace --movetempfiles for the first time.
Also note the following information about temporary or transaction files:
• You can dedicate only one filespace for temporary or transaction files,
although you can use the same filespace to store other types of files.
• You cannot drop a filespace if it used by temporary files.
• You must create the filespace in advance.
如果要修改Greenplum臨時文件的存放地,操作如下:
首先要創建filespace, 然後確保沒有活躍會話,使用gpfilespace --movetempfilespace filespace_name命令遷移臨時文件目錄。
To move temporary files using gpfilespace
1. Check that the filespace exists and is different from the filespace used to store all other user data.
2. Issue smart shutdown to bring the Greenplum Database offline.
If any connections are still in progess, the gpfilespace --movetempfiles utility will fail.
3. Bring Greenplum Database online with no active session and run the following command:
gpfilespace --movetempfilespace filespace_name
The location of the temporary files is stored in the segment configuration shared memory
(PMModuleState) and used whenever temporary files are created, opened, or dropped.
To move transaction files using gpfilespace
1. Check that the filespace exists and is different from the filespace used to store all other user data.
2. Issue smart shutdown to bring the Greenplum Database offline.
If any connections are still in progess,the gpfilespace --movetransfiles utility will fail.
3. Bring Greenplum Database online with no active session and run the following command:
gpfilespace --movetransfilespace filespace_name
The location of the transaction files is stored in the segment configuration shared memory
(PMModuleState) and used whenever transaction files are created, opened, or dropped.
Creating a Tablespace
After you create a filespace, use the CREATE TABLESPACE command to define a tablespace that uses that
filespace.
For example:
=# CREATE TABLESPACE fastspace FILESPACE fastdisk;