Skip to content

Commit 4b085be

Browse files
committed
Keep username and datname empty if we cannot get real values
If transaction state is not 'in progress' than we cannot do database access. For that case we should keep username and datname just empty instad of filling them with error message. Also we can improve collecting these fields by grabbing them early in utillity hook, when transaction state wasn't yet changed by actual hook processing.
1 parent b6ffd37 commit 4b085be

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

pg_stat_monitor.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,6 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10181018
!IsA(parsetree, PrepareStmt) &&
10191019
!IsA(parsetree, DeallocateStmt))
10201020
{
1021-
pgsmEntry *entry;
10221021
char *query_text;
10231022
int location;
10241023
int query_len;
@@ -1030,6 +1029,7 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10301029
BufferUsage bufusage_start = pgBufferUsage;
10311030
WalUsage walusage;
10321031
WalUsage walusage_start = pgWalUsage;
1032+
pgsmEntry *entry = pgsm_create_hash_entry(0, queryId, NULL);;
10331033

10341034
if (getrusage(RUSAGE_SELF, &rusage_start) != 0)
10351035
elog(DEBUG1, "[pg_stat_monitor] pgsm_ProcessUtility: Failed to execute getrusage.");
@@ -1090,9 +1090,6 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10901090
memset(&bufusage, 0, sizeof(BufferUsage));
10911091
BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
10921092

1093-
/* Create an entry for this query */
1094-
entry = pgsm_create_hash_entry(0, queryId, NULL);
1095-
10961093
location = pstmt->stmt_location;
10971094
query_len = pstmt->stmt_len;
10981095
query_text = (char *) CleanQuerytext(queryString, &location, &query_len);
@@ -1743,19 +1740,16 @@ pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info)
17431740
{
17441741
datname = get_database_name(entry->key.dbid);
17451742
username = GetUserNameFromId(entry->key.userid, true);
1746-
}
17471743

1748-
if (!datname)
1749-
datname = pnstrdup("<database name not available>", sizeof(entry->datname) - 1);
1744+
snprintf(entry->datname, sizeof(entry->datname), "%s", datname);
1745+
snprintf(entry->username, sizeof(entry->username), "%s", username);
17501746

1751-
if (!username)
1752-
username = pnstrdup("<user name not available>", sizeof(entry->username) - 1);
1747+
if (datname)
1748+
pfree(datname);
17531749

1754-
snprintf(entry->datname, sizeof(entry->datname), "%s", datname);
1755-
snprintf(entry->username, sizeof(entry->username), "%s", username);
1756-
1757-
pfree(datname);
1758-
pfree(username);
1750+
if (username)
1751+
pfree(username);
1752+
}
17591753

17601754
MemoryContextSwitchTo(oldctx);
17611755

0 commit comments

Comments
 (0)