Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

How to get the count of hourly inserts on a table in PostgreSQL

I need a setup where rows older than 60 days get removed from the table in PostgreSQL.

I Have created a function and a trigger:

BEGIN
  DELETE FROM table
    WHERE updateDate < NOW() - INTERVAL '60 days';
  RETURN NULL;
END;
$$;

But I believe if the insert frequency is high, this will have to scan the entire table quite often, which will cause high DB load. I could run this function through a cron job or Lambda function every hour/day. I need to know the insert every hour on that table to take that decision.

Is there a query or job that I can setup which will collect the details?

question from:https://stackoverflow.com/questions/65936325/how-to-get-the-count-of-hourly-inserts-on-a-table-in-postgresql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just to count the number of records per hour, you could run this query:

SELECT  CAST(updateDate AS date) AS day
    ,   EXTRACT(HOUR FROM updateDate) AS hour
    ,   COUNT(*)
FROM _your_table
WHERE   updateDate BETWEEN ? AND ?
GROUP BY
    1,2
ORDER BY
    1,2;

We do about 40 million INSERT's a day on a single table, that is partitioned by month. And after 3 months we just drop the partition. That is way faster than a DELETE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...