PostgreSQL replication slot - how to see standby nodes - postgresql

I am using PostgreSQL 11.2. I have replication slots setup. I am able to commit to a table and see it on the standby. I have few more standbys. How can I see from the master what other standbys I have?

By selecting from pg_stat_replication. client_addr will be the IP address of a standby.

You can use the following query and check for client_addr, client_port, and client_hostname.
SELECT * FROM pg_stat_replication;

Related

Choosing schema in the slave host replication set Usin Slony-I

I am using slony-I to replicate tables from one server to another. I have to databases on the master slave that have same exact tables , and i want to replicate them to a single table in the slave. I can create the same tables in different schemas in the slave table , however i cant determine the schema in the replication set in the slave host.
I want to be able to determine the schema i am replicating to on the slave host.
How can i do this in slony?
Thank you
Unfortunately you can't choose the schema on the slave host in slony.The schema name and table name should be identical on both the master and slave. A Work around for this thing is to create another schema on both databases on slave and master and use them in your slony replication

View on slave postgres database

In postgres streaming replication master-slave setup, can we create views only on slave/standby server?
Can I have more than one standby slaves?
I am using postgresql 9.3.
re 1): this is not possible. The slave is always a 100% identical copy of the master.
re 2): yes this is possible. Quote from the manual:
You can have any number of standby servers

Postgres master / slave based on table

Currently I have 1 postgres instance which is starting to receive too much load and want create a cluster of 2 postgres nodes.
From reading the documentation for postgres and pgpool, it seems like I can only write to a master and read from a slave or run parallel queries.
What I'm looking for is a simple replication of a database but with master/slave based on which table is being updated. Is this possible? Am i missing it somewhere in the documentation?
e.g.
update users will be executed on server1 and replicated to server2
update big_table will be executed on server2 and replicated back to server1
What you are looking for is called MASTER/MASTER replication. This is supported natively (without PgPool) since 9.5. Note, that it's an "eventually consistent" architecture, so your application should be aware of possible temporary differences between the two servers.
See PG documentation for more details and setup instructions.

Stop PostgreSQL streaming replication

I want to make my PostgreSQL master / slave streaming replication setup into a single master slave setup without replication and without HA.
How is it possible to tell master that it no longer has slave and it should not replicate its data to the slave ?
Of course it should also not keep WALs to be sent to the slave as is done when the slave is temporarily down.
Here is what I did:
on both master / slave, edit pg_hba.conf, remove:
host replication replicator x.x.x.x/32 md5
in master, reload config, via:
select pg_reload_conf()
in slave's data dir, remove file: standby.signal
restart slave,
Then slave will keep the data, but don't replica from master any more, and slave is writable.
Depending on the version of Postgres you're using, it's possible your replication is done using "replication slots". If you don't have a client streaming from a replication slot anymore, you can drop the slot. Here are some useful queries:
Get disk usage per replication slot (for Postgres 9.6):
SELECT
redo_location,
slot_name,restart_lsn,
round((redo_location-restart_lsn) / 1024 / 1024 / 1024, 2) AS GB_behind
FROM
pg_control_checkpoint(),
pg_replication_slots;
Get disk usage per replication slot (for Postgres 10+):
SELECT redo_lsn,
slot_name,
restart_lsn,
round((redo_lsn-restart_lsn) / 1024 / 1024 / 1024, 2) AS GB_behind
FROM pg_control_checkpoint(),
pg_replication_slots;
Drop replication slot:
select pg_drop_replication_slot('slot_name');
References:
https://severalnines.com/database-blog/using-postgresql-replication-slots :
it is very important to also stop walsender to recover server memory with the following command:
select pg_terminate_backend(pid) from pg_stat_replication;
this will free up your server storage memory.

How to check if hot standby serves read-only queries

I have setup replication: master - slave. Slave server works as hot-standby, which means we can run read-only sql queries.
How actually can I see that slave server is serving read-only queries?
You can use pg_is_in_recovery() which returns True if recovery is still in progress(so the server is running in standby mode). Check the System Administration Functions for further informations.
=# SELECT pg_is_in_recovery();
pg_is_in_recovery
───────────────────
f
(1 row)
You can see simply with linux top command with pressing c. For example it is a process from our standby site top command list
postgres: pgUserNameSeen pgDatabaseName 10.10.10.10 (56608) idle
if you do not see anything like that, your standby does not server for read-only queries.