2 9.23. Merge Support Functions #
4 PostgreSQL includes one merge support function that may be used in the
5 RETURNING list of a MERGE command to identify the action taken for each
8 Table 9.68. Merge Support Functions
14 merge_action ( ) → text
16 Returns the merge action command executed for the current row. This
17 will be 'INSERT', 'UPDATE', or 'DELETE'.
21 USING stock s ON p.product_id = s.product_id
22 WHEN MATCHED AND s.quantity > 0 THEN
23 UPDATE SET in_stock = true, quantity = s.quantity
25 UPDATE SET in_stock = false, quantity = 0
27 INSERT (product_id, in_stock, quantity)
28 VALUES (s.product_id, true, s.quantity)
29 RETURNING merge_action(), p.*;
31 merge_action | product_id | in_stock | quantity
32 --------------+------------+----------+----------
33 UPDATE | 1001 | t | 50
35 INSERT | 1003 | t | 10
37 Note that this function can only be used in the RETURNING list of a
38 MERGE command. It is an error to use it in any other part of a query.