|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += pg_readonly |
| 5 | + |
| 6 | +== Overview |
| 7 | +pg_readonly is a plugin that can set all PostgreSQL databases to read-only mode. |
| 8 | + |
| 9 | +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. |
| 10 | +This global flag is cluster-level: either all databases in the cluster are read-only, or all are read-write. |
| 11 | + |
| 12 | +Read-only mode is implemented by filtering SQL statements. |
| 13 | +[literal] |
| 14 | +---- |
| 15 | +SELECT statements without write-operation functions are allowed; |
| 16 | +DML (INSERT, UPDATE, DELETE) and DDL statements including TRUNCATE are completely disallowed; |
| 17 | +DCL statements GRANT and REVOKE are also prohibited; |
| 18 | +---- |
| 19 | + |
| 20 | +== Installation |
| 21 | + |
| 22 | +[TIP] |
| 23 | +The source installation environment is Ubuntu 24.04 (x86_64), with IvorySQL already installed at /path-to/ivorysql |
| 24 | + |
| 25 | +=== Source Installation |
| 26 | + |
| 27 | +[literal] |
| 28 | +---- |
| 29 | +# Download the 1.0.5 source package 1.0.5.tar.gz from https://github.com/pierreforstmann/pg_readonly/releases/tag/1.0.5 |
| 30 | +tar xvf 1.0.5.tar.gz |
| 31 | +cd pg_readonly-1.0.5 |
| 32 | +
|
| 33 | +# Compile and install |
| 34 | +make PG_CONFIF=/path-to/ivorysql/bin/pg_config |
| 35 | +make PG_CONFIF=/path-to/ivorysql/bin/pg_config install |
| 36 | +---- |
| 37 | + |
| 38 | +=== Configure Preloaded Libraries |
| 39 | +Modify the ivorysql.conf file in the data directory to append pg_readonly to shared_preload_libraries. |
| 40 | +shared_preload_libraries = 'pg_readonly' |
| 41 | + |
| 42 | +== Create Extension and Verify pg_readonly Version |
| 43 | + |
| 44 | +Connect to the database using psql and execute the following commands: |
| 45 | +[literal] |
| 46 | +---- |
| 47 | +ivorysql=# CREATE EXTENSION pg_readonly; |
| 48 | +CREATE EXTENSION |
| 49 | +
|
| 50 | +ivorysql=# SELECT * FROM pg_available_extensions WHERE name = 'pg_readonly'; |
| 51 | + name | default_version | installed_version | location | comment |
| 52 | +-------------+-----------------+-------------------+----------+---------------------------- |
| 53 | + pg_readonly | 1.0.4 | 1.0.4 | $system | cluster database read only |
| 54 | +(1 row) |
| 55 | +---- |
| 56 | + |
| 57 | +== Usage |
| 58 | + |
| 59 | +=== Check Individual Functions |
| 60 | + |
| 61 | +[literal] |
| 62 | +---- |
| 63 | +-- Query the current cluster read-only status |
| 64 | +select get_cluster_readonly(); |
| 65 | + get_cluster_readonly |
| 66 | +---------------------- |
| 67 | + f |
| 68 | +(1 row) |
| 69 | + |
| 70 | +-- Set the current cluster to read-only mode |
| 71 | +select set_cluster_readonly(); |
| 72 | + set_cluster_readonly |
| 73 | +---------------------- |
| 74 | + t |
| 75 | +(1 row) |
| 76 | +
|
| 77 | +-- Read-only cluster only allows SELECT statements |
| 78 | +select * from t; |
| 79 | + x | y |
| 80 | +---+--- |
| 81 | +(0 rows) |
| 82 | +
|
| 83 | +update t set x=33 where y='abc'; |
| 84 | +ERROR: cannot execute UPDATE in a read-only transaction |
| 85 | +
|
| 86 | +select 1 into tmp; |
| 87 | +ERROR: cannot execute UPDATE in a read-only transaction |
| 88 | +
|
| 89 | +create table tmp(c text); |
| 90 | +ERROR: cannot execute UPDATE in a read-only transaction |
| 91 | +---- |
| 92 | + |
| 93 | +For more detailed usage and advanced features, please refer to the https://github.com/pierreforstmann/pg_readonly [official pg_readonly documentation]. |
| 94 | + |
0 commit comments