Skip to content

Commit 0b493f8

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 0b493f8

3 files changed

Lines changed: 13 additions & 19 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

regression/expected/application_name_unique.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ SELECT query,application_name FROM pg_stat_monitor ORDER BY query, application_n
2525
SELECT 1 AS num | naeem
2626
SELECT 1 AS num | psql
2727
SELECT pg_stat_monitor_reset() | pg_regress/application_name_unique
28-
SET application_name = 'naeem' | naeem
29-
SET application_name = 'psql' | psql
28+
SET application_name = 'naeem' | pg_regress/application_name_unique
29+
SET application_name = 'psql' | naeem
3030
(5 rows)
3131

3232
SELECT pg_stat_monitor_reset();

regression/expected/user.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ SELECT username, query FROM pg_stat_monitor ORDER BY username, query COLLATE "C"
3939
su | DROP OWNED BY u2
4040
su | DROP USER u2
4141
su | SELECT pg_stat_monitor_reset()
42-
su | SET ROLE su
42+
su | SET ROLE u1
4343
u1 | CREATE TABLE t1 (a int)
4444
u1 | SELECT * FROM t1
4545
u1 | SELECT pg_stat_monitor_reset();
46-
u1 | SET ROLE u1
46+
u1 | SET ROLE u2
4747
u2 | CREATE TABLE t2 (a int)
4848
u2 | DROP TABLE t2
4949
u2 | SELECT * FROM t2
50-
u2 | SET ROLE u2
50+
u2 | SET ROLE su
5151
(12 rows)
5252

5353
SELECT pg_stat_monitor_reset();

0 commit comments

Comments
 (0)