How to implement RETURNING #4452
refset
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
RETURNINGis a SQL concept used in Postgres, let's take a look at how it applies to XTDB...From the Postgres docs:
Borrowing the RETURNING example from that page:
Let's try to run it in XT Play - oh - the RETURNING is ignored?!
This is XTDB working as intended, since RETURNING is not possible to implement correctly due to the async nature of transactions. Despite looking like and behaving like Postgres in many ways, 'DML' statements sent to XTDB are never actually interactive.
You also can't run a SELECT at the end of a transaction containing DML (writes), but that also counts as 'interactive'.
So then, how can you return some output of a transaction back to the client? You have a couple of options:
SNAPSHOT_TIMEto read the data needed as-of the state of the database immediately after the transaction finished (and thereby ignore changes from subsequent transactions):Play
Ah, but how do you know the exact TIMESTAMP of the last transaction you just wrote? For that we have a session variable (aka "run-time parameter") called
WATERMARK, which is the latest transaction ID observed by the client.Currently you need to run a separate query to resolve a timestamp from the watermark ID, before passing it into SNAPSHOT_TIME, such that you have 3 'transactions':
Play
This will ensure you don't see later transactions.
However, the journey isn't quite complete, because the original RETURNING statement would only return values for the modified entries. Luckily, being an immutable database, we can trivially use
_system_fromfor this:(i.e. repeating the same timestamp argument for both parameter slots)
What do you think?
Bonus Thoughts
SHOW WATERMARK;step could ideally be combined into the timestamp resolution step viacurrent_setting, i.e.SELECT system_time FROM xt.txs WHERE _id = current_setting('WATERMARK');- but `current_setting is not properly implemented/supported yet.Beta Was this translation helpful? Give feedback.
All reactions