pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Can't drop trigger function

Open h0rmiga opened this issue 1 year ago • 1 comments

Hi!

Let's say we have existing database defined like this:

CREATE TABLE foo (id INT);

CREATE FUNCTION bar()
    RETURNS TRIGGER
    LANGUAGE plpgsql
AS $$
BEGIN
    RETURN NULL;
END;
$$;

CREATE TRIGGER foo_insert BEFORE INSERT ON foo EXECUTE FUNCTION bar();

and now we want to migrate to this schema (drop trigger and trigger function):

CREATE TABLE foo (id INT);

In this situation "apply" command will try to drop function before dropping trigger and will fail:

Plan: 1 to modify, 1 to drop.

Summary by type:
  functions: 1 to drop
  tables: 1 to modify

Functions:
  - bar

Tables:
  ~ foo
    - foo_insert (trigger)

DDL to be executed:
--------------------------------------------------

DROP FUNCTION IF EXISTS bar();

DROP TRIGGER IF EXISTS foo_insert ON foo;

Applying changes...

Executing group 1/1...
  Executing 2 statements in implicit transaction
Error: failed to execute concatenated statements in group 1: ERROR: cannot drop function bar() because other objects depend on it (SQLSTATE 2BP01)

h0rmiga avatar Nov 07 '25 10:11 h0rmiga

I'm running pgschema 1.11.1 and also have this issue:

--
-- Name: task_queue; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS task_queue (
    id BIGSERIAL,
    task_name text NOT NULL,
    task_args jsonb DEFAULT '[]' NOT NULL,
    task_kwargs jsonb DEFAULT '{}' NOT NULL,
    status text DEFAULT 'queued' NOT NULL,
    created_at timestamp DEFAULT now() NOT NULL,
    started_at timestamp,
    finished_at timestamp,
    error text,
    CONSTRAINT task_queue_pkey PRIMARY KEY (id)
);

--
-- Name: task_queue_notify(); Type: FUNCTION; Schema: -; Owner: -
--

CREATE OR REPLACE FUNCTION task_queue_notify()
RETURNS trigger
LANGUAGE plpgsql
VOLATILE
AS $$
BEGIN
    PERFORM pg_notify('task_queue', NEW.id::text);
    RETURN NEW;
END;
$$;

--
-- Name: task_queue_notify_trigger; Type: TRIGGER; Schema: -; Owner: -
--

CREATE OR REPLACE TRIGGER task_queue_notify_trigger
    AFTER INSERT ON task_queue
    FOR EACH ROW
    EXECUTE FUNCTION task_queue_notify();

When I run pgschema apply with this changes it works fine, but if I stash the change and want to rollback I get:


Summary by type:
  functions: 1 to drop
  tables: 1 to drop

Functions:
  - task_queue_notify

Tables:
  - task_queue

DDL to be executed:
--------------------------------------------------

DROP FUNCTION IF EXISTS task_queue_notify();

DROP TABLE IF EXISTS task_queue CASCADE;

Do you want to apply these changes? (yes/no): yes

Applying changes...

Executing group 1/1...
  Executing 2 statements in implicit transaction
Error: failed to execute concatenated statements in group 1: ERROR: cannot drop function task_queue_notify() because other objects depend on it (SQLSTATE 2BP01)```

Real-Gecko avatar Jun 26 '26 09:06 Real-Gecko