apple=# create table test(id integer, info text);
CREATE TABLE
apple=# insert into test select generate_series(1, 100000), md5('test');
INSERT 0 100000
apple=# select * from test limit 1;
id | info
----+----------------------------------
1 | 098f6bcd4621d373cade4e832627b4f6
(1 row) apple=# select pg_size_pretty(pg_relation_size('test'));
pg_size_pretty
----------------
6672 kB
(1 row) apple=# create index idx_test_id on test(id);
CREATE INDEX
apple=# select * from pg_indexes_size('test');
pg_indexes_size
-----------------
2260992
(1 row) apple=# select * from pg_size_pretty(pg_indexes_size('test'));
pg_size_pretty
----------------
2208 kB
(1 row) apple=# truncate test;
TRUNCATE TABLE
apple=# select * from pg_size_pretty(pg_indexes_size('test'));
pg_size_pretty
----------------
8192 bytes
(1 row) execute query success, query is select * from pg_size_pretty(pg_indexes_size('test'));
apple=# select pg_size_pretty(pg_relation_size('test'));
pg_size_pretty
----------------
0 bytes
(1 row)

1.delete,表占用空间不变,这个众所周知,所谓的water-mark不变;表上索引占用空间也不会变;

2.默认不会重置表对应的sequence,除非在后面加上 RESTART IDENTITY。

3.truncate table tb_name,表与表上索引的空间占用均回到建立索引或表时的initial参数。

4.定期rebuild索引是一个好习惯,一是提高索引效率二是释放存贮区。

5.cascade慎用。

TRUNCATE

TRUNCATE — empty a table or set of tables

Synopsis

TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
Description TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but since it does not actually scan the tables it is faster. Furthermore, it reclaims disk space immediately, rather than requiring a subsequent VACUUM operation. This is most useful on large tables. Parameters name
The name (optionally schema-qualified) of a table to truncate. If ONLY is specified before the table name, only that table is truncated. If ONLY is not specified, the table and all its descendant tables (if any) are truncated. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included. RESTART IDENTITY
Automatically restart sequences owned by columns of the truncated table(s). CONTINUE IDENTITY
Do not change the values of sequences. This is the default. CASCADE
Automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE. RESTRICT
Refuse to truncate if any of the tables have foreign-key references from tables that are not listed in the command. This is the default. Notes You must have the TRUNCATE privilege on a table to truncate it. TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which blocks all other concurrent operations on the table. When RESTART IDENTITY is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the DELETE command should be used instead. TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. Checking validity in such cases would require table scans, and the whole point is not to do one. The CASCADE option can be used to automatically include all dependent tables — but be very careful when using this option, or else you might lose data you did not intend to! TRUNCATE will not fire any ON DELETE triggers that might exist for the tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers are defined for any of the tables, then all BEFORE TRUNCATE triggers are fired before any truncation happens, and all AFTER TRUNCATE triggers are fired after the last truncation is performed and any sequences are reset. The triggers will fire in the order that the tables are to be processed (first those listed in the command, and then any that were added due to cascading). TRUNCATE is not MVCC-safe. After truncation, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the truncation occurred. See Section 13.5 for more details. TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit. When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART operations are also done transactionally; that is, they will be rolled back if the surrounding transaction does not commit. This is unlike the normal behavior of ALTER SEQUENCE RESTART. Be aware that if any additional sequence operations are done on the restarted sequences before the transaction rolls back, the effects of these operations on the sequences will be rolled back, but not their effects on currval(); that is, after the transaction currval() will continue to reflect the last sequence value obtained inside the failed transaction, even though the sequence itself may no longer be consistent with that. This is similar to the usual behavior of currval() after a failed transaction. TRUNCATE is not currently supported for foreign tables. This implies that if a specified table has any descendant tables that are foreign, the command will fail.

PostgreSQL truncate table会释放索引的空间的更多相关文章

  1. TRUNCATE TABLE (Transact-SQL)

    删除表中的所有行,而不记录单个行删除操作. TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快,使用的系统资源和事务日志资源 ...

  2. Truncate table、Delete与Drop table的区别

    Truncate table.Delete与Drop table的区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNC ...

  3. Truncate Table 用法

    TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TRUNC ...

  4. Truncate Table user

    Truncate Table   百科名片 Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用. 目录 语法 参数 注释 示例 ...

  5. SQL中truncate table和delete的区别

    truncate table table_name delete from table_name drop table table_name truncate table在功能上与不带where子句的 ...

  6. Oracle 使用TRUNCATE TABLE删除所有行

    若要删除表中的所有行,则 TRUNCATE TABLE 语句是一种快速.有效的方法.TRUNCATE TABLE 与不含 WHERE 子句的 DELETE 语句类似.但是,TRUNCATE TABLE ...

  7. TRUNCATE TABLE 与 DELETE (转)

    TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作.TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快, ...

  8. truncate table,drop,delete的区别

    TRUNCATE TABLE 在功能上与不带 Where 子句的 Delete 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 Delete 速度快,且使用的系统和事务日志资源 ...

  9. truncate table (tablename )表明

    Truncate是SQL中的一个删除数据表内容的语句,用法是: 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 下面是对Truncate语 ...

随机推荐

  1. GraphQL:一种不同于REST的接口风格

    从去年开始,JS算是完全踏入ES6时代.在React相关项目中接触到了一些ES6的语法.这次接着GraphQL这种新型的接口风格,从后端的角度接触ES6. 这篇文章从ES6的特征讲起,打好语法基础:然 ...

  2. HahMap

    HashMap的定义 public class HashMap<K, V> extends AbstractMap<K, V> implements Map<K, V&g ...

  3. POJ1144:Network(无向连通图求割点)

    题目:http://poj.org/problem?id=1144 求割点.判断一个点是否是割点有两种判断情况: 如果u为割点,当且仅当满足下面的1条 1.如果u为树根,那么u必须有多于1棵子树 2. ...

  4. Selenium IDE脚本录制步骤简介

    录制脚本步骤: 1.打开Selenium IDE,输入需要录制脚本的地址,然后启动Firefox,输入selenium IDE需录制的地址,根据实际需求,做相关操作: 2.录制过程中,会发现做的相关操 ...

  5. 39. Combination Sum(回溯)

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  6. iOS App迁移(App Transfer)注意点

    1.App迁移需要苹果审核吗? 答:不需要 2.App迁移需要多长时间? 答:迁移操作过程很快,A账号发出申请,B账号接收,几分钟时间.App Store 展示B账号相关信息可能几分钟,也可能有延迟几 ...

  7. android自定义Activity窗口大小(theme运用)

    http://gundumw100.iteye.com/blog/906195 正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示: 实现起来非 ...

  8. GOEXIF读取和写入EXIF信息

    最新版本的gexif,直接基于gdi+实现了exif信息的读取和写入,代码更清晰. /* * File: gexif.h * Purpose: cpp EXIF reader * 3/2/2017 & ...

  9. ngular6开发不完全笔记(二)-- 管道

    自定义管道 管道(过滤器)为过滤数据显示下列list数据 pip.ts 文件 import { Pipe, PipeTransform } from '@angular/core'; import { ...

  10. awk根据指定的字符串分割字符串

    以从字符串"hello-kitty-red-for-you"中获取-for前面的内容为例: echo "hello-kitty-red-for-you" |aw ...