truncate 普通表
使用语法:truncate table xx yy zz;
具体例子如下所示:
teledb=# truncate table teledb_pg1;
TRUNCATE TABLE
也可以一次truncate 多个数据表。
teledb=# truncate table t_update, teledb_serial;
TRUNCATE TABLE
truncate 分区表
-
不允许truncate主表
teledb=# truncate table t_range; ERROR: trancate a partitioned table is forbidden, trancate a partition is allowed
使用语法:truncate xx partition for(x)
具体例子如下所示:
-
truncate 一个时间分区表。
teledb=# \d+ t_time_range Table "public.t_time_range" Column | Type | Collation | Nullable | Default | Storage | Stats target | Description --------+-----------------------------+-----------+----------+---------+---------+--------------+------------- f1 | bigint | | | | plain | | f2 | timestamp without time zone | | | | plain | | f3 | bigint | | | | plain | | Distribute By: SHARD(f1) Location Nodes: ALL DATANODES Partition By: RANGE(f2) # Of Partitions: 12 Start With: 2017-09-01 Interval Of Partition: 1 MONTH teledb=# select * from t_time_range; f1 | f2 | f3 ----+---------------------+----- 1 | 2017-09-01 00:00:00 | 100 1 | 2017-10-01 00:00:00 | 100 1 | 2017-11-01 00:00:00 | 100 (3 rows) teledb=# truncate t_time_range partition for ('2017-09-01' ::timestamp without time zone); TRUNCATE TABLE teledb=# select * from t_time_range; f1 | f2 | f3 ----+---------------------+----- 1 | 2017-10-01 00:00:00 | 100 1 | 2017-11-01 00:00:00 | 100 (2 rows)
-
truncate 一个数字分区表。
teledb=# select * from t_range; f1 | f2 | f3 ----+----------------------------+----- 1 | 2023-08-23 10:29:46.263768 | 1 2 | 2023-08-23 10:29:46.263768 | 50 2 | 2023-08-23 10:29:46.263768 | 110 3 | 2023-08-23 10:29:46.263768 | 100 (4 rows) teledb=# \d+ t_range Table "public.t_range" Column | Type | Collation | Nullable | Default | Storage | Stats target | Description --------+-----------------------------+-----------+----------+---------+---------+--------------+------------- f1 | bigint | | | | plain | | f2 | timestamp without time zone | | | now() | plain | | f3 | integer | | | | plain | | Distribute By: SHARD(f1) Location Nodes: ALL DATANODES Partition By: RANGE(f3) # Of Partitions: 3 Start With: 1 Interval Of Partition: 50 teledb=# truncate t_range partition for (1); TRUNCATE TABLE teledb=# truncate t_range partition for (2); TRUNCATE TABLE teledb=# select * from t_range; f1 | f2 | f3 ----+----------------------------+---------------- 2 | 2023-08-23 10:29:46.263768 | 110 3 | 2023-08-23 10:29:46.263768 | 100 (2 rows)