sqlpostgresqlpostgres-9.6

Does deleting a table delete its triggers as well as functions?


I have created a table score and also created a function and a trigger along with it.

CREATE TABLE scores( 
--fields
);

CREATE FUNCTION scores_before_save() RETURNS trigger AS $$
BEGIN
   -- function logic
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER scores_before_save BEFORE INSERT OR UPDATE ON scores
    FOR EACH ROW EXECUTE PROCEDURE scores_before_save();

Now, I dropped this table

DROP TABLE scores;

So my question is that are triggers and functions automatically deleted once we delete the table or we need to manually delete them ?


Solution

  • You can easily check them

    -- check function existence
    SELECT proname  FROM pg_proc WHERE proname    = 'scores_before_save';
    
    -- check trigger existence
    SELECT trigger_name from information_schema.triggers 
    where trigger_name = 'scores_before_save';
    

    as you can see, after drop table, function stays here, but trigger is removed.