patternsqlModerate
Truncate parent table but not children
Viewed 0 times
childrenbutparenttruncatenottable
Problem
I have a table in PostgreSQL 9.4 that I partitioned so that all inserts to the parent actually go into one of the child partitions. The problem is that I already have hundreds of millions of rows of data in the parent table from before I created partitions.
The parent and children exist in different schemas:
parent:
children:
Here is how I created the partitions.
How to
The parent and children exist in different schemas:
parent:
public.history_uintchildren:
partitions.history_uint_p2015_mm_ddHere is how I created the partitions.
How to
TRUNCATE the parent but not the children?Solution
Use the
The manual:
If
truncated. If
tables (if any) are truncated. Optionally,
the table name to explicitly indicate that descendant tables are included.
The schemas where parent and children live are only relevant as far as user privileges on the schema are concerned. Otherwise it's all the same to Postgres.
ONLY key word:TRUNCATE ONLY public.history_uint;The manual:
If
ONLY is specified before the table name, only that table istruncated. If
ONLY is not specified, the table and all its descendanttables (if any) are truncated. Optionally,
* can be specified afterthe table name to explicitly indicate that descendant tables are included.
The schemas where parent and children live are only relevant as far as user privileges on the schema are concerned. Otherwise it's all the same to Postgres.
Code Snippets
TRUNCATE ONLY public.history_uint;Context
StackExchange Database Administrators Q#105815, answer score: 13
Revisions (0)
No revisions yet.