add missing file
authorMarko Kreen <markokr@gmail.com>
Mon, 9 Apr 2007 14:51:42 +0000 (14:51 +0000)
committerMarko Kreen <markokr@gmail.com>
Mon, 9 Apr 2007 14:51:42 +0000 (14:51 +0000)
sql/pgq/functions/pgq.force_tick.sql [new file with mode: 0644]

diff --git a/sql/pgq/functions/pgq.force_tick.sql b/sql/pgq/functions/pgq.force_tick.sql
new file mode 100644 (file)
index 0000000..5d0fbe0
--- /dev/null
@@ -0,0 +1,46 @@
+
+
+create or replace function pgq.force_tick(i_queue_name text)
+returns bigint as $$
+-- ----------------------------------------------------------------------
+-- Function: pgq.force_tick(2)
+--
+--      Simulate lots of events happening to force ticker to tick.
+--
+--      Should be called in loop, with some delay until last tick
+--      changes or too much time is passed.
+--
+--      Such function is needed because paraller calls o ticker() are
+--      dangerous, and cannot be protected with locks as snapshot
+--      is taken before.
+--
+-- Parameters:
+--      i_queue_name     - Name of the queue
+--
+-- Returns:
+--      Currently last tick id.
+-- ----------------------------------------------------------------------
+declare
+    q  record;
+    t  record;
+begin
+    -- bump seq and get queue id
+    select queue_id,
+           setval(queue_event_seq, nextval(queue_event_seq)
+                                   + queue_tick_max_count * 2) as tmp
+      into q from pgq.queue;
+     where queue_name = i_queue_name
+       and not queue_external_ticker;
+    if not found then
+        raise exception 'queue not found or ticks not allowed';
+    end if;
+
+    -- return last tick id
+    select tick_id into t from pgq.tick
+     where tick_queue = q.queue_id
+     order by tick_queue desc, tick_id desc limit 1;
+
+    return t.tick_id;
+end;
+$$ language plpgsql security definer;
+