How can we declare multiple partitions in a single statement in Postgres

0

We can create multiple partitions in a single statement in Oracle, but how can we implement the same in PostgreSQL.

oracle partitioning postgresql sql
2021-11-24 04:44:10
2

1

Simple: run several CREATE TABLE statements in a single DO statement:

DO
'BEGIN
   CREATE TABLE IF NOT EXISTS part1 PARTITION OF tab FOR VALUES IN (1);
   CREATE TABLE IF NOT EXISTS part2 PARTITION OF tab FOR VALUES IN (2);
   CREATE TABLE IF NOT EXISTS part3 PARTITION OF tab FOR VALUES IN (3);
END';

But I don't see why it is so important to you do do it with a single SQL statement.

2021-11-24 06:46:07
0

An equivalent doesn't exist in PostgreSQL. Due to how it implements its partitioning system, it would require you to create multiple tables at once. You will have to create the partitioned table and instead of specifying its partitions and bounds right there and then, you'll need to create each partition as a separate table, specifying partition bounds for each.

As always, there is much more to it but the below is meant to be a simplified take on the topic: in Oracle table partitions are a way to divide the original, first-class table "under the hood" and within its internal structure, while in PostgreSQL it's the other way around - it is the partitions that are the standalone, first-class tables and the partitioned table only links them together. Because of this, a partition cannot exist on its own in Oracle and be detached, individually referenced or linked to, and you typically need to go through the partitioned table to interact with its partitions. In PostgreSQL, for the most part you can work with the partitions like regular tables, and the partitioned table doesn't really work without them, acting as an empty view.

2021-11-24 07:24:59

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................