Skip to content

Conversation

@james-owen
Copy link
Member

  • Adds paginate() to postgres query functions.
  • Adds PAGE_SIZE env var, that when defined, sets up pagination by default
  • No functional change if PAGE_SIZE is not defined

Pagination will work by fetching the first page, then doing another request with the last item that was returned prev=<uri>. Paginating in this way using the "seek" method isn't obviously better than using offset at first glance, especially considering that it requires an order by clause, but in local testing it is significantly faster especially at high offsets.

Here are the results from querying a table of clay-paragraph components with a page size of 10:

0th item

  • no difference

10,000

  • offset: 9 ms
  • seek: 5 ms

100,000

  • offset: 3189 ms
  • seek: 5 ms

500,000

  • offset: 17375 ms
  • seek: 9 ms

No paginating, get all instances

  • 8285 ms

This approach closes a current DoS vector and also avoids creating an even worse vector.

Copy link
Contributor

@mattoberle mattoberle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good.
Appreciate the additional work that went into performance testing too!

I left feedback on a couple of documentation-related items, nothing critical.

One case that is not covered in the tests is explicitly setting the CLAY_STORAGE_PAGE_SIZE environment variable. That could be a little tricky to mock depending on where your test is importing postgres/client.js, but might be worth covering since a future change to the parsing logic or default value could constitute a breaking change. What do you think?

Comment on lines 47 to 52
### `CLAY_STORAGE_PAGE_SIZE`

**Default:** `null` _(Number)_

Default page size for list queries. Enables pagination by default on list endpoints.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should note in the docs here that a page size of 0 will explicitly disable pagination.

- [`CLAY_STORAGE_POSTGRES_DB`](#clay_storage_postgres_db)
- [`CLAY_STORAGE_POSTGRES_CACHE_ENABLED`](#clay_storage_postgres_cache_enabled)
- [`CLAY_STORAGE_POSTGRES_CACHE_HOST`](#clay_storage_postgres_cache_host)
- [`CLAY_STORAGE_POSTGRES_PAGE_SIZE`](#clay_storage_postgres_page_size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is referred to everywhere else as CLAY_STORAGE_PAGE_SIZE.
It seems that we already have an inconsistent naming scheme for the environment variables in this project, but maybe stick with the more generic CLAY_STORAGE_PAGE_SIZE (something that other storage back-ends could adhere to).

@james-owen james-owen requested a review from jpope19 December 10, 2020 19:21
@james-owen james-owen requested review from amirandalibi and removed request for jpope19 August 16, 2022 17:16
Copy link
Member

@jjpaulino jjpaulino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants