|
1 | 1 | # Using Spock in Read-Only Mode |
2 | 2 |
|
3 | | -Spock supports operating a cluster in read-only mode. Read-only status is managed using a GUC (Grand Unified Configuration) parameter named `spock.readonly`. This parameter can be set to enable or disable the read-only mode. Read-only mode restricts non-superusers to read-only operations, while superusers can still perform both read and write operations regardless of the setting. |
| 3 | +Spock supports operating a node in read-only mode. Read-only status is managed using a GUC parameter named `spock.readonly`, which can be set to one of three values: |
4 | 4 |
|
5 | | -The flag is at cluster level: either all databases are read-only or all databases |
6 | | -are read-write (the usual setting). |
| 5 | +| Value | Description | |
| 6 | +|---------|-------------| |
| 7 | +| `off` | No restrictions. All users may write. This is the default. | |
| 8 | +| `local` | Non-superuser local sessions are read-only. Replicated writes from apply workers are still permitted, so inbound replication continues normally. Superusers may still perform write operations. (The legacy alias `user` is accepted for backward compatibility.) | |
| 9 | +| `all` | Non-superuser local sessions and apply workers are blocked from writing. Superusers may still perform write operations. Use this mode when you need to stop both local application writes and inbound replication. | |
7 | 10 |
|
8 | | -Read-only mode is implemented by filtering SQL statements: |
| 11 | +The setting is at cluster level: either all databases are read-only or all |
| 12 | +databases are read-write (the usual setting). |
9 | 13 |
|
10 | | -- `SELECT` statements are allowed if they don't call functions that write. |
11 | | -- DML (`INSERT`, `UPDATE`, `DELETE`) and DDL statements including `TRUNCATE` are forbidden entirely. |
12 | | -- DCL statements `GRANT` and `REVOKE` are also forbidden. |
| 14 | +Read-only mode is enforced by setting PostgreSQL's `transaction_read_only` flag for affected sessions. This means that any statement that would modify data — including DML (`INSERT`, `UPDATE`, `DELETE`), DDL, `TRUNCATE`, and DCL (`GRANT`, `REVOKE`) — will be rejected by PostgreSQL's standard read-only transaction checks. |
13 | 15 |
|
14 | | -This means that the databases are in read-only mode at SQL level: however, the |
15 | | -checkpointer, background writer, walwriter, and the autovacuum launcher are still |
16 | | -running. This means that the database files are not read-only and that in some |
17 | | -cases the database may still write to disk. |
| 16 | +The databases are in read-only mode at the SQL level: however, the checkpointer, |
| 17 | +background writer, walwriter, and the autovacuum launcher are still running. This |
| 18 | +means that the database files are not read-only and that in some cases the |
| 19 | +database may still write to disk. |
18 | 20 |
|
19 | 21 | ## Setting Read-Only Mode |
20 | 22 |
|
21 | | -You can control read-only mode with the Spock parameter `spock.readonly`; only a superuser can modify this setting. When the cluster is set to read-only mode, non-superusers will be restricted to read-only operations, while superusers will still be able to perform read and write operations regardless of the setting. |
22 | | - |
23 | | -This value can be changed using the `ALTER SYSTEM` command. |
| 23 | +Only a superuser can modify the `spock.readonly` parameter. The value can be changed using the `ALTER SYSTEM` command: |
24 | 24 |
|
25 | 25 | ```sql |
26 | | -ALTER SYSTEM SET spock.readonly = 'on'; |
| 26 | +-- Block non-superuser local writes; inbound replication continues |
| 27 | +ALTER SYSTEM SET spock.readonly = 'local'; |
| 28 | +SELECT pg_reload_conf(); |
| 29 | + |
| 30 | +-- Block all non-superuser writes including replication |
| 31 | +ALTER SYSTEM SET spock.readonly = 'all'; |
| 32 | +SELECT pg_reload_conf(); |
| 33 | + |
| 34 | +-- Restore normal operation |
| 35 | +ALTER SYSTEM SET spock.readonly = 'off'; |
27 | 36 | SELECT pg_reload_conf(); |
28 | 37 | ``` |
29 | 38 |
|
30 | | -To set the cluster to read-only mode for a session, use the `SET` command. Here are the steps: |
| 39 | +To set the mode for the current session only: |
31 | 40 |
|
32 | 41 | ```sql |
33 | | -SET spock.readonly TO on; |
| 42 | +SET spock.readonly TO local; |
34 | 43 | ``` |
35 | 44 |
|
36 | | -To query the current status of the cluster, you can use the following SQL command: |
| 45 | +To query the current status: |
37 | 46 |
|
38 | 47 | ```sql |
39 | 48 | SHOW spock.readonly; |
40 | 49 | ``` |
41 | 50 |
|
42 | | -This command will return on if the cluster is in read-only mode and off if it is not. |
| 51 | +## Superuser writes and outbound replication |
| 52 | + |
| 53 | +In both `local` and `all` modes, superusers are exempt from the read-only |
| 54 | +restriction and may perform write operations. **The readonly setting has no |
| 55 | +effect on the walsender (outbound replication).** Any writes made by a |
| 56 | +superuser are captured in WAL and will be replicated outbound to other nodes |
| 57 | +in the cluster, regardless of the readonly mode. |
| 58 | + |
| 59 | +To perform repair operations that should **not** replicate to other nodes, use |
| 60 | +[`spock.repair_mode()`](../spock_functions/index.md) to suppress outbound |
| 61 | +replication of DML/DDL statements: |
| 62 | + |
| 63 | +```sql |
| 64 | +BEGIN; |
| 65 | +SELECT spock.repair_mode(true); |
| 66 | + |
| 67 | +-- Perform repair DML/DDL here... |
| 68 | + |
| 69 | +SELECT spock.repair_mode(false); |
| 70 | +COMMIT; |
| 71 | +``` |
| 72 | + |
| 73 | +## Behavior of `all` mode |
| 74 | + |
| 75 | +In `all` mode, apply workers detect the setting and stop consuming inbound WAL. |
| 76 | +When the mode is switched back to `off` or `local`, replication resumes from |
| 77 | +where it left off — no data is lost. |
43 | 78 |
|
44 | 79 | Notes: |
45 | 80 | - Only superusers can set and unset the `spock.readonly` parameter. |
46 | | - - When the cluster is in read-only mode, only non-superusers are restricted to read-only operations. Superusers can continue to perform both read and write operations. |
47 | | - - By using a GUC parameter, you can easily manage the cluster's read-only status through standard PostgreSQL configuration mechanisms. |
| 81 | + - By using a GUC parameter, you can easily manage the node's read-only status through standard PostgreSQL configuration mechanisms. |
48 | 82 |
|
0 commit comments