@neon/skills 3.0.11 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/package.json +5 -2
- package/plugin.json +1 -1
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/SKILL.md +42 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/backup-restore.md +372 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/bulk-loading.md +315 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/connection-pooling.md +310 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/hot-standby.md +221 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/indexing.md +318 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/logical-replication.md +610 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/major-version-upgrades.md +334 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/performance-diagnostics.md +553 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/query-optimization.md +294 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/query-patterns.md +566 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/schema-design.md +362 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/security-roles.md +489 -0
- package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/references/transaction-isolation.md +344 -0
- package/vendor/github.com/neondatabase/postgres-skills/source.json +6 -0
package/README.md
CHANGED
|
@@ -30,6 +30,12 @@ A comprehensive index of documentation and best practices for Lakebase Postgres
|
|
|
30
30
|
|
|
31
31
|
> **Migration:** Branching and egress optimization now live in `neon-postgres`. If you previously installed `neon-postgres-branches` or `neon-postgres-egress-optimizer`, update `neon-postgres` and remove those stale local skill directories.
|
|
32
32
|
|
|
33
|
+
### Postgres Best Practices
|
|
34
|
+
|
|
35
|
+
[](https://skills.sh/neondatabase/postgres-skills/postgres-best-practices)
|
|
36
|
+
|
|
37
|
+
Vendor-agnostic best practices for PostgreSQL 14 through 18: schema design, indexing, query optimization and patterns, performance diagnostics, transaction isolation, connection pooling, bulk loading, security and roles, backup and restore, logical replication, hot standby, and major version upgrades. Maintained in [neondatabase/postgres-skills](https://github.com/neondatabase/postgres-skills).
|
|
38
|
+
|
|
33
39
|
### Neon Auth
|
|
34
40
|
|
|
35
41
|
[](https://skills.sh/neondatabase/agent-skills/neon-auth)
|
|
@@ -141,6 +147,18 @@ Or straight from this repo:
|
|
|
141
147
|
pi install git:github.com/neondatabase/agent-skills
|
|
142
148
|
```
|
|
143
149
|
|
|
150
|
+
### npm package
|
|
151
|
+
|
|
152
|
+
[`@neon/skills`](https://www.npmjs.com/package/@neon/skills) contains every skill under `skills/` and a copy of `postgres-best-practices` from [neondatabase/postgres-skills](https://github.com/neondatabase/postgres-skills), for tools that read skills from the package:
|
|
153
|
+
|
|
154
|
+
```text
|
|
155
|
+
node_modules/@neon/skills/skills/<skill>/SKILL.md
|
|
156
|
+
node_modules/@neon/skills/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/SKILL.md
|
|
157
|
+
node_modules/@neon/skills/vendor/github.com/neondatabase/postgres-skills/source.json # upstream commit
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Pi, the plugins, and `npx skills add neondatabase/agent-skills` load only `skills/`. Install `postgres-best-practices` on its own with `npx skills add neondatabase/postgres-skills`.
|
|
161
|
+
|
|
144
162
|
## Usage
|
|
145
163
|
|
|
146
164
|
Example prompts:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neon/skills",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Agent Skills for Neon: Lakebase Postgres, Managed Better Auth, Realtime, Object Storage, Functions, the Data API, and AI Gateway",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"skills/",
|
|
25
25
|
"assets/",
|
|
26
|
+
"vendor/",
|
|
26
27
|
"plugin.json",
|
|
27
28
|
"mcp.json"
|
|
28
29
|
],
|
|
@@ -39,6 +40,7 @@
|
|
|
39
40
|
"fmt": "prettier --write .",
|
|
40
41
|
"sync:docs": "node scripts/sync-skill-docs.mjs",
|
|
41
42
|
"sync:plugins": "node scripts/sync-plugin-skills.mjs",
|
|
43
|
+
"sync:postgres-skills": "node scripts/sync-postgres-skills.mjs",
|
|
42
44
|
"sync:versions": "node scripts/sync-versions.mjs",
|
|
43
45
|
"validate:agent-plugin": "node scripts/validate-agent-plugin.mjs",
|
|
44
46
|
"validate:antigravity-plugin": "node scripts/validate-antigravity-plugin.mjs",
|
|
@@ -50,7 +52,8 @@
|
|
|
50
52
|
"validate:skills": "node scripts/validate-skills.mjs",
|
|
51
53
|
"validate:references": "node scripts/check-skill-references.mjs",
|
|
52
54
|
"validate:skill-docs": "node scripts/sync-skill-docs.mjs --check",
|
|
53
|
-
"validate:
|
|
55
|
+
"validate:postgres-skills": "node scripts/sync-postgres-skills.mjs --check",
|
|
56
|
+
"validate:ci": "npm run validate:agent-plugin && npm run validate:antigravity-plugin && npm run validate:plugins && npm run validate:versions && npm run validate:skills && npm run validate:references && npm run validate:skill-docs && npm run validate:postgres-skills"
|
|
54
57
|
},
|
|
55
58
|
"devDependencies": {
|
|
56
59
|
"prettier": "3.9.6",
|
package/plugin.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json",
|
|
3
3
|
"name": "neon-postgres",
|
|
4
|
-
"version": "3.0
|
|
4
|
+
"version": "3.1.0",
|
|
5
5
|
"description": "Manage your Neon backend with the Neon agent skills and the Neon MCP Server: Lakebase Postgres, Managed Better Auth, Realtime, Object Storage, Functions, the Data API, and AI Gateway",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Neon",
|
package/vendor/github.com/neondatabase/postgres-skills/skills/postgres-best-practices/SKILL.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: postgres-best-practices
|
|
3
|
+
description: Best practices and guidelines for working with Postgres. Covers schema design, indexing strategies, query optimization, migrations, and common pitfalls. Use when writing SQL, designing database schemas, optimizing queries, or setting up a Postgres database.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Postgres Best Practices
|
|
7
|
+
|
|
8
|
+
Guidelines and best practices for working with Postgres, covering schema design, indexing, query optimization, and common pitfalls.
|
|
9
|
+
|
|
10
|
+
## Supported Versions
|
|
11
|
+
|
|
12
|
+
This skill covers PostgreSQL 14 through 18. Version-specific features are tagged (e.g., `[PG15+]`, `[PG18+]`); environment-dependent examples identify required privileges, extensions, or multi-node setup.
|
|
13
|
+
|
|
14
|
+
PostgreSQL provides 5 years of support per major version. Always run the latest minor release.
|
|
15
|
+
|
|
16
|
+
| Version | Initial Release | End of Life |
|
|
17
|
+
| ------- | ------------------ | ------------------ |
|
|
18
|
+
| 18 | September 2025 | November 2030 |
|
|
19
|
+
| 17 | September 2024 | November 2029 |
|
|
20
|
+
| 16 | September 2023 | November 2028 |
|
|
21
|
+
| 15 | October 2022 | November 2027 |
|
|
22
|
+
| 14 | September 2021 | November 2026 |
|
|
23
|
+
|
|
24
|
+
Source: [postgresql.org/support/versioning](https://www.postgresql.org/support/versioning/)
|
|
25
|
+
|
|
26
|
+
## References
|
|
27
|
+
|
|
28
|
+
| Area | Resource | When to Use |
|
|
29
|
+
| ----------------------- | --------------------------------------- | ------------------------------------------------------------------ |
|
|
30
|
+
| Schema Design | `references/schema-design.md` | Designing tables, choosing data types, normalizing, partitioning |
|
|
31
|
+
| Indexing | `references/indexing.md` | Choosing index types, composite indexes, partial/covering indexes |
|
|
32
|
+
| Query Optimization | `references/query-optimization.md` | Reading EXPLAIN ANALYZE, fixing bottlenecks, planner tuning |
|
|
33
|
+
| Query Patterns | `references/query-patterns.md` | CTEs, window functions, lateral joins, UPSERT, JSONB, anti-patterns|
|
|
34
|
+
| Performance Diagnostics | `references/performance-diagnostics.md` | pg_stat views, lock analysis, VACUUM, connection management |
|
|
35
|
+
| Logical Replication | `references/logical-replication.md` | Pub/sub replication, live migrations, CDC |
|
|
36
|
+
| Hot Standby | `references/hot-standby.md` | Streaming replication, read replicas, failover |
|
|
37
|
+
| Transaction Isolation | `references/transaction-isolation.md` | Isolation levels, lost updates, serialization failures, retry logic |
|
|
38
|
+
| Backup & Restore | `references/backup-restore.md` | pg_dump/pg_restore, pg_basebackup, PITR, recovery |
|
|
39
|
+
| Security & Roles | `references/security-roles.md` | Privileges, RLS, pg_hba.conf, authentication, SSL |
|
|
40
|
+
| Bulk Data Loading | `references/bulk-loading.md` | COPY patterns, ETL staging, optimizing large loads, batch ops |
|
|
41
|
+
| Connection Pooling | `references/connection-pooling.md` | PgBouncer config, pool modes, prepared statements, sizing |
|
|
42
|
+
| Major Version Upgrades | `references/major-version-upgrades.md` | pg_upgrade, logical replication migration, pre/post checklists |
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
# Backup & Restore Reference
|
|
2
|
+
|
|
3
|
+
## Contents
|
|
4
|
+
- Backup strategy overview
|
|
5
|
+
- Logical backups (pg_dump / pg_restore)
|
|
6
|
+
- Physical backups (pg_basebackup)
|
|
7
|
+
- Point-in-Time Recovery (PITR)
|
|
8
|
+
- Verification and testing
|
|
9
|
+
- Automation patterns
|
|
10
|
+
|
|
11
|
+
## Backup Strategy Overview
|
|
12
|
+
|
|
13
|
+
| Method | What it captures | Granularity | Speed | Use case |
|
|
14
|
+
|--------|-----------------|-------------|-------|----------|
|
|
15
|
+
| `pg_dump` | Logical (SQL/custom) | Per-database, per-table | Slow on large DBs | Dev snapshots, migrations, selective restore |
|
|
16
|
+
| `pg_dumpall` | All databases + globals | Entire cluster | Slow | Full cluster backup including roles/tablespaces |
|
|
17
|
+
| `pg_basebackup` | Physical (file copy) | Entire cluster | Fast | Production backups, PITR base, replica setup |
|
|
18
|
+
| Continuous archiving | WAL segments | Incremental | Continuous | PITR — restore to any point in time |
|
|
19
|
+
|
|
20
|
+
**Production recommendation**: `pg_basebackup` + continuous WAL archiving for PITR capability. Supplement with periodic `pg_dump` for portable, version-independent backups.
|
|
21
|
+
|
|
22
|
+
## Logical Backups (pg_dump / pg_restore)
|
|
23
|
+
|
|
24
|
+
### pg_dump Formats
|
|
25
|
+
|
|
26
|
+
| Format | Flag | Parallel restore? | Selective restore? | Notes |
|
|
27
|
+
|--------|------|-------------------|-------------------|-------|
|
|
28
|
+
| Custom | `-Fc` | Yes | Yes | **Recommended default** — compressed, flexible |
|
|
29
|
+
| Directory | `-Fd` | Yes | Yes | One file per table, good for large DBs |
|
|
30
|
+
| Plain SQL | `-Fp` | No | No (manual editing) | Human-readable, good for version control |
|
|
31
|
+
| Tar | `-Ft` | No | Yes | Compatibility option |
|
|
32
|
+
|
|
33
|
+
### Common pg_dump Patterns
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# Full database backup (custom format — recommended)
|
|
37
|
+
pg_dump -Fc -f backup.dump mydb
|
|
38
|
+
|
|
39
|
+
# With compression level (0-9, default varies by format)
|
|
40
|
+
pg_dump -Fc -Z 6 -f backup.dump mydb
|
|
41
|
+
|
|
42
|
+
# Parallel dump (directory format only, 4 workers)
|
|
43
|
+
pg_dump -Fd -j 4 -f backup_dir/ mydb
|
|
44
|
+
|
|
45
|
+
# Schema only (no data)
|
|
46
|
+
pg_dump -Fc --schema-only -f schema.dump mydb
|
|
47
|
+
|
|
48
|
+
# Data only (no schema)
|
|
49
|
+
pg_dump -Fc --data-only -f data.dump mydb
|
|
50
|
+
|
|
51
|
+
# Specific tables
|
|
52
|
+
pg_dump -Fc -t orders -t customers -f subset.dump mydb
|
|
53
|
+
|
|
54
|
+
# Specific schema
|
|
55
|
+
pg_dump -Fc -n public -f public_schema.dump mydb
|
|
56
|
+
|
|
57
|
+
# Exclude large tables
|
|
58
|
+
pg_dump -Fc -T audit_log -T event_archive -f without_logs.dump mydb
|
|
59
|
+
|
|
60
|
+
# Include CREATE DATABASE in output
|
|
61
|
+
pg_dump -Fc --create -f backup_with_create.dump mydb
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### pg_restore Patterns
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# Restore to an existing (empty) database
|
|
68
|
+
pg_restore -d mydb backup.dump
|
|
69
|
+
|
|
70
|
+
# Parallel restore (4 workers — significantly faster for large DBs)
|
|
71
|
+
pg_restore -d mydb -j 4 backup.dump
|
|
72
|
+
|
|
73
|
+
# Create the database during restore
|
|
74
|
+
pg_restore --create -d postgres backup.dump
|
|
75
|
+
|
|
76
|
+
# List contents of a backup (inspect before restoring)
|
|
77
|
+
pg_restore --list backup.dump
|
|
78
|
+
|
|
79
|
+
# Restore specific tables only
|
|
80
|
+
pg_restore -d mydb -t orders -t customers backup.dump
|
|
81
|
+
|
|
82
|
+
# Schema only
|
|
83
|
+
pg_restore -d mydb --schema-only backup.dump
|
|
84
|
+
|
|
85
|
+
# Data only (schema already exists)
|
|
86
|
+
pg_restore -d mydb --data-only backup.dump
|
|
87
|
+
|
|
88
|
+
# Clean (drop) objects before recreating
|
|
89
|
+
pg_restore -d mydb --clean --if-exists backup.dump
|
|
90
|
+
|
|
91
|
+
# Restore atomically and stop on the first error
|
|
92
|
+
pg_restore -d mydb --single-transaction -j 1 backup.dump
|
|
93
|
+
# Note: --single-transaction is incompatible with -j > 1
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Selective restore patterns do not automatically include dependencies. Schema-qualify table patterns when names may exist in multiple schemas, and verify required types, sequences, constraints, and referenced tables separately.
|
|
97
|
+
|
|
98
|
+
### Restoring Plain SQL Dumps
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# Plain SQL dumps are restored with psql, not pg_restore
|
|
102
|
+
psql -d mydb -f backup.sql
|
|
103
|
+
|
|
104
|
+
# With error handling
|
|
105
|
+
psql -d mydb -v ON_ERROR_STOP=1 -f backup.sql
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### pg_dumpall — Cluster-Wide Backup
|
|
109
|
+
|
|
110
|
+
`pg_dumpall` is the only way to back up global objects (roles, tablespaces):
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
# Full cluster (all databases + globals) — plain SQL only
|
|
114
|
+
pg_dumpall -f cluster_backup.sql
|
|
115
|
+
|
|
116
|
+
# Globals only (roles, tablespaces) — use alongside per-database pg_dump
|
|
117
|
+
pg_dumpall --globals-only -f globals.sql
|
|
118
|
+
|
|
119
|
+
# Roles only
|
|
120
|
+
pg_dumpall --roles-only -f roles.sql
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Best practice**: Use `pg_dumpall --globals-only` for roles/tablespaces, then `pg_dump -Fc` per database for data. This gives you parallel restore capability while preserving globals.
|
|
124
|
+
|
|
125
|
+
### Optimizer Statistics in Backups (PG18+)
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# Dump optimizer statistics (speeds up post-restore query performance)
|
|
129
|
+
pg_dump -Fc --statistics -f backup.dump mydb
|
|
130
|
+
|
|
131
|
+
# Statistics only (no schema or data)
|
|
132
|
+
pg_dump -Fc --statistics-only -f stats.dump mydb
|
|
133
|
+
|
|
134
|
+
# Skip statistics
|
|
135
|
+
pg_dump -Fc --no-statistics -f backup.dump mydb
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Without statistics, the planner uses default estimates after restore until `ANALYZE` runs on all tables. Dumping statistics avoids the post-restore performance dip.
|
|
139
|
+
|
|
140
|
+
### Performance Tips for Large Databases
|
|
141
|
+
|
|
142
|
+
1. **Use parallel dump/restore** (`-Fd -j N`) — scales well with CPU cores
|
|
143
|
+
2. **Dump to fast storage** — local NVMe, not network mounts
|
|
144
|
+
3. **Increase `maintenance_work_mem`** on the restore target for faster index rebuilds
|
|
145
|
+
4. **Disable triggers during data-only restore**: `pg_restore --disable-triggers` (requires superuser)
|
|
146
|
+
5. **Drop indexes before restore, recreate after** — faster than incremental index maintenance during bulk inserts
|
|
147
|
+
6. **Restore schema first, then data, then indexes**:
|
|
148
|
+
```bash
|
|
149
|
+
pg_restore -d mydb --section=pre-data backup.dump
|
|
150
|
+
pg_restore -d mydb --data-only -j 4 backup.dump
|
|
151
|
+
pg_restore -d mydb --section=post-data -j 4 backup.dump
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Physical Backups (pg_basebackup)
|
|
155
|
+
|
|
156
|
+
`pg_basebackup` creates a byte-for-byte copy of the entire cluster, suitable as a base for PITR or setting up replicas.
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
# Basic backup (plain format)
|
|
160
|
+
pg_basebackup -D /backup/base -Fp -Xs -P
|
|
161
|
+
|
|
162
|
+
# Compressed tar format
|
|
163
|
+
pg_basebackup -D /backup/base -Ft -z -Xs -P
|
|
164
|
+
|
|
165
|
+
# With checkpoint mode (fast = don't wait for next scheduled checkpoint)
|
|
166
|
+
pg_basebackup -D /backup/base -Fp -Xs -P --checkpoint=fast
|
|
167
|
+
|
|
168
|
+
# To a remote server
|
|
169
|
+
pg_basebackup -h primary_host -U repl_user -D /backup/base -Fp -Xs -P
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
| Flag | Meaning |
|
|
173
|
+
|------|---------|
|
|
174
|
+
| `-D` | Target directory |
|
|
175
|
+
| `-Fp` | Plain format (ready-to-use data directory) |
|
|
176
|
+
| `-Ft` | Tar format (one tar per tablespace) |
|
|
177
|
+
| `-z` | Compress (with tar format) |
|
|
178
|
+
| `-Xs` | Stream WAL during backup (ensures consistency) |
|
|
179
|
+
| `-P` | Show progress |
|
|
180
|
+
| `--checkpoint=fast` | Start backup immediately (don't wait for next checkpoint) |
|
|
181
|
+
|
|
182
|
+
### Prerequisites
|
|
183
|
+
|
|
184
|
+
The connection used by `pg_basebackup` must authenticate as a superuser or a role with `REPLICATION` privilege. `max_wal_senders` must also have enough capacity for the backup connection:
|
|
185
|
+
|
|
186
|
+
```sql
|
|
187
|
+
-- On the primary
|
|
188
|
+
CREATE ROLE backup_user WITH REPLICATION LOGIN PASSWORD '...';
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
And `pg_hba.conf` must allow replication connections:
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
host replication backup_user backup_server_ip/32 scram-sha-256
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
[PG15+] `--target=server:/path` writes the backup on the database server. A non-superuser using this target needs both `REPLICATION` privilege for the backup connection and membership in `pg_write_server_files` for the server-side write. `--target` is unavailable in PG14 and cannot be combined with `-Xstream`; use `-Xfetch` or `-Xnone`.
|
|
198
|
+
|
|
199
|
+
## Point-in-Time Recovery (PITR)
|
|
200
|
+
|
|
201
|
+
PITR lets you restore a database to any specific point in time — invaluable for recovering from accidental data deletion or corruption.
|
|
202
|
+
|
|
203
|
+
### How PITR Works
|
|
204
|
+
|
|
205
|
+
1. Take a **base backup** (`pg_basebackup`)
|
|
206
|
+
2. Continuously **archive WAL segments** as they're produced
|
|
207
|
+
3. To recover: restore the base backup, then **replay WAL** up to the target time
|
|
208
|
+
|
|
209
|
+
### Step 1: Configure WAL Archiving
|
|
210
|
+
|
|
211
|
+
In `postgresql.conf`:
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
archive_mode = on
|
|
215
|
+
archive_command = 'cp %p /archive/wal/%f' # or use pgBackRest, barman, etc.
|
|
216
|
+
# archive_library = '' # PG15+: use archive modules instead of shell commands
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Requires a restart after enabling `archive_mode`.
|
|
220
|
+
|
|
221
|
+
Verify archiving is working:
|
|
222
|
+
|
|
223
|
+
```sql
|
|
224
|
+
SELECT * FROM pg_stat_archiver;
|
|
225
|
+
-- Check: archived_count is increasing, last_failed_time is NULL
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Step 2: Take Base Backups Regularly
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
# Weekly base backup (adjust frequency based on WAL volume)
|
|
232
|
+
pg_basebackup -D /backup/base_$(date +%Y%m%d) -Ft -z -Xs -P --checkpoint=fast
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Step 3: Recovery
|
|
236
|
+
|
|
237
|
+
To recover to a specific point in time:
|
|
238
|
+
|
|
239
|
+
1. Stop PostgreSQL
|
|
240
|
+
2. Replace the data directory with the base backup
|
|
241
|
+
3. Create `recovery.signal` (PG12+) or `recovery.conf` (PG11-)
|
|
242
|
+
4. Configure recovery target in `postgresql.conf`:
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
# In postgresql.conf (PG12+):
|
|
246
|
+
restore_command = 'cp /archive/wal/%f %p'
|
|
247
|
+
recovery_target_time = '2024-06-15 14:30:00'
|
|
248
|
+
recovery_target_action = 'promote' # 'pause' to inspect before promoting
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
5. Start PostgreSQL — it replays WAL up to the target time, then promotes to read-write
|
|
252
|
+
|
|
253
|
+
### Recovery Target Options
|
|
254
|
+
|
|
255
|
+
```text
|
|
256
|
+
-- Recover to a specific time
|
|
257
|
+
recovery_target_time = '2024-06-15 14:30:00+00'
|
|
258
|
+
|
|
259
|
+
-- Recover to a specific transaction ID
|
|
260
|
+
recovery_target_xid = '12345678'
|
|
261
|
+
|
|
262
|
+
-- Recover to a named restore point
|
|
263
|
+
recovery_target_name = 'before_migration'
|
|
264
|
+
|
|
265
|
+
-- Stop as soon as a consistent state is reached
|
|
266
|
+
-- (for an online backup, normally the point where the backup ended)
|
|
267
|
+
recovery_target = 'immediate'
|
|
268
|
+
|
|
269
|
+
-- Recover to a specific WAL position
|
|
270
|
+
recovery_target_lsn = '0/1A2B3C4D'
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
To recover through the end of all available WAL, omit every `recovery_target*` setting. `recovery_target = 'immediate'` is an explicit early stopping target, so later available WAL can remain unapplied. PostgreSQL may still replay the WAL required to make an online backup consistent.
|
|
274
|
+
|
|
275
|
+
### Creating Named Restore Points
|
|
276
|
+
|
|
277
|
+
Before risky operations, create a named restore point:
|
|
278
|
+
|
|
279
|
+
```sql
|
|
280
|
+
SELECT pg_create_restore_point('before_schema_migration');
|
|
281
|
+
SELECT pg_create_restore_point('before_bulk_delete');
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
This gives you an exact target to recover to if the operation goes wrong.
|
|
285
|
+
|
|
286
|
+
## Verification and Testing
|
|
287
|
+
|
|
288
|
+
### Verify Backup Integrity
|
|
289
|
+
|
|
290
|
+
```bash
|
|
291
|
+
# List contents without restoring
|
|
292
|
+
pg_restore --list backup.dump
|
|
293
|
+
|
|
294
|
+
# Verify a plain-format physical backup (PG13+)
|
|
295
|
+
pg_verifybackup /backup/base_20240615
|
|
296
|
+
|
|
297
|
+
# PG18+: verify a tar-format backup directly
|
|
298
|
+
pg_verifybackup --no-parse-wal /backup/base_tar_20240615
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Test Restores Regularly
|
|
302
|
+
|
|
303
|
+
**A backup that hasn't been tested is not a backup.** Schedule regular restore tests:
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
# Restore to a test database
|
|
307
|
+
createdb mydb_restore_test
|
|
308
|
+
pg_restore -d mydb_restore_test backup.dump
|
|
309
|
+
|
|
310
|
+
# Verify row counts
|
|
311
|
+
psql -d mydb_restore_test -c "
|
|
312
|
+
SELECT schemaname, relname, n_live_tup
|
|
313
|
+
FROM pg_stat_user_tables
|
|
314
|
+
ORDER BY n_live_tup DESC;
|
|
315
|
+
"
|
|
316
|
+
|
|
317
|
+
# Clean up
|
|
318
|
+
dropdb mydb_restore_test
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### Post-Restore Checklist
|
|
322
|
+
|
|
323
|
+
After restoring a backup:
|
|
324
|
+
|
|
325
|
+
1. **Run `ANALYZE`** on all tables — optimizer statistics may be stale or missing
|
|
326
|
+
```sql
|
|
327
|
+
ANALYZE; -- all tables
|
|
328
|
+
```
|
|
329
|
+
2. **Verify sequences** — if restoring data-only, sequences may not match the data
|
|
330
|
+
```sql
|
|
331
|
+
SELECT sequencename, last_value FROM pg_sequences WHERE schemaname = 'public';
|
|
332
|
+
```
|
|
333
|
+
3. **Check for invalid indexes** — concurrent index builds may have been in progress
|
|
334
|
+
```sql
|
|
335
|
+
SELECT indexrelid::regclass, indisvalid FROM pg_index WHERE NOT indisvalid;
|
|
336
|
+
```
|
|
337
|
+
4. **Verify replication slots are clean** — stale slots from the source won't work
|
|
338
|
+
```sql
|
|
339
|
+
SELECT slot_name, active FROM pg_replication_slots;
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Automation Patterns
|
|
343
|
+
|
|
344
|
+
### Scripted Backup with Retention
|
|
345
|
+
|
|
346
|
+
```bash
|
|
347
|
+
#!/bin/bash
|
|
348
|
+
BACKUP_DIR="/backup/pg"
|
|
349
|
+
RETENTION_DAYS=30
|
|
350
|
+
DB_NAME="mydb"
|
|
351
|
+
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
|
352
|
+
|
|
353
|
+
# Dump
|
|
354
|
+
pg_dump -Fc -Z 6 -f "${BACKUP_DIR}/${DB_NAME}_${TIMESTAMP}.dump" "${DB_NAME}"
|
|
355
|
+
|
|
356
|
+
# Verify dump was created and is non-empty
|
|
357
|
+
if [ ! -s "${BACKUP_DIR}/${DB_NAME}_${TIMESTAMP}.dump" ]; then
|
|
358
|
+
echo "ERROR: Backup file is empty or missing" >&2
|
|
359
|
+
exit 1
|
|
360
|
+
fi
|
|
361
|
+
|
|
362
|
+
# Clean old backups
|
|
363
|
+
find "${BACKUP_DIR}" -name "${DB_NAME}_*.dump" -mtime +${RETENTION_DAYS} -delete
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Dedicated Backup Tools
|
|
367
|
+
|
|
368
|
+
For production environments, consider purpose-built tools that handle scheduling, retention, compression, and PITR:
|
|
369
|
+
|
|
370
|
+
- **pgBackRest** — parallel backup/restore, incremental backups, S3/GCS/Azure support, built-in PITR
|
|
371
|
+
- **Barman** — backup management with retention policies, remote backup, WAL archiving
|
|
372
|
+
- **pg_probackup** — incremental backups with page-level tracking, merge, validation
|