简体   繁体   中英

Using JPA to query postgres replication status

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM