Skip to content

Commit

Permalink
added truncate instead of delete (tembo-io#321)
Browse files Browse the repository at this point in the history
* added truncate instead of delete

Signed-off-by: Abinand P <abinand0911@gmail.com>

* updated and resolved the function name to get the table

---------

Signed-off-by: Abinand P <abinand0911@gmail.com>
  • Loading branch information
Abiji-2020 authored Oct 20, 2024
1 parent e2740d0 commit 222fecc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pgmq-extension/sql/pgmq.sql
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,24 @@ END
$$ LANGUAGE plpgsql;
-- purge queue, deleting all entries in it.
CREATE FUNCTION pgmq."purge_queue"(queue_name TEXT)
CREATE OR REPLACE FUNCTION pgmq."purge_queue"(queue_name TEXT)
RETURNS BIGINT AS $$
DECLARE
deleted_count INTEGER;
qtable TEXT := pgmq.format_table_name(queue_name, 'q');
BEGIN
EXECUTE format('DELETE FROM pgmq.%I', qtable);
GET DIAGNOSTICS deleted_count = ROW_COUNT;
-- Get the row count before truncating
EXECUTE format('SELECT count(*) FROM pgmq.%I', qtable) INTO deleted_count;
-- Use TRUNCATE for better performance on large tables
EXECUTE format('TRUNCATE TABLE pgmq.%I', qtable);
-- Return the number of purged rows
RETURN deleted_count;
END
$$ LANGUAGE plpgsql;
-- unassign archive, so it can be kept when a queue is deleted
CREATE FUNCTION pgmq."detach_archive"(queue_name TEXT)
RETURNS VOID AS $$
Expand Down

0 comments on commit 222fecc

Please sign in to comment.