main: trigger comment is omitted when adding a trigger to an existing table
Summary
On current main, when the desired state adds a commented trigger to an existing table, the first pgschema apply creates the trigger but omits its COMMENT ON TRIGGER statement.
An immediate repeat plan proposes only the missing comment. A second apply adds it, after which the plan is clean.
Version
Version: 1.12.0@e18d9ede7973537919c02f25eced5c97271af1dc linux/amd64 2026-07-07T19:58:45-07:00
Commit e18d9ede7973537919c02f25eced5c97271af1dc was refs/heads/main when checked on 2026-07-22.
Reproduced with PostgreSQL 17.6 using an external plan database.
Minimal reproduction
Bootstrap
Create an existing schema and table:
CREATE SCHEMA issue_repro;
CREATE TABLE issue_repro.items (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
updated_at timestamptz NOT NULL DEFAULT now()
);
Desired state
Save this as desired.sql:
CREATE TABLE issue_repro.items (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE OR REPLACE FUNCTION issue_repro.set_updated_at()
RETURNS trigger
LANGUAGE plpgsql
SET search_path = ''
AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER set_items_updated_at
BEFORE UPDATE ON issue_repro.items
FOR EACH ROW
EXECUTE FUNCTION issue_repro.set_updated_at();
COMMENT ON TRIGGER set_items_updated_at ON issue_repro.items IS
'Sets updated_at before an item is changed.';
Commands
Apply the desired state once:
pgschema apply \
--host <host> --port <port> --db <db> --user <user> --password <password> --sslmode disable \
--plan-host <host> --plan-port <port> --plan-db <db> --plan-user <user> --plan-password <password> --plan-sslmode disable \
--schema issue_repro --file desired.sql --no-color --auto-approve
Check the stored trigger comment:
SELECT obj_description(t.oid, 'pg_trigger')
FROM pg_trigger AS t
JOIN pg_class AS c ON c.oid = t.tgrelid
JOIN pg_namespace AS n ON n.oid = c.relnamespace
WHERE n.nspname = 'issue_repro'
AND c.relname = 'items'
AND t.tgname = 'set_items_updated_at';
Then run an immediate repeat plan:
pgschema plan \
--host <host> --port <port> --db <db> --user <user> --password <password> --sslmode disable \
--plan-host <host> --plan-port <port> --plan-db <db> --plan-user <user> --plan-password <password> --plan-sslmode disable \
--schema issue_repro --file desired.sql --no-color
Expected
The first apply creates the function, trigger, and trigger comment. The stored comment is Sets updated_at before an item is changed., and the immediate repeat plan reports No changes detected.
Actual
The first apply reports success, but its generated DDL contains only the function and trigger:
CREATE OR REPLACE FUNCTION set_updated_at()
RETURNS trigger
LANGUAGE plpgsql
VOLATILE
SET search_path = ''
AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER set_items_updated_at
BEFORE UPDATE ON items
FOR EACH ROW
EXECUTE FUNCTION set_updated_at();
The catalog query returns NULL. The immediate repeat plan proposes only:
COMMENT ON TRIGGER set_items_updated_at ON items IS 'Sets updated_at before an item is changed.';
Applying the same desired state a second time adds that comment. The next plan then reports No changes detected.
Real schema impact
In a 12-schema application trial, the first apply omitted comments from three newly added triggers across two schemas. Both schemas became clean only after applying the same desired state a second time.
Impact
- A successful single apply does not fully match the declared schema.
- Trigger comments remain missing unless another apply is run.
- A convergence check immediately after apply reports drift even though no desired SQL changed.
Likely cause
The added-trigger branch in internal/diff/table.go collects the generated CREATE TRIGGER SQL but does not call generateTriggerComment. The modified-trigger branch does call that helper when needed.
This appears related to the trigger comment support added in #479 and the follow-up in #483.
Suggested direction
When processing td.AddedTriggers, emit COMMENT ON TRIGGER after CREATE TRIGGER whenever the desired trigger comment is non-empty, using the same comment generator as the modified-trigger branch.
I can test a proposed fix against both this minimal reproduction and the 12-schema trial.