I am working on a project where I generate a large number of users in PostgreSQL using a function that randomly generates user data, including a password of variable length (N). I want to assess the performance of both user data generation and insertion into the database. Specifically, I want to measure distinct time intervals: the time it takes to generate the data, the time taken for the insertion process, and the database processing latency from query initiation
Here is the PostgreSQL function I am using to generate users:
CREATE OR REPLACE FUNCTION generate_users(count INT)
RETURNS TABLE (
first_name_new TEXT,
last_name_new TEXT,
email TEXT,
gender TEXT,
dob DATE,
password TEXT
) AS $$
DECLARE
-- (omitting the declaration part for brevity)
BEGIN
FOR i IN 1..count LOOP
RETURN QUERY
SELECT
-- fields generation logic
generate_password(20); -- password generation function
END LOOP;
END;
$$ LANGUAGE plpgsql;
And the generate_password function:
CREATE OR REPLACE FUNCTION generate_password(length INT)
RETURNS TEXT AS $$
DECLARE
chars TEXT = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
result TEXT = '';
i INT;
BEGIN
FOR i IN 1..length LOOP
result := result || substr(chars::text, floor(random() * length(chars) + 1)::int, 1);
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
I would like guidance on how to measure each of the following:
- Data Generation Time: The time taken exclusively for generating user data.
- Insertion Time: The time taken exclusively for inserting generated data into the database.
- Database Processing Latency: The time it takes for the database to process the query from initiation to completion.
P.S. I am aware that tools like pgAdmin can provide the total execution time, but I need to break down these specific time intervals. Are there any built-in tools, queries, or methodologies within PostgreSQL that can help me achieve this granularity in performance measurement?
I also searched for profiling utilities but couldn't find anything that'd meet my goals here although I've been told that there's at least one that does anaylsis and shows you BOTH theoretical and actual execution times which would just be perfect in my case
