Append tables in PostgreSQL: How to combine multiple tables into one

Updated: January 5, 2024 By: Guest Contributor Post a comment

Introduction

Combining tables in PostgreSQL can streamline queries and organize data efficiently. This guide will take you through various methods of appending tables, from basic UNION operations to advanced partitioning techniques.

Understanding UNION and UNION ALL

The simplest method of appending tables in PostgreSQL is using the UNION operator. This merges the results of two or more SELECT statements into a single result set, eliminating duplicate rows:

SELECT * FROM table1
UNION
SELECT * FROM table2;

UNION ALL is similar, but it doesn’t remove duplicates, resulting in a potentially faster query:

SELECT * FROM table1
UNION ALL
SELECT * FROM table2;

Joining Tables with Similar Structures

When tables have the same number of columns and corresponding data types, you can append them directly. If there are differences, you can alias columns:

SELECT column1, column2 FROM table1
UNION ALL
SELECT columnA AS column1, columnB AS column2 FROM table2;

Using JOINs for Combining Related Tables

If tables are related but don’t have identical structures, you can use JOIN clauses to combine them into a single view:

SELECT * FROM table1
INNER JOIN table2 ON table1.key = table2.foreign_key;

Horizontal Partitioning Through Inheritance

In PostgreSQL, you can design a system of parent and child tables, where the child inherits from the parent. This can be especially useful for large datasets, but queries become slightly more complex:

CREATE TABLE parent (id INT PRIMARY KEY, data TEXT);
CREATE TABLE child1 ( CHECK ( id \< 1000 ) ) INHERITS (parent);
CREATE TABLE child2 ( CHECK ( id >= 1000 ) ) INHERITS (parent);

To retrieve data from both child tables, you would query the parent table:

SELECT * FROM parent;

Appending Tables with data Partitioning

PostgreSQL supports table partitioning, which allows you to break down a large table into smaller, more manageable pieces that can be appended when necessary:

CREATE TABLE measurement (
    city_id         INT,
    logdate         DATE NOT NULL,
    peaktemp        INT,
    unitsales       INT
) PARTITION BY RANGE (logdate);

Advanced Techniques: CTEs and Views

You can use Common Table Expressions (CTEs) to create temporary, composable subquery results for complex append operations:

WITH first_set AS (
    SELECT * FROM table1
),
second_set AS (
    SELECT * FROM table2
)
SELECT * FROM first_set
UNION ALL
SELECT * FROM second_set;

Alternatively, creating a view allows you to append multiple tables and access them as if they were a single table:

CREATE VIEW combined_tables AS
SELECT * FROM table1
UNION ALL
SELECT * FROM table2;

Performance Considerations

Merging tables can impact performance, so it’s important to index appropriately, analyze the execution plan, and consider the trade-offs between different methods of combining tables based on your specific use case.

Practical Example: E-commerce Orders

Consider an e-commerce platform with separate tables for each year’s orders. These can be appended into a unified view for analysis. Ensure that each table has the same structure, or utilize aliasing where necessary:

CREATE VIEW all_orders AS
SELECT * FROM orders_2020
UNION ALL
SELECT * FROM orders_2021
UNION ALL
SELECT * FROM orders_2022;

Conclusion

Appending multiple tables in PostgreSQL requires careful consideration of the structure and relationship between the tables. Various methods like UNION operations, inheritance, partitioning, CTEs, and Views offer a range of solutions. Choose the method that best fits your data and performance needs to effectively combine your database tables.