[MDEV-10014] Add RETURNING to INSERT Created: 2016-05-02 Updated: 2023-10-18 Resolved: 2019-10-14 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Manipulation - Insert |
| Fix Version/s: | 10.5.0 |
| Type: | Task | Priority: | Major |
| Reporter: | Daniël van Eeden | Assignee: | Sergei Golubchik |
| Resolution: | Fixed | Votes: | 5 |
| Labels: | gsoc19 | ||
| Issue Links: |
|
||||||||||||
| Description |
|
Please add a RETURNING option to INSERT. Example from PostgreSQL
Inspired by: https://evertpot.com/writing-sql-for-postgres-mysql-sqlite/ This could make it easier to write statements which work with both MariaDB and PostgreSQL. And this might improve compatibility with Oracle RDBMS. How it'll work:
|
| Comments |
| Comment by Daniël van Eeden [ 2016-05-02 ] | |||||||||
|
For what I know this is not in the SQL:2003 standard. This might save a roundtrip. But I do think last_insert_id sent to the client anyways, so a 'SELECT LAST_INSERT_ID()' should not be needed. This could also allow multiple columns like it does in PostgreSQL. This could be efficient if defaults are used. Example:
So from a client perspective normally you would need to do this:
Often the solution to this is to keep all defaults client side to prevent the select, but then ad-hoc sql or other scripts might use other defaults. It should be possible to keep the defaults in the database w/o losing efficiency. | |||||||||
| Comment by Michaël de groot [ 2019-09-18 ] | |||||||||
|
In reply to Daniël, this is extremely beneficial when you are bulk adding rows plus their relationships when innodb_autoinc_lock_mode = 2 (required for Galera clusters). As the insert IDs cannot be guaranteed with innodb_autoinc_lock_mode=2 the last insert id is not sufficient when adding multiple rows: To add 100 rows the way Daniël described it would cause 200 extra round trips. Also, afaik the protocol only returns the LAST auto increment id of your query, not the auto increment ids of the rows in between. With this feature implemented, you can multi-row insert 100 rows and get the corresponding ids back in 1 rtt. Another new possibility with this feature: when doing an INSERT IGNORE .... RETURNING query you can see which rows actually inserted successfully, and trigger stuff in the application based on these results. Very nice work rucha174 and everybody else involved, I am looking forward to it being released. | |||||||||
| Comment by Elena Stepanova [ 2019-09-24 ] | |||||||||
|
Could you please specify here in JIRA description:
probably more questions to follow | |||||||||
| Comment by Sergei Golubchik [ 2019-09-24 ] | |||||||||
|
see https://github.com/MariaDB/server/compare/bb-10.5-serg...bb-10.5-10014 | |||||||||
| Comment by Oleksandr Byelkin [ 2019-10-10 ] | |||||||||
|
Generally all is OK except returning non-inserted data, so tests should be added:
| |||||||||
| Comment by Sergei Golubchik [ 2019-10-11 ] | |||||||||
|
post-review fixes: https://github.com/MariaDB/server/commit/b4626e12e2df239051ab6738b4cbfb6c6e10ac3a | |||||||||
| Comment by Kaspars [ 2020-10-12 ] | |||||||||
Is the result set guaranteed to be in the same order as value list? |