Can rename table but can not truncate table,renametruncate

來源:互聯網
上載者:User

Can rename table but can not truncate table,renametruncate
一個表無法truncate但是可以rename,這個乍聽起來覺得好奇怪,下面類比該過程。
3個session:
session1執行truncate和rename操作;
session2執行lock表操作;
session3進行監控。
 
session1:

[gpadmin@wx60 contrib]$ psql gtlionspsql (8.2.15)Type "help" for help. gtlions=# \d test             Table "public.test" Column |          Type          | Modifiers --------+------------------------+----------- id     | integer                |  name   | character varying(200) | Indexes:    "idxtestid" btree (id)    "idxtestname" btree (name)Distributed by: (id) gtlions=# select pg_backend_pid(); pg_backend_pid ----------------           1473(1 row) gtlions=# truncate table test;Cancel request sentERROR:  relation "test" does not existgtlions=# alter table test rename to test1;ALTER TABLE


 
session2:
[gpadmin@wx60 ~]$ psql gtlionspsql (8.2.15)Type "help" for help. gtlions=# select pg_backend_pid(); pg_backend_pid ----------------           1555(1 row) gtlions=# begin;BEGINgtlions=# select * from test limit 10;  id   |    name    -------+------------ 19672 | 19672-asfd 19674 | 19674-asfd 19676 | 19676-asfd 19678 | 19678-asfd 19680 | 19680-asfd 19682 | 19682-asfd 19684 | 19684-asfd 19686 | 19686-asfd 19688 | 19688-asfd 19690 | 19690-asfd(10 rows) gtlions=# end;COMMIT


 
session3:
[gpadmin@wx60 ~]$ psql gtlionspsql (8.2.15)Type "help" for help. gtlions=# select locktype,relation::regclass,pid,mode,granted,gp_segment_id from pg_locks where pid in (1473,1555) order by pid; locktype | relation | pid | mode | granted | gp_segment_id ----------+----------+-----+------+---------+---------------(0 rows) gtlions=# select locktype,relation::regclass,pid,mode,granted,gp_segment_id from pg_locks where pid in (1473,1555) order by pid;   locktype    |  relation   | pid  |      mode       | granted | gp_segment_id ---------------+-------------+------+-----------------+---------+--------------- relation      | test        | 1555 | AccessShareLock | t       |            -1 relation      | idxtestname | 1555 | AccessShareLock | t       |            -1 transactionid |             | 1555 | ExclusiveLock   | t       |            -1 relation      | idxtestid   | 1555 | AccessShareLock | t       |            -1(4 rows) gtlions=# select locktype,relation::regclass,pid,mode,granted,gp_segment_id from pg_locks where pid in (1473,1555) order by pid;   locktype    |  relation   | pid  |        mode         | granted | gp_segment_id ---------------+-------------+------+---------------------+---------+--------------- transactionid |             | 1473 | ExclusiveLock       | t       |            -1 relation      | test        | 1473 | AccessExclusiveLock | f       |            -1 transactionid |             | 1555 | ExclusiveLock       | t       |            -1 relation      | idxtestid   | 1555 | AccessShareLock     | t       |            -1 relation      | idxtestname | 1555 | AccessShareLock     | t       |            -1 relation      | test        | 1555 | AccessShareLock     | t       |            -1(6 rows)


 
-EOF-
SQL幫忙Cannot truncate table 'tranlog' because it is partitioned如上錯誤什原因

because your table is a partition table ,so you can not truncate it immediately。
perhaps you can truncate it by partition.or,you can delete it direct.
and then,create table temp as select * from yourtable where rownum<1,then drop your table.
perhaps can help you!
give your withs!
 
Truncate table 刪除表中的所有行,但是表中的結構、列、約束等不會被改動?

刪除所有行不代表刪除所有的東西,就像你的windows一樣,你刪除了一個盤下所有的資料,你的盤就會消失嗎?
不可能吧,你只不過是把非系統硬碟的資料刪除了,你的盤卷標、統計資訊,盤符的大小之類的都在啊
只有你刪除這個盤符的物理資訊,重新格式化的時候盤才會消失,這在資料庫中就是drop,使用drop table,才會將這張表從根本上刪掉,一切約束結構也才會消失
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.