Using JPA to query postgres replication status - java

I am trying to query the replication status of the underlying database using the query:
em.createNativeQuery("SELECT application_name, backend_start, state, cast(write_lag as text) FROM pg_stat_replication;").getResultList();
Unfortunately, all columns except application_name return null. Even if I only run
em.createNativeQuery("SELECT backend_start FROM pg_stat_replication;").getResultList();
I get a null result. If I run the same query in the postgres command-line client, I get a meaningful result:
dbname=# select backend_start from pg_stat_replication;
backend_start
-------------------------------
2018-10-16 09:01:58.262578+02
(1 row)
I am using spring-boot 1.2.6 with hibernate, and postgresql 10.5.
What can I do so I get the results for this query?

Just after posting, I realised I user different users in the CLI and from java. Apparently, the user needs to be superuser or have the pg_read_all_stats default role. Like this:
GRANT pg_read_all_stats TO user;

Related

How to get postgres schema from runinng query in Java

assume you do query on pg_stat_activity table and you get example result:
datid
datname
pid
usesysid
usename
application_name
client_addr
client_hostname
client_port
backend_start
xact_start
query_start
state_change
wait_event_type
wait_event
state
backend_xid
backend_xmin
query
backend_type
7198
10
rdsadmin
7195
16384
rdsadmin
32375
10
rdsadmin
PostgreSQL JDBC Driver
16409
c-t-s
21143
16410
c-t-s
c-t-s
10.10.3.1
48037
2021-01-18 13:19:03
2021-01-18 13:31:23
2021-01-18 13:31:23
Client
ClientRead
idle
COMMIT
client backend
I would like to know on which schema the query COMMIT was executed?
My case is i have schema-based multitenancy and i would like to distinguish between schemas (tenants). We always make a single-schema queries, so we dont mix them. To achieve that we set search_path on each getConnection method invocation. Code is developed in java and we dont use schema names in queries, as it is always dynamic -- taken from current request context and set in getConnection method.
With current result I dont know which tenant (schema) is causing slow / long queries.
I have tried to select from pg_class by ids taken from pg_stat_activity but without luck.
So far the comments did not answer my problem, is that possible at all?

Fetch result set from a postgres Stored procedure in java

I created a postgres function which returns a set of ref cursors. When I call the stored procedure from java the JDBC statement build is
select * from ga_rpt_movement('CODE','2018-5-10','2018-5-10','2018-5-10','C','STAT1','2018-5-10','2018-5-10','12344','A','T','34',25,50,'M','1','firstname',0,10) as result
When I run the same query on postgres terminal it gives me the four coursors.
Now I am not able to understand how do I fetch this cursor data in java.
Any suggestions, ideas are welcome and appreciated.
Result set when I run this query on postgres terminal look like this :
p_cr_personstatus
p_cr_identification
p_cr_phone
p_cr_count
(4 rows)
I am trying to implement this using spring JPA only.

SQL aliases not working anymore after migrating from Hibernate 3 to 4

I am migrating a Struts2 Web Application from Hibernate V3 to Hibernate 4.3.5.
In Hibernate V3 we used SQL statements like the following, and they worked well :
select u.id as id,
u.userId as userId,
sum(u.totalSearchedFields) as total,
u.date as date,
u.status as status
from user u;
After migrating to Hibernate V4.3.5, the above SQL statement shows the error:
org.hibernate.QueryException: , expected in SELECT
We made it work by removing the aliases from the SQL statement, like this:
select u.id,
u.userId,
sum(u.totalSearchedFields),
u.date,
u.status
from user u;
Does anyone recognize the cause for this? How to fix this problem without removing the aliases?
Appearently, according to the Hibernate 4 developer guide, the aliases should be used in an "inversed" way from what you did before:
SELECT <PROPERTY_NAME> AS <ALIAS_TABLE.PROPERTY_NAME>
FROM TABLE AS <ALIAS_TABLE>
So try changing this:
select u.id as id,
u.userId as userId,
sum(u.totalSearchedFields) as total,
u.date as date,
u.status as status
from user u;
to this:
select id as {u.id},
userId as {u.userId},
sum(totalSearchedFields) as total,
date as {u.date},
status as {u.status}
from user u;
The approach seems a bit different from the SQL's one, because this aliases are intended to be used by Hibernate to prevent multiple columns with the same name to conflict, while in SQL they were used to generate an user-friendly ResultSet.
A long shot BTW, hope that helps.

How to execute a "SET ROLE XXXX..." with Hibernate

I'm developing a web app (java) and I'm using Hibernate 4.1.4 and oracle 11g. By security's rules, the dbuser (the db user that app uses) doesnt have insert privileges. Then every time my app is gonna do a insert, it must execute the query "SET ROLE XXXX ...." before the insertion-query be executed. This "set role.." will give to dbuser the privileges to insert into some tables int that transaction. How to do it with Hibernate? I don't wanna put it in a default conf because I should execute the above query only in insertions (not to selects...). I tried do it on my save method:
public Object saveOrUpdate(Object entity) {
Query query = getSessionFactory().getCurrentSession().createSQLQuery("set role XXXX identified by XXXX");
query.executeUpdate();
getSessionFactory().getCurrentSession().saveOrUpdate(entity);
return entity;
}
but it doesnt work. I get the error "view or table not exist". When I try execute only the query.executeUpdate() or the saveOrUpdate(entity) no error happened.
I guess it is all. If you need more details, please ask to me. Thanks!

Using jdbcTemplate, what is a simple query I can do to test for connectivity?

I'm creating a simple health check page that ensures my application can connect to mysql.
I'm using jdbctemplate, what query do you suggest I perform that will not throw an error if the table is empty or anything, just something that ensures my spring app can connect to the database.
You could try select 1 from my_table
You can execute this below query.
SELECT COUNT(*) col FROM dual WHERE 1=0

Categories