How to force trigger a SpiceDB datastore GC?
# spicedb
w
A question regarding deleting relationships for postgres, how does this work? From what I can tell, it just sets the deleted_xid. I'm definitely missing a piece of information of how this operates
j
That’s how it marks the relationship deleted but leaves it available for historical snapshot queries. After the gc window (a spicedb config parameter) a gc process comes by and deletes the row.
Relationship queries are aware of this lifetime management and will include or exclude the relationship accordingly
w
Ah that makes sense. On the gc, what are the default settings? I've just got a load of very old relationships that need to purged, and not occupy space in the table
j
I believe the default is 25h.
w
Okay cool, and it will purge deleted relationships older than that? Is there a way to 'force' a gc to run?
j
Check out the datastore gc window and datastore gc frequency command line flags for setting these parameters
Then if you run another spicedb with those parameters it will gc out from under you. Another option is to manually run a sql query that deletes relationship rows that have a deleted xid < than the max, though that’s not guaranteed to work in the future.
Also, Postgres has its own gc logic so even after deleting the rows, it will take some time for Postgres to gc as well
You can run vacuum to reclaim the space.
w
We have autovacuum set on the table - it should run pretty frequently. It would be nice to have an endpoint/parameter to 'force' gc for given parameters, I would rather not manually run the sql query.
Like maybe on DeleteRelationships - immediate cleanup or something
Thank you very much for this! I wasn't 100% clear on why I was deleting relationships but the tuples were not getting removed from the table
v
For reference, from
spicedb serve --help
Copy code
--datastore-gc-interval duration                                  amount of time between passes of garbage collection (postgres driver only) (default 3m0s)
      --datastore-gc-max-operation-time duration                        maximum amount of time a garbage collection pass can operate before timing out (postgres driver only) (default 1m0s)
      --datastore-gc-window duration                                    amount of time before revisions are garbage collected (default 24h0m0s)
GC runs every 3m by default, but that does not mean that it will delete stuff. It will only delete stuff that sees outside of the GC window. If you want to force that, you need to configure SpiceDB with a shorter window, and then put it back to its original value
Also please note that an issue was identified with Postgres GC in SpiceDB < 1.26. GC can cause spikes in database usage that can in turn impact SpiceDB. This was addressed in 1.26 with a new index, but it requires Postgres 15 in order for the planner to select it: https://github.com/authzed/spicedb/releases/tag/v1.26.0 https://github.com/authzed/spicedb/pull/1550
If you are not doing API requests with
at_east_as_fresh
or
at_exact_snapshot
, then you can keep the GC window small
w
Ah - we are currently running the latest version v.127.0, and postgres 15, so hopefully this shouldn't be an issue for us
We do not very often run -
at_least_as_fresh
or
at_exact_snapshot
Is like a manual 'run GC' endpoint something you guys would consider to be useful?
v
let me double check but I think there may be a command for that already
yep
spicedb datastore gc
w
Oh nice, that's super helpful
Tested that in my sandbox environment - works super well, thanks for this
spicedb datastore gc --datastore-conn-uri <postgres_uri> --datastore-gc-window=1h --datastore-engine postgres
v
happy to help!
w
After running this manually, I have noticed that the automatic garbage collection is not running It instead fails with: { "level": "warn", "error": "error retrieving watermark: no rows in result set", "next-attempt-in": 3707098.236455, "time": "2023-12-08T10:22:27Z", "message": "error attempting to perform garbage collection" }
v
@Will Thornton that's odd, it would imply that the GC command deleted transaction 1 (which is added by the migrate command when you first initialize your SpiceDB cluster), or that it was never there, or perhaps that you guys manually deleted it at some point directly in the database. I couldn't reproduce this locally after running
spicedb datastore gc
on the postgres datastore. Would you mind sharing the command arguments you used?
I was only able to reproduce this in SpiceDB if you set the GC window to a value larger than unix epoch time zero. That would mean the transaction 1 would never be selected and throw that error. For example I triggered it with
525960h
, which is 50 years ago
w
Yeah of course, I ran
spicedb datastore gc --datastore-conn-uri <postgres_uri> --datastore-gc-window=5m --datastore-engine postgres
It seems to have resolved itself - if it's helpful I could dig through the logs and find the exact time? Vs the ~approx time I ran that command I wonder, and this is very much a guess, if it could be a temporary error state that occurs if a manual gc command with a smaller gc window is run on a datastore managed by a cluster with the larger window (maybe specifically for postgres)?
v
ah yeah, I see what's going on. And is precisely the fact that the GC window used by the server is larger than the GC window used by the command. When SpiceDB queries to get the most recent transaction outside of the GC window, it will find none, and the error will happen. The error is not problematic by itself, and it will eventually go away when the last revision makes is outside of the GC window defined in SpiceDB.
w
Nice thanks for confirming that suspicion
u
I also have another question regarding the GC config for the spiceDB. I am now trying to run the spiceDB with the operator, but unfortunately I found that the related parameter (such as the gc-window) looks like is not supported by the current config implementation in the operator (i.e https://github.com/authzed/spicedb-operator/blob/dd54d291a3d09241be71ffb828202e6945808bcf/pkg/config/config.go#L468). Is there another way to adjust the GC config (such as changing the GC window) when running with the operator?
v
@6enjamin99 I believe the operator will forward any unknown flag via a passthrough: https://github.com/authzed/spicedb-operator/blob/c94d78b9c597eba8337d105ba742726db0674c6d/pkg/config/config.go#L466-L469
u
cool, thx for the timely replies.
2 Views