-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add spock.resolutions_retention_days GUC and cleanup_resolutions() #412
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e7a3344
Add spock.resolutions_retention_days GUC and cleanup_resolutions()
rasifr aa6d5e5
docs: document spock.resolutions_retention_days and cleanup_resolutio…
rasifr b9adb1d
test: add resolutions_retention regression test
rasifr 482410f
Address PR 412 review: GUC level, optional days arg, doc fix
rasifr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
docs/spock_functions/functions/spock_cleanup_resolutions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| ## NAME | ||
|
|
||
| spock.cleanup_resolutions() | ||
|
|
||
| ### SYNOPSIS | ||
|
|
||
| spock.cleanup_resolutions([days integer]) | ||
|
|
||
| ### RETURNS | ||
|
|
||
| bigint — the number of rows deleted from `spock.resolutions`. | ||
|
|
||
| ### DESCRIPTION | ||
|
|
||
| Deletes rows from `spock.resolutions` whose `log_time` is older than the | ||
| retention window. Returns the number of rows deleted. | ||
|
|
||
| This function is a superuser-only manual trigger for the same cleanup that | ||
| the apply worker runs automatically once per day. It is useful for | ||
| immediate cleanup via `pg_cron` or when the apply worker has not been | ||
| running. | ||
|
|
||
| When `days` is provided it takes precedence over `spock.resolutions_retention_days`, | ||
| including when the GUC is set to 0 (automatic cleanup disabled). If `days` is | ||
| omitted, the GUC value is used; if the GUC is also 0, the function returns `0` | ||
| without deleting anything. | ||
|
|
||
| ### ARGUMENTS | ||
|
|
||
| | Argument | Type | Default | Description | | ||
| |----------|------|---------|-------------| | ||
| | `days` | `integer` | `NULL` | Retention window in days. Overrides `spock.resolutions_retention_days` for this call. Pass an explicit value to perform a one-off cleanup when automatic cleanup is disabled (`resolutions_retention_days = 0`). | | ||
|
|
||
| ### EXAMPLE | ||
|
|
||
| Delete conflict history rows older than the configured retention window: | ||
|
|
||
| SELECT spock.cleanup_resolutions(); | ||
|
|
||
| Delete rows older than 60 days, regardless of the GUC setting: | ||
|
|
||
| SELECT spock.cleanup_resolutions(60); | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| `spock.resolutions_retention_days` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,6 +218,9 @@ static dlist_head sync_replica_lsn = DLIST_STATIC_INIT(sync_replica_lsn); | |
| static XLogRecPtr skip_xact_finish_lsn = InvalidXLogRecPtr; | ||
| #define is_skipping_changes() (unlikely(!XLogRecPtrIsInvalid(skip_xact_finish_lsn))) | ||
|
|
||
| /* How often the apply worker runs spock_cleanup_resolutions() (milliseconds). */ | ||
| #define RESOLUTIONS_CLEANUP_INTERVAL_MS (86400L * 1000L) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could leave this, but ms resolution seems like overkill when seconds would do? |
||
|
|
||
| /* | ||
| * Whereas MessageContext is used for the duration of a transaction, | ||
| * ApplyOperationContext can be used for individual operations | ||
|
|
@@ -2947,6 +2950,7 @@ apply_work(PGconn *streamConn) | |
| XLogRecPtr last_received = InvalidXLogRecPtr; | ||
| XLogRecPtr last_inserted = InvalidXLogRecPtr; | ||
| TimestampTz last_receive_timestamp = GetCurrentTimestamp(); | ||
| TimestampTz last_cleanup_timestamp = 0; | ||
| bool need_replay; | ||
| ErrorData *edata = NULL; | ||
|
|
||
|
|
@@ -3050,6 +3054,26 @@ apply_work(PGconn *streamConn) | |
| } | ||
| } | ||
|
|
||
| /* | ||
| * Periodically clean up old rows from spock.resolutions. We run | ||
| * at most once per day regardless of whether the worker is idle | ||
| * or processing traffic. spock_cleanup_resolutions() manages its | ||
| * own transaction and error handling. | ||
| */ | ||
| if (!IsTransactionState() && | ||
| spock_resolutions_retention_days > 0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docs mention it checks save_resolutions, too. |
||
| { | ||
| TimestampTz cleanup_due; | ||
|
|
||
| cleanup_due = TimestampTzPlusMilliseconds(last_cleanup_timestamp, | ||
| RESOLUTIONS_CLEANUP_INTERVAL_MS); | ||
| if (GetCurrentTimestamp() >= cleanup_due) | ||
| { | ||
| spock_cleanup_resolutions(); | ||
| last_cleanup_timestamp = GetCurrentTimestamp(); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| Assert(CurrentMemoryContext == MessageContext); | ||
|
|
||
| for (;;) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, since you created this, what might be nice is if takes an optional argument that overrides resolutions_retention_days. For example, maybe they set retention to 0, then periodically manually run something like
SELECT spock.cleanup_resolutions(60)when they think it is getting too big.