-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ## NAME | ||
|
|
||
| spock.cleanup_resolutions() | ||
|
|
||
| ### SYNOPSIS | ||
|
|
||
| spock.cleanup_resolutions() | ||
|
|
||
| ### RETURNS | ||
|
|
||
| bigint — the number of rows deleted from `spock.resolutions`. | ||
|
|
||
| ### DESCRIPTION | ||
|
|
||
| Deletes rows from `spock.resolutions` whose `log_time` is older than the | ||
| value configured by `spock.resolutions_retention_days`. 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. | ||
|
|
||
| The function respects both `spock.save_resolutions` and | ||
| `spock.resolutions_retention_days`. If either setting disables cleanup | ||
| (`save_resolutions = off` or `resolutions_retention_days = 0`), the | ||
| function returns `0` without deleting anything. | ||
|
|
||
| ### ARGUMENTS | ||
|
|
||
| None. | ||
|
|
||
| ### EXAMPLE | ||
|
|
||
| Delete conflict history rows older than the configured retention window: | ||
|
|
||
| SELECT spock.cleanup_resolutions(); | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| `spock.save_resolutions`, `spock.resolutions_retention_days` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1012,6 +1012,17 @@ _PG_init(void) | |
| 0, | ||
| NULL, NULL, NULL); | ||
|
|
||
| DefineCustomIntVariable("spock.resolutions_retention_days", | ||
| "Number of days to retain rows in spock." CATALOG_LOGTABLE " table. " | ||
| "Rows older than this are deleted periodically by the apply worker. " | ||
| "Set to 0 to disable automatic cleanup.", | ||
| NULL, | ||
| &spock_resolutions_retention_days, | ||
| 100, 0, INT_MAX, | ||
| PGC_SUSET, | ||
|
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. Should this use PGC_SIGHUP since it is used in the apply worker? |
||
| 0, | ||
| NULL, NULL, NULL); | ||
|
|
||
| DefineCustomBoolVariable("spock.enable_quiet_mode", | ||
| "Reduce message verbosity for cleaner output", | ||
| "When enabled, downgrades DDL replication INFO/WARNING messages to LOG level " | ||
|
|
||
| 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.
Show resolved
Hide resolved
|
||
|
|
||
| Assert(CurrentMemoryContext == MessageContext); | ||
|
|
||
| for (;;) | ||
|
|
||
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.