sql: SELECT INTO <session_var> FROM <table> [WHERE...] [AS OF SYSTEM TIME...] #69704
Labels
C-enhancement
Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
no-issue-activity
T-sql-foundations
SQL Foundations Team (formerly SQL Schema + SQL Sessions)
X-stale
Is your feature request related to a problem? Please describe.
Moving data from one location to another can be cumbersome if
INSERT INTO ... SELECT
is not available. In particular, suppose some rows were accidentally deleted from a production table with no recent backup. Those rows are visible viaSELECT ... AS OF SYSTEM TIME 'before the delete'
, but the easiest way of getting them back into the table isBACKUP ... AS OF SYSTEM TIME
and then restoring from backup, possibly into a separate table so that the old data can be merged in. Not the easiest thing to do quickly, especially since if you're in this situation, you probably aren't set up for easy backup/restore. If the data is small enough, you can also query with a format like csv, thenIMPORT
it or copy/paste it into an insert statement, but that requires creating a new client connection and is error-prone. It would be great to have a way of quickly recovering using pure SQL.Describe the solution you'd like
There is a SQL quasi-standard form that is basically copy/paste:
SELECT * INTO old_data FROM users
would create a session variable named old_data, give it the type appropriate to an array of rows from users, and populate it with the result of the query, returning the number of rows rather than the data. old_data could then be interacted with like any other array of tuples, so that you could doINSERT INTO new_users VALUES (old_data)
. We'd want an upper bound on the size of variables created this way, with an error pointing you toward backup or export functionality if that limit is exceeded. Since session variables exist outside of database time, no paradoxes would apply if you used AS OF SYSTEM TIME to populate one and then wrote from it in the present, just like if you'd literally copy/pasted.Describe alternatives you've considered
Basically anything else that lets you write to the present from a system time in the past, such as unreverting #57320, would solve this, but looks like that's been tricky. Similarly,
SELECT INTO table
is a Postgres alias to the same idea. Restoring a table in-place to a recent timestamp would likely be ideal but has unclear interactions with in-flight transactions. However, SELECT INTO variable would also just be more convenient in some cases than using subqueries even when they'd work fine.Jira issue: CRDB-9732
The text was updated successfully, but these errors were encountered: