HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Truncate parent table but not children

Submitted by: @import:stackexchange-dba··
0
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: public.history_uint

children: partitions.history_uint_p2015_mm_dd

Here is how I created the partitions.

How to TRUNCATE the parent but not the children?

Solution

Use the ONLY key word:

TRUNCATE ONLY public.history_uint;


The manual:


If ONLY is specified before the table name, only that table is
truncated. If ONLY is not specified, the table and all its descendant
tables (if any) are truncated. Optionally, * can be specified after
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.

Code Snippets

TRUNCATE ONLY public.history_uint;

Context

StackExchange Database Administrators Q#105815, answer score: 13

Revisions (0)

No revisions yet.