PostgreSQL和Greenplum的临时表空间介绍

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;
时间: 2024-08-01 09:44:48

PostgreSQL和Greenplum的临时表空间介绍的相关文章

临时表空间过大的解决方法

Temporary tablespace是oracle里临时表空间,临时表空间主要用途是在数据库进行排序运算.管理索引.访问视图等操作时提供临时的运算空间,当运算完成之后系统会自动清理.当oracle里需要用到sort的时候,PGA中sort_area_size大小不够时,将会把数据放入临时表空间里进行排序,同时如果有异常情况的话,也会被放入临时表空间,正常来说,在完成Select语句.create index等一些使用TEMP表空间的排序操作后,Oracle是会自动释放掉临时段的.但有些有侯我

ORACLE临时表空间总结

临时表空间概念   临 时表空间用来管理数据库排序操作以及用于存储临时表.中间排序结果等临时对象,当ORACLE里需要用到SORT的时候,并且当PGA中 sort_area_size大小不够时,将会把数据放入临时表空间里进行排序.像数据库中一些操作: CREATE INDEX. ANALYZE.SELECT DISTINCT.ORDER BY.GROUP BY. UNION ALL. INTERSECT.MINUS.SORT-MERGE JOINS.HASH JOIN等都可能会用到临时表空间.当

《Greenplum企业应用实战》一1.3 PostgreSQL与Greenplum的关系

1.3 PostgreSQL与Greenplum的关系 1.3.1 PostgreSQL PostgreSQL是一种非常先进的对象–关系型数据库管理系统(ORDBMS),是目前功能最强大,特性最丰富和技术最先进的自由软件数据库系统之一,其某些特性甚至连商业数据库都不具备.这个起源于伯克利(BSD)的数据库研究计划目前已经衍生成一项国际开发项目,并且有非常广泛的用户. PostgreSQL的特点可以说是数不胜数,称其为最先进的开源软件数据库当之无愧,支持绝大部分的主流数据库特性,主要体现在如下几方

PostgreSQL vs Greenplum Hash outer join (hash表的选择)

标签 PostgreSQL , Greenplum , hash outer join , hash table 背景 数据分析.大表JOIN.多表JOIN时,哈希JOIN是比较好的提速手段. hash join会首先扫描其中的一张表(包括需要输出的字段),根据JOIN列生成哈希表.然后扫描另一张表. hash join介绍 https://www.postgresql.org/docs/10/static/planner-optimizer.html the right relation is

MySQL 5.7怎么爬出临时表空间的坑

如何确认临时表是由哪个用户连接创建的? 上次我们介绍了MySQL 5.7临时表空间怎么玩才能不掉坑里,这次我们来介绍如何确认是哪个用户连接创建的临时表,以及如何释放临时表. 首先,我们查看当前的连接ID: yejr@imysql.com>SELECT CONNECTION_ID(); +-----------------+ | CONNECTION_ID() | +-----------------+ | 2470 | +-----------------+ 在当前会话中创建临时表: yejr@

DB2 pureScale如何调优系统临时表空间

本文基于我们在实际试验中遇到的性能问题,阐述了我们所尝试的各种解决方法以及得出的结论,最后归纳出在 DB2 pureScale 环境中调优系统临时表空间的最佳实践. 索引是数据库中的一个重要对象,它对于提高用户查询的性能起着至关重要的作用.在数据库中根据应用需要创建相应的索引是数据库管理员或者用户经常进行的操作,因此提高索引创建的性能对于用户来说非常重要.创建索引的过程涉及到很多排序操作,这时我们就需要用系统临时表空间来存储排序时生成的临时数据,因此系统临时表空间的性能表现在某种意义上决定了索引

临时表空间组的使用

一个临时表空间组至少包含一个临时表空间. 临时表空间组无法显式创建,当第一个临时表空间分配给该组时自动创建,当组内所有临时表空间被移除时自动删除. 临时表空间组支持的操作: 可以将临时表空间从一个组移动到另外一个组(如果组不存在,则自动创建). 可以将组中的临时表空间从组中移除. 可以将目前不属于任何组的临时表空间加入一个组. 使用临时表空间组的好处: 防止一个临时表空间出现空间不足的问题. 同一个用户同时连接多个session时可以使用不同的临时表空间. 在并行操作中可以并行使用多个临时表空间

Oracle临时表空间

当 Oracle 在执行一些 SQL 时,会需要一些临时空间来存储执行时产生的中间数据.这些临时空 间由 Orac le 从指定的临时表空间中分配给进程.主要有三种情况会占用临时空间:临时表/索引操作.排序和 临时 LO B 对象操作. A.临时表/索引: 在会话中,当第一次对临时表进行 INSERT(包括 CTAS)时 ,Orac le 会从临时表空间中为临时 表 及其索引分配临时空间一存储数据. B.排序: 任何会使用到排序的操作,包括 JOIN.创建(重建)INDEX.ORDER  BY.

ORACLE 临时表空间TEMP 满了怎么办?

oracle|临时表         最近遇到这样一个问题:前一段时间网页查询ORACLE表正常,可最近一直页面无法显示.打印出SQL放到PL/SQL Devoloper 执行,报"无法通过8(在表空间XXX中)扩展 temp 段",还有一个页面,可以查询出记录,但无法统计数据!       经过分析产生原因可能是:ORACLE临时段表空间不足,因为ORACLE总是尽量分配连续空间,一但没有足够的可分配空间或者分配不连续就会出现上述的现象.  解决方法:知道由于ORACLE将表空间作为