Skip to content

Commit

Permalink
[pgexporter#64] Retrieving Server's Information
Browse files Browse the repository at this point in the history
  • Loading branch information
resyfer committed May 13, 2023
1 parent 43233f0 commit ce46653
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/include/internal.h
Expand Up @@ -264,6 +264,11 @@ extern "C" {
" type: counter\n" \
" description: pg_stat_database_conflicts_confl_deadlock"

/**
* SQL Query for retrieving floor of server's PostgreSQL version.
*/
#define VERSION_SQL "SELECT split_part(split_part(version(), ' ', 2), '.', 1);"

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/include/pgexporter.h
Expand Up @@ -100,6 +100,8 @@ extern "C" {
#define SERVER_QUERY_PRIMARY 1
#define SERVER_QUERY_REPLICA 2

#define SERVER_UNDERTERMINED_VERSION 0

#define START_STATUS 0
#define SEQUENCE_STATUS 1
#define BLOCK_STATUS 2
Expand Down Expand Up @@ -183,6 +185,8 @@ extern void* shmem;
*/
extern void* prometheus_cache_shmem;

typedef unsigned int version_t;

/** @struct
* Defines a server
*/
Expand All @@ -198,6 +202,7 @@ struct server
bool new; /**< Is the connection new */
bool extension; /**< Is the pgexporter_ext extension installed */
int state; /**< The state of the server */
version_t version;
} __attribute__ ((aligned (64)));

/** @struct
Expand Down
10 changes: 10 additions & 0 deletions src/include/queries.h
Expand Up @@ -86,6 +86,16 @@ pgexporter_query_get_functions(int server, struct query** query);
int
pgexporter_query_execute(int server, char* sql, char* tag, struct query** query);

/**
* Extracts the server's version as a `version_t` (aka unsigned int), and sets
* the value of `config->servers[server].version` to it.
*
* @param server The server index in config
* @return 0 upon success, otherwise 1
*/
int
pgexporter_server_version(int server);

/**
* Query for used disk space
* @param server The server
Expand Down
1 change: 1 addition & 0 deletions src/libpgexporter/configuration.c
Expand Up @@ -728,6 +728,7 @@ pgexporter_read_configuration(void* shm, char* filename)

if (strlen(srv.name) > 0)
{
srv.version = SERVER_UNDERTERMINED_VERSION;
memcpy(&(config->servers[idx_server - 1]), &srv, sizeof(struct server));
}

Expand Down
27 changes: 27 additions & 0 deletions src/libpgexporter/queries.c
Expand Up @@ -28,6 +28,7 @@

/* pgexporter */
#include <pgexporter.h>
#include <internal.h>
#include <logging.h>
#include <management.h>
#include <message.h>
Expand Down Expand Up @@ -86,6 +87,7 @@ pgexporter_open_connections(void)
{
config->servers[server].new = true;
pgexporter_server_info(server);
pgexporter_server_version(server);
}
else
{
Expand Down Expand Up @@ -140,6 +142,31 @@ pgexporter_close_connections(void)
}
}

int
pgexporter_server_version(int server)
{
struct query* q;
int ret;
struct configuration* config;

config = (struct configuration*)shmem;

ret = query_execute(server, VERSION_SQL, "version", 1, NULL, &q);

if (q)
{
struct tuple* t = q->tuples;
if (t && t->data[0])
{
config->servers[server].version = strtoul(t->data[0], NULL, 10);
}
}

pgexporter_free_query(q);

return ret;
}

int
pgexporter_query_get_functions(int server, struct query** query)
{
Expand Down

0 comments on commit ce46653

Please sign in to comment.