Skip to content

Commit

Permalink
Merge pull request #123 from darkfronza/PG-262_fix_comments_extraction
Browse files Browse the repository at this point in the history
PG-262: Fix comments extraction
  • Loading branch information
ibrarahmad authored Oct 18, 2021
2 parents 350ef72 + c390f24 commit e2679e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
35 changes: 30 additions & 5 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ _PG_init(void)
/*
* Compile regular expression for extracting out query comments only once.
*/
rc = regcomp(&preg_query_comments, "/\\*.*\\*/", 0);
rc = regcomp(&preg_query_comments, "/\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/", REG_EXTENDED);
if (rc != 0)
{
elog(ERROR, "pg_stat_monitor: query comments regcomp() failed, return code=(%d)\n", rc);
Expand Down Expand Up @@ -3461,12 +3461,37 @@ extract_query_comments(const char *query, char *comments, size_t max_len)
int rc;
size_t nmatch = 1;
regmatch_t pmatch;
regoff_t comment_len, total_len = 0;
const char *s = query;

rc = regexec(&preg_query_comments, query, nmatch, &pmatch, 0);
if (rc != 0)
return;
while (total_len < max_len)
{
rc = regexec(&preg_query_comments, s, nmatch, &pmatch, 0);
if (rc != 0)
break;

comment_len = pmatch.rm_eo - pmatch.rm_so;

if (total_len + comment_len > max_len)
break; /* TODO: log error in error view, insufficient space for comment. */

snprintf(comments, max_len, "%.*s", pmatch.rm_eo - pmatch.rm_so -4, &query[pmatch.rm_so + 2]);
total_len += comment_len;

/* Not 1st iteration, append ", " before next comment. */
if (s != query)
{
if (total_len + 2 > max_len)
break; /* TODO: log error in error view, insufficient space for ", " + comment. */

memcpy(comments, ", ", 2);
comments += 2;
total_len += 2;
}

memcpy(comments, s + pmatch.rm_so, comment_len);
comments += comment_len;
s += pmatch.rm_eo;
}
}

#if PG_VERSION_NUM < 140000
Expand Down
6 changes: 3 additions & 3 deletions regression/expected/tags.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ SELECT 1 AS num /* { "application", psql_app, "real_ip", 192.168.1.3) */;
(1 row)

SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | comments
---------------------------------------------------------------------------+------------------------------------------------------
SELECT $1 AS num /* { "application", psql_app, "real_ip", 192.168.1.3) */ | { "application", psql_app, "real_ip", 192.168.1.3)
query | comments
---------------------------------------------------------------------------+----------------------------------------------------------
SELECT $1 AS num /* { "application", psql_app, "real_ip", 192.168.1.3) */ | /* { "application", psql_app, "real_ip", 192.168.1.3) */
SELECT pg_stat_monitor_reset(); |
SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C"; |
(3 rows)
Expand Down

0 comments on commit e2679e4

Please sign in to comment.