diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 815c721..948a78e 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -59,7 +59,9 @@ void _PG_fini(void); /* Current nesting depth of ExecutorRun+ProcessUtility calls */ static int nested_level = 0; +#if PG_VERSION_NUM >= 130000 static int plan_nested_level = 0; +#endif /* The array to store outer layer query id*/ uint64 *nested_queryids; @@ -76,10 +78,12 @@ static bool IsSystemInitialized(void); static void dump_queries_buffer(int bucket_id, unsigned char *buf, int buf_len); static double time_diff(struct timeval end, struct timeval start); -static PlannedStmt * pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams); /* Saved hook values in case of unload */ + +#if PG_VERSION_NUM >= 130000 static planner_hook_type planner_hook_next = NULL; +#endif static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL; static ExecutorStart_hook_type prev_ExecutorStart = NULL; static ExecutorRun_hook_type prev_ExecutorRun = NULL; @@ -104,11 +108,6 @@ static int pg_get_application_name(char* application_name); static PgBackendStatus *pg_get_backend_status(void); static Datum intarray_get_datum(int32 arr[], int len); -#if PG_VERSION_NUM < 130000 -static void BufferUsageAccumDiff(BufferUsage* bufusage, BufferUsage* pgBufferUsage, BufferUsage* bufusage_start); -static PlannedStmt *pgss_planner_hook(Query *parse, int opt, ParamListInfo param); -#endif - static void pgss_post_parse_analyze(ParseState *pstate, Query *query); static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags); static void pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once); @@ -117,6 +116,7 @@ static void pgss_ExecutorEnd(QueryDesc *queryDesc); static bool pgss_ExecutorCheckPerms(List *rt, bool abort); #if PG_VERSION_NUM >= 130000 +static PlannedStmt * pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams); static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, @@ -124,6 +124,7 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, QueryCompletion *qc ); #else +static void BufferUsageAccumDiff(BufferUsage* bufusage, BufferUsage* pgBufferUsage, BufferUsage* bufusage_start); static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, @@ -250,8 +251,10 @@ _PG_init(void) ExecutorEnd_hook = pgss_ExecutorEnd; prev_ProcessUtility = ProcessUtility_hook; ProcessUtility_hook = pgss_ProcessUtility; +#if PG_VERSION_NUM >= 130000 planner_hook_next = planner_hook; planner_hook = pgss_planner_hook; +#endif emit_log_hook = pgsm_emit_log_hook; prev_ExecutorCheckPerms_hook = ExecutorCheckPerms_hook; ExecutorCheckPerms_hook = pgss_ExecutorCheckPerms; @@ -510,7 +513,11 @@ pgss_ExecutorEnd(QueryDesc *queryDesc) queryDesc->totaltime->total * 1000.0, /* totaltime */ queryDesc->estate->es_processed, /* rows */ &queryDesc->totaltime->bufusage, /* bufusage */ +#if PG_VERSION_NUM >= 130000 &queryDesc->totaltime->walusage, /* walusage */ +#else + NULL, +#endif NULL, PGSS_EXEC); /* pgssStoreKind */ } @@ -571,7 +578,8 @@ pgss_ExecutorCheckPerms(List *rt, bool abort) return true; } -static PlannedStmt * +#if PG_VERSION_NUM >= 130000 +static PlannedStmt* pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams) { PlannedStmt *result; @@ -641,6 +649,7 @@ pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, Par } return result; } +#endif /* * ProcessUtility hook @@ -683,13 +692,12 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, instr_time start; instr_time duration; uint64 rows; - BufferUsage bufusage_start; BufferUsage bufusage; - WalUsage walusage_start; WalUsage walusage; - - bufusage_start = pgBufferUsage; - walusage_start = pgWalUsage; + BufferUsage bufusage_start = pgBufferUsage; +#if PG_VERSION_NUM >= 130000 + WalUsage walusage_start = pgWalUsage; +#endif INSTR_TIME_SET_CURRENT(start); PG_TRY(); { diff --git a/pg_stat_monitor.h b/pg_stat_monitor.h index 1760dae..c6b9762 100644 --- a/pg_stat_monitor.h +++ b/pg_stat_monitor.h @@ -112,6 +112,15 @@ typedef struct GucVariables bool guc_restart; } GucVariable; +#if PG_VERSION_NUM < 130000 +typedef struct WalUsage +{ + long wal_records; /* # of WAL records produced */ + long wal_fpi; /* # of WAL full page images produced */ + uint64 wal_bytes; /* size of WAL records produced */ +} WalUsage; +#endif + typedef enum OVERFLOW_TARGET { OVERFLOW_TARGET_NONE = 0, diff --git a/regression/expected/guc_1.out b/regression/expected/guc_1.out index c840a74..6abf062 100644 --- a/regression/expected/guc_1.out +++ b/regression/expected/guc_1.out @@ -16,16 +16,16 @@ SELECT * FROM pg_stat_monitor_settings ORDER BY name COLLATE "C"; ------------------------------------------+--------+---------------+----------------------------------------------------------------------------------------------------------+---------+------------+--------- pg_stat_monitor.pgsm_bucket_time | 300 | 300 | Sets the time in seconds per bucket. | 1 | 2147483647 | 1 pg_stat_monitor.pgsm_enable | 1 | 1 | Enable/Disable statistics collector. | 0 | 0 | 0 + pg_stat_monitor.pgsm_enable_query_plan | 0 | 0 | Enable/Disable query plan monitoring | 0 | 0 | 0 pg_stat_monitor.pgsm_histogram_buckets | 10 | 10 | Sets the maximum number of histogram buckets | 2 | 2147483647 | 1 pg_stat_monitor.pgsm_histogram_max | 100000 | 100000 | Sets the time in millisecond. | 10 | 2147483647 | 1 pg_stat_monitor.pgsm_histogram_min | 0 | 0 | Sets the time in millisecond. | 0 | 2147483647 | 1 pg_stat_monitor.pgsm_max | 100 | 100 | Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor. | 1 | 1000 | 1 pg_stat_monitor.pgsm_max_buckets | 10 | 10 | Sets the maximum number of buckets. | 1 | 10 | 1 pg_stat_monitor.pgsm_normalized_query | 1 | 1 | Selects whether save query in normalized format. | 0 | 0 | 0 - pg_stat_monitor.pgsm_overflow_target | 1 | 1 | Sets the overflow target for pg_stat_monitor | 0 | 1 | 1 + pg_stat_monitor.pgsm_overflow_target | 0 | 1 | Sets the overflow target for pg_stat_monitor | 0 | 1 | 1 pg_stat_monitor.pgsm_query_max_len | 1024 | 1024 | Sets the maximum length of query. | 1024 | 2147483647 | 1 pg_stat_monitor.pgsm_query_shared_buffer | 20 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | 1 - pg_stat_monitor.pgsm_track_planning | 0 | 1 | Selects whether planning statistics are tracked. | 0 | 0 | 0 pg_stat_monitor.pgsm_track_utility | 1 | 1 | Selects whether utility commands are tracked. | 0 | 0 | 0 (13 rows) diff --git a/regression/expected/relations_1.out b/regression/expected/relations_1.out deleted file mode 100644 index 63ea0dd..0000000 --- a/regression/expected/relations_1.out +++ /dev/null @@ -1,41 +0,0 @@ -CREATE EXTENSION pg_stat_monitor; -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - -CREATE TABLE foo1(a int); -CREATE TABLE foo2(a int); -CREATE TABLE foo3(a int); -CREATE TABLE foo4(a int); -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - -SELECT * FROM foo1, foo2, foo3, foo4; - a | a | a | a ----+---+---+--- -(0 rows) - -SELECT query, relations from pg_stat_monitor ORDER BY query; - query | relations --------------------------------------------------------------+--------------------------------------------------- - SELECT * FROM foo1, foo2, foo3, foo4 | {public.foo1,public.foo2,public.foo3,public.foo4} - SELECT pg_stat_monitor_reset() | - SELECT query, relations from pg_stat_monitor ORDER BY query | -(3 rows) - -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - -DROP TABLE foo1; -DROP TABLE foo2; -DROP TABLE foo3; -DROP TABLE foo4; -DROP EXTENSION pg_stat_monitor;