postgres redirect queries to standby? - postgresql

I am trying to create a connection pooling system with load balancing. From what I unsderstand PGbouncer doesn't have a load balancing option and all I can do is to create a file with all the users+pass and configure the dbs/clusters. but in this option i cannot direct the connections to specific cluster. i'll explain: inserts will go to primary and selects will go to slave. what is possible is to let user "user1" connect to cluster on port 5432 to DB "database123".
How can I redirect queries to standby with other tools?
I tried to do this with pgpool but for some reason the standby is always on "waiting" status --> Cannot configure pgpool with master and slave nodes

It is impossible to tell from an SQL statement if it will modify data or not. What about SELECT delete_my_data();?
So all tools that try to figure that out by looking at the SQL statement are potentially problematic.
The best you can do is to write your application so that it uses two data sources: one for reading and one for writing, and you determine what goes where.

Related

Zalando operator- load balance read-write pgbouncer

I have installed Postgres cluster using zalando operator.
Also enabled pgbouncer for replicas and master.
But I would like to combine or load balance replicase and master connections,
So that read requests can be routed to read replicas and write requests can be routed to master.
Can anyone help me out in achieving this.
Thanks in advance.
Tried enabling pgbouncer.
pgbouncer is getting enabled either to master or to slave.
But I need a single point where it can route read requests to slaves and write requests to master.
There is no safe way to distinguish reading and writing statements in PostgreSQL. pgPool tries to do that, but I think any such solution is flaky. You will have to teach your application to direct reads and writes to different data sources.
I don't think Pgbouncer provides any out of the box way to load balance read and write queries. An alternative to that is the use of pgpool as a connection pooler. Pgpool provides a mode known as load_balance_mode which you can turn it on and it will try to load balance queries and send write queries to master and read queries to replica. You can read more about the load_balance_mode here

Is there any possible way for Data Replication?

Suppose my Master server have 10-15 database registered.
And on my slave server only have one or two of them.
So is there any possible way to sync between master and slave for that two databases only.
Cause while configuring data replication, there are settings for configure server only.
So, Is there any configuration to specify databases which to replicate?

Streaming replication solution in Postgres

I'm reading the article below how to achieve streaming replication in Postgres DB.
https://www.percona.com/blog/2018/09/07/setting-up-streaming-replication-postgresql
Some things are not quite clear
1) Are both DB instances active OR the slave instance is just a clone of master (o it communicates with master, but not the backend?
2) If DB master node failed, what will happen until second node will get back online? Is this covered by default by just having wal sender and wal receiver processes or something else needs to be added?
3) Which DB_HOST:PORT should be configured in the backend app if for example I have two backend nodes (both of them are active)?
If hot_standby = on in postgresql.conf, clients can connect to the standby, but only read data and not modify them. The standby is an identical physical cooy of the primary, just as if you had copied it file by file.
If the primary fails, the standby will remain up and running, but you still can only read data until somebody promotes the standby. You have to understand that PostgreSQL does not ship with cluster software that allows this to happen automatically. You have to usr some other software like Patroni for that.
That depends on the API your software is using. With libpq (the C API) or JDBC you can have a connection string that contains both servers and will select the primary automatically, but with other clients you may have to use external load balancing software.

PostgreSQL Multi master Synchronisation

I have a scenario as follows,
One cloud server is running an application with PGSQL as DB
Multiple local servers are running with same application with PGSQL as DB
User may access the cloud server for read/write data
User may access any of the local server to read/write data
What I need is synchronisation between all these databases. The synchronisation can be done live if connectivity is available, or immediately when connectivity is available.
Please guide me with some inputs, where can i start from.
Rethink your requirements.
Multimaster replication is full of pitfalls, and it is easy to get your databases out of sync unless you plan carefully. You'd probably be better off with a single master node.
That said, you could look at BDR by 2ndQuadrant which provides such functionality.

Pgpool master-slave replication with load balancing - prepared statement "S_380" does not exist

We are using pgpool to run 2 postgresql servers in master-slave mode with load balancing.
Sometimes (it is inconsistent, it works fine other times), I see this exception in the application logs -
09-10-17 01:35:01:627 - {ERROR} util.JDBCExceptionReporter
Thread[https-jsse-nio-8443-exec-3]; ERROR: cannot execute UPDATE in a read-only transaction
This is around a call to the write psql function called by the app server. Don't see any issues around other write statement, only this.
Don't know if it's pgpool issue.
You need to tell the Pgpool-II about the usage of write functions in SELECT statements.
The reason is when the load balancing is enabled in master-slave mode, Pgpool sends the write queries to MASTER database and load balances the read queries. More specifically statements such as DELETE, INSERT, UPDATE are routed to Master while SELECT gets load balanced. Now the problem is, when SELECT statement uses a write function, Pgpool still considers the statement as read-only and load balance it. Since it has no visibility of function definition.
So in nutshell Pgpool-II needs to know the usage of functions in SELECT statements which can write to database to make sure such SELECTs should not be load balanced.
For that you have two options.
Use Pgpool's black_function_list or whitle_function_list
configurations.
http://www.pgpool.net/docs/latest/en/html/runtime-config-load-balancing.html
Use /*NO LOAD BALANCE*/ prefix for queries containing write
function.
/*NO LOAD BALANCE*/ SELECT my_writing_function();
But the latter needs modifications in SQL statements of the existing applications which makes it not a very viable option.
I found the cause, this was for a psql function which updated the DB, such functions should be included in the black_function_list in pgpool configuration.
Add the write functions name list to the black_function_list parameter in the pgpool configuration.