J
J
Jonas2020-01-21 18:10:02
PostgreSQL
Jonas, 2020-01-21 18:10:02

How to make pagination with dynamic condition in PostgreSQL?

Hey! There is a large table containing a list of files (chunks that are later glued together (for the sitemap)), their weight and number of lines (changes periodically):
5e27130abbad7803528959.png
There are 2 variables: ROWS_LIMIT and SIZE_LIMIT It is
required to do pagination using the above conditional variables. \Page\, is considered until the sum of the selected data (rowCount or fileSize rows) hits the set limit.
Is such an implementation possible in pure PostgreSQL?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
QuickJoey, 2020-01-22
@kriakiku

I think window functions will suit you, something like:

WITH 
file_list AS    (  
                SELECT  id
                        ,SUM(filesize) OVER (PARTITION BY component ORDER BY id) AS filesize_summary
                        ,SUM(rowcount) OVER (PARTITION BY component ORDER BY id) AS rowcount_summary
                FROM file_chunks
                )
SELECT * 
FROM file_chunks        c
INNER JOIN file_list    l ON c.id=l.id
WHERE   TRUE
        AND (filesize_summary<=_SIZE_LIMIT OR rowcount_summary<=ROWS_LIMIT);

Here in the CTE we select the id of the files that fall under the condition, and then by these ids we already select everything that is new from the main table. PARTITION BY is the area within which we summarize the data (that is, in the example, the sums by componentid will be different), ORDER BY is the order in which we accumulate the total.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question