PostgreSQL中的删除列操作是什么
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇内容主要讲解"PostgreSQL中的删除列操作是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"PostgreSQL中的删除列操作是什么"吧!创建
千家信息网最后更新 2026年10月31日PostgreSQL中的删除列操作是什么
本篇内容主要讲解"PostgreSQL中的删除列操作是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"PostgreSQL中的删除列操作是什么"吧!
创建数据表
[local:/data/run/pg12]:5120 pg12@testdb=# create table t_drop(id int);CREATE TABLE[local:/data/run/pg12]:5120 pg12@testdb=# insert into t_drop select generate_series(1,10000000);INSERT 0 10000000[local:/data/run/pg12]:5120 pg12@testdb=# [local:/data/run/pg12]:5120 pg12@testdb=# SELECT pg_size_pretty( pg_relation_size( 't_drop' ) ); pg_size_pretty ---------------- 346 MB(1 row)
新增列
[local:/data/run/pg12]:5120 pg12@testdb=# \timing onTiming is on.[local:/data/run/pg12]:5120 pg12@testdb=# ALTER TABLE t_drop ADD COLUMN c1 text DEFAULT md5( random()::text );ALTER TABLETime: 45769.146 ms (00:45.769)[local:/data/run/pg12]:5120 pg12@testdb=# SELECT pg_size_pretty( pg_relation_size( 't_drop' ) ); pg_size_pretty ---------------- 651 MB(1 row)Time: 0.840 ms[local:/data/run/pg12]:5120 pg12@testdb=#
新增列后,占用空间达到了651MB.
删除列
[local:/data/run/pg12]:5120 pg12@testdb=# alter table t_drop drop c1;ALTER TABLETime: 2.886 ms[local:/data/run/pg12]:5120 pg12@testdb=# SELECT pg_size_pretty( pg_relation_size( 't_drop' ) ); pg_size_pretty ---------------- 651 MB(1 row)Time: 1.788 ms[local:/data/run/pg12]:5120 pg12@testdb=#
删除列,但空间没有释放.
数据字典
[local:/data/run/pg12]:5120 pg12@testdb=# \d pg_attribute Table "pg_catalog.pg_attribute" Column | Type | Collation | Nullable | Default ---------------+-----------+-----------+----------+--------- attrelid | oid | | not null | attname | name | | not null | atttypid | oid | | not null | attstattarget | integer | | not null | attlen | smallint | | not null | attnum | smallint | | not null | attndims | integer | | not null | attcacheoff | integer | | not null | atttypmod | integer | | not null | attbyval | boolean | | not null | attstorage | "char" | | not null | attalign | "char" | | not null | attnotnull | boolean | | not null | atthasdef | boolean | | not null | atthasmissing | boolean | | not null | attidentity | "char" | | not null | attgenerated | "char" | | not null | attisdropped | boolean | | not null | attislocal | boolean | | not null | attinhcount | integer | | not null | attcollation | oid | | not null | attacl | aclitem[] | | | attoptions | text[] | C | | attfdwoptions | text[] | C | | attmissingval | anyarray | | | Indexes: "pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname) "pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)[local:/data/run/pg12]:5120 pg12@testdb=# select attrelid,attname,atttypid,attisdropped from pg_attribute where attrelid = 't_drop'::regclass; attrelid | attname | atttypid | attisdropped ----------+------------------------------+----------+-------------- 994249 | tableoid | 26 | f 994249 | cmax | 29 | f 994249 | xmax | 28 | f 994249 | cmin | 29 | f 994249 | xmin | 28 | f 994249 | ctid | 27 | f 994249 | id | 23 | f 994249 | ........pg.dropped.2........ | 0 | t(8 rows)Time: 0.896 ms[local:/data/run/pg12]:5120 pg12@testdb=#
查看数据字典,发现删除的c1列变为pg.dropped.2,逻辑标记为删除.
使用vacuum/vacuum full回收空间.
[local:/data/run/pg12]:5120 pg12@testdb=# vacuum t_drop;VACUUMTime: 2510.368 ms (00:02.510)[local:/data/run/pg12]:5120 pg12@testdb=# SELECT pg_size_pretty( pg_relation_size( 't_drop' ) ); pg_size_pretty ---------------- 651 MB(1 row)Time: 0.718 ms[local:/data/run/pg12]:5120 pg12@testdb=# vacuum full t_drop;VACUUMTime: 7996.658 ms (00:07.997)[local:/data/run/pg12]:5120 pg12@testdb=# SELECT pg_size_pretty( pg_relation_size( 't_drop' ) ); pg_size_pretty ---------------- 346 MB(1 row)Time: 1.258 ms[local:/data/run/pg12]:5120 pg12@testdb=#
但数据字典仍保留删除列的信息
[local:/data/run/pg12]:5120 pg12@testdb=# select attrelid,attname,atttypid,attisdropped from pg_attribute where attrelid = 't_drop'::regclass; attrelid | attname | atttypid | attisdropped ----------+------------------------------+----------+-------------- 994249 | tableoid | 26 | f 994249 | cmax | 29 | f 994249 | xmax | 28 | f 994249 | cmin | 29 | f 994249 | xmin | 28 | f 994249 | ctid | 27 | f 994249 | id | 23 | f 994249 | ........pg.dropped.2........ | 0 | t(8 rows)Time: 0.757 ms[local:/data/run/pg12]:5120 pg12@testdb=#
新增列,查看数据字典
[local:/data/run/pg12]:5120 pg12@testdb=# ALTER TABLE t_drop ADD COLUMN c1 text DEFAULT md5( random()::text );ALTER TABLETime: 24483.254 ms (00:24.483)[local:/data/run/pg12]:5120 pg12@testdb=# select attrelid,attname,atttypid,attisdropped from pg_attribute where attrelid = 't_drop'::regclass; attrelid | attname | atttypid | attisdropped ----------+------------------------------+----------+-------------- 994249 | tableoid | 26 | f 994249 | cmax | 29 | f 994249 | xmax | 28 | f 994249 | cmin | 29 | f 994249 | xmin | 28 | f 994249 | ctid | 27 | f 994249 | id | 23 | f 994249 | ........pg.dropped.2........ | 0 | t 994249 | c1 | 25 | f(9 rows)Time: 1.067 ms[local:/data/run/pg12]:5120 pg12@testdb=#
到此,相信大家对"PostgreSQL中的删除列操作是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
数据
字典
空间
内容
学习
实用
更深
信息
兴趣
实用性
实际
操作简单
数据表
方法
更多
朋友
标记
网站
逻辑
频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网高科技
云信互联网科技有限公司
互联网金融监管科技数据
金融科技还是互联网
互联网科技有限公司图片
与互联网科技公司
碳银互联网科技
居家互联网科技有限公司
互联网科技革命什么开始
移动互联网年度科技创新
注册互联网科技公司经营范围大全
中国互联网十大科技股排名
想开互联网科技公司怎么样
西安小鹰速贷互联网科技有限公司
科技大学互联网
互联网企业科技类企业如何做账
易企秀互联网科技是做什么的
神州科技互联网有限公司
科技互联网企业在行动
小牛互联网科技有限公司地址
朗新科技与工业互联网
苏州互联网科技贷款公司
菲比寻常互联网科技
港股科技和互联网
互联网科技公司实习生
知域互联网科技有限公司大骗子
银川新华互联网科技学校联系电话
图游互联网科技
九思连强互联网电子科技有限公司
云南互联网科技学费