Add support for UNLOGGED tables#424
Conversation
Adds three fixtures under testdata/diff/create_table/ covering `UNLOGGED` tables: - add_table_unlogged: empty schema -> CREATE UNLOGGED TABLE - set_unlogged: LOGGED table -> ALTER TABLE ... SET UNLOGGED - set_logged: UNLOGGED table -> ALTER TABLE ... SET LOGGED All currently fail because pgschema does not track `UNLOGGED` in the DDL.
Adds c.relpersistence::text to GetTables and GetTablesForSchema and
regenerates the sqlc output (sqlc 1.30) so the inspector can read
table persistence ('p' permanent, 'u' unlogged) when building the IR.
No behavior change yet; the new field is wired up in a follow-up
commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Greptile SummaryThis PR adds support for Confidence Score: 4/5Safe to merge; only P2 style findings, no correctness or runtime issues. The implementation is correct end-to-end: inspection, diffing, SQL generation, and plan rendering all handle UNLOGGED properly. Only two minor style issues found (comment typo and an implicit fallback in extractTablePathFromSubResource). No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[pg_class.relpersistence] -->|inspected via LEFT JOIN| B[ir.Table.Unlogged bool]
B --> C{Diff: old vs new}
C -->|New table added| D[generateTableSQL]
D -->|Unlogged=true| E[CREATE UNLOGGED TABLE IF NOT EXISTS ...]
D -->|Unlogged=false| F[CREATE TABLE IF NOT EXISTS ...]
C -->|Existing table changed| G[tableDiff.PersistenceChanged=true]
G --> H[generateAlterTableStatements]
H -->|New.Unlogged=true| I[ALTER TABLE x SET UNLOGGED]
H -->|New.Unlogged=false| J[ALTER TABLE x SET LOGGED]
I --> K[DiffTypeTablePersistence / CanRunInTransaction=true]
J --> K
|
pgschema previously emitted plain `CREATE TABLE` for both `LOGGED` and `UNLOGGED` tables, and silently no-op'd persistence transitions. This wires `UNLOGGED` end-to-end: the inspector now reads `relpersistence` into a new `ir.Table.Unlogged field`, the table-diff detects persistence flips, and the SQL emitter produces the relevant `UNLOGGED` modifiers (incl `ALTER TABLE ... SET LOGGED/UNLOGGED` for transitions). The persistence change is emitted before column and constraint alterations because PostgreSQL rewrites the heap on the toggle, so doing it first minimizes downstream data movement.
There was a problem hiding this comment.
Pull request overview
Adds first-class support for PostgreSQL UNLOGGED tables so that schema inspection/dumps and generated migrations preserve and apply table persistence correctly (fixes #423).
Changes:
- Extend table inspection to capture persistence via
pg_class.relpersistenceand store it onir.TableasUnlogged. - Teach table DDL generation to emit
CREATE UNLOGGED TABLE ...and to diff persistence changes viaALTER TABLE ... SET LOGGED/UNLOGGED(new diff typetable.persistence). - Add golden testdata cases covering create/unlogged and logged↔unlogged transitions.
Reviewed changes
Copilot reviewed 24 out of 25 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
ir/ir.go |
Adds Table.Unlogged to IR to represent UNLOGGED tables. |
ir/queries/queries.sql |
Selects pg_class.relpersistence for tables so persistence can be inspected. |
ir/queries/queries.sql.go |
Regenerates sqlc output structs/methods to include relpersistence. |
ir/queries/sqlc.yaml |
Minor config formatting adjustment (no functional change). |
ir/inspector.go |
Sets Table.Unlogged based on relpersistence == 'u'. |
internal/diff/diff.go |
Introduces DiffTypeTablePersistence (table.persistence) for plan/JSON typing. |
internal/diff/table.go |
Emits CREATE UNLOGGED TABLE and generates ALTER TABLE ... SET LOGGED/UNLOGGED when persistence changes. |
testdata/diff/create_table/add_table_unlogged/* |
Golden outputs for creating an unlogged table. |
testdata/diff/create_table/set_unlogged/* |
Golden outputs for changing a table to UNLOGGED. |
testdata/diff/create_table/set_logged/* |
Golden outputs for changing a table back to LOGGED. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tianzhou
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution
This is currently dropped by table definitions, although
UNLOGGEDmakes the table behave differently wrt replication and persistence (docs)Fix #423