news 2026/9/3 2:37:17

表分区简介1

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
表分区简介1

文章目录

  • 文档用途
  • 详细信息

文档用途

了解Postgresql表分区

详细信息

Postgreslq内核中支持表分区(Table partitioning)包括:范围(range)、列表(list)

PostgreSQL offers built-in support for the following forms of partitioning:

Range Partitioning

The table is partitioned into “ranges” defined by a key column or set of columns, with no overlap between the ranges of values assigned to different partitions. For example, one might partition by date ranges, or by ranges of identifiers for particular business objects.

List Partitioning

The table is partitioned by explicitly listing which key values appear in each partition.

If your application needs to use other forms of partitioning not listed above, alternative methods such as inheritance and UNION ALL views can be used instead. Such methods offer flexibility but do not have some of the performance benefits of built-in declarative partitioning.

事例:

1、范围分区

createtablet_range(idint,namevarchar(100),i_timetimestampnotnull)partitionbyrange(id);postgres=# \d+ t_rangecreatetablet_range_1partitionoft_rangeforvaluesfrom(1)to(1000);createtablet_range_2partitionoft_rangeforvaluesfrom(1000)to(3000);createtablet_range_3partitionoft_rangeforvaluesfrom(3000)to(5000);createtablet_range_4partitionoft_rangeforvaluesfrom(5000)to(8000);createtablet_range_5partitionoft_rangeforvaluesfrom(8000)to(10000);postgres=# \d+ t_rangeinsertintot_rangeselectid,md5(random()::text),current_date-idfromgenerate_series(1,10001)t(id);postgres=>insertintot_rangeselectid,md5(random()::text),current_date-idfromgenerate_series(1,10001)t(id);ERROR:nopartitionofrelation"t_range"foundforrowDETAIL:Partitionkeyofthe failingrowcontains(id)=(10000).postgres=>INSERTINTOt_rangeSELECTgenerate_series(1,9999),md5(random()::text),clock_timestamp();selectcount(*)fromt_range_1;selectcount(*)fromt_range_5;selectcount(*)fromonly t_range;

不允许分区字段修改后跨越分区

postgres=# update t_range set id=2000 where id=500;ERROR: newrowforrelation"t_range_1"violatespartitionconstraintDETAIL: Failingrowcontains(2000,7ed9bf07d00e369c74997bee774cea69,2017-03-2400:00:00).postgres=# update t_range set id=200 where id=500;UPDATE1

非分区字段的字段修改操作会下发到子表上。

truncate主表

postgres=# truncate table t_range;TRUNCATETABLEpostgres=# select count(*) from t_range_5;count-------0(1row)

会清除所有子表的数据。

2、list 分区

创建主表

createtablet_list(idint,namevarchar(100),regionvarchar(50))partitionbylist(region);createtablet_list_1partitionoft_listforvaluesin('beijing');createtablet_list_2partitionoft_listforvaluesin('nanjing');createtablet_list_3partitionoft_listforvaluesin('shanghai');createtablet_list_4partitionoft_listforvaluesin('chongqing');createtablet_list_5partitionoft_listforvaluesin('hangzhou');postgres=# \d+ t_listinsertintot_listvalues(1,'a','beijing');insertintot_listvalues(2,'b','nanjing');insertintot_listvalues(3,'c','chongqing');insertintot_listvalues(4,'d','hangzhou');insertintot_listvalues(5,'e','shanghai');insertintot_listvalues(6,'f','chongqing');insertintot_listvalues(7,'g','nanjing');insertintot_listvalues(8,'h','beijing');postgres=>select*fromt_list_2;id|name|region----+------+---------2|b|nanjing7|g|nanjing(2rows)

使用explain查看执行计划,验证分区的使用

postgres=# explain select * from t_list where region='nanjing';

添加删除分区

添加分区

createtablet_list_6(liket_list);altertablet_list attachpartitiont_list_6forvaluesin('guangzhou');

分区离线

altertablet_range detachpartitiont_range_5;postgres=>select*fromt_rangewhereid=9001;id|name|i_time----+------+--------(0rows)postgres=>select*fromt_range_5whereid=9001;id|name|i_time------+----------------------------------+----------------------------9001|2ff1f5c36a0ef69b1b5d283a2312f019|2019-03-2009:47:14.304022(1row)altertablet_list detachpartitiont_list_6;select*fromt_listwhereregion='guangzhou';select*fromt_list_6;

detach后,表还是存在的,成为独立的表。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 22:33:18

已知缺陷列表公示:当前IndexTTS 2.0存在的问题汇总

IndexTTS 2.0 已知缺陷与技术边界:在惊艳功能背后的真实挑战 你有没有遇到过这样的场景?剪辑视频时,画面已经卡点完美,配音却总是慢半拍;想让虚拟角色“愤怒地喊出”一句台词,结果语气平淡得像在念通知&…

作者头像 李华
网站建设 2026/9/2 22:33:15

Mos鼠标平滑滚动工具:彻底解决macOS鼠标滚动卡顿问题

Mos鼠标平滑滚动工具:彻底解决macOS鼠标滚动卡顿问题 【免费下载链接】Mos 一个用于在 macOS 上平滑你的鼠标滚动效果或单独设置滚动方向的小工具, 让你的滚轮爽如触控板 | A lightweight tool used to smooth scrolling and set scroll direction independently fo…

作者头像 李华
网站建设 2026/8/31 20:56:04

如何快速掌握faster-whisper:语音识别性能优化的完整指南

如何快速掌握faster-whisper:语音识别性能优化的完整指南 【免费下载链接】faster-whisper 项目地址: https://gitcode.com/gh_mirrors/fas/faster-whisper 还在为语音转录速度慢、内存占用高而苦恼吗?faster-whisper正是你需要的解决方案&#…

作者头像 李华
网站建设 2026/9/2 23:04:04

惠普游戏本终极性能解放:OmenSuperHub完全配置指南

惠普游戏本终极性能解放:OmenSuperHub完全配置指南 【免费下载链接】OmenSuperHub 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 还在为官方OMEN Gaming Hub的臃肿体积和频繁弹窗困扰吗?OmenSuperHub作为一款纯净开源的硬件控制工…

作者头像 李华
网站建设 2026/9/1 18:47:58

NodeMCU PyFlasher终极指南:轻松掌握ESP8266图形化烧录技巧

还在为ESP8266固件烧录的复杂流程而烦恼吗?当你面对串口连接、波特率设置、命令行操作等一系列技术障碍时,是否渴望一个真正简单高效的解决方案?NodeMCU PyFlasher正是为此而生,这款基于Python的图形化烧录工具将彻底改变你的开发…

作者头像 李华
网站建设 2026/9/1 17:04:09

ONNX格式转换实践:让IndexTTS 2.0兼容更多推理引擎

ONNX格式转换实践:让IndexTTS 2.0兼容更多推理引擎 在语音合成技术飞速发展的今天,自回归模型凭借其高自然度的语音生成能力,已成为零样本音色克隆、情感可控合成等前沿任务的核心选择。B站开源的 IndexTTS 2.0 正是这一领域的佼佼者——它不…

作者头像 李华