pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Expose can_run_in_transaction on plan JSON steps

Open thommeo opened this issue 4 months ago • 3 comments

Type: feature Affects: v1.12.1

Summary

plan --output-json does not say which steps cannot run inside a transaction. The planner computes it — RewriteStep.CanRunInTransaction in internal/plan/rewrite.go:14, already JSON-tagged — but groupDiffs consumes it at internal/plan/plan.go:185 and the marshalled Step (internal/plan/plan.go:36) does not carry it. Please add it.

Why it matters

#397 asked for --no-concurrent and was closed with:

This is not a covered case for pgschema right now. We expect user to use pgschema to plan and apply. You need to post-process it yourself.

That is a reasonable position, and this issue asks only for what post-processing needs. We use plan to generate goose migration files. goose wraps a migration in a transaction unless the file carries -- +goose NO TRANSACTION, so a consumer must know which statements Postgres will refuse inside one — otherwise the generated migration fails with SQLSTATE 25001.

Current behaviour

Adding an index to an existing table, --output-json gives:

{"groups": [
  {"steps": [{"sql": "ALTER TABLE projects ADD COLUMN color text;", "type": "table.column", "operation": "create", "path": "public.projects.color"}]},
  {"steps": [{"sql": "CREATE INDEX CONCURRENTLY IF NOT EXISTS projects_archived_at_idx ON projects (archived_at);", "type": "table.index", "operation": "create", "path": "public.projects.projects_archived_at_idx"}]},
  {"steps": [{"sql": "SELECT ...", "directive": {"type": "wait", ...}, "type": "table.index", "operation": "create", "path": "public.projects.projects_archived_at_idx"}]}
]}

Group 2 is non-transactional and group 1 is not, but nothing distinguishes them. Group membership does not either: a lone transactional step is also a one-step group (plan.go:190, plan.go:218). The only signals available are grepping the SQL for CONCURRENTLY or inferring isolation from a following wait directive — both implementation details rather than contract.

Suggested fix

Carry the field onto Step and set it where the rewrite step is converted (internal/plan/plan.go:176-181):

type Step struct {
	SQL                 string     `json:"sql"`
	Directive           *Directive `json:"directive,omitempty"`
	CanRunInTransaction bool       `json:"can_run_in_transaction"`
	...
}

Canonical (non-rewrite) statements at plan.go:204 are transactional, so true is the correct default there. This is additive — no existing JSON field changes.

Happy to send a PR if you would take one.

thommeo avatar Aug 03 '26 14:08 thommeo

Let me think about it.

tianzhou avatar Aug 03 '26 14:08 tianzhou

This is not what pgschema plans to support right now. The generated plan file is only intended to be consumed by pgschema apply. It's not designed for being consumed by other tools.

I created sample PR https://github.com/pgplex/pgschema/pull/524 in case you want to fork it

tianzhou avatar Aug 03 '26 14:08 tianzhou

Thanks for thorough consideration 🙏

thommeo avatar Aug 04 '26 07:08 thommeo