Skip to content

Commit

Permalink
PG-1383 Record fait PG version in telemetry file
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Feb 24, 2025
1 parent ad69dd0 commit 03af9c2
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions percona_pg_telemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,60 @@ write_database_info(PTDatabaseInfo *dbinfo, List *extlist)
return true;
}


/*
* Function to write the pg_version to the telemetry file.
*/
static void
write_pg_version()
{
int spi_result;
char *query = "select setting from pg_settings where name='server_version'";
char buf[1024] = {0};
size_t buf_size = sizeof(buf);
FILE *fp;

/* Open file in append mode. */
fp = open_telemetry_file(ptss->dbtemp_filepath, "a+");

SetCurrentStatementStartTimestamp();
StartTransactionCommand();

/* Initialize SPI */
spi_result = SPI_connect();
if (spi_result != SPI_OK_CONNECT)
{
ereport(ERROR, (errmsg("Failed to connect to SPI")));
}

PushActiveSnapshot(GetTransactionSnapshot());

/* Execute the query */
spi_result = SPI_execute(query, true, 0);
if (spi_result != SPI_OK_SELECT)
{
SPI_finish();
ereport(ERROR, (errmsg("Failed to execute query")));
}

if (SPI_processed > 0)
{
char *version = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);

construct_json_block(buf, buf_size, "pillar_version", version, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
write_telemetry_file(fp, buf);
}

/* Close the file */
fclose(fp);

/* Disconnect from SPI */
SPI_finish();

PopActiveSnapshot();
CommitTransactionCommand();
}

/*
* Main function for the background launcher process
*/
Expand Down Expand Up @@ -949,10 +1003,6 @@ percona_pg_telemetry_main(Datum main_arg)
construct_json_block(buf, buf_size, "db_instance_id", str, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
write_telemetry_file(fp, buf);

/* Construct and initiate the active extensions array block. */
construct_json_block(buf, buf_size, "pillar_version", PG_VERSION, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
write_telemetry_file(fp, buf);

/* Construct and initiate the active extensions array block. */
pg_snprintf(str, sizeof(str), "%ld", server_uptime());
construct_json_block(buf, buf_size, "uptime", str, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
Expand Down Expand Up @@ -1088,7 +1138,10 @@ percona_pg_telemetry_worker(Datum main_arg)

/* Get the settings */
if (ptss->first_db_entry)
{
write_pg_version();
write_pg_settings();
}

extlist = get_extensions_list(&ptss->dbinfo, tmpcxt);

Expand Down

0 comments on commit 03af9c2

Please sign in to comment.