Insert and Route Data
In this step, you will insert sample data. A key feature of partitioning is that you insert data directly into the parent table (sales), and PostgreSQL automatically routes each row to the correct partition based on the value of the partition key (sale_date).
You should still be in the psql interactive terminal.
Execute the following INSERT statement to add 16 sample sales records spanning 2023 and 2024:
INSERT INTO sales (sale_date, product_id, sale_amount) VALUES
('2023-01-15', 101, 50.00),
('2023-02-20', 102, 75.50),
('2023-04-10', 103, 100.00),
('2023-05-25', 104, 60.25),
('2023-07-01', 105, 120.00),
('2023-08-12', 106, 80.75),
('2023-10-05', 107, 90.00),
('2023-11-18', 108, 110.50),
('2024-01-22', 109, 55.00),
('2024-03-01', 110, 70.00),
('2024-04-15', 111, 95.50),
('2024-06-10', 112, 65.00),
('2024-07-08', 113, 125.00),
('2024-09-20', 114, 85.25),
('2024-10-12', 115, 95.00),
('2024-12-01', 116, 115.75);
After the command completes, you will see the output INSERT 0 16, which indicates that 16 rows were successfully inserted.
To verify that the data was routed correctly, you can query the individual partitions. For example, let's check the count of records in the first quarter of 2023:
SELECT COUNT(*) FROM sales_2023_q1;
The output should be:
count
-------
2
(1 row)
Now, check the count for the fourth quarter of 2024:
SELECT COUNT(*) FROM sales_2024_q4;
The output should also be 2. This confirms that PostgreSQL has placed the data into the correct underlying partition tables.