Upsert with EXCLUDE value in PostgreSQL (insert, merge on repeated updates)
Last time, we read about how to use it in PostgreSQL UPSERT
.
In a quick review, UPSERT
is short for , which tends to insert values into the table INSERT ON DUPLICATE UPDATE
if they don't match a previous entry . If they do, they are automatically updated.INSERT
EXCLUDED
What is
in PostgreSQL
EXCLUDED
is the name given by the DBMS to a special table in which we have INSERTION
all the rows proposed for . Once INSERT
the operation runs, these rows may be inserted into this table.
This is mainly ON CONFLICT DO UPDATE
before the clause, specifically for this table. In addition, the SET
and WHERE
clauses often have EXCLUDED
permissions to access this table.
So, next time you try INSERT
something, if it seems to suit your needs, you can use EXCLUDED
a table. We hope that you are clear about the basic terminology used in this article and the mechanisms behind it.
Let's start by learning some methods, many of which are short but effective workarounds, to achieve EXCLUDED
the use of tables.
EXCLUDE
Basic usage
in PostgreSQL
A very simple first EXCLUDE
usage can be achieved.
-
Let's create one for the animals
TABLE
.create table animal( id int PRIMARY KEY, age int, type TEXT );
TYPE
This represents the type of animal. It can be a cat, dog, horse, etc. -
Let's plug in some values.
insert into animal values (1, 10, 'Dog'), (2, 12, 'Horse')
-
Now, let's move on to writing
EXCLUDED
our query.insert into animal values (1, 3, 'Cat'), (3, 4, 'Kitten') on conflict (id) do update set id = excluded.id, age = excluded.age, type = excluded.type;
So what's going on here? First, there's a copy.
The collection (1, 3, 'Cat')
violates PRIMARY KEY
the unique constraint of because a with key already 1
exists (1, 10, 'Dog')
.
So we call ON CONFLICT DO UPDATE
, and then once it finds a violation, we set the key of that row to the key of the new dataset being inserted, thus overwriting the previous entry.
The output will be something like this.
Output:
However, it depends on whether you want to do this or not. If while inserting, you probably don't want to overwrite but keep the previous entry intact, in which case you shouldn't use this query at all.
EXCLUDED
Structural differences in
basic usage in PostgreSQL
If a user might be trying to keep statements compact and readable, they might try to avoid using the above query, where:
set id = excluded.id, age = excluded.age, type = excluded.type;
If a lot of data is INSERTED
or UPDATED
, this can become confusing and subsequently cause problems.
A simpler approach is to use this instead.
set (id, age, type) = (EXCLUDED.id, EXCLUDED.age, EXCLUDED.type)
This tends to group elements and is pretty much the same as described above, but more ordered and readable.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab