Home Database Mysql Tutorial PostgreSQL源码定制:在线global read only

PostgreSQL源码定制:在线global read only

Jun 07, 2016 pm 04:00 PM
postgresql

基于某云上功能需求,最近实现了类似于MySQL global read only的功能。PG的read only功能,也不再需要通过重起PG实现来实现。直接

基于某云上功能需求,最近实现了类似于MySQL global read only的功能。PG的read only功能,也不再需要通过重起PG实现来实现。直接可以online更改PG实例级别

global read only 和 global read write功能, 以达到快速实现主备切换的功能。大大缩短了主备切换时间,提高了PG的高可性。弥补了PG在这一功能上的不足。

此次通过源码定制更改实现的PG版本global read only有许多明显的优势:

1.在设置global read only时,新的查询不会被堵

新进来session,read only直接生效,不需要重起PG实例。并用在设置global read only时,不会堵住新会话。因此避免了连接拥堵现象。

2.正在跑的事务,分级别对待

a.如果是事务块,也就是用户发起的”BEGIN”语句,那么已经执行完毕的语句不受影响。对于此事务中后面执行的语句,会受read only约束,不能执行DML操作。本事务会被终止。

b.如果是正在跑的语句,比如说有一个大的insert或者update。那么此操作不会受影响,设置 global read only需要等待此操作完成后,才返回。

global read only操作会等待所有running 的DML操作完成,并且做完”Immediate Checkpoint”后,再返回响应。这样做的理由是为了确保数据库状态的一致性。

尤其是在主备切换的情况下,更为关键和重要。

3.中断处理

如果在设置 global read only时失败,那么会被回滚,会被重新置为read write状态。

下面展示下源码patch实现结果:

session 1:查看当前数据库read only状态,显示当前为”Read Write”状态,即PG实例级别可以读写。

session 2:起事务,事务中为两个insert语句。我们先执行一个insert语句,但是不提交事务。

表创建语句:

create table grl_test (id int);

begin;

insert into grl_test values(1);

PostgreSQL源码定制:在线global read only

session 1:尝试将PG实例设置为global read only。此时可以看到,不能设置为”Read Only“状态,设置”Read Only”操作没有返回。

原因为 session 2并没有提交。这个符合我们的设计初衷,就是read only设置成功返回时,数据库为一致状态,此后没有user 级别写事务。

PostgreSQL源码定制:在线global read only

session 2:尝试发起第二条insert语句。可以看到insert失败了。原因为session 1尝试设置为 global read only时,虽然操作没有返回,

但是新的任何DML操作以及新的事务已经被约束为read only状态。不允许新的写事务容易理解,但是为什么不允许之前已经发起的事务中,,不能DML操作呢?

这样做的原因是:

我们不想让先于设置read only之前的事务块,无限制的跑下去。这会让设置global read only的操作一直进行下去。如果read only设置不成功,

直接影响到主备的快速切换。细心的同学可能会发现,这只限于事务块。的确如此,事务块与一般事务的区别,请见我另外一篇文章”PostgreSQL 事务模型介绍“。

因为一般事务,只在command级别,跑完就结束了。我们可以等待,一般用户也容易理解,如果我们强行终止此类操作,会对应用影响比较大。一般command

级别的事务总是非常快的结束,尤其在OLTP系统中,基本上都是简单的command级别事务。事务块一般逻辑比较复杂,有这一限制,也是为了数据一致性考虑,

失败了,顶多重新跑就行了。总体上来讲,这也是基于目前市面上OLTP类应用系统的现实需求而定制的。

insert into grl_test values(2);

session 1:此时我们再来看session 1时,设置global read only已经成功了。session 2因为第二个命令违背了read only,导致事务被终止了。因此,也就再没有

running transaction了。global read only设置完成后,checkpoint向前推。我们就可以以此checkpoint为准,进行主备一致性切换。

PostgreSQL源码定制:在线global read only

上面完全实现了在线更改PG实例read only状态。我们再将实例在线改回到read write。是不是非常方便呢?

PostgreSQL源码定制:在线global read only

------------------------------------华丽丽的分割线------------------------------------

CentOS 6.3环境下yum安装PostgreSQL 9.3

PostgreSQL缓存详述

Windows平台编译 PostgreSQL

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装

Ubuntu上的phppgAdmin安装及配置

CentOS平台下安装PostgreSQL9.3

PostgreSQL配置Streaming Replication集群

如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin 

------------------------------------华丽丽的分割线------------------------------------

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

本文永久更新链接地址

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL and PostgreSQL: Best Practices in Web Development MySQL and PostgreSQL: Best Practices in Web Development Jul 14, 2023 pm 02:34 PM

MySQL and PostgreSQL: Best Practices in Web Development Introduction: In the modern world of web development, databases are an essential component. When choosing a database, common choices are MySQL and PostgreSQL. This article will cover best practices for using MySQL and PostgreSQL in web development and provide some code examples. 1. Applicable scenarios MySQL is suitable for most web applications, especially those that require high performance, scalability and ease of use.

MySQL and PostgreSQL: Performance comparison and optimization tips MySQL and PostgreSQL: Performance comparison and optimization tips Jul 13, 2023 pm 03:33 PM

MySQL and PostgreSQL: Performance Comparison and Optimization Tips When developing web applications, the database is an indispensable component. When choosing a database management system, MySQL and PostgreSQL are two common choices. They are both open source relational database management systems (RDBMS), but there are some differences in performance and optimization. This article will compare the performance of MySQL and PostgreSQL and provide some optimization tips. Performance comparison comparing two database management

Learn database functions in Go language and implement addition, deletion, modification and query operations of PostgreSQL data Learn database functions in Go language and implement addition, deletion, modification and query operations of PostgreSQL data Jul 31, 2023 pm 12:54 PM

Learn the database functions in the Go language and implement the addition, deletion, modification, and query operations of PostgreSQL data. In modern software development, the database is an indispensable part. As a powerful programming language, Go language provides a wealth of database operation functions and toolkits, which can easily implement addition, deletion, modification and query operations of the database. This article will introduce how to learn database functions in Go language and use PostgreSQL database for actual operations. Step 1: Install the database driver in Go language for each database

Using PostgreSQL in Go: The Complete Guide Using PostgreSQL in Go: The Complete Guide Jun 18, 2023 am 09:28 AM

Go language is a fast and efficient programming language suitable for building web services and back-end applications. PostgreSQL is an open source relational database management system that promises to provide higher reliability, scalability and data security. In this article, we’ll take a deep dive into using PostgreSQL with Go and provide some practical code examples and tips. Installing and setting up PostgreSQL First, we need to install and set up PostgreSQL. Can be found on the official website

MySQL and PostgreSQL: Data Security and Backup Strategies MySQL and PostgreSQL: Data Security and Backup Strategies Jul 13, 2023 pm 03:31 PM

MySQL and PostgreSQL: Data Security and Backup Strategies Introduction: In modern society, data has become an indispensable part of business and personal life. For database management systems, data security and backup strategies are crucial, both to protect data from loss or damage and to ensure the reliability and integrity of recovered data. This article will focus on the data security and backup strategies of two mainstream relational database systems, MySQL and PostgreSQL. 1. Data security: (1) User rights

How to use PostgreSQL database in PHP programming? How to use PostgreSQL database in PHP programming? Jun 12, 2023 am 09:27 AM

With the development of database technology, database management systems also present a variety of choices. Developers can choose the most suitable database according to their needs and preferences. As an advanced open source relational database system, PostgreSQL is attracting more and more attention and use by developers. So, how to use PostgreSQL database in PHP programming? 1. Install and configure the PostgreSQL database. Before using PostgreSQL, you need to install and configure it. first

PHP implements open source PostgreSQL relational database PHP implements open source PostgreSQL relational database Jun 18, 2023 am 08:40 AM

With the development of the Internet, the amount of data continues to grow, and the need for data management becomes increasingly urgent. Relational database is an important way of data management, among which PostgreSQL is popular for its flexibility, scalability and security. This article introduces how to use the PHP language to implement an open source PostgreSQL relational database. I hope it will be helpful to developers with corresponding needs. Overview PostgreSQL is a powerful relational database system that follows the SQL standard and has many

MySQL and PostgreSQL: How to optimize database query performance? MySQL and PostgreSQL: How to optimize database query performance? Jul 12, 2023 pm 03:15 PM

MySQL and PostgreSQL: How to optimize database query performance? Overview: Database query performance is an important consideration when developing applications. Good query performance improves application responsiveness and user experience. This article will introduce some methods to optimize database query performance, focusing on two commonly used databases, MySQL and PostgreSQL. Optimization of database indexes: Database indexes are an important factor in improving query performance. Indexes can speed up data search and reduce query scanning time.

See all articles