pgq.drop_queue: add force flag
authorMarko Kreen <markokr@gmail.com>
Wed, 27 Oct 2010 14:57:39 +0000 (17:57 +0300)
committerMarko Kreen <markokr@gmail.com>
Tue, 2 Nov 2010 13:41:48 +0000 (15:41 +0200)
sql/pgq/functions/pgq.drop_queue.sql

index 3819a9148965e76de46dfc6b8fcb6d001867bafc..09e024fee80c50c184ff631311ce5371b8de75a3 100644 (file)
@@ -1,34 +1,34 @@
-create or replace function pgq.drop_queue(x_queue_name text)
+create or replace function pgq.drop_queue(x_queue_name text, x_force bool)
 returns integer as $$
 -- ----------------------------------------------------------------------
--- Function: pgq.drop_queue(1)
+-- Function: pgq.drop_queue(2)
 --
 --     Drop queue and all associated tables.
---     No consumers must be listening on the queue.
 --
+-- Parameters:
+--      x_queue_name    - queue name
+--      x_force         - ignore consumers
 -- ----------------------------------------------------------------------
 declare
     tblname  text;
     q record;
     num integer;
 begin
-    -- check ares
-    if x_queue_name is null then
-        raise exception 'Invalid NULL value';
-    end if;
-
     -- check if exists
     select * into q from pgq.queue
-        where queue_name = x_queue_name;
+        where queue_name = x_queue_name
+        for update;
     if not found then
         raise exception 'No such event queue';
     end if;
 
-    -- check if no consumers
-    select count(*) into num from pgq.subscription
-        where sub_queue = q.queue_id;
-    if num > 0 then
-        raise exception 'cannot drop queue, consumers still attached';
+    if not x_force then
+        -- check if no consumers
+        select count(*) into num from pgq.subscription
+            where sub_queue = q.queue_id;
+        if num > 0 then
+            raise exception 'cannot drop queue, consumers still attached';
+        end if;
     end if;
 
     -- drop data tables
@@ -54,3 +54,17 @@ begin
 end;
 $$ language plpgsql security definer;
 
+create or replace function pgq.drop_queue(x_queue_name text)
+returns integer as $$
+-- ----------------------------------------------------------------------
+-- Function: pgq.drop_queue(1)
+--
+--     Drop queue and all associated tables.
+--     No consumers must be listening on the queue.
+--
+-- ----------------------------------------------------------------------
+begin
+    return pgq.drop_queue(x_queue_name, false);
+end;
+$$ language plpgsql strict;
+