postgresqlhttppaginationdatabase-cursorcontinuous

How to fetch the continuous list with PostgreSQL in web


I am making an API over HTTP that fetches many rows from PostgreSQL with pagination. In ordinary cases, I usually implement such pagination through naive OFFET/LIMIT clause. However, there are some special requirements in this case:

How can I achieve the mission? I am ready to change my whole database schema for it!


Solution

  • Assuming it's only the ordering of the results that fluctuates and not the data in the rows, Fredrik's answer makes sense. However, I'd suggest the following additions:

    Don't even consider using real db cursors for this.

    Keeping the result ids in Redis lists is another way to handle this (see the LRANGE command), but be careful with expiry and memory usage if you go down that path. Your Redis key would be the cursor_token and the ids would be the members of the list.