psql :var substitution fails inside DO dollar-quoted blocks (Job still failing after #318) #319
Labels
No labels
domain:backend
domain:devops
domain:frontend
status:approved
status:in-progress
status:needs-fix
status:qa
type:bug
type:devops
type:feature
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ldraney/pal-e-platform#319
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Bug
Lineage
Discovered scope post-#318 merge (2026-04-30). PR #318 fixed the
DO $$→DO $HCL escape issue. Apply retry surfaced the next bug::'admin_pw'(psql variable substitution) does NOT work insideDO $body$ ... $body$;blocks — psql treats the dollar-quoted body as a single literal token and skips substitution. Postgres receives literal:'admin_pw'and errors withsyntax error at or near ":" at character 164.Repo
forgejo_admin/pal-e-platform
What Broke
After #318 merged, the rendered SQL became valid HCL (
bodypreserved). Apply retried, Job created fresh. Pod ran and hit:The
:'admin_pw'is psql client-side variable interpolation. psql substitutes:variablereferences in regular SQL but NOT inside dollar-quoted strings (treats them as literals). So the SQL sent to postgres still has the literal:'admin_pw'token, which is invalid SQL syntax.This is a fundamental design conflict: cannot use psql variables inside
DO $$...$$blocks. The dev's original design (PR #304) tried to combine both. Either approach works alone; together they fail.Repro Steps
cd terraform && tofu apply -target=module.databaseDO $body$block referencing:'admin_pw'syntax error at or near ":" at character 164Expected Behavior
Conditional CREATE/ALTER ROLE works idempotently with the password from the
admin_pwpsql variable.Environment
terraform/modules/database/main.tflines 192-202:varand:'var'work in regular SQL, NOT inside dollar-quoted stringsUser Story
story:admin-row-crud — As the operator running the apply, I need the conditional CREATE/ALTER ROLE logic to actually use the password from the psql variable so the role gets provisioned.
Architecture
arch:postgres
Context
Three viable fixes:
\if/\gset(chosen) — drop the DO block, use psql client-side conditional outside any dollar-quoting. Cleanest; psql variable substitution works in regular SQL.current_setting()server-side —SET app.admin_pw = '<literal>'; DO $body$ ... current_setting('app.admin_pw') ... $body$;. Requires literal password in SET (still needs psql substitution outside DO).ON_ERROR_STOP.Going with #1. Replaces the procedural
DO BEGIN IF NOT EXISTS ... ELSE ... ENDwith:Same idempotent semantics, no dollar-quote, psql variables work natively.
This is the fourth post-merge runtime gap during the westside-admin bootstrap apply (after salt master crash, label
/, andDO $$HCL escape). All four would have been caught by a pre-mergetofu applyto a real cluster slice. Reinforces the lesson captured infeedback_tofu_validate_not_k8s_api. Worth a dedicated convention/process improvement after this loop closes.File Targets
terraform/modules/database/main.tflines 192-202 — replace the DO block with psql\if/\gsetconditionalAcceptance Criteria
\if+\gsetpatterntofu apply -target=module.databaseJob pod completes Succeeded==> admin_app role provisioned successfully\du admin_appfrom postgres pod shows the roleTest Expectations
Constraints
Checklist
Closes #THISRelated
/), pal-e-platform#317 (DOHCL escape)feedback_tofu_validate_not_k8s_api,feedback_verification_before_completion