Skip to content

Commit

Permalink
A flyway script that creates a custom function to replace a
Browse files Browse the repository at this point in the history
query that becomes unreasonably expensive for an installation with
a lot of download/access activity.
This solution is very efficient, but very PostgresQL-specific, hence
the use of a stored function. (#7804)
  • Loading branch information
landreev committed Sep 28, 2021
1 parent b43cda0 commit 9c653ff
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- This creates a function that ESTIMATES the size of the
-- GuestbookResponse table (for the metrics display), instead
-- of relying on straight "SELECT COUNT(*) ..."
-- Significant potential savings for an active installation.

CREATE OR REPLACE FUNCTION estimateGuestBookResponseTableSize()
RETURNS bigint AS $$
DECLARE
estimatedsize bigint;
BEGIN
SELECT ((reltuples / relpages)
* (pg_relation_size('public.guestbookresponse') / current_setting('block_size')::int)
)::bigint
FROM pg_class
WHERE oid = 'public.guestbookresponse'::regclass INTO estimatedsize;

RETURN estimatedsize;
END;
$$ LANGUAGE plpgsql IMMUTABLE;



0 comments on commit 9c653ff

Please sign in to comment.