PostgreSQL Function Dependencies (Trigger)
See original GitHub issueSystem information:
- Operating system = Windows 10
- DBeaver version = 21.3.1
Connection specification:
- Database name and version = PostgreSQL 13
Describe the problem you’re observing:
On initial startup of the application the dependency list of a function that is used by multiple triggers is correct. Once you refresh the list then all the entries display the same table/schema as shown in the screenshot.
You need to restart the application to see the correct list again but if the list is refreshed once more, the same problem occurs.
Steps to reproduce, if exist:
- Create schema
- Create a function that returns trigger
- Create 2 tables each one having a trigger that uses the function created above
- Check the dependency list of the function (It will show 2 records each one referencing the trigger/table/schema combination created above)
- Refresh the list (Now it will show 2 records but the table/schema combination is wrong)
DDL:
CREATE SCHEMA test1;
CREATE OR REPLACE FUNCTION test1.trigger_set_timestamp()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.created_date = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$function$;
CREATE TABLE test1.test1 (
id int,
created_date timestamp
);
CREATE TRIGGER trigger_set_timestamp
BEFORE INSERT OR UPDATE ON test1.test1
FOR EACH ROW
EXECUTE FUNCTION test1.trigger_set_timestamp();
CREATE TABLE test1.test2 (
id int,
created_date timestamp
);
CREATE TRIGGER trigger_set_timestamp
BEFORE INSERT OR UPDATE ON test1.test2
FOR EACH ROW
EXECUTE FUNCTION test1.trigger_set_timestamp();
Simple query showing the correct results:
SELECT
pp.proname AS function_name , pc.relname AS table_name, pc.relnamespace::regnamespace::TEXT AS schema_name, pt.tgname AS trigger_name
FROM pg_catalog.pg_proc pp
JOIN pg_catalog.pg_depend pd
ON pp.oid = pd.refobjid
JOIN pg_catalog.pg_trigger pt
ON pd.objid = pt."oid"
JOIN pg_catalog.pg_class pc
ON pt.tgrelid = pc.oid
WHERE pp."oid" = <function object ID>
AND pc.relkind = 'r'
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Dependencies of functions used by triggers? - Stack Overflow
So you want to know what tables are used in the function called by a trigger. That's not possible. See under Halting Problem...
Read more >Documentation: 15: 9.29. Event Trigger Functions - PostgreSQL
PostgreSQL provides these helper functions to retrieve information from event triggers. For more information about event triggers, see Chapter 40.
Read more >postgresql and trigger functions - Toad Data Modeler
For example, generating a database, you have to carefully manage dependencies, so that functions are generated AFTER the table (because they ...
Read more >Triggers to enforce constraints & how to write them correctly
Finally, PostgreSQL has the option to create “constraint triggers” with CREATE CONSTRAINT TRIGGER . It sounds like such triggers could be used ...
Read more >[Solved]-Get all triggers that call a certain function-postgresql
Since functions in languages other than c and internal are stored as strings, PostgreSQL doesn't track dependencies between functions.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello @LonwoLonwo
I have updated the bug report with more detailed explanation of the issue and added some test DDLs as well. Please let me know if you need any additional information in order to reproduce the issue
yes. Thanks for the investigation!