question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PostgreSQL Function Dependencies (Trigger)

See original GitHub issue

System 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. image

Steps to reproduce, if exist:

  1. Create schema
  2. Create a function that returns trigger
  3. Create 2 tables each one having a trigger that uses the function created above
  4. Check the dependency list of the function (It will show 2 records each one referencing the trigger/table/schema combination created above)
  5. 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
GnsCycommented, Jan 5, 2022

Hello @GnsCy

Thanks for the bug report. Please provide test DDLs for steps 1 and 2 (trigger function and tables).

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

0reactions
LonwoLonwocommented, Jan 12, 2022

So I guess we can close the issue as resolved ?

yes. Thanks for the investigation!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found