How to stop postgresql PGPOOL replication - database

I'm trying postgres database backup and restore. The access to postgres is through pgpool.
To achieve the back up I am following the official postgres pg_dumpall documentation.
Commands taken from postgres website: https://www.postgresql.org/docs/14/app-pg-dumpall.html
$ pg_dumpall > db.out
To reload database(s) from this file, you can use:
$ psql -f db.out postgres
The backup works fine.
However, when attempting to restore, I'm getting the following error because of replication feature enabled by pgpool.
psql:tmp/backup/postgresDump/pg_data.out:15: ERROR: database "xyz" is being accessed by other users
DETAIL: There is 1 other session using the database.
Here are the following ideas I tried by browsing other SO questions.
I tried to update active='f' in pg_catalog.pg_replication_slots view. It failed with the error below
DETAIL: Views that do not select from a single table or view are not automatically updatable.
HINT: To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
List the process ids for the replication slot and used pg_terminate_backend along with the pid, followed by restore.
Command to terminate the replication slot
psql -U postgres -h pgpool.default.cluster.local -c "SELECT pg_terminate_backend(3402)"
pg_terminate_backend
----------------------
f
(1 row)
As per the second answer in this link, Postgresql - unable to drop database because of some auto connections to DB
I executed the terminate_backend command multiple times until it returned 0 results. Although this step was successful, restore failed with error saying
psql:tmp/postgresDump/pg_data.out:14: ERROR: database "xyz" is being accessed by other users
DETAIL: There is 1 other session using the database.
Looks like as soon as I drop a replication slot, the pgpool recreates the replication slot and establishes a connection for it.
Tried by dropping the replication_slot following the official documentation.
https://www.postgresql.org/docs/9.5/functions-admin.html
psql -U postgres -h pgpool.default.local -c "select pg_drop_replication_slot('repmgr_slot_1001');"
ERROR: replication slot "repmgr_slot_1001" is active for PID 3402
Any information on how to execute restore functionality through psql is highly appreciated.

Related

PostgreSQL pg_upgrade error on pg_restore. "role '29648' does not exist"

When using pg_upgrade to upgrade PostgreSQL from 11 to 13 I receive the below error in step "Restoring database schemas in the new cluster":
pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 3801; 0 0 ACL FUNCTION "pg_stat_statements_reset"() postgres
pg_restore: error: could not execute query: ERROR: role "29648" does not exist
I can see pg_restore has already successfully restored other databases and all custom tables and constraints.
After researching online I can see that other suggest using the pg_restore option "-x, --no-privileges" however I do not see a way of applying this to the pg_upgrade command.
I've tried to locate this role in origin to no avail using SELECT * FROM pg_roles; but I see no role with rolname or oid as "29648".
You somehow managed to corrupt your database: there are permissions on the function pg_stat_statements_reset() for a user that doesn't exist. You'll have to search your conscience or statement history for the cause.
The solution for this problem is simple, since the function belongs to an extension:
DROP EXTENSION pg_stat_statements;
CREATE EXTENSION pg_stat_statements;
Now the function will have the default permissions, and the upgrade should work without problems.

In springboot, can I connect with two database so that if one database is giving exception then I can connect to the other database dynamically

Requirement in detail:
So I have two databases (both are in sync) and somehow one goes down and Spring Boot application starts giving exceptions. In this case I want the application to connect to the second database.
Please help me with this.
Thanks in advance.
As you have a DataGuard implementation in Oracle with a Primary database and another one in Standby mode, Oracle Transparent Application Failover is the way to go.
Transparent Application Failover (TAF) is a feature of the Java
Database Connectivity (JDBC) Oracle Call Interface (OCI) driver. It
enables the application to automatically reconnect to a database, if
the database instance to which the connection is made fails. In this
case, the active transactions roll back.
Database Setup
I am assuming your implementation of DG uses Oracle Restart.
Datatase: TESTDB
Service in TAF: TESTDB_HA
Primary site
srvctl add service -d testdb -s testdb_ha -l PRIMARY -y AUTOMATIC -e select -m BASIC -z 200 -w 1
srvctl start service -d testdb -s testdbha
Standby site
srvctl add service -d testdb -s testdb_ha-l PRIMARY -y AUTOMATIC -e select -m BASIC -z 200 -w 1
srvctl modify service -d testdb -s testdb_ha -failovermethod basic
Your JDBC connection
jdbc:oracle:thin:#(description=(address=(host=primaryserver)(protocol=tcp)(port=yourdbport))(address=(host=standbyserver)(protocol=tcp)(port=yourport))(failover=yes)(connect_data=(service_name=testdb_ha)(failover_mode=(type=select)(method=basic))))
In this setup, in case a failover from Primary to Standby, the connection will keep working once the failover is completed without manual intervention.
I am using this configuration currently in applications store in Kubernetes, using Spring Boot and/or Hibernate, and in normal Jboss Java applications. I have personally tested failover scenarios totally transparent for the applications. Obviously, if you have a transaction or query running in the moment the failover is being performed, you will get an error. But you don't need to manually change any jdbc settings in case of switch from primary site to standby site.

disconnecting pgadmin's connection via bash

after registering the user in the postgres database(in ubuntu) we can execute some basic command like dropdb, createdb directly from the terminal to alter the database.
I basically was creating a shell script to renew the database. So, I thought doing this would suffice:
dropdb veganary_test && createdb veganary_test
as always, I was wrong. Since I had multiple connections to the database, db wouldn't drop. I also tried this:
psql <database_name> -c "SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '<database_name>'
AND pid <> pg_backend_pid();"
and, yet it wasn't successful on disrupting pgadmin's connection. How can I disconnect every user connected to my <database_name> database from the terminal?(bash)
Upgrade to PostgreSQL v13 and use
dropdb --force veganary_test

How to solve error "Can not perform requested operation on nested resource. Parent resource not found" az sql db import throws

As part of my Azure Devops release pipeline I want to restore the bacpac of the previous days data from Azure storage.
I am running the following az command
az sql db import -s myprod.database.windows.net -n mydb
-g myresourcegroup -p "${mypassword}" -u myuser
--storage-key "${mystoragekey}" --storage-key-type StorageAccessKey
--storage-uri
"https://mystore.blob.core.windows.net/db/bk$(date -d "yesterday" '+%Y-%m-%d').bacpac"
This command fails with the following ERROR: Can not perform requested operation on nested resource. Parent resource 'myprod.database.windows.net/mydb' not found.
The db already exists on the server. Is this saying that the parent in this case is the server or the db? It should be able to find both

ERROR: right sibling's left-link doesn't match: block 19 links to 346956 instead of expected 346955 in index "pg_depend_reference_index"

I have a DB in postgres. The DB is big with total size over 4TB and over 500,000 tables and many indexes. The DB is over 4 yr old.
Recently, the Pgsql DB server was not starting up, so I did the following to get it started again:
/usr/pgsql-9.3/bin/pg_resetxlog -f /var/lib/pgsql/9.3/data
/usr/pgsql-9.3/bin/pg_ctl -D /var/lib/pgsql/9.3/data stop
/usr/pgsql-9.3/bin/pg_ctl -D /var/lib/pgsql/9.3/data start
/usr/pgsql-9.3/bin/pg_ctl -D /var/lib/pgsql/9.3/data stop
systemctl restart postgresql-9.3
Since then I am getting the following error whenever I try to create a new table in the DB:
mps_schools=> create table test_test(hello int);
ERROR: right sibling's left-link doesn't match: block 19 links to 346956 instead of expected 346955 in index "pg_depend_reference_index"
I have tried re-indexing the DB, but it doesnt work. What more can I do?
pg_resetxlog destroyed your database, which is something that can easily happen, which is why you don't call it just because you don't get the database started. It's something of a last ditch effort to get a corrupted database up.
What can you do?
Best solution: restore from a backup from before you ran pg_resetxlog.
Perform an offline backup of your database.
Then start the database in single user mode:
postgres --single -P -D /your/database/directory yourdbname
Then try to reindex pg_depend:
REINDEX TABLE pg_catalog.pg_depend;
Exit the single user session, restart the database, run pg_dumpall to dump the database (and hope that it works), create a new database cluster with initdb and import the dump.
Don't continue using the cluster where you ran pg_resetxlog.

Resources