Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
MERGE
Prev UpSQL CommandsHome Next

MERGE

MERGE — conditionally insert, update, or delete rows of a table

Synopsis

[ WITHwith_query [, ...] ]MERGE INTO [ ONLY ]target_table_name [ * ] [ [ AS ]target_alias ]USINGdata_source ONjoin_conditionwhen_clause [...][ RETURNING { * |output_expression [ [ AS ]output_name ] } [, ...] ]wheredata_source is:{ [ ONLY ]source_table_name [ * ] | (source_query ) } [ [ AS ]source_alias ]andwhen_clause is:{ WHEN MATCHED [ ANDcondition ] THEN {merge_update |merge_delete | DO NOTHING } |  WHEN NOT MATCHED BY SOURCE [ ANDcondition ] THEN {merge_update |merge_delete | DO NOTHING } |  WHEN NOT MATCHED [ BY TARGET ] [ ANDcondition ] THEN {merge_insert | DO NOTHING } }andmerge_insert is:INSERT [(column_name [, ...] )][ OVERRIDING { SYSTEM | USER } VALUE ]{ VALUES ( {expression | DEFAULT } [, ...] ) | DEFAULT VALUES }andmerge_update is:UPDATE SET {column_name = {expression | DEFAULT } |             (column_name [, ...] ) = [ ROW ] ( {expression | DEFAULT } [, ...] ) |             (column_name [, ...] ) = (sub-SELECT )           } [, ...]andmerge_delete is:DELETE

Description

MERGE performs actions that modify rows in the target table identified astarget_table_name, using thedata_source.MERGE provides a singleSQL statement that can conditionallyINSERT,UPDATE orDELETE rows, a task that would otherwise require multiple procedural language statements.

First, theMERGE command performs a join fromdata_source to the target table producing zero or more candidate change rows. For each candidate change row, the status ofMATCHED,NOT MATCHED BY SOURCE, orNOT MATCHED [BY TARGET] is set just once, after whichWHEN clauses are evaluated in the order specified. For each candidate change row, the first clause to evaluate as true is executed. No more than oneWHEN clause is executed for any candidate change row.

MERGE actions have the same effect as regularUPDATE,INSERT, orDELETE commands of the same names. The syntax of those commands is different, notably that there is noWHERE clause and no table name is specified. All actions refer to the target table, though modifications to other tables may be made using triggers.

WhenDO NOTHING is specified, the source row is skipped. Since actions are evaluated in their specified order,DO NOTHING can be handy to skip non-interesting source rows before more fine-grained handling.

The optionalRETURNING clause causesMERGE to compute and return value(s) based on each row inserted, updated, or deleted. Any expression using the source or target table's columns, or themerge_action() function can be computed. When anINSERT orUPDATE action is performed, the new values of the target table's columns are used. When aDELETE is performed, the old values of the target table's columns are used. The syntax of theRETURNING list is identical to that of the output list ofSELECT.

There is no separateMERGE privilege. If you specify an update action, you must have theUPDATE privilege on the column(s) of the target table that are referred to in theSET clause. If you specify an insert action, you must have theINSERT privilege on the target table. If you specify a delete action, you must have theDELETE privilege on the target table. If you specify aDO NOTHING action, you must have theSELECT privilege on at least one column of the target table. You will also needSELECT privilege on any column(s) of thedata_source and of the target table referred to in anycondition (includingjoin_condition) orexpression. Privileges are tested once at statement start and are checked whether or not particularWHEN clauses are executed.

MERGE is not supported if the target table is a materialized view, foreign table, or if it has any rules defined on it.

Parameters

with_query

TheWITH clause allows you to specify one or more subqueries that can be referenced by name in theMERGE query. SeeSection 7.8 andSELECT for details. Note thatWITH RECURSIVE is not supported byMERGE.

target_table_name

The name (optionally schema-qualified) of the target table or view to merge into. IfONLY is specified before a table name, matching rows are updated or deleted in the named table only. IfONLY is not specified, matching rows are also updated or deleted in any tables inheriting from the named table. Optionally,* can be specified after the table name to explicitly indicate that descendant tables are included. TheONLY keyword and* option do not affect insert actions, which always insert into the named table only.

Iftarget_table_name is a view, it must either be automatically updatable with noINSTEAD OF triggers, or it must haveINSTEAD OF triggers for every type of action (INSERT,UPDATE, andDELETE) specified in theWHEN clauses. Views with rules are not supported.

target_alias

A substitute name for the target table. When an alias is provided, it completely hides the actual name of the table. For example, givenMERGE INTO foo AS f, the remainder of theMERGE statement must refer to this table asf notfoo.

source_table_name

The name (optionally schema-qualified) of the source table, view, or transition table. IfONLY is specified before the table name, matching rows are included from the named table only. IfONLY is not specified, matching rows are also included from any tables inheriting from the named table. Optionally,* can be specified after the table name to explicitly indicate that descendant tables are included.

source_query

A query (SELECT statement orVALUES statement) that supplies the rows to be merged into the target table. Refer to theSELECT statement orVALUES statement for a description of the syntax.

source_alias

A substitute name for the data source. When an alias is provided, it completely hides the actual name of the table or the fact that a query was issued.

join_condition

join_condition is an expression resulting in a value of typeboolean (similar to aWHERE clause) that specifies which rows in thedata_source match rows in the target table.

Warning

Only columns from the target table that attempt to matchdata_source rows should appear injoin_condition.join_condition subexpressions that only reference the target table's columns can affect which action is taken, often in surprising ways.

If bothWHEN NOT MATCHED BY SOURCE andWHEN NOT MATCHED [BY TARGET] clauses are specified, theMERGE command will perform aFULL join betweendata_source and the target table. For this to work, at least onejoin_condition subexpression must use an operator that can support a hash join, or all of the subexpressions must use operators that can support a merge join.

when_clause

At least oneWHEN clause is required.

TheWHEN clause may specifyWHEN MATCHED,WHEN NOT MATCHED BY SOURCE, orWHEN NOT MATCHED [BY TARGET]. Note that theSQL standard only definesWHEN MATCHED andWHEN NOT MATCHED (which is defined to mean no matching target row).WHEN NOT MATCHED BY SOURCE is an extension to theSQL standard, as is the option to appendBY TARGET toWHEN NOT MATCHED, to make its meaning more explicit.

If theWHEN clause specifiesWHEN MATCHED and the candidate change row matches a row in thedata_source to a row in the target table, theWHEN clause is executed if thecondition is absent or it evaluates totrue.

If theWHEN clause specifiesWHEN NOT MATCHED BY SOURCE and the candidate change row represents a row in the target table that does not match a row in thedata_source, theWHEN clause is executed if thecondition is absent or it evaluates totrue.

If theWHEN clause specifiesWHEN NOT MATCHED [BY TARGET] and the candidate change row represents a row in thedata_source that does not match a row in the target table, theWHEN clause is executed if thecondition is absent or it evaluates totrue.

condition

An expression that returns a value of typeboolean. If this expression for aWHEN clause returnstrue, then the action for that clause is executed for that row.

A condition on aWHEN MATCHED clause can refer to columns in both the source and the target relations. A condition on aWHEN NOT MATCHED BY SOURCE clause can only refer to columns from the target relation, since by definition there is no matching source row. A condition on aWHEN NOT MATCHED [BY TARGET] clause can only refer to columns from the source relation, since by definition there is no matching target row. Only the system attributes from the target table are accessible.

merge_insert

The specification of anINSERT action that inserts one row into the target table. The target column names can be listed in any order. If no list of column names is given at all, the default is all the columns of the table in their declared order.

Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is none.

If the target table is a partitioned table, each row is routed to the appropriate partition and inserted into it. If the target table is a partition, an error will occur if any input row violates the partition constraint.

Column names may not be specified more than once.INSERT actions cannot contain sub-selects.

Only oneVALUES clause can be specified. TheVALUES clause can only refer to columns from the source relation, since by definition there is no matching target row.

merge_update

The specification of anUPDATE action that updates the current row of the target table. Column names may not be specified more than once.

Neither a table name nor aWHERE clause are allowed.

merge_delete

Specifies aDELETE action that deletes the current row of the target table. Do not include the table name or any other clauses, as you would normally do with aDELETE command.

column_name

The name of a column in the target table. The column name can be qualified with a subfield name or array subscript, if needed. (Inserting into only some fields of a composite column leaves the other fields null.) Do not include the table's name in the specification of a target column.

OVERRIDING SYSTEM VALUE

Without this clause, it is an error to specify an explicit value (other thanDEFAULT) for an identity column defined asGENERATED ALWAYS. This clause overrides that restriction.

OVERRIDING USER VALUE

If this clause is specified, then any values supplied for identity columns defined asGENERATED BY DEFAULT are ignored and the default sequence-generated values are applied.

DEFAULT VALUES

All columns will be filled with their default values. (AnOVERRIDING clause is not permitted in this form.)

expression

An expression to assign to the column. If used in aWHEN MATCHED clause, the expression can use values from the original row in the target table, and values from thedata_source row. If used in aWHEN NOT MATCHED BY SOURCE clause, the expression can only use values from the original row in the target table. If used in aWHEN NOT MATCHED [BY TARGET] clause, the expression can only use values from thedata_source row.

DEFAULT

Set the column to its default value (which will beNULL if no specific default expression has been assigned to it).

sub-SELECT

ASELECT sub-query that produces as many output columns as are listed in the parenthesized column list preceding it. The sub-query must yield no more than one row when executed. If it yields one row, its column values are assigned to the target columns; if it yields no rows, NULL values are assigned to the target columns. If used in aWHEN MATCHED clause, the sub-query can refer to values from the original row in the target table, and values from thedata_source row. If used in aWHEN NOT MATCHED BY SOURCE clause, the sub-query can only refer to values from the original row in the target table.

output_expression

An expression to be computed and returned by theMERGE command after each row is changed (whether inserted, updated, or deleted). The expression can use any columns of the source or target tables, or themerge_action() function to return additional information about the action executed.

Writing* will return all columns from the source table, followed by all columns from the target table. Often this will lead to a lot of duplication, since it is common for the source and target tables to have a lot of the same columns. This can be avoided by qualifying the* with the name or alias of the source or target table.

output_name

A name to use for a returned column.

Outputs

On successful completion, aMERGE command returns a command tag of the form

MERGEtotal_count

Thetotal_count is the total number of rows changed (whether inserted, updated, or deleted). Iftotal_count is 0, no rows were changed in any way.

If theMERGE command contains aRETURNING clause, the result will be similar to that of aSELECT statement containing the columns and values defined in theRETURNING list, computed over the row(s) inserted, updated, or deleted by the command.

Notes

The following steps take place during the execution ofMERGE.

  1. Perform anyBEFORE STATEMENT triggers for all actions specified, whether or not theirWHEN clauses match.

  2. Perform a join from source to target table. The resulting query will be optimized normally and will produce a set of candidate change rows. For each candidate change row,

    1. Evaluate whether each row isMATCHED,NOT MATCHED BY SOURCE, orNOT MATCHED [BY TARGET].

    2. Test eachWHEN condition in the order specified until one returns true.

    3. When a condition returns true, perform the following actions:

      1. Perform anyBEFORE ROW triggers that fire for the action's event type.

      2. Perform the specified action, invoking any check constraints on the target table.

      3. Perform anyAFTER ROW triggers that fire for the action's event type.

      If the target relation is a view withINSTEAD OF ROW triggers for the action's event type, they are used to perform the action instead.

  3. Perform anyAFTER STATEMENT triggers for actions specified, whether or not they actually occur. This is similar to the behavior of anUPDATE statement that modifies no rows.

In summary, statement triggers for an event type (say,INSERT) will be fired whenever wespecify an action of that kind. In contrast, row-level triggers will fire only for the specific event type beingexecuted. So aMERGE command might fire statement triggers for bothUPDATE andINSERT, even though onlyUPDATE row triggers were fired.

You should ensure that the join produces at most one candidate change row for each target row. In other words, a target row shouldn't join to more than one data source row. If it does, then only one of the candidate change rows will be used to modify the target row; later attempts to modify the row will cause an error. This can also occur if row triggers make changes to the target table and the rows so modified are then subsequently also modified byMERGE. If the repeated action is anINSERT, this will cause a uniqueness violation, while a repeatedUPDATE orDELETE will cause a cardinality violation; the latter behavior is required by theSQL standard. This differs from historicalPostgreSQL behavior of joins inUPDATE andDELETE statements where second and subsequent attempts to modify the same row are simply ignored.

If aWHEN clause omits anAND sub-clause, it becomes the final reachable clause of that kind (MATCHED,NOT MATCHED BY SOURCE, orNOT MATCHED [BY TARGET]). If a laterWHEN clause of that kind is specified it would be provably unreachable and an error is raised. If no final reachable clause is specified of either kind, it is possible that no action will be taken for a candidate change row.

The order in which rows are generated from the data source is indeterminate by default. Asource_query can be used to specify a consistent ordering, if required, which might be needed to avoid deadlocks between concurrent transactions.

WhenMERGE is run concurrently with other commands that modify the target table, the usual transaction isolation rules apply; seeSection 13.2 for an explanation on the behavior at each isolation level. You may also wish to consider usingINSERT ... ON CONFLICT as an alternative statement which offers the ability to run anUPDATE if a concurrentINSERT occurs. There are a variety of differences and restrictions between the two statement types and they are not interchangeable.

Examples

Perform maintenance oncustomer_accounts based upon newrecent_transactions.

MERGE INTO customer_account caUSING recent_transactions tON t.customer_id = ca.customer_idWHEN MATCHED THEN  UPDATE SET balance = balance + transaction_valueWHEN NOT MATCHED THEN  INSERT (customer_id, balance)  VALUES (t.customer_id, t.transaction_value);

Notice that this would be exactly equivalent to the following statement because theMATCHED result does not change during execution.

MERGE INTO customer_account caUSING (SELECT customer_id, transaction_value FROM recent_transactions) AS tON t.customer_id = ca.customer_idWHEN MATCHED THEN  UPDATE SET balance = balance + transaction_valueWHEN NOT MATCHED THEN  INSERT (customer_id, balance)  VALUES (t.customer_id, t.transaction_value);

Attempt to insert a new stock item along with the quantity of stock. If the item already exists, instead update the stock count of the existing item. Don't allow entries that have zero stock. Return details of all changes made.

MERGE INTO wines wUSING wine_stock_changes sON s.winename = w.winenameWHEN NOT MATCHED AND s.stock_delta > 0 THEN  INSERT VALUES(s.winename, s.stock_delta)WHEN MATCHED AND w.stock + s.stock_delta > 0 THEN  UPDATE SET stock = w.stock + s.stock_deltaWHEN MATCHED THEN  DELETERETURNING merge_action(), w.*;

Thewine_stock_changes table might be, for example, a temporary table recently loaded into the database.

Updatewines based on a replacement wine list, inserting rows for any new stock, updating modified stock entries, and deleting any wines not present in the new list.

MERGE INTO wines wUSING new_wine_list sON s.winename = w.winenameWHEN NOT MATCHED BY TARGET THEN  INSERT VALUES(s.winename, s.stock)WHEN MATCHED AND w.stock != s.stock THEN  UPDATE SET stock = s.stockWHEN NOT MATCHED BY SOURCE THEN  DELETE;


Prev Up Next
LOCK Home MOVE
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp