PostgreSQL: operations manual¶
This page covers running FeeCheck's Cloud SQL database once it exists: keeping and restoring its data, changing its schema safely, its connections, its retention, what it may log, how to read its monitoring, and what it costs. Each section states the proof of concept's default and the production setting. The placeholders are those of the installation and setup guide.
Backups and point-in-time recovery¶
| PoC default | automated backups off, no point-in-time recovery, deletion protection on (ADR-0013 (docs/adr/adr-0013-postgresql-on-cloud-sql.md) §7 point 4; 8.3, backup and restore). A lost database is not restored: the data is synthetic, and Flyway rebuilds the schema on an empty database at the next start |
| Production setting | daily automated backups in a window outside working hours, point-in-time recovery with the transaction logs kept 7 days, one more backup kept than days of logs, deletion protection on |
Without backups, a dropped table, a faulty data migration or a deleted instance loses every case for good. Point-in-time recovery replays the transaction logs onto a backup, so a restore can stop a minute before the mistake rather than at last night's backup.
The limits that shape the setting (point-in-time recovery):
- Point-in-time recovery needs automated backups.
- Enterprise edition keeps the logs 1 to 7 days, 7 by default; Enterprise Plus up to 35.
- Google recommends keeping one more backup than days of logs, so the oldest log still has a backup to replay onto.
- The logs take storage: on the instance's disk, where more days of logs need a larger disk, or in
Cloud Storage, where newer instances keep them;
transactionalLogStorageStateingcloud sql instances describesays which.
Turn both on for an existing instance:
gcloud sql instances patch <instance> --project=<project> \
--backup-start-time=<HH:MM> --enable-point-in-time-recovery \
--retained-transaction-log-days=7 --retained-backups-count=8
Before a data migration that rewrites rows, take an on-demand backup as well (a major version
upgrade takes its own): gcloud sql backups create --instance=<instance> --project=<project>.
It stays until it is deleted, independent of the retained count: delete it once the migration is
confirmed (Data retention).
What a backup costs is in Monthly cost. What a backup keeps that the application has deleted is in Data retention.
Restoring from a backup¶
| PoC default | no backup to restore from; a lost database starts empty and the evaluation dataset is seeded again (8.3, test data) |
| Production setting | point-in-time recovery into a new instance, checked, then switched to; a backup restored over the instance only when the instance itself is lost |
Cloud SQL offers two ways back (restoring, point-in-time recovery):
| Way | Result | Use it when |
|---|---|---|
Point-in-time recovery (gcloud sql instances clone --point-in-time) |
a new instance with the state of the chosen moment; the old instance stays as it is | a mistake in the data or the schema: the moment just before it can be chosen, and the old instance stays for comparison |
Backup restore (gcloud sql backups restore) |
overwrites all data of the target instance, its recovery logs included | the instance itself is broken, and the last backup is good enough |
A backup restores only to an instance of the same major version, and the target's replicas must be deleted first. A deleted instance takes its automated backups with it, unless backups are set to be retained after deletion; deletion protection is what keeps that from happening by mistake.
Point-in-time recovery, step by step¶
- Find the moment. The deploy's time, or the first error, minus a margin; in UTC, in the form
2026-11-23T09:41:00Z. - Clone at that moment. The clone takes the instance's settings, its network included:
gcloud sql instances clone <instance> <instance>-restored \
--point-in-time=<UTC time> --project=<project>
- Check the clone in Cloud SQL Studio:
select * from flyway_schema_history order by installed_rank;shows the expected last migration, and the rows of the cases in question are as they were. - Switch the backend to it. The deploy names the instance in the backend's environment
(
CLOUD_SQL_INSTANCE_CONNECTION_NAME), and the runtime account's two roles are held by an IAM condition on the instance's resource name: change both to the clone, then deploy. Point the dashboard's Cloud SQL tiles at the clone too (gcloud monitoring dashboards update), or they keep showing the old instance. When the restore undoes a migration, the image of this deploy must carry the corrected one: the clone does not hold the faulty migration, and an image that still carries it applies it again. Readiness passes once the backend reaches the clone. - Keep the old instance stopped until the restore is confirmed, then delete it — deletion protection first.
The database is the only store restored. The object store and the queue are not: a PDF uploaded after the chosen moment stays in the bucket with no case referring to it, and a queued message may name a case the restored database does not hold (8.3, consistency).
A backup over the instance¶
gcloud sql backups list --instance=<instance> --project=<project>
gcloud sql backups restore <backup id> --restore-instance=<instance> --project=<project>
The backend's readiness fails while the restore runs and passes again afterwards; no deploy is needed, because the instance keeps its name. This works only while the instance exists: a deleted instance's name cannot be reused for some time, so its retained backup is restored to a new instance, and the backend is switched to it as in step 4 above.
A Flyway migration that fails halfway¶
| PoC default | the table is dropped and recreated, or the local database dropped with its volume; the evaluation dataset is seeded again (8.3, schema changes, rule 5) |
| Production setting | a forward fix in a pull request; a restore only when the migration destroyed data that a fix cannot bring back |
What happens when a migration fails in a deploy:
- The new revision does not start. Flyway runs when the backend starts, and a failed migration
stops the start. The startup probe fails, Cloud Run sends no traffic to the revision, and the
previous revision keeps serving (deploy guide, health (
docs/guides/feecheck-cloud-run.md)). - The failed migration leaves nothing behind, as a rule. PostgreSQL runs DDL inside transactions, and Flyway runs each migration in a transaction of its own, so a failure rolls that migration back whole, and it is not recorded as applied.
- Earlier migrations of the same deploy stay applied. A deploy that brings three migrations and fails on the third leaves the first two in place, and the previous revision runs on them. That is why each migration stays compatible with the revision before it.
- The exception is a migration that cannot run in a transaction, such as
CREATE INDEX CONCURRENTLY. Flyway runs it outside one, so a failure can leave part of it done, and Flyway records the migration as failed. Every later start stops at the validation until the record is cleared.
The procedure:
- Read the cause in the failed revision's log: Flyway names the migration, the statement and the database's error.
- Check what is applied:
select version, description, success from flyway_schema_history order by installed_rank;in Cloud SQL Studio. - A transactional migration failed (no row for it, or no
success = falserow): the database is as it was before that migration. Correct the migration in a pull request and merge; the next deploy runs it again. The migration was never applied in production, so rule 1 of 8.3, schema changes does not forbid correcting its file. A database where the faulty version did apply, such as a developer's, needs Flyway'srepairto accept the new checksum, or is rebuilt. - A non-transactional migration failed (a
success = falserow): undo by hand what it did (an index leftINVALIDis dropped), delete its row fromflyway_schema_history— what Flyway'srepairdoes for a failed migration — and then continue as in step 3. - A migration succeeded but destroyed data — a wrong
UPDATE, a dropped column still needed: no forward fix brings the data back. Correct the migration in a pull request first, then restore to the moment before the deploy, and switch to the clone with a deploy of the corrected image.
Compatible migrations during a deploy¶
| PoC default | the PoC reset mode: a migration may drop and recreate a table; a revision that cannot read the new schema during a deploy is accepted, and no backfill is written (8.3, schema changes, rule 5) |
| Production setting | rule 3 of 8.3, schema changes holds without exception: each migration stays compatible with the previous revision |
Rule 3 explains why: the previous revision keeps serving on the migrated schema while the new one starts, and after a failed deploy too. It names the add-before-use, drop-after-last-reader and rename patterns. Two more keep a migration compatible:
- A column added to a table is nullable or has a default, on an empty table too, so the previous revision's inserts, which do not name it, still succeed.
- A new constraint holds for every write from the moment it is added, the previous revision's
included: add it only once no serving revision writes rows that break it. Where existing rows may
break a
CHECKor foreign-key constraint, add itNOT VALIDand validate it in a later migration, so that the migration itself does not fail on them. A unique constraint has noNOT VALID: build its index first withCREATE UNIQUE INDEX CONCURRENTLY, in a migration of its own that runs outside a transaction (failed halfway), and add the constraintUSING INDEXin the next.
Connection budget and pool sizing¶
| PoC default | db-f1-micro admits 25 connections; the backend's pool holds 3, the service runs at most one instance, and a deploy briefly runs two revisions: 6 at most (8.3, connection budget) |
| Production setting | the same sum, computed for the production numbers and checked against the tier's max_connections before any of them rises |
The sum that has to fit:
max instances × pool size × 2 (two revisions during a deploy)
+ the migration step, while it runs
+ the case engine's job executor, once it runs
+ a reserve for people (Cloud SQL Studio, psql) and for Cloud SQL's own connections
≤ max_connections of the tier
Each idle connection holds a few megabytes of memory on the instance, so a pool larger than the
work needs costs memory as well as connections. Raise the pool only when the backend waits for
connections, not ahead of it. When the sum outgrows the tier, a larger tier raises
max_connections with its memory (setup, sizing).
Data retention and the PoC reset mode¶
| PoC default | a case is kept until a reviewer or an approver deletes it, or its uploader cancels it; the delete is soft, and the rows and pages stay (8.3, retention; ADR-0016, proposed). The PoC reset mode lets a migration drop data, and the evaluation dataset is seeded afterwards (8.3, test data) |
| Production setting | the same soft delete; the PoC reset mode off; no dataset seeded; backups and their logs kept no longer than the retention rule allows |
Retention is the application's rule, and the database keeps what the rule keeps: a soft-deleted case's pages, contract text included, stay in the table. When a case's content has to go for good is open: ADR-0016 leaves a purge of deleted cases to a later decision.
The backups extend every retention period. A backup taken before a case was deleted keeps it for as long as the backup is retained, and point-in-time recovery can bring it back for as long as the logs reach. So a production installation sets its backup retention from its data-retention rule, and a purge is effective only once the last backup that holds the purged rows has expired. An on-demand backup — the one before a data migration, and the one Cloud SQL takes before a major version upgrade — never expires by itself: delete it by hand within the same retention period.
The evaluation dataset belongs to developer machines and the proof of concept, never to the production database.
NFR-4 on the database¶
| PoC default | Cloud SQL's logging defaults: no statement logging, no slow-query logging, no parameter values; no database auditing |
| Production setting | the same defaults, set explicitly as flags; database auditing for schema and role changes only |
NFR-4 allows no client-identifying data — customer names, contract identifiers, extracted values,
cited passages — in any log at any level (SPEC.md (SPEC.md) §6). The database must not
undo what the backend keeps to. The backend sends its values as bind parameters, never inside the
SQL text. So the rule comes down to two things: never log parameter values, and never log every
statement.
| Flag | Production value | Why |
|---|---|---|
log_statement |
none |
mod or all writes every write or every statement to the log |
log_min_duration_statement |
-1 |
a slow-query log records the statement, and with it the parameters |
log_parameter_max_length |
0 |
no parameter value in a statement that is logged |
log_parameter_max_length_on_error |
0 |
no parameter value in the log of a failed statement; the statement text with its $1 placeholders stays, and is what the operator needs |
cloudsql.enable_pgaudit with pgaudit.log |
on, ddl,role |
records who changed the schema or a role, never a read or a write of rows |
Cloud SQL lists the flags it accepts per major version
(flags); check the list on a test instance
before the production one. The audit flags take effect only once the administrator has run
CREATE EXTENSION pgaudit; in the database, and the comma in ddl,role needs gcloud's other list
delimiter (Database flags).
An error's detail can still quote a value: a unique or foreign-key violation names the key that failed. Every key in the schema is an internal ID, never client content, and a new key keeps it so.
Query Insights, when it is turned on, stores normalized queries with every literal replaced by a placeholder (Query Insights), so it records no values; keep its options to store client addresses and application tags off.
No person reads contract content from Cloud SQL Studio for diagnosis. A failure is diagnosed from the case ID, the statement and the error, the same as in the backend's log.
Reading the dashboard and the readiness alert¶
| PoC default | the alert policy FeeCheck backend not ready emails the owner when the readiness uptime check fails from more than one region for 10 minutes; the dashboard shows the backend, the uptime check, the database and the dead letters (deploy guide, health (docs/guides/feecheck-cloud-run.md)) |
| Production setting | the same alert and dashboard, matched to the production tier and sent to an on-call group (setup, monitoring) |
The backend's readiness includes the database, and liveness does not (8.14). So the alert means the database or the backend is down, not one region's network.
When the alert fires, read the dashboard from the database outward:
| Tile | What it shows | What it means for the alert |
|---|---|---|
| Cloud SQL — up | 1 while the instance runs | 0: the instance is stopped, restarting or under maintenance; the backend's readiness fails because of it. Check the instance's operations (gcloud sql operations list --instance=<instance>) |
| Cloud SQL — CPU | the instance's CPU use | near 100 % for long: queries queue, and readiness times out; a larger tier or a query to look at |
| Cloud SQL — connections | open connections, against the tier's limit | at the limit: new connections are refused, and readiness fails on them. Recheck the budget |
| Uptime check — by region | 1 per region while readiness passes | every region at 0 with the database up: the backend itself, its deploy or its network path |
| Cloud Run — requests by class, latency, instances | the backend's traffic | 5xx rising with the database tiles normal: the backend itself. A deploy that does not start shows as a failed revision in Cloud Run, while the previous one keeps serving (above) |
| Pub/Sub dead letters | messages the push gave up on | conversions that failed repeatedly; not part of readiness, but a conversion fails too while the database is out of reach |
A planned stop — a maintenance window, a major version upgrade, a restore over the instance — fires the alert too. Announce it to whoever receives the alert, or snooze the policy for the window.
Monthly cost¶
| PoC default | about USD 8–11 for the db-f1-micro compute, always on, and about USD 2 for 10 GB of SSD; no backups (Cloud SQL guide (docs/guides/cloud-sql.md)) |
| Production setting | a dedicated-core tier, its storage, and the backups; computed from the region's price list for the chosen tier |
What a production month is made of (Cloud SQL pricing):
| Item | Billed by | Note |
|---|---|---|
| Compute | vCPU and memory per hour, about 730 hours a month | the largest item; a dedicated-core tier costs several times db-f1-micro. High availability doubles it |
| Storage | provisioned GB per month | automatic increase raises it and never lowers it; point-in-time recovery logs kept on the disk count |
| Backups | GB of backup per month | backups after the first are incremental, so the sum follows the data's rate of change, not the count of backups |
| Extended support | on top of compute, for a major version past its community end of life | avoided by upgrading in time |
| Monitoring | — | Google's own metrics and dashboards are not charged; the uptime check runs some 8,700 times a month (deploy guide, health (docs/guides/feecheck-cloud-run.md)) |
The billing report, filtered to Cloud SQL, is the measure; the price list only estimates it.