mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Use A Trigger To Mirror Inserts To Another Table as a Postgres TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1330 TILs and counting..._
|
||||
_1331 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -740,6 +740,7 @@ _1330 TILs and counting..._
|
||||
- [Types By Category](postgres/types-by-category.md)
|
||||
- [Union All Rows Including Duplicates](postgres/union-all-rows-including-duplicates.md)
|
||||
- [Use A psqlrc File For Common Settings](postgres/use-a-psqlrc-file-for-common-settings.md)
|
||||
- [Use A Trigger To Mirror Inserts To Another Table](postgres/use-a-trigger-to-mirror-inserts-to-another-table.md)
|
||||
- [Use Argument Indexes](postgres/use-argument-indexes.md)
|
||||
- [Use Not Valid To Immediately Enforce A Constraint](postgres/use-not-valid-to-immediately-enforce-a-constraint.md)
|
||||
- [Use Rename To Hot Swap Two Tables](postgres/use-rename-to-hot-swap-two-tables.md)
|
||||
|
||||
38
postgres/use-a-trigger-to-mirror-inserts-to-another-table.md
Normal file
38
postgres/use-a-trigger-to-mirror-inserts-to-another-table.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Use A Trigger To Mirror Inserts To Another Table
|
||||
|
||||
On a PostgreSQL server, a trigger can be set up to execute a function whenever
|
||||
a certain action happens. In this case, I want set up a trigger to call a
|
||||
custom function whenever an `insert` happens on a specific table
|
||||
(`original_table`). That custom function will then mirror the inserted values
|
||||
into a secondary table (`another_table`).
|
||||
|
||||
First, I have to create a function that will respond to `insert` operations by
|
||||
inserting the newly inserted rows into `another_table`.
|
||||
|
||||
```sql
|
||||
create or replace function mirror_table_to_another_table()
|
||||
returns trigger as $mirrored_table$
|
||||
begin
|
||||
if (TG_OP = 'insert') then
|
||||
insert into another_table
|
||||
select * from new_table;
|
||||
end if;
|
||||
return null; -- result is ignored since this is an after trigger
|
||||
end;
|
||||
$mirrored_table$ language plpgsql;
|
||||
```
|
||||
|
||||
This function can then be referenced by the trigger I set up. After any insert
|
||||
on the `original_table`, the function defined above will be executed.
|
||||
|
||||
```sql
|
||||
create trigger mirror_table_to_another_table_trigger
|
||||
after insert on original_table
|
||||
referencing new table as new_table
|
||||
for each statement
|
||||
execute function mirror_table_to_another_table();
|
||||
```
|
||||
|
||||
Note that I am handling inserts at a statement level and that multiple rows can
|
||||
be inserted in a single statement. That is why the function mirrors to the
|
||||
other table with `select * from new_table`.
|
||||
Reference in New Issue
Block a user