Skip to content

Commit

Permalink
Reduce variables scope
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Mar 5, 2025
1 parent b6e30ae commit 0f3204c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
28 changes: 13 additions & 15 deletions percona_pg_telemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,14 +582,11 @@ server_uptime(void)
static void
write_pg_settings(void)
{
SPITupleTable *tuptable;
int spi_result;
char *query = "SELECT name, unit, setting FROM pg_settings where vartype != 'string'";
char buf[4096] = {0};
size_t buf_size = sizeof(buf);
FILE *fp;
int flags;


/* Open file in append mode. */
fp = open_telemetry_file(ptss->dbtemp_filepath, "a+");
Expand Down Expand Up @@ -617,7 +614,8 @@ write_pg_settings(void)
/* Process the result */
if (SPI_processed > 0)
{
tuptable = SPI_tuptable;
int flags;
SPITupleTable *tuptable = SPI_tuptable;

for (int row_count = 0; row_count < SPI_processed; row_count++)
{
Expand Down Expand Up @@ -675,7 +673,6 @@ get_database_list(void)
Relation rel;
TableScanDesc scan;
HeapTuple tup;
MemoryContext oldcxt;
ScanKeyData key;

/* Start a transaction to access pg_database */
Expand All @@ -694,6 +691,7 @@ get_database_list(void)

while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
MemoryContext oldcxt;
PTDatabaseInfo *dbinfo;
int64 datsize;
Form_pg_database pgdatabase = (Form_pg_database) GETSTRUCT(tup);
Expand Down Expand Up @@ -736,7 +734,6 @@ get_extensions_list(PTDatabaseInfo *dbinfo, MemoryContext cxt)
Relation rel;
TableScanDesc scan;
HeapTuple tup;
MemoryContext oldcxt;

Assert(dbinfo);

Expand All @@ -749,6 +746,7 @@ get_extensions_list(PTDatabaseInfo *dbinfo, MemoryContext cxt)

while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
MemoryContext oldcxt;
PTExtensionInfo *extinfo;
Form_pg_extension extform = (Form_pg_extension) GETSTRUCT(tup);

Expand Down Expand Up @@ -854,12 +852,6 @@ void
percona_pg_telemetry_main(Datum main_arg)
{
int rc = 0;
List *dblist = NIL;
ListCell *lc = NULL;
FILE *fp;
char str[2048] = {0};
char buf[4096] = {0};
size_t buf_size = sizeof(buf);
bool first_time = true;

/* Setup signal callbacks */
Expand Down Expand Up @@ -891,6 +883,11 @@ percona_pg_telemetry_main(Datum main_arg)
/* Should never really terminate unless... */
while (!sigterm_recvd && ptss->error_code == PT_SUCCESS)
{
FILE *fp;
char buf[4096] = {0};
size_t buf_size = sizeof(buf);
List *dblist = NIL;

/* Don't sleep the first time */
if (first_time == false)
{
Expand Down Expand Up @@ -924,7 +921,7 @@ percona_pg_telemetry_main(Datum main_arg)
*/
if (dblist == NIL && (rc & WL_TIMEOUT || first_time))
{
char temp_buff[100];
char str[2048] = {0};

/* Data collection will start now */
first_time = false;
Expand Down Expand Up @@ -956,8 +953,8 @@ percona_pg_telemetry_main(Datum main_arg)
write_telemetry_file(fp, buf);

/* Construct and initiate the active extensions array block. */
pg_snprintf(temp_buff, sizeof(temp_buff), "%d", list_length(dblist));
construct_json_block(buf, buf_size, "databases_count", temp_buff, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
pg_snprintf(str, sizeof(str), "%d", list_length(dblist));
construct_json_block(buf, buf_size, "databases_count", str, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
write_telemetry_file(fp, buf);

/* Let's close the file now so that processes may add their stuff. */
Expand All @@ -969,6 +966,7 @@ percona_pg_telemetry_main(Datum main_arg)
{
PTDatabaseInfo *dbinfo;
BgwHandleStatus status;
ListCell *lc = NULL;

/* First or the next cell */
#if PG_VERSION_NUM >= 130000
Expand Down
3 changes: 1 addition & 2 deletions pt_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static char *json_escape_str(char *str);
char *
json_escape_str(char *str)
{
int i;
int len;
int maxlen;
char *str_escaped;
Expand All @@ -40,7 +39,7 @@ json_escape_str(char *str)
str_escaped = (char *) palloc(maxlen);
s = str_escaped;

for (i = 0; i < len; i++)
for (int i = 0; i < len; i++)
{
/* Escape the quote and backslash characters. */
if (str[i] == '"' || str[i] == '\\')
Expand Down

0 comments on commit 0f3204c

Please sign in to comment.