
# time_monitor extensioncomment = 'find out the sql that is exceeding expectations' #描述扩展的功能default_version = '1.0' #扩展的默认版本号module_pathname = '$libdir/time_monitor' #指定扩展模块文件的路径relocatable = false #扩展是否可以被重新定位。如果设置为 true,则表示扩展可以在安装后移动到其他位置,而不会影响其功能。
\echo Use "CREATE EXTENSION time_monitor" to load this file.\quit
#include "postgres.h"#include "fmgr.h"#include "executor/executor.h"#include <time.h>#include <stdio.h>PG_MODULE_MAGIC;static ExecutorStart_hook_type prev_ExecutorStart = NULL;static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;static long long start_time = 0;static long long end_time = 0;static long long total_time = 0;void _PG_fini(void);void _PG_init(void);static void check_ExecutorStart(QueryDesc *queryDesc, int eflags);static void check_ExecutorEnd(QueryDesc *queryDesc);void _PG_init(void){prev_ExecutorStart = ExecutorStart_hook;ExecutorStart_hook = check_ExecutorStart;prev_ExecutorEnd = ExecutorEnd_hook;ExecutorEnd_hook = check_ExecutorEnd;}static void check_ExecutorStart(QueryDesc *queryDesc, int eflags){start_time = time(NULL);if (prev_ExecutorStart)prev_ExecutorStart(queryDesc, eflags);elsestandard_ExecutorStart(queryDesc, eflags);}static void check_ExecutorEnd(QueryDesc *queryDesc){end_time = time(NULL);total_time = end_time - start_time;if( total_time >= 0){FILE *fptr;fptr = fopen("log.txt", "a");time_t timep;time (&timep);fprintf(fptr, "The current time:%s,execution time: %lld, Sql: %s\n", asctime( gmtime(&timep)), total_time, queryDesc->sourceText);fclose(fptr);}if (prev_ExecutorEnd)prev_ExecutorEnd(queryDesc);elsestandard_ExecutorEnd(queryDesc);}void _PG_fini(void){ExecutorStart_hook = prev_ExecutorStart;ExecutorEnd_hook = prev_ExecutorEnd;}
# contrib/time_monitor/MakefileMODULE_big = time_monitorOBJS = time_monitor.o $(WIN32RES)EXTENSION = time_monitorDATA = time_monitor--1.0.sqlPGFILEDESC = "time_monitor - find out the sql that is exceeding expectations"REGRESS = check check_btreePG_CONFIG = pg_configPGXS := $(shell $(PG_CONFIG) --pgxs)include $(PGXS)
make -sj && make install
CREATE EXTENSION time_monitor;
load 'libdir/name' # name:扩展名
select * from pg_extension;

drop extension time_monitor;



AtomGit https://atomgit.com/opentenbase/OpenTenBase GitHub https://github.com/OpenTenBase/OpenTenBase
文章转载自OpenAtom OpenTenBase,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




