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 16, 2023
1 parent 43233f0 commit 197e314
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/include/pgexporter.h
Original file line number Diff line number Diff line change
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 @@ -198,6 +200,7 @@ struct server
bool new; /**< Is the connection new */
bool extension; /**< Is the pgexporter_ext extension installed */
int state; /**< The state of the server */
char version; /**< The PostgreSQL version (char for minimum bytes)*/
} __attribute__ ((aligned (64)));

/** @struct
Expand Down
11 changes: 11 additions & 0 deletions src/include/queries.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ 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 char (minimum integral data type), 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
Original file line number Diff line number Diff line change
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
26 changes: 26 additions & 0 deletions src/libpgexporter/queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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 +141,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, "SELECT split_part(split_part(version(), ' ', 2), '.', 1);", "version", 1, NULL, &q);

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

pgexporter_free_query(q);

return ret;
}

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

0 comments on commit 197e314

Please sign in to comment.