热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PostgreSQLreadonlystandby只读库如何写数据?DDLDML操作透明传输到主库

digoal德哥专栏PostgreSQLreadonlystandby只读库如何写数据?-DDLDM

作者

digoal


日期

2020-03-26


标签

PostgreSQL , 主库 , 只读库 , 透明传输 , ddl , dml




背景

https://github.com/citusdata/pgmasq

只读库当分析库使用时, 分析通常比较复杂, 会有中间结果, 中间结果如何保持?是个问题, 因为只读库是只读的, 无法保存数据, pgmasq通过在只读库实现透明ddl dml传输到主库, 实现了只读库的写, 同时只读库会及时收到写结果.


pgmasq

Pgmasq is a PostgreSQL extension that transparently forwards transactions from a hot standby to a primary to enable DDL and DML from any node, while providing a means of offloading some SELECTs to a standby.

Pqmasq is currently a prototype, with some important limitations.


Installation

Compile the extension from source:

git clone https://github.com/citusdata/pgmasq.git
cd pgmasq
make && sudo PATH=$PATH make install


Setting up pgmasq

Add the following to postgresql.conf:

shared_preload_libraries = 'pgmasq'
pgmasq.execute_locally = immutable # one of: none, immutable, selects
lock_timeout = '2s'

The pgmasq.execute_locally setting controls which queries are executed locally on the standby. The following settings are supported:



  • none forwards all commands to the primary.

  • immutable executes selects outside of a transactino block that contain no functions that could modify the database on the standby.

  • selects executes all selects outside of a transaction block on the standby.

Additionally, we recommend setting synchronous_commit = remote_apply on the primary to provide read-your-writes consistency.

To enable pgmasq in a particular database, run CREATE EXTENSION pgmasq in that database on the primary.


Authentication

For pgmasq to work your replication user also needs to be superuser. You can make that the case by running:

ALTER USER replicator SUPERUSER;

In addition, you may need to add a line to pg_hba.conf to ensure access to the database, in addition to replication access:

host replication replicator 10.0.0.0/8 md5
host all replicator 10.0.0.0/8 md5


Limitations

CREATE TEMPORARY TABLE is not supported as the parser on the hot standby cannot see the temporary table.

Multi-statement transactions of the form BEGIN; [DDL on table X]; [SELECT/DML on table X]; COMMIT; may create an undetectable deadlock, since the DDL will take an exclusive lock when it gets replicated, which will cause the parser to block when processing the SELECT/DML.


PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.


9.9元购买3个月阿里云RDS PostgreSQL实例


PostgreSQL 解决方案集合


德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat


推荐阅读
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文介绍了在安装或运行 Python 项目时遇到的 'ModuleNotFoundError: No module named setuptools_rust' 错误,并提供了解决方案。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 资源推荐 | TensorFlow官方中文教程助力英语非母语者学习
    来源:机器之心。本文详细介绍了TensorFlow官方提供的中文版教程和指南,帮助开发者更好地理解和应用这一强大的开源机器学习平台。 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
author-avatar
mobiledu2502858037
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有