WikiGalaxy

Personalize

PostgreSQL Upsert

Understanding Upsert in PostgreSQL

Upsert, a combination of "update" and "insert", allows you to insert a new record or update an existing one if a conflict arises. This is crucial for maintaining data integrity and efficiency.


INSERT INTO users (id, name, email) VALUES (1, 'John Doe', 'john@example.com')
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, email = EXCLUDED.email;
    

Conflict Handling

The "ON CONFLICT" clause specifies how to handle conflicts. In this example, if the "id" already exists, the "name" and "email" fields are updated to the new values.

Upsert with Additional Conditions

Conditional Update

You can add conditions to the update part of the upsert to handle more complex scenarios.


INSERT INTO products (product_id, price, stock) VALUES (101, 29.99, 100)
ON CONFLICT (product_id) DO UPDATE SET price = EXCLUDED.price WHERE products.stock > 0;
    

Conditional Logic

In this example, the price is only updated if the product is in stock, demonstrating conditional logic within an upsert operation.

Using Upsert for Data Synchronization

Data Synchronization

Upsert can be used to synchronize data between different databases or systems by ensuring that the latest data is always present.


INSERT INTO orders (order_id, order_date, status) VALUES (2001, '2023-10-01', 'Shipped')
ON CONFLICT (order_id) DO UPDATE SET order_date = EXCLUDED.order_date, status = EXCLUDED.status;
    

Consistency Assurance

This ensures that the order information is consistent and up-to-date across different systems or database replicas.

Handling Multiple Conflicts

Multiple Conflict Management

PostgreSQL allows handling multiple conflicts using different columns or unique constraints.


INSERT INTO employees (emp_id, department, salary) VALUES (123, 'HR', 50000)
ON CONFLICT (emp_id) DO UPDATE SET department = EXCLUDED.department WHERE employees.salary < 60000;
    

Conflict Resolution Strategy

This example updates the department only if the employee's salary is below a certain threshold, showcasing flexible conflict resolution strategies.

Upsert with Returning Clause

Returning Data

The "RETURNING" clause in an upsert can be used to return data after the operation, useful for obtaining generated values or confirmation of changes.


INSERT INTO inventory (item_id, quantity) VALUES (300, 50)
ON CONFLICT (item_id) DO UPDATE SET quantity = EXCLUDED.quantity + inventory.quantity
RETURNING item_id, quantity;
    

Data Retrieval

This example returns the item_id and updated quantity, allowing for immediate verification of the operation's result.

Advanced Upsert with Subqueries

Using Subqueries

Subqueries can be integrated into the upsert to dynamically calculate values or conditions.


INSERT INTO sales (sale_id, total) VALUES (400, 100)
ON CONFLICT (sale_id) DO UPDATE SET total = (SELECT SUM(amount) FROM transactions WHERE transactions.sale_id = sales.sale_id);
    

Dynamic Calculations

This upsert uses a subquery to recalculate the total based on related transactions, demonstrating complex data manipulation capabilities.

Upsert with Unique Constraints

Unique Constraint Management

Upserts can be used effectively with unique constraints to manage data integrity without manual checks.


INSERT INTO customers (cust_id, phone) VALUES (500, '123-456-7890')
ON CONFLICT ON CONSTRAINT unique_phone DO NOTHING;
    

Integrity Assurance

This upsert does nothing if the phone number already exists, thus preventing duplicate entries while maintaining data integrity.

Upsert with JSONB Data

Handling JSONB

PostgreSQL's JSONB type can be used in upserts to store and update semi-structured data efficiently.


INSERT INTO logs (log_id, data) VALUES (600, '{"event": "login", "user": "admin"}'::jsonb)
ON CONFLICT (log_id) DO UPDATE SET data = logs.data || EXCLUDED.data;
    

JSONB Merging

This upsert merges the existing JSONB data with the new data, allowing for flexible updates to semi-structured data.

Upsert in Partitioned Tables

Partitioned Table Management

Upsert operations can be performed on partitioned tables, which helps in managing large datasets efficiently by dividing them into smaller, manageable pieces.


INSERT INTO sales_partitioned (sale_id, amount) VALUES (700, 150)
ON CONFLICT (sale_id) DO UPDATE SET amount = EXCLUDED.amount + sales_partitioned.amount;
    

Efficient Data Handling

This example demonstrates how upserts can efficiently handle conflicts and updates in partitioned tables, ensuring data consistency across partitions.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025