Replicating Postgres Partitioned Tables v6.2
Both PostgreSQL and Advanced Server support partitioned tables, which can be replicated with xDB Replication Server in either a single-master or multi-master replication system.
The following are the various partitioning techniques:
- Advanced Server partitioning compatible with Oracle databases
- Postgres declarative partitioning (applies to both PostgreSQL and Advanced Server version 10 and later)
- Postgres table inheritance (applies to both PostgreSQL and Advanced Server)
If you are using Advanced Server, partitioned tables can be created using the CREATE TABLE
statement with partitioning syntax compatible with Oracle databases. For information on partitioning compatible with Oracle databases, see Database Compatibility: Table Partitioning in the EDB Postgres Advanced Server documentation.
If you are using version 10 or later of PostgreSQL or Advanced Server, declarative partitioning can be used to create partitioned tables. The CREATE TABLE
syntax for creating a declarative partitioned table is similar to the partitioning compatible with Oracle databases, but the individual partitions of the declarative partitioned table must be separately created with their own CREATE TABLE statements.
For information on declarative partitioning and table inheritance, see the PostgreSQL core documentation available at:
https://www.postgresql.org/docs/current/static/ddl-partitioning.html
Regardless of the partitioning method, the resulting partitioned table is comprised of a parent table with a set of child tables.
Replication of these Postgres partitioned tables in a single-master or multi-master replication system is accomplished in the same manner.
Note the following general restrictions when the publication contains a partitioned table:
- SQL Server cannot be used as a subscription database.
- When using table inheritance, the subscription databases must be Postgres – they cannot be Oracle or SQL Server.
All three partitioning techniques are illustrated on the emp
table used as an example throughout this document. The partitioned table is then used in a publication of a multi-master replication system in the following sections:
- For creating a publication in Postgres 9.x, see Creating a Postgres 9.x Partitioned Table Publication.
- For creating a publication in Postgres 10 or later, see Section Creating a Postgres Version 10 or Later Partitioned Table Publication.
The following creates the partitioned table in Advanced Server using partitioning compatible with Oracle databases:
The following creates the partitioned table in PostgreSQL or Advanced Server 10 or later using declarative partitioning:
Note
When creating a declarative partitioned table that is to be replicated using xDB Replication Server, the PRIMARY KEY constraint must be included in the CREATE TABLE statements of the individual partitions, not in the CREATE TABLE statement of the parent table to be partitioned.
The following creates the partitioned table in PostgreSQL or Advanced Server using table inheritance:
The following illustrates the types of SQL queries that can be made on the parent and child tables to show which tables actually contain the rows.
Querying the parent table, emp, with the asterisk appended to the table name in the SELECT statement, shows the rows in the parent and child tables. This is the default behavior if the asterisk is omitted.
The following queries show how the rows are physically divided amongst the child tables. The use of the ONLY
keyword results in rows only in the specified table of the SELECT
statement, and not from any of its children.
Section Creating a Postgres Version 10 or Later Partitioned Table Publication shows creation of the publication when using partitioning compatible with Oracle databases or declarative partitioning on a Postgres 10 or later database server.
Creating a Postgres Version 10 or Later Partitioned Table Publication
The following describes creating the publication using either partitioning compatible with Oracle databases or Postgres declarative partitioning on a Postgres 10 or later database server.
Note
If you are using table inheritance, you must still use the process described in Section Creating a Postgres 9.x Partitioned Table Publication even when creating the publication on a Postgres 10 or later database server.
The following restrictions apply when the publication contains a table with partitioning compatible with Oracle databases or declarative partitioning:
- The log-based method of synchronization replication must be selected for the publication database. The trigger-based method cannot be used.
- In a single-master replication system, the subscription databases must be Postgres version 10 or later. Oracle and SQL Server cannot be used as a subscription database.
- In a multi-master replication system, all primary nodes must be Postgres version 10 or later with the same compatibility mode as the primary definition node (that is, either compatible with native PostgreSQL or compatible with Oracle databases). For more information on the multi-master replication system compatibility modes, see Permitted MMR Database Server Configurations.
Follow the directions in Section Creating a Publication to create a primary definition node along with a publication containing the partitioned table. (For a single-master replication system, create the publication database along with the publication according to the directions in Section Creating a Publication.)
When creating the publication, only the parent table appears and is selected.
Figure 7-109: Creating a publication for a Postgres 10 or later partitioned table
The following shows the resulting replication tree for the partitioned table in the primary definition node:
Figure 7-110: Publication containing a Postgres 10 or later partitioned table
Create additional primary nodes as described in Section Creating Additional Primary nodes. (For a single-master replication system, create the subscription database and subscription according to the directions in Section Creating a Subscription.)
The following shows the resulting multi-master replication system after you have added an additional primary node.
Figure 7-111: MMR system with a Postgres 10 or later partitioned table
The partitioned table can now be kept synchronized on the primary nodes of the multi-master replication system.