Skip to content

Commit cc2dd60

Browse files
committed
Rewrite decode_error_level() and get_cmd_type() in C
While rewriting these functions in C means more lines of code it is a net improvement since we can use the defines in the C code instead of magic numbers plus that we can simply add an ifdef when a new value is added instead of having to update the SQL function and call current_setting(). The way we handle the zero values is legacy to not break any old code.
1 parent 081658e commit cc2dd60

2 files changed

Lines changed: 96 additions & 32 deletions

File tree

pg_stat_monitor--2.3--next.sql

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
11
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
22
\echo Use "ALTER EXTENSION pg_stat_monitor" to load this file. \quit
33

4-
-- Drop PostgreSQL 13 support from function
54
CREATE OR REPLACE FUNCTION decode_error_level(elevel int)
65
RETURNS text
76
PARALLEL SAFE
8-
LANGUAGE sql
9-
RETURN CASE
10-
WHEN elevel = 0 THEN ''
11-
WHEN elevel = 10 THEN 'DEBUG5'
12-
WHEN elevel = 11 THEN 'DEBUG4'
13-
WHEN elevel = 12 THEN 'DEBUG3'
14-
WHEN elevel = 13 THEN 'DEBUG2'
15-
WHEN elevel = 14 THEN 'DEBUG1'
16-
WHEN elevel = 15 THEN 'LOG'
17-
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
18-
WHEN elevel = 17 THEN 'INFO'
19-
WHEN elevel = 18 THEN 'NOTICE'
20-
WHEN elevel = 19 THEN 'WARNING'
21-
WHEN elevel = 20 THEN 'WARNING_CLIENT_ONLY'
22-
WHEN elevel = 21 THEN 'ERROR'
23-
WHEN elevel = 22 THEN 'FATAL'
24-
WHEN elevel = 23 THEN 'PANIC'
25-
END;
7+
LANGUAGE c
8+
AS 'MODULE_PATHNAME', 'pg_stat_monitor_decode_error_level';
269

2710
CREATE OR REPLACE FUNCTION get_cmd_type(cmd_type int)
2811
RETURNS text
2912
PARALLEL SAFE
30-
LANGUAGE sql
31-
RETURN CASE
32-
WHEN cmd_type = 0 THEN ''
33-
WHEN cmd_type = 1 THEN 'SELECT'
34-
WHEN cmd_type = 2 THEN 'UPDATE'
35-
WHEN cmd_type = 3 THEN 'INSERT'
36-
WHEN cmd_type = 4 THEN 'DELETE'
37-
WHEN cmd_type = 5 AND current_setting('server_version_num')::int >= 150000 THEN 'MERGE'
38-
WHEN cmd_type = 5 AND current_setting('server_version_num')::int < 150000 THEN 'UTILITY'
39-
WHEN cmd_type = 6 AND current_setting('server_version_num')::int >= 150000 THEN 'UTILITY'
40-
WHEN cmd_type = 6 AND current_setting('server_version_num')::int < 150000 THEN 'NOTHING'
41-
WHEN cmd_type = 7 THEN 'NOTHING'
42-
END;
13+
LANGUAGE c
14+
AS 'MODULE_PATHNAME', 'pg_stat_monitor_get_cmd_type';
4315

4416
CREATE OR REPLACE FUNCTION range()
4517
RETURNS text[]

src/pg_stat_monitor.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ PG_FUNCTION_INFO_V1(pg_stat_monitor_2_3);
198198
PG_FUNCTION_INFO_V1(pg_stat_monitor);
199199
PG_FUNCTION_INFO_V1(get_histogram_timings);
200200
PG_FUNCTION_INFO_V1(pg_stat_monitor_hook_stats);
201+
PG_FUNCTION_INFO_V1(pg_stat_monitor_decode_error_level);
202+
PG_FUNCTION_INFO_V1(pg_stat_monitor_get_cmd_type);
201203

202204
static uint pg_get_client_addr(void);
203205
static int pg_get_application_name(char *name, int buff_size);
@@ -2432,6 +2434,96 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
24322434
pgsm_lock_release(pgsm);
24332435
}
24342436

2437+
static const char *
2438+
decode_error_level(int elevel)
2439+
{
2440+
switch (elevel)
2441+
{
2442+
case 0: /* compatibility with legacy code */
2443+
return "";
2444+
case DEBUG5:
2445+
return "DEBUG5";
2446+
case DEBUG4:
2447+
return "DEBUG4";
2448+
case DEBUG3:
2449+
return "DEBUG3";
2450+
case DEBUG2:
2451+
return "DEBUG2";
2452+
case DEBUG1:
2453+
return "DEBUG1";
2454+
case LOG:
2455+
return "LOG";
2456+
case LOG_SERVER_ONLY:
2457+
return "LOG_SERVER_ONLY";
2458+
case INFO:
2459+
return "INFO";
2460+
case NOTICE:
2461+
return "NOTICE";
2462+
case WARNING:
2463+
return "WARNING";
2464+
case WARNING_CLIENT_ONLY:
2465+
return "WARNING_CLIENT_ONLY";
2466+
case ERROR:
2467+
return "ERROR";
2468+
case FATAL:
2469+
return "FATAL";
2470+
case PANIC:
2471+
return "PANIC";
2472+
default:
2473+
return NULL;
2474+
}
2475+
}
2476+
2477+
Datum
2478+
pg_stat_monitor_decode_error_level(PG_FUNCTION_ARGS)
2479+
{
2480+
const char *severity = decode_error_level(PG_GETARG_INT32(0));
2481+
2482+
if (severity == NULL)
2483+
PG_RETURN_NULL();
2484+
2485+
PG_RETURN_TEXT_P(cstring_to_text(severity));
2486+
}
2487+
2488+
static const char *
2489+
get_cmd_type(int cmd_type)
2490+
{
2491+
switch (cmd_type)
2492+
{
2493+
case CMD_UNKNOWN:
2494+
return ""; /* compatibility with legacy code */
2495+
case CMD_SELECT:
2496+
return "SELECT";
2497+
case CMD_UPDATE:
2498+
return "UPDATE";
2499+
case CMD_INSERT:
2500+
return "INSERT";
2501+
case CMD_DELETE:
2502+
return "DELETE";
2503+
#if PG_VERSION_NUM >= 150000
2504+
case CMD_MERGE:
2505+
return "MERGE";
2506+
#endif
2507+
case CMD_UTILITY:
2508+
return "UTILITY";
2509+
case CMD_NOTHING:
2510+
return "NOTHING";
2511+
default:
2512+
return NULL;
2513+
}
2514+
}
2515+
2516+
Datum
2517+
pg_stat_monitor_get_cmd_type(PG_FUNCTION_ARGS)
2518+
{
2519+
const char *cmd_string = get_cmd_type(PG_GETARG_INT32(0));
2520+
2521+
if (cmd_string == NULL)
2522+
PG_RETURN_NULL();
2523+
2524+
PG_RETURN_TEXT_P(cstring_to_text(cmd_string));
2525+
}
2526+
24352527
static uint64
24362528
get_next_wbucket(pgsmSharedState *pgsm)
24372529
{

0 commit comments

Comments
 (0)