Trigger functions

Updated at:

PolarDB for Oracle (PostgreSQL-compatible) provides several built-in trigger functions that you can attach directly to user-defined triggers, without writing custom trigger logic.

For details on the CREATE TRIGGER syntax, see CREATE TRIGGER.

Built-in trigger functions

The following table lists all built-in trigger functions, with each function's signature, behavior, and a minimal CREATE TRIGGER call showing the expected argument order.

FunctionDescriptionExample
tsvector_update_trigger() → triggerAutomatically updates a tsvector column from one or more plain-text source columns. The text search configuration is specified by name as a trigger argument.CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column() → triggerAutomatically updates a tsvector column from one or more plain-text source columns. The text search configuration is read from a regconfig column in the same table, allowing per-row configuration.CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, tsconfigcol, title, body)
suppress_redundant_updates_trigger() → triggerSkips UPDATE operations that would not change any data in the row. See When to use suppress_redundant_updates_trigger for details.CREATE TRIGGER ... suppress_redundant_updates_trigger()

Use tsvector_update_trigger

The following example shows the complete workflow for using tsvector_update_trigger to maintain a full-text search index on a messages table.

-- Create a table with a tsvector column
CREATE TABLE messages (
    title  text,
    body   text,
    tsv    tsvector
);

-- Create a trigger to update tsv automatically on insert or update
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
ON messages FOR EACH ROW EXECUTE FUNCTION
tsvector_update_trigger(tsv, 'pg_catalog.swedish', title, body);

-- Insert a row
INSERT INTO messages VALUES('title here', 'the body text is here');

-- Query the tsvector column to verify the trigger ran
SELECT * FROM messages;
--    title    |         body          |            tsv
-- ------------+-----------------------+----------------------------
--  title here | the body text is here | 'bodi':4 'text':5 'titl':1

-- Use the tsvector column for full-text search
SELECT title, body FROM messages WHERE tsv @@ to_tsquery('title & body');

To use a per-row text search configuration instead of a fixed configuration name, use tsvector_update_trigger_column and pass the name of a regconfig column as the second argument.

When to use suppress_redundant_updates_trigger

By default, PostgreSQL performs a physical row update on every UPDATE statement, even if the new values are identical to the existing ones. This is intentional: skipping the check makes updates faster in the common case.

However, redundant updates carry a real cost when they are frequent:

  • Extra time spent updating indexes that do not need to change

  • Dead rows accumulate faster, increasing VACUUM pressure

Filtering out no-op updates in application code is often impractical—the logic is scattered, error-prone, or simply unavailable at the call site. suppress_redundant_updates_trigger handles this at the database level: applied as a row-level BEFORE UPDATE trigger, it compares the incoming row with the existing row and aborts the update if nothing has changed.

Important

The comparison itself has a cost. If most updates in your workload actually change data, the trigger overhead will make updates slower on average. Use suppress_redundant_updates_trigger only on tables where a significant portion of UPDATE statements are genuine no-ops.

Add suppress_redundant_updates_trigger to a table

CREATE TRIGGER z_min_update
BEFORE UPDATE ON tablename
FOR EACH ROW EXECUTE FUNCTION suppress_redundant_updates_trigger();

Triggers fire in alphabetical order by name. In most cases, you need to ensure suppress_redundant_updates_trigger runs last—after any other BEFORE UPDATE triggers that may modify the row—so give it a name that sorts after all other trigger names on the table. The z_ prefix in the example achieves this.