Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9c41015

Browse files
jeltzCommitfest Bot
authored and
Commitfest Bot
committed
Add support for ON CONFLICT DO SELECT [ FOR ... ]
Adds support for DO SELECT action for ON CONFLICT clause where we select the tuples and optionally lock them. If the tuples are locked with check for conflicts, otherwise not.
1 parent a7187c3 commit 9c41015

File tree

16 files changed

+571
-105
lines changed

16 files changed

+571
-105
lines changed

doc/src/sgml/ref/insert.sgml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ INSERT INTO <replaceable class="parameter">table_name</replaceable> [ AS <replac
3737
<phrase>and <replaceable class="parameter">conflict_action</replaceable> is one of:</phrase>
3838

3939
DO NOTHING
40+
DO SELECT [ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } ]
4041
DO UPDATE SET { <replaceable class="parameter">column_name</replaceable> = { <replaceable class="parameter">expression</replaceable> | DEFAULT } |
4142
( <replaceable class="parameter">column_name</replaceable> [, ...] ) = [ ROW ] ( { <replaceable class="parameter">expression</replaceable> | DEFAULT } [, ...] ) |
4243
( <replaceable class="parameter">column_name</replaceable> [, ...] ) = ( <replaceable class="parameter">sub-SELECT</replaceable> )
@@ -88,18 +89,24 @@ INSERT INTO <replaceable class="parameter">table_name</replaceable> [ AS <replac
8889

8990
<para>
9091
The optional <literal>RETURNING</literal> clause causes <command>INSERT</command>
91-
to compute and return value(s) based on each row actually inserted
92-
(or updated, if an <literal>ON CONFLICT DO UPDATE</literal> clause was
93-
used). This is primarily useful for obtaining values that were
92+
to compute and return value(s) based on each row actually inserted.
93+
If an <literal>ON CONFLICT DO UPDATE</literal> clause was used,
94+
<literal>RETURNING</literal> also returns tuples which were updated, and
95+
in the presence of an <literal>ON CONFLICT DO SELECT</literal> clause all
96+
input rows are returned. With a traditional <command>INSERT</command>,
97+
the <literal>RETURNING</literal> clause is primarily useful for obtaining
98+
values that were
9499
supplied by defaults, such as a serial sequence number. However,
95100
any expression using the table's columns is allowed. The syntax of
96101
the <literal>RETURNING</literal> list is identical to that of the output
97-
list of <command>SELECT</command>. Only rows that were successfully
102+
list of <command>SELECT</command>. If an <literal>ON CONFLICT DO SELECT</literal>
103+
clause is not present, only rows that were successfully
98104
inserted or updated will be returned. For example, if a row was
99105
locked but not updated because an <literal>ON CONFLICT DO UPDATE
100106
... WHERE</literal> clause <replaceable
101107
class="parameter">condition</replaceable> was not satisfied, the
102-
row will not be returned.
108+
row will not be returned. <literal>ON CONFLICT DO SELECT</literal>
109+
works similarly, except no update takes place.
103110
</para>
104111

105112
<para>

src/backend/commands/explain.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4651,10 +4651,35 @@ show_modifytable_info(ModifyTableState *mtstate, List *ancestors,
46514651

46524652
if (node->onConflictAction != ONCONFLICT_NONE)
46534653
{
4654-
ExplainPropertyText("Conflict Resolution",
4655-
node->onConflictAction == ONCONFLICT_NOTHING ?
4656-
"NOTHING" : "UPDATE",
4657-
es);
4654+
const char *resolution;
4655+
4656+
if (node->onConflictAction == ONCONFLICT_NOTHING)
4657+
resolution = "NOTHING";
4658+
else if (node->onConflictAction == ONCONFLICT_UPDATE)
4659+
resolution = "UPDATE";
4660+
else
4661+
{
4662+
switch (node->onConflictLockingStrength)
4663+
{
4664+
case LCS_NONE:
4665+
resolution = "SELECT";
4666+
break;
4667+
case LCS_FORKEYSHARE:
4668+
resolution = "SELECT FOR KEY SHARE";
4669+
break;
4670+
case LCS_FORSHARE:
4671+
resolution = "SELECT FOR SHARE";
4672+
break;
4673+
case LCS_FORNOKEYUPDATE:
4674+
resolution = "SELECT FOR NO KEY UPDATE";
4675+
break;
4676+
default: /* LCS_FORUPDATE */
4677+
resolution = "SELECT FOR UPDATE";
4678+
break;
4679+
}
4680+
}
4681+
4682+
ExplainPropertyText("Conflict Resolution", resolution, es);
46584683

46594684
/*
46604685
* Don't display arbiter indexes at all when DO NOTHING variant

0 commit comments

Comments
 (0)