Fork me on GitHub
#xtdb
<
2022-08-28
>
lispyclouds13:08:19

Hello, is there a recommended way to cascade delete related docs when the root doc is deleted? This would be a similar behaviour like a ON DELETE CASCADE in SQL. My current way of recursively locating all dependent docs and submitting a deletion txn seems a bit clunky 😅

👍 1
refset15:08:01

Hey @U7ERLH6JX 🙂 there's no built-in way to perform a cascade delete like this. However, unless the cascade is massive, doing it within a single transaction function is probably the simplest approach

lispyclouds17:08:43

Thanks for the info @U899JBRPF! Will continue with the current approach! Out of curiousitly what would be classified as "massive" 😛 ? Mine could be 10s of thousands of tiny docs (log lines) at the worst case.

👌 1
lispyclouds17:08:37

i have a m:n:p relation going on. n = 10m and p = 5k n. Thats the kind of average numbers i have. this was quite nice to do in a RDBMS but pretty much everything else I'd rather use XT 😆

refset21:08:21

"Massive" depends on your transaction throughput tolerances and how long you might be prepared to hog the transaction writer for exclusively to run the procedure as a singe atomic tx. You could always break the delete ops into more granular transactions if that is a concern, but then you have to think about scheduling/retrying/userspace-locking etc.

refset21:08:23

Mainstream RDBMSs 'complect' a tonne of stuff, but undoubtedly they do offer some pretty handy features/machinery

lgessler01:08:05

I use custom tx functions for doing deep deletions like this--if :as have :bs have :cs, c's delete tx fn deletes the c, b's deletes the b and any dependent cs, and so on

lgessler01:08:26

wherever appropriate the "higher" deletion is defined in terms of the "lower" deletion to keep it DRY

lgessler01:08:03

some single deletion actions end up creating txs with 1000s of xt delete ops but that doesn't seem to have been an issue so far so I agree with @U899JBRPF that aiming for a single tx if at all possible is the way to go

lgessler01:08:31

here's https://github.com/lgessler/glam/blob/master/src/main/glam/xtdb/token_layer.clj#L148 if you're curious--I'm using some custom helper functions but I hope it's somewhat legible. note I'm using a convention where if a function returns a valid xtdb transaction it ends in **, and if it returns a valid xtdb operation it ends in *

🙏 2
lispyclouds07:08:50

Great info! Thanks a lot @U899JBRPF @U49U72C4V 🙏