Merge pull request #123 from darkfronza/PG-262_fix_comments_extraction

PG-262: Fix comments extraction
pull/125/head
Ibrar Ahmed 2021-10-18 22:13:17 +05:00 committed by GitHub
commit e2679e42aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 8 deletions

View File

@ -251,7 +251,7 @@ _PG_init(void)
/* /*
* Compile regular expression for extracting out query comments only once. * 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) if (rc != 0)
{ {
elog(ERROR, "pg_stat_monitor: query comments regcomp() failed, return code=(%d)\n", rc); elog(ERROR, "pg_stat_monitor: query comments regcomp() failed, return code=(%d)\n", rc);
@ -3461,12 +3461,37 @@ extract_query_comments(const char *query, char *comments, size_t max_len)
int rc; int rc;
size_t nmatch = 1; size_t nmatch = 1;
regmatch_t pmatch; regmatch_t pmatch;
regoff_t comment_len, total_len = 0;
const char *s = query;
rc = regexec(&preg_query_comments, query, nmatch, &pmatch, 0); while (total_len < max_len)
if (rc != 0) {
return; rc = regexec(&preg_query_comments, s, nmatch, &pmatch, 0);
if (rc != 0)
break;
snprintf(comments, max_len, "%.*s", pmatch.rm_eo - pmatch.rm_so -4, &query[pmatch.rm_so + 2]); 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. */
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 #if PG_VERSION_NUM < 140000

View File

@ -12,9 +12,9 @@ SELECT 1 AS num /* { "application", psql_app, "real_ip", 192.168.1.3) */;
(1 row) (1 row)
SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C"; SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | comments 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 $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 pg_stat_monitor_reset(); |
SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C"; | SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C"; |
(3 rows) (3 rows)