Skip to content

Commit

Permalink
pg table rewrite trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiong committed Dec 8, 2024
1 parent 578c980 commit 239a878
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions _posts/2024-09-15-postgres-online-ddl.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,40 @@ admincoin=# select c.* from pg_cast c
10129 | 1043 | 25 | 0 | i | b
```

That seems to be the end of the story until I realized
[this line](https://github.com/postgres/postgres/blob/3f9b9621766796983c37e192f73c5f8751872c18/src/backend/commands/tablecmds.c#L5871).
Table rewrite has its own trigger! Following
[this example](https://www.postgresql.org/docs/current/event-trigger-table-rewrite-example.html),
we can quickly write a simple one.

```sql
CREATE OR REPLACE FUNCTION log_rewrite()
RETURNS event_trigger
LANGUAGE plpgsql AS
$$
BEGIN
RAISE NOTICE 'Table rewrite event detected for table %', pg_event_trigger_table_rewrite_oid()::regclass;
END;
$$;

CREATE EVENT TRIGGER no_rewrite_allowed ON table_rewrite EXECUTE FUNCTION log_rewrite();
```

Not fancy, just log it. Let's test it.

```
CREATE TABLE my_table (
id SERIAL PRIMARY KEY,
num INTEGER
);
ALTER TABLE my_table ALTER COLUMN num TYPE TEXT;
NOTICE: Table rewrite event detected for table my_table
```

OK. So now we have a systematic way to test it locally whenever we are not sure
whether a rewrite will be triggered.

`castfunc = 0` means no cast needed. `castmethod = b` means that the types are
binary-coercible.

Expand Down

0 comments on commit 239a878

Please sign in to comment.