Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid scanning non-default partitions #662

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nicolagi
Copy link

@nicolagi nicolagi commented May 30, 2024

This is a minimal change to address #660.
It changes one query from selecting the max timestamp in a given partition (which may incur a full table scan) to selecting any timestamp (which should be fast).

Testing. I tested it with:

pg_prove -U $myuser -d $mydb -ovf test/*.sql

Proof. I'll argue that the query change does not change the behavior of the function.

Let P be the partition being iterated on.

If it's empty, then v_max_timestamp evaluates to null in both old and new code, and the loop iteration does nothing in both old and new code.

Let's then consider a non empty partition P.
Let A (for any timestamp) be the timestamp returned by the new code, and let M (for max) be that returned by the old code.
We have A <= M.

If A >= CURRENT_TIMESTAMP then show_partition_name gives back P for both old and new code. (Using A or M doesn't make a difference, as they're both in P.)

Let's look at the case when A < CURRENT_TIMESTAMP.
If also M < CURRENT_TIMESTAMP, both the old and the new code would use CURRENT_TIMESTAMP, so no difference.

It remans to consider the case

A < CURRENT_TIMESTAMP <= M,

in which case CURRENT_TIMESTAMP still belongs to partition P, so the old code (uses M) and the new code (uses CURRENT_TIMESTAMP) both return P.

@keithf4
Copy link
Collaborator

keithf4 commented Jun 10, 2024

Thanks! Will take a look at this when I return to development.

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

Successfully merging this pull request may close these issues.

None yet

2 participants