Skip to content

Fatih Acar's blog

About of Database Systems and Information Technologies

Menu
  • Home
  • About Me
  • Contact Me
Menu

PostgreSQL Table and Column Based Logical Replication

Posted on 25/12/202225/12/2022 by Fatih Acar
With PostgreSQL 15 version, logical replication became applicable by filtering on the tables by column and row. In this way, replication can be made in the desired columns and rows in the tables.

Logical Replication

wal_level=logical

For logical replication, the wal_level parameter must be logical in the postgresql.conf file . By default, the value of wal_level is replica . The replica value provides the necessary wal files for streaming replication . wal_level parameter is logical when set to , it allows adding the necessary wal information for logical replication to the wal information provided with the replica value. In other words, when you set the wal_level parameter as logical, it also includes the replica value and streaming replication can continue to work. When wal_level runs logically, the size of wal files grows. Parameter editing is done in both databases.

Example Database and Tables

postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=# \c testdb
You are now connected to database “testdb” as user “postgres”.
testdb=# CREATE TABLE person(person_id int, name text, surname text, gender text, PRIMARY KEY(person_id));
CREATE TABLE
testdb=# INSERT INTO person values ​​(1,’Fatih’,’Acar’,’Male’);
INSERT 0 1
testdb=# INSERT INTO person values ​​(2,’Ayşe’,’Fatma’,’Woman’);
INSERT 0 1
testdb=# INSERT INTO person values ​​(3,’John’,’Jack’,’Male’);
INSERT 0 1

testdb=# select * from person;
person_id | name | surname | gender
———–+——-+———+——–
1 | Fatih | Acar | Male
2 | Ayse | Fatima | Woman
3 | Ahmet | Mehmet | Male
(3 rows)

Publication Creation

A publication is created by determining the desired fields of the table or tables to be used for logical replication.

postgres=# CREATE PUBLICATION pub_person FOR TABLE person (person_id, name, surname);
CREATE PUBLICATION

 

You can add a where condition to the query as follows to add a row-based filter in the publicized table.

CREATE PUBLICATION pub_person FOR TABLE person (person_id, name, surname) where (gender=’Male’);

If you want to include all tables in the publication scope, you can use the following query.

CREATE PUBLICATION pub_tables FOR ALL TABLES;

If you want to include certain tables in the scope of publication with all their fields, you can use the following query.

CREATE PUBLICATION pub_tablo123 FOR TABLE table1,table2,table3;

Replication User

There is a need for a user that has been created specifically for replication and has authorization to the tables to be replicated. For example, the repuser user can be created as follows.

[postgres@btcozumleri ~]$ createuser -p 5432 repuser -P –replication

postgres=# GRANT ALL ON person TO repuser;
GRANT

Subscription Creation

A table is created in the standby database to be the same as the columns specified in the publication in the master database.

postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=# \c testdb;
You are now connected to database “testdb” as user “postgres”.
testdb=# CREATE TABLE person(person_id int, name text, surname text, PRIMARY KEY(person_id));
CREATE TABLE

testdb=# CREATE SUBSCRIPTION sub_person CONNECTION ‘host=localhost dbname=testdb user=repuser password=123456 application_name=sub_person’ PUBLICATION pub_person;
NOTICE: created replication slot “sub_person” on publisher
CREATE SUBSCRIPTION

postgres=# \c testdb
You are now connected to database “testdb” as user “postgres”.
testdb=# select * from person;
person_id | name | surname
———–+——+———
1 | Fatih | Acar
2 | Ayse | Fatma
3 | Ahmet | Mehmet
(3 rows)

Data came into the standby database. You can perform test operations by adding and removing data from the master database.

select * from pg_replication_slots; You can list replication slots that are open on the master side.

Source : https://btcozumleri.com/postgresql-tablo-ve-kolon-bazli-logical-replication/

 

Share this:

  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.



View Fatih ACAR's profile on LinkedIn

Categories

  • Databases (329)
    • MsSQL Server (18)
      • Administration (16)
      • Errors and Solutions (4)
      • SQL (2)
    • MySQL (5)
      • Administration (4)
      • Backup And Recovery (1)
      • Errors and Solutions (1)
    • Oracle (290)
      • Administration (79)
      • Backup And Recovery (29)
      • Errors and Solutions (195)
      • Procedure (10)
      • SQL (32)
    • PostgreSQL (16)
      • Administration (15)
      • Errors and Solutions (3)
      • SQL (2)
    • Redis (1)
  • Microsotf Dynamics CRM (1)
  • Operating Systems (47)
    • Linux & Unix (34)
    • Windows (13)
  • Programing (5)
    • Python (5)
  • SAP (15)
    • Errors and Solutions (4)
    • SAP Basis (12)
  • Security (8)
    • Database Security (6)
    • Information Security (1)
    • System Security (4)
  • VMware (4)

Recent Posts

  • Redis 7 Sentinel Infrastructure Installation
  • Oracle Database 19c Multi Data Guard and DML Redirect, Switchover-Failover Operations on Oracle Linux 8
  • Oracle Database 23ai Free Installation Steps on Oracle Linux 9
  • Oracle Database 19c Active Data Guard Installation and DML Redirect, Switchover-Failover Operations on Oracle Linux 8
  • Oracle Database 19c Installation and 19.22 RU Patch Apply on Oracle Linux 8

Resources

  • Oracle
  • PostgreSQL

Blogroll

  • Gökhan Atıl
  • H. Koray Gündüz
  • Hakan Talip Öztürk
  • Zekeriya Beşiroğlu

Web Pages

  • BT Çözümleri

Tag Cloud

Administration Backup and Recovery Cluster Systems Database Administration Database Security Linux Linux Administration MsSQL Server MsSQL Server Error and Solutions MySQL MySQL Administration ORA-00018 ORA-00020 Oracle Oracle 12c Oracle 19c Oracle Administration Oracle Backup and Restore Oracle Data Guard Oracle Data Guard Failover Oracle Data Guard Switchover Oracle Error Solutions Oracle Linux Oracle Rman Backup Oracle Security Oracle SQL Query PostgreSQL PostgreSQL Administration PostgreSQL High Availability PostgreSQL Hot Standby Python SAP Basis Sap Errors and Solutions Sap System Administration SQL SQL Server Administration SQL Server Availability Group Stored Procedure System Administration System Security VMware VMware Administration Windows Windows Batch Script Windows Server

Social

  • View facar1987’s profile on Twitter
  • View facar’s profile on LinkedIn

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 17 other subscribers
©2026 Fatih Acar's blog | Built using WordPress and Responsive Blogily theme by Superb