PostgreSQL 10.0 preview 功能增强 - hash index 支持wal(灾难恢复)

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS PostgreSQL Serverless,0.5-4RCU 50GB 3个月
推荐场景:
对影评进行热评分析
简介:

标签

PostgreSQL , 10.0 , hash index , wal , 灾难恢复


背景

PostgreSQL 10.0 将支持hash index WAL. 因此建hash index再也不怕数据库crash或者备库hash index不可用了。

$SUBJECT will make hash indexes reliable and usable on standby.  
AFAIU, currently hash indexes are not recommended to be used in  
production mainly because they are not crash-safe and with this patch,  
I hope we can address that limitation and recommend them for use in  
production.  

This patch is built on my earlier patch [1] of making hash indexes  
concurrent.  The main reason for doing so is that the earlier patch  
allows to complete the split operation and used light-weight locking  
due to which operations can be logged at granular level.  

WAL for different operations:  

This has been explained in README as well, but I am again writing it  
here for the ease of people.  
=================================  

Multiple WAL records are being written for create index operation,  
first for initializing the metapage, followed by one for each new  
bucket created during operation followed by one for initializing the  
bitmap page.  If the system crashes after any operation, the whole  
operation is rolledback.  I have considered to write a single WAL  
record for the whole operation, but for that we need to limit the  
number of initial buckets that can be created during the operation. As  
we can log only fixed number of pages XLR_MAX_BLOCK_ID (32) with  
current XLog machinery, it is better to write multiple WAL records for  
this operation.  The downside of restricting the number of buckets is  
that we need to perform split operation if the number of tuples are  
more than what can be accommodated in initial set of buckets and it is  
not unusual to have large number of tuples during create index  
operation.  

Ordinary item insertions (that don't force a page split or need a new  
overflow page) are single WAL entries.  They touch a single bucket  
page and meta page, metapage is updated during replay as it is updated  
during original operation.  

An insertion that causes an addition of an overflow page is logged as  
a single WAL entry preceded by a WAL entry for a new overflow page  
required to insert a tuple.  There is a corner case where by the time  
we try to use newly allocated overflow page, it already gets used by  
concurrent insertions, for such a case, a new overflow page will be  
allocated and a separate WAL entry will be made for the same.  

An insertion that causes a bucket split is logged as a single WAL  
entry, followed by a WAL entry for allocating a new bucket, followed  
by a WAL entry for each overflow bucket page in the new bucket to  
which the tuples are moved from old bucket, followed by a WAL entry to  
indicate that split is complete for both old and new buckets.  

A split operation which requires overflow pages to complete the  
operation will need to write a WAL record for each new allocation of  
an overflow page. As splitting involves multiple atomic actions, it's  
possible that the system crashes between moving tuples from bucket  
pages of old bucket to new bucket.  After recovery, both the old and  
new buckets will be marked with in_complete split flag.  The reader  
algorithm works correctly, as it will scan both the old and new  
buckets.  

We finish the split at next insert or split operation on old bucket.  
It could be done during searches, too, but it seems best not to put  
any extra updates in what would otherwise be a read-only operation  
(updating is not possible in hot standby mode anyway).  It would seem  
natural to complete the split in VACUUM, but since splitting a bucket  
might require to allocate a new page, it might fail if you run out of  
disk space.  That would be bad during VACUUM - the reason for running  
VACUUM in the first place might be that you run out of disk space, and  
now VACUUM won't finish because you're out of disk space.  In  
contrast, an insertion can require enlarging the physical file anyway.  

Deletion of tuples from a bucket is performed for two reasons, one for  
removing the dead tuples and other for removing the tuples that are  
moved by split.  WAL entry is made for each bucket page from which  
tuples are removed, followed by a WAL entry to clear the garbage flag  
if the tuples moved by split are removed.  Another separate WAL entry  
is made for updating the metapage if the deletion is performed for  
removing the dead tuples by vaccum.  

As deletion involves multiple atomic operations, it is quite possible  
that system crashes after (a) removing tuples from some of the bucket  
pages (b) before clearing the garbage flag (c) before updating the  
metapage.  If the system crashes before completing (b), it will again  
try to clean the bucket during next vacuum or insert after recovery  
which can have some performance impact, but it will work fine. If the  
system crashes before completing (c), after recovery there could be  
some additional splits till the next vacuum updates the metapage, but  
the other operations like insert, delete and scan will work correctly.  
We can fix this problem by actually updating the metapage based on  
delete operation during replay, but not sure if it is worth the  
complication.  

Squeeze operation moves tuples from one of the buckets later in the  
chain to one of the bucket earlier in chain and writes WAL record when  
either the bucket to which it is writing tuples is filled or bucket  
from which it is removing the tuples becomes empty.  

As Squeeze operation involves writing multiple atomic operations, it  
is quite possible, that system crashes before completing the operation  
on entire bucket.  After recovery, the operations will work correctly,  
but the index will remain bloated and can impact performance of read  
and insert operations until the next vacuum squeezes the bucket  
completely.  

=====================================  


One of the challenge in writing this patch was that the current code  
was not written with a mindset that we need to write WAL for different  
operations.  Typical example is _hash_addovflpage() where pages are  
modified across different function calls and all modifications needs  
to be done atomically, so I have to refactor some code so that the  
operations can be logged sensibly.  

Thanks to Ashutosh Sharma who has helped me in completing the patch by  
writing WAL for create index and delete operation and done the  
detailed testing of patch by using pg_filedump tool. I think it is  
better if he himself explains the testing he has done to ensure  
correctness of patch.  


Thoughts?  

Note - To use this patch, first apply latest version of concurrent  
hash index patch [2].  

[1] - https://commitfest.postgresql.org/10/647/  
[2] - https://www.postgresql.org/message-id/CAA4eK1LkQ_Udism-Z2Dq6cUvjH3dB5FNFNnEzZBPsRjw0haFqA@mail.gmail.com  

--   
With Regards,  
Amit Kapila.  
EnterpriseDB: http://www.enterprisedb.com  

这个patch的讨论,详见邮件组,本文末尾URL。

PostgreSQL社区的作风非常严谨,一个patch可能在邮件组中讨论几个月甚至几年,根据大家的意见反复的修正,patch合并到master已经非常成熟,所以PostgreSQL的稳定性也是远近闻名的。

参考

https://commitfest.postgresql.org/13/740/

https://www.postgresql.org/message-id/flat/CAA4eK1JOBX=YU33631Qh-XivYXtPSALh514+jR8XeD7v+K3r_Q@mail.gmail.com/

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
5月前
|
自然语言处理 关系型数据库 MySQL
mysql 全文搜索功能优缺点
mysql 全文搜索功能优缺点
|
1月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL的WAL预写日志文件
PostgreSQL数据库的物理存储结构包含多种文件,其中WAL(预写日志)用于确保数据完整性和高效恢复。WAL机制允许在不频繁刷新数据至磁盘的情况下,通过先写日志再改数据的方式,减少I/O操作,提高性能。每个WAL文件默认大小为16MB,位于pg_wal目录下,支持手动和自动切换。WAL不仅有助于数据恢复,还能显著降低I/O成本。
|
2月前
|
SQL 关系型数据库 MySQL
MySql5.6版本开启慢SQL功能-本次采用永久生效方式
MySql5.6版本开启慢SQL功能-本次采用永久生效方式
46 0
|
5月前
|
存储 关系型数据库 MySQL
基于python django 医院管理系统,多用户功能,包括管理员、用户、医生,数据库MySQL
本文介绍了一个基于Python Django框架开发的医院管理系统,该系统设计了管理员、用户和医生三个角色,具备多用户功能,并使用MySQL数据库进行数据存储和管理。
204 4
基于python django 医院管理系统,多用户功能,包括管理员、用户、医生,数据库MySQL
|
5月前
|
JavaScript 关系型数据库 MySQL
node连接mysql,并实现增删改查功能
【8月更文挑战第26天】node连接mysql,并实现增删改查功能
135 3
|
6月前
|
关系型数据库 MySQL 存储
|
6月前
|
分布式计算 大数据 关系型数据库
MaxCompute产品使用合集之如何实现类似mysql实例中的数据库功能
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
6月前
|
存储 负载均衡 关系型数据库
面试题MySQL问题之通过配置FastDFS提高性能如何解决
面试题MySQL问题之通过配置FastDFS提高性能如何解决
59 1
|
6月前
|
关系型数据库 MySQL 调度
MySQL高级功能与优化策略深度探索
MySQL高级功能与优化策略深度探索
|
7月前
|
JSON 关系型数据库 MySQL
理解和利用MySQL中的JSON功能
理解和利用MySQL中的JSON功能
300 2

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版