Skip to content

sql: populate pg_class.relhastriggers#170891

Merged
trunk-io[bot] merged 3 commits into
cockroachdb:masterfrom
rafiss:fix-relhastriggers
May 25, 2026
Merged

sql: populate pg_class.relhastriggers#170891
trunk-io[bot] merged 3 commits into
cockroachdb:masterfrom
rafiss:fix-relhastriggers

Conversation

@rafiss
Copy link
Copy Markdown
Collaborator

@rafiss rafiss commented May 25, 2026

pg_dump uses relhastriggers as a fast-path filter when collecting triggers — if it's false, the trigger query is skipped for that table. Hard-coding it to false caused pg_dump to silently omit all triggers.

Resolves: #170842
Epic: none

Release note (bug fix): Fixed a bug where pg_class.relhastriggers always reported false, even for tables that had triggers defined.

@rafiss rafiss requested a review from spilchen May 25, 2026 14:12
@rafiss rafiss requested a review from a team as a code owner May 25, 2026 14:12
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented May 25, 2026

😎 Merged successfully - details.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Contributor

@spilchen spilchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: nice find

@spilchen made 2 comments.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on rafiss).


pkg/sql/pg_catalog.go line 4174 at r1 (raw file):

					tree.MakeDBool(tree.DBool(table.IsPhysicalTable())), // hasindexes
					tree.DBoolFalse, // hasrules
					tree.DBoolFalse, // hastriggers

is this the same bug?

rafiss and others added 2 commits May 25, 2026 15:23
pg_dump uses relhastriggers as a fast-path filter when collecting
triggers — if it's false, the trigger query is skipped for that table.
Hard-coding it to false caused pg_dump to silently omit all triggers.

Also fix pg_tables; the view-style summary of pg_class. Both columns were
hard-coded to false in CockroachDB even when the table had triggers or
row-level security enabled. The pg_class equivalents are already
populated correctly from the table descriptor, so derive these from
the same source.

Resolves: cockroachdb#170842
Epic: none

Release note (bug fix): Fixed a bug where pg_class.relhastriggers
always reported false, even for tables that had triggers defined.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
PostgreSQL initializes tgenabled to 'O' (TRIGGER_FIRES_ON_ORIGIN) when
a trigger is created, and only sets 'A' (ALWAYS) when the user runs
ALTER TABLE ... ENABLE ALWAYS TRIGGER. CockroachDB has no
session_replication_role equivalent and no ENABLE ALWAYS / ENABLE
REPLICA syntax, so a trigger is either enabled (the PostgreSQL default,
'O') or disabled ('D').

The previous default of 'A' caused pg_dump to emit a spurious
"ALTER TABLE ... ENABLE ALWAYS TRIGGER" statement after every trigger
because pg_dump treats tgenabled != 'O' as a non-default state worth
restoring.

Epic: none
Release note: None

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@rafiss rafiss force-pushed the fix-relhastriggers branch from 4e7e125 to a06a851 Compare May 25, 2026 19:25
Copy link
Copy Markdown
Collaborator Author

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR!

/trunk merge

@rafiss made 2 comments.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on spilchen).


pkg/sql/pg_catalog.go line 4174 at r1 (raw file):

Previously, spilchen wrote…

is this the same bug?

done; nice catch!

PostgreSQL's pg_get_triggerdef returns schema-qualified but not
catalog-qualified names for the target table (and unqualified or
search-path-resolved names for the trigger function). CockroachDB's
renderCreateTriggerStatement was emitting the full
database.schema.relation form for both, which made the output unusable
under pg_dump_compatibility=postgres: pg_dump treats the dumped string
as a CREATE TRIGGER body and replays it against the target database,
where CockroachDB rejects three-part names as cross-database references.

Drop the catalog component from both the table and the function name in
the rendered statement, matching pg_get_triggerdef and aligning with
how pg_get_indexdef already behaves.

Epic: none
Release note: None

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@rafiss rafiss force-pushed the fix-relhastriggers branch from a06a851 to 42294b9 Compare May 25, 2026 20:39
@trunk-io trunk-io Bot merged commit 8cf7d10 into cockroachdb:master May 25, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

roachtest: pg_dump failed

3 participants