pg_readonly
1. Overview
pg_readonly is a plugin that can set all PostgreSQL databases to read-only mode.
pg_readonly does not have specific GUC parameters. It relies on a global flag in memory to manage the read-only state and provides SQL functions to set or query this global flag. This global flag is cluster-level: either all databases in the cluster are read-only, or all are read-write.
Read-only mode is implemented by filtering SQL statements.
SELECT statements without write-operation functions are allowed; DML (INSERT, UPDATE, DELETE) and DDL statements including TRUNCATE are completely disallowed; DCL statements GRANT and REVOKE are also prohibited;
2. Installation
| The source installation environment is Ubuntu 24.04 (x86_64), with IvorySQL already installed at /path-to/ivorysql |
2.1. Source Installation
# Download the 1.0.6 source package 1.0.6.tar.gz from https://github.com/pierreforstmann/pg_readonly/releases/tag/1.0.6 tar xvf 1.0.6.tar.gz cd pg_readonly-1.0.6 # Compile and install make PG_CONFIF=/path-to/ivorysql/bin/pg_config make PG_CONFIF=/path-to/ivorysql/bin/pg_config install
2.2. Building from Source on Windows
This section was verified on Windows 11 with Visual Studio 2022 Build Tools (MSVC 19.44). The Windows port of IvorySQL is still under review, so the verification used commit 947b50e1 of the Windows test baseline: master plus the open Windows port PRs (IvorySQL #2321, #2332, #2333, #2485, #2530, #2811, #3418, #3419, #3420 and #3421).
|
Build and install IvorySQL in "Developer PowerShell for VS 2022". Git, Strawberry Perl, winflexbison and Meson (with Ninja) must be on PATH. The verification build also used -Dcassert=true -Dtap_tests=enabled to run the tests.
git clone --branch win/reference https://github.com/jiabin777on-hue/IvorySQL.git
Set-Location IvorySQL
git checkout 947b50e1
meson setup build --prefix=C:\ivorysql -Dauto_features=disabled -Duuid=none
ninja -C build
meson install -C build
Set-Location ..
Windows has no PGXS, so compile the extension directly with MSVC’s cl and link, linking against postgres.lib from the IvorySQL installation. Clone with core.autocrlf=false so that test data files are not converted to CRLF.
$IVY = 'C:\ivorysql'
$INC = "$IVY\include\postgresql\server"
$CFLAGS = @("/nologo", "/c", "/O2", "/MD", "/utf-8", "/std:c11", "/DWIN32", "/DWINDOWS",
"/D__WINDOWS__", "/D__WIN32__", "/D_CRT_SECURE_NO_DEPRECATE", "/D_CRT_NONSTDC_NO_DEPRECATE",
"/I.", "/I$INC\port\win32_msvc", "/I$INC\port\win32", "/I$INC", "/I$IVY\include")
git clone -c core.autocrlf=false https://github.com/pierreforstmann/pg_readonly.git
Set-Location pg_readonly
git checkout 18cc73c9b161
cl @CFLAGS pg_readonly.c
link /nologo /DLL /OUT:pg_readonly.dll pg_readonly.obj "$IVY\lib\postgres.lib"
Copy-Item pg_readonly.dll "$IVY\lib\postgresql\"
Copy-Item pg_readonly.control, pg_readonly--*.sql "$IVY\share\postgresql\extension\"
The verified version is pg_readonly 1.0.6 (commit 18cc73c9b161); the source needs no changes.
pg_readonly must be preloaded. IvorySQL sets shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora' in ivorysql.conf by default; append pg_readonly to that list, for example shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_readonly'. Setting it to 'pg_readonly' alone replaces the default list, and connections in Oracle mode then fail with liboracle_parser not found.
Results: the extension’s regression test test passes in PG mode. In Oracle mode (with / lines added to the script) the results match the expected output, except that error contexts show the type as pg_catalog.int4 and anonymous blocks as PL/iSQL. The extension’s regression scripts are written for PostgreSQL. In Oracle mode, psql sends a CREATE FUNCTION only after a line containing just /, even when the body is dollar-quoted, so to run these scripts in Oracle mode, add a / line after each CREATE FUNCTION statement.
3. Create Extension and Verify pg_readonly Version
Connect to the database using psql and execute the following commands:
ivorysql=# CREATE EXTENSION pg_readonly;
CREATE EXTENSION
ivorysql=# SELECT * FROM pg_available_extensions WHERE name = 'pg_readonly';
name | default_version | installed_version | location | comment
-------------+-----------------+-------------------+----------+----------------------------
pg_readonly | 1.0.6 | 1.0.6 | $system | cluster database read only
(1 row)
4. Usage
4.1. Check Individual Functions
-- Query the current cluster read-only status select get_cluster_readonly(); get_cluster_readonly ---------------------- f (1 row) -- Set the current cluster to read-only mode select set_cluster_readonly(); set_cluster_readonly ---------------------- t (1 row) -- Read-only cluster only allows SELECT statements select * from t; x | y ---+--- (0 rows) update t set x=33 where y='abc'; ERROR: cannot execute UPDATE in a read-only transaction select 1 into tmp; ERROR: cannot execute UPDATE in a read-only transaction create table tmp(c text); ERROR: cannot execute UPDATE in a read-only transaction
For more detailed usage and advanced features, please refer to the https://github.com/pierreforstmann/pg_readonly .