29.1. Separating Hot and Cold Data between a Heap Table and an Analytical Table #
This scenario describes how to store the hot data in a heap table and the cold data in an analytical table. In this case, applications continue to read and insert the data stored in both tables using a single view.
For the steps below, it is assumed that the hot data is stored in the axe_heap_table heap table, and the cold data is stored in the axe_archive_table partitioned analytical table. However, you do not need to partition the analytical table to reproduce the steps. For more information about Hive partitioning, refer to Chapter 28.
To separate hot and cold data on the Postgres Pro AXE server:
Create a view that merges the data from the
axe_heap_tableheap table and theaxe_archive_tableanalytical table.For example:
CREATE VIEW public.axe_table AS SELECT * FROM public.axe_heap_table UNION ALL SELECT id, "name", price, created_at FROM public.axe_archive_table;
Redirect the inserts on the view into the heap table using an
INSERTrule.For example:
CREATE RULE axe_table_insert AS ON INSERT TO public.axe_table DO INSTEAD INSERT INTO public.axe_heap_table VALUES (NEW.*);
Periodically copy the cold data from the heap table to the analytical table (e.g., using the pg_cron extension), so that only the data for the current month remains in the heap table.
For example:
BEGIN; LOCK TABLE public.axe_heap_table IN SHARE MODE; INSERT INTO public.axe_archive_table (id, "name", price, created_at, year, month) SELECT id, "name", price, created_at, EXTRACT(year FROM created_at)::int, EXTRACT(month FROM created_at)::int FROM public.axe_heap_table WHERE created_at < date_trunc('month', current_timestamp); DELETE FROM public.axe_heap_table WHERE created_at < date_trunc('month', current_timestamp); COMMIT;Periodically maintain Parquet files of the analytical table:
rewrite Parquet files to remove deleted rows from them
Note
The heap table must be locked in the
SHAREmode until the end of the transaction. This prevents new rows from being inserted between theINSERT INTOandDELETEcommands. Otherwise, such rows would be deleted without being copied to the analytical table.The heap table must not be partitioned because Postgres Pro AXE does not support rules on partitioned tables.