1414 * Copyright (c) 2008-2009, PostgreSQL Global Development Group
1515 *
1616 * IDENTIFICATION
17- * $PostgreSQL: pgsql/contrib/pg_stat_statements/pg_stat_statements.c,v 1.6 2009/12/0101:08:45 momjian Exp $
17+ * $PostgreSQL: pgsql/contrib/pg_stat_statements/pg_stat_statements.c,v 1.7 2009/12/0102:31:11 momjian Exp $
1818 *
1919 *-------------------------------------------------------------------------
2020 */
2424
2525#include "access/hash.h"
2626#include "catalog/pg_type.h"
27- #include "commands/copy.h"
2827#include "executor/executor.h"
2928#include "executor/instrument.h"
3029#include "mb/pg_wchar.h"
3332#include "storage/fd.h"
3433#include "storage/ipc.h"
3534#include "storage/spin.h"
36- #include "tcop/utility.h"
3735#include "utils/builtins.h"
3836#include "utils/hsearch.h"
3937#include "utils/guc.h"
@@ -115,7 +113,6 @@ static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
115113static ExecutorStart_hook_type prev_ExecutorStart = NULL ;
116114static ExecutorRun_hook_type prev_ExecutorRun = NULL ;
117115static ExecutorEnd_hook_type prev_ExecutorEnd = NULL ;
118- static ProcessUtility_hook_type prev_ProcessUtility = NULL ;
119116
120117/* Links to shared memory state */
121118static pgssSharedState * pgss = NULL ;
@@ -127,11 +124,10 @@ typedef enum
127124{
128125PGSS_TRACK_NONE ,/* track no statements */
129126PGSS_TRACK_TOP ,/* only top level statements */
130- PGSS_TRACK_ALL /* all statements, including nested ones */
127+ PGSS_TRACK_ALL , /* all statements, including nested ones */
131128}PGSSTrackLevel ;
132129
133- static const struct config_enum_entry track_options []=
134- {
130+ static const struct config_enum_entry track_options []= {
135131{"none" ,PGSS_TRACK_NONE , false},
136132{"top" ,PGSS_TRACK_TOP , false},
137133{"all" ,PGSS_TRACK_ALL , false},
@@ -140,7 +136,6 @@ static const struct config_enum_entry track_options[] =
140136
141137static int pgss_max ;/* max # statements to track */
142138static int pgss_track ;/* tracking level */
143- static bool pgss_track_ddl ;/* whether to track ddl commands */
144139static bool pgss_save ;/* whether to save stats across shutdown */
145140
146141
@@ -151,9 +146,7 @@ static bool pgss_save;/* whether to save stats across shutdown */
151146/*---- Function declarations ----*/
152147
153148void _PG_init (void );
154- #ifdef NOT_USED
155149void _PG_fini (void );
156- #endif
157150
158151Datum pg_stat_statements_reset (PG_FUNCTION_ARGS );
159152Datum pg_stat_statements (PG_FUNCTION_ARGS );
@@ -168,12 +161,10 @@ static void pgss_ExecutorRun(QueryDesc *queryDesc,
168161ScanDirection direction ,
169162long count );
170163static void pgss_ExecutorEnd (QueryDesc * queryDesc );
171- static void pgss_ProcessUtility (Node * parsetree ,
172- const char * queryString ,ParamListInfo params ,bool isTopLevel ,
173- DestReceiver * dest ,char * completionTag );
174164static uint32 pgss_hash_fn (const void * key ,Size keysize );
175165static int pgss_match_fn (const void * key1 ,const void * key2 ,Size keysize );
176- static void pgss_store (const char * query ,double total_time ,uint64 rows );
166+ static void pgss_store (const char * query ,
167+ const Instrumentation * instr ,uint32 rows );
177168static Size pgss_memsize (void );
178169static pgssEntry * entry_alloc (pgssHashKey * key );
179170static void entry_dealloc (void );
@@ -223,16 +214,6 @@ _PG_init(void)
223214NULL ,
224215NULL );
225216
226- DefineCustomBoolVariable ("pg_stat_statements.track_ddl" ,
227- "Selects whether DDL commands are tracked by pg_stat_statements." ,
228- NULL ,
229- & pgss_track_ddl ,
230- true,
231- PGC_SUSET ,
232- 0 ,
233- NULL ,
234- NULL );
235-
236217DefineCustomBoolVariable ("pg_stat_statements.save" ,
237218"Save pg_stat_statements statistics across server shutdowns." ,
238219NULL ,
@@ -264,11 +245,8 @@ _PG_init(void)
264245ExecutorRun_hook = pgss_ExecutorRun ;
265246prev_ExecutorEnd = ExecutorEnd_hook ;
266247ExecutorEnd_hook = pgss_ExecutorEnd ;
267- prev_ProcessUtility = ProcessUtility_hook ;
268- ProcessUtility_hook = pgss_ProcessUtility ;
269248}
270249
271- #ifdef NOT_USED
272250/*
273251 * Module unload callback
274252 */
@@ -279,10 +257,8 @@ _PG_fini(void)
279257ExecutorStart_hook = prev_ExecutorStart ;
280258ExecutorRun_hook = prev_ExecutorRun ;
281259ExecutorEnd_hook = prev_ExecutorEnd ;
282- ProcessUtility_hook = prev_ProcessUtility ;
283260shmem_startup_hook = prev_shmem_startup_hook ;
284261}
285- #endif
286262
287263/*
288264 * shmem_startup hook: allocate or attach to shared memory,
@@ -563,7 +539,7 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
563539InstrEndLoop (queryDesc -> totaltime );
564540
565541pgss_store (queryDesc -> sourceText ,
566- queryDesc -> totaltime -> total ,
542+ queryDesc -> totaltime ,
567543queryDesc -> estate -> es_processed );
568544}
569545
@@ -573,59 +549,6 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
573549standard_ExecutorEnd (queryDesc );
574550}
575551
576- /*
577- * ProcessUtility hook
578- */
579- static void
580- pgss_ProcessUtility (Node * parsetree ,const char * queryString ,
581- ParamListInfo params ,bool isTopLevel ,
582- DestReceiver * dest ,char * completionTag )
583- {
584- if (pgss_track_ddl && isTopLevel && pgss_enabled ())
585- {
586- instr_time start ;
587- instr_time duration ;
588- uint64 rows = 0 ;
589-
590- INSTR_TIME_SET_CURRENT (start );
591-
592- nested_level ++ ;
593- PG_TRY ();
594- {
595- if (prev_ProcessUtility )
596- prev_ProcessUtility (parsetree ,queryString ,params ,isTopLevel ,dest ,completionTag );
597- else if ((nodeTag (parsetree ))== T_CopyStmt )
598- {
599- rows = DoCopy ((CopyStmt * )parsetree ,queryString );
600- if (completionTag )
601- snprintf (completionTag ,COMPLETION_TAG_BUFSIZE ,
602- "COPY " UINT64_FORMAT ,rows );
603- }
604- else
605- standard_ProcessUtility (parsetree ,queryString ,params ,isTopLevel ,dest ,completionTag );
606- nested_level -- ;
607- }
608- PG_CATCH ();
609- {
610- nested_level -- ;
611- PG_RE_THROW ();
612- }
613- PG_END_TRY ();
614-
615- INSTR_TIME_SET_CURRENT (duration );
616- INSTR_TIME_SUBTRACT (duration ,start );
617-
618- pgss_store (queryString ,INSTR_TIME_GET_DOUBLE (duration ),rows );
619- }
620- else
621- {
622- if (prev_ProcessUtility )
623- prev_ProcessUtility (parsetree ,queryString ,params ,isTopLevel ,dest ,completionTag );
624- else
625- standard_ProcessUtility (parsetree ,queryString ,params ,isTopLevel ,dest ,completionTag );
626- }
627- }
628-
629552/*
630553 * Calculate hash value for a key
631554 */
@@ -664,7 +587,7 @@ pgss_match_fn(const void *key1, const void *key2, Size keysize)
664587 * Store some statistics for a statement.
665588 */
666589static void
667- pgss_store (const char * query ,double total_time , uint64 rows )
590+ pgss_store (const char * query ,const Instrumentation * instr , uint32 rows )
668591{
669592pgssHashKey key ;
670593double usage ;
@@ -708,7 +631,7 @@ pgss_store(const char *query, double total_time, uint64 rows)
708631
709632SpinLockAcquire (& e -> mutex );
710633e -> counters .calls += 1 ;
711- e -> counters .total_time += total_time ;
634+ e -> counters .total_time += instr -> total ;
712635e -> counters .rows += rows ;
713636e -> counters .usage += usage ;
714637SpinLockRelease (& e -> mutex );