Skip to content

Commit

Permalink
Merge pull request #3842 from EnterpriseDB/release/2023-03-23
Browse files Browse the repository at this point in the history
Release: 2023-03-23
  • Loading branch information
drothery-edb authored Mar 23, 2023
2 parents 0673a36 + efbbabe commit 12e8296
Show file tree
Hide file tree
Showing 15 changed files with 880 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Also note the following points:
```

- The examples use the sample tables, `dept`, `emp`, and `jobhist`, created and loaded when EDB Postgres Advanced Server was installed. The `emp` table is installed with triggers that you must disable to reproduce the same results as the examples. Log into EDB Postgres Advanced Server as the `enterprisedb` superuser and disable the triggers by issuing the following command:

-
```sql
ALTER TABLE emp DISABLE TRIGGER USER;
```

You can later reactivate the triggers on the `emp` table with the following command:

You can later reactivate the triggers on the `emp` table with the following command:
```sql
ALTER TABLE emp ENABLE TRIGGER USER;
```
4 changes: 2 additions & 2 deletions product_docs/docs/epas/15/epas_compat_sql/64_grant.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ You don't need to grant privileges to the owner of an object (usually the user t

Depending on the type of object, the initial default privileges can include granting some privileges to `PUBLIC`. The default is no public access for tables and `EXECUTE` privilege for functions, procedures, and packages. The object owner can revoke these privileges.

!!! Note
For maximum security, issue the `REVOKE` in the same transaction that creates the object. This approach prevents a window from occurring in which another user can use the object.
!!! Note
For maximum security, issue the `REVOKE` in the same transaction that creates the object. This approach prevents a window from occurring in which another user can use the object.

The possible privileges are:

Expand Down
154 changes: 154 additions & 0 deletions product_docs/docs/epas/15/epas_compat_sql/65a_merge.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
title: "MERGE"
---

## Name

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

## Synopsis

```sql
MERGE INTO target_table_name [ target_alias ]
USING data_source [ source_alias ] ON ( join_condition )
-- merge_update_clause
WHEN MATCHED THEN
UPDATE SET column = value, ... [ WHERE where_condition ]
[ DELETE [ WHERE where_condition ] ]

-- merge_insert_clause
WHEN NOT MATCHED THEN
INSERT [(col_list)] VALUES (val_list) [ WHERE where_condition ]
```

## Description

`MERGE` allows you to select rows from one or more sources for update or insertion into a table. You specify the join condition to determine whether to update or insert into the target table. You specify conditional UPDATE and INSERT statements using the WHERE clause in the MERGE statement.

It allows you to combine multiple operations and avoid multiple INSERT, UPDATE, and DELETE DML statements.

For more information, see [MERGE statement](https://www.postgresql.org/docs/current/sql-merge.html).


## Parameters

`target_table_name`

The name (optionally schema-qualified) of the target table to merge into.

`target_alias`

A substitute name for the target table. When an alias is provided, it completely hides the actual name of the table.

`data_source`

The name (optionally schema-qualified) of the source table or a source query. The source table is the source table name, view name, or transition table name. The source query (SELECT or VALUES statement) supplies the rows to be merged into the target_table_name.

`source_alias`

A substitute name for the data source. When an alias is provided, it completely hides the actual name of the table.

`join_condition`

An expression resulting in a value of type boolean similar to a WHERE clause that specifies which rows in the data_source match rows in the target_table_name.

`merge_update_clause`

Specifies the new column values of the target table. It performs this update if the join_condition of the ON clause is true.

Specify the where_condition if you want the database to execute the update operation only if the specified condition is true. The condition can refer to either the data source or the target table. If the condition is not true, then the database skips the update operation when merging the row into the table.

Specify the DELETE where_condition to clean up data in a table while populating or updating it. The only rows affected by this clause are those rows in the target table that are updated by the merge operation. The DELETE WHERE condition evaluates the updated value, not the original value that was evaluated by the UPDATE SET ... WHERE condition. If a row of the target table meets the DELETE condition but is not included in the join defined by the ON clause, then it is not deleted.

You can specify this clause by itself or with the merge_insert_clause. If you specify both, then they can be in either order.

`merge_insert_clause`

The merge_insert_clause specifies values to insert into the column of the target table if the condition of the ON clause is false. If you omit the column list after the INSERT keyword, then the number of columns in the target table must match the number of values in the VALUES clause.

To insert all of the source rows into the table, you can use a constant filter predicate in the ON clause condition. An example of a constant filter predicate is `ON (0=1)`. It recognizes such a predicate and makes an unconditional insert of all source rows into the table. This approach is different from omitting the merge_update_clause. In that case, the database still must perform a join. With constant filter predicate, no join is performed.

Specify the where_clause if you want the database to execute the insert operation only if the specified condition is true. The condition can refer only to the data source table. Database skips the insert operation for all rows for which the condition is not true.

You can specify this clause by itself or with the merge_update_clause. If you specify both, then they can be in either order.


## Examples

Create tables `target` and `source`:

```sql
CREATE TABLE target (tid integer, balance integer);
CREATE TABLE source (sid integer, delta integer);
```

Add rows to both the tables:

```sql
# Insert rows into target table
INSERT INTO target VALUES (1, 0);
INSERT INTO target VALUES (2, 20);
INSERT INTO target VALUES (3, 0);

# Insert rows into source table
INSERT INTO source VALUES (1, 100);
INSERT INTO source VALUES (2, 200);
INSERT INTO source VALUES (3, 300);
INSERT INTO source VALUES (4, 100);
INSERT INTO source VALUES (5, 300);
INSERT INTO source VALUES (6, 600);
```

This example shows how to use the WHERE condition for conditional UPDATE and INSERT statements in the MERGE statement:

```sql
BEGIN;
MERGE INTO target t
USING source s
ON (t.tid = s.sid)
WHEN MATCHED THEN
UPDATE SET balance = s.delta WHERE balance = 0
WHEN NOT MATCHED THEN
INSERT VALUES (s.sid, s.delta) WHERE s.sid >= 5;
```

This example shows how to use PostgreSQL style UPDATE and conditional INSERT in the MERGE statement:

```sql
BEGIN;
MERGE INTO target t
USING source s
ON (t.tid = s.sid)
WHEN MATCHED AND balance > 0 THEN
UPDATE SET balance = s.delta
WHEN NOT MATCHED THEN
INSERT VALUES (s.sid, s.delta) WHERE s.sid >= 5;
```

This example shows how to use conditional UPDATE and PostgreSQL style UPDATE in the MERGE statement:

```sql
BEGIN;
MERGE INTO target t
USING source s
ON (t.tid = s.sid)
WHEN MATCHED THEN
UPDATE SET balance = s.delta WHERE balance > 0
WHEN NOT MATCHED AND s.sid >= 5 THEN
INSERT VALUES (s.sid, s.delta);
```

This example shows how to use the WHERE condition for UPDATE and DELETE statements, and also WHEN MATCHED THEN DELETE:

```sql
MERGE INTO target t
USING source s
ON (t.tid = s.sid)
WHEN MATCHED THEN
UPDATE SET t.balance = t.balance + 100 WHERE t.balance = 20
DELETE WHERE t.balance = 30
WHEN NOT MATCHED THEN
INSERT VALUES (s.sid, s.delta)
WHEN MATCHED THEN
DELETE;
```
Loading

2 comments on commit 12e8296

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸŽ‰ Published on https://edb-docs.netlify.app as production
πŸš€ Deployed on https://641cc6b2f4405f0b87d1bdb6--edb-docs.netlify.app

Please sign in to comment.