1313 *
1414 * Copyright (c) 2001-2009, PostgreSQL Global Development Group
1515 *
16- * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.190 2009/08/12 20:53:30 tgl Exp $
16+ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.191 2009/09/04 22:32:33 tgl Exp $
1717 * ----------
1818 */
1919#include "postgres.h"
@@ -246,6 +246,8 @@ static void pgstat_beshutdown_hook(int code, Datum arg);
246246static void pgstat_sighup_handler (SIGNAL_ARGS );
247247
248248static PgStat_StatDBEntry * pgstat_get_db_entry (Oid databaseid , bool create );
249+ static PgStat_StatTabEntry * pgstat_get_tab_entry (PgStat_StatDBEntry * dbentry ,
250+ Oid tableoid , bool create );
249251static void pgstat_write_statsfile (bool permanent );
250252static HTAB * pgstat_read_statsfile (Oid onlydb , bool permanent );
251253static void backend_read_statsfile (void );
@@ -2940,6 +2942,52 @@ pgstat_get_db_entry(Oid databaseid, bool create)
29402942}
29412943
29422944
2945+ /*
2946+ * Lookup the hash table entry for the specified table. If no hash
2947+ * table entry exists, initialize it, if the create parameter is true.
2948+ * Else, return NULL.
2949+ */
2950+ static PgStat_StatTabEntry *
2951+ pgstat_get_tab_entry (PgStat_StatDBEntry * dbentry , Oid tableoid , bool create )
2952+ {
2953+ PgStat_StatTabEntry * result ;
2954+ bool found ;
2955+ HASHACTION action = (create ? HASH_ENTER : HASH_FIND );
2956+
2957+ /* Lookup or create the hash table entry for this table */
2958+ result = (PgStat_StatTabEntry * ) hash_search (dbentry -> tables ,
2959+ & tableoid ,
2960+ action , & found );
2961+
2962+ if (!create && !found )
2963+ return NULL ;
2964+
2965+ /* If not found, initialize the new one. */
2966+ if (!found )
2967+ {
2968+ result -> numscans = 0 ;
2969+ result -> tuples_returned = 0 ;
2970+ result -> tuples_fetched = 0 ;
2971+ result -> tuples_inserted = 0 ;
2972+ result -> tuples_updated = 0 ;
2973+ result -> tuples_deleted = 0 ;
2974+ result -> tuples_hot_updated = 0 ;
2975+ result -> n_live_tuples = 0 ;
2976+ result -> n_dead_tuples = 0 ;
2977+ result -> last_anl_tuples = 0 ;
2978+ result -> blocks_fetched = 0 ;
2979+ result -> blocks_hit = 0 ;
2980+
2981+ result -> vacuum_timestamp = 0 ;
2982+ result -> autovac_vacuum_timestamp = 0 ;
2983+ result -> analyze_timestamp = 0 ;
2984+ result -> autovac_analyze_timestamp = 0 ;
2985+ }
2986+
2987+ return result ;
2988+ }
2989+
2990+
29432991/* ----------
29442992 * pgstat_write_statsfile() -
29452993 *
@@ -3553,10 +3601,10 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
35533601 tabentry -> tuples_hot_updated = tabmsg [i ].t_counts .t_tuples_hot_updated ;
35543602 tabentry -> n_live_tuples = tabmsg [i ].t_counts .t_new_live_tuples ;
35553603 tabentry -> n_dead_tuples = tabmsg [i ].t_counts .t_new_dead_tuples ;
3604+ tabentry -> last_anl_tuples = 0 ;
35563605 tabentry -> blocks_fetched = tabmsg [i ].t_counts .t_blocks_fetched ;
35573606 tabentry -> blocks_hit = tabmsg [i ].t_counts .t_blocks_hit ;
35583607
3559- tabentry -> last_anl_tuples = 0 ;
35603608 tabentry -> vacuum_timestamp = 0 ;
35613609 tabentry -> autovac_vacuum_timestamp = 0 ;
35623610 tabentry -> analyze_timestamp = 0 ;
@@ -3734,19 +3782,10 @@ pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
37343782 PgStat_StatDBEntry * dbentry ;
37353783
37363784 /*
3737- * Lookup the database in the hashtable. Don't create the entry if it
3738- * doesn't exist, because autovacuum may be processing a template
3739- * database. If this isn't the case, the database is most likely to have
3740- * an entry already. (If it doesn't, not much harm is done anyway --
3741- * it'll get created as soon as somebody actually uses the database.)
3785+ * Store the last autovacuum time in the database's hashtable entry.
37423786 */
3743- dbentry = pgstat_get_db_entry (msg -> m_databaseid , false);
3744- if (dbentry == NULL )
3745- return ;
3787+ dbentry = pgstat_get_db_entry (msg -> m_databaseid , true);
37463788
3747- /*
3748- * Store the last autovacuum time in the database entry.
3749- */
37503789 dbentry -> last_autovac_time = msg -> m_start_time ;
37513790}
37523791
@@ -3763,18 +3802,11 @@ pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
37633802 PgStat_StatTabEntry * tabentry ;
37643803
37653804 /*
3766- * Don't create either the database or table entry if it doesn't already
3767- * exist. This avoids bloating the stats with entries for stuff that is
3768- * only touched by vacuum and not by live operations.
3805+ * Store the data in the table's hashtable entry.
37693806 */
3770- dbentry = pgstat_get_db_entry (msg -> m_databaseid , false);
3771- if (dbentry == NULL )
3772- return ;
3807+ dbentry = pgstat_get_db_entry (msg -> m_databaseid , true);
37733808
3774- tabentry = hash_search (dbentry -> tables , & (msg -> m_tableoid ),
3775- HASH_FIND , NULL );
3776- if (tabentry == NULL )
3777- return ;
3809+ tabentry = pgstat_get_tab_entry (dbentry , msg -> m_tableoid , true);
37783810
37793811 if (msg -> m_autovacuum )
37803812 tabentry -> autovac_vacuum_timestamp = msg -> m_vacuumtime ;
@@ -3821,18 +3853,11 @@ pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
38213853 PgStat_StatTabEntry * tabentry ;
38223854
38233855 /*
3824- * Don't create either the database or table entry if it doesn't already
3825- * exist. This avoids bloating the stats with entries for stuff that is
3826- * only touched by analyze and not by live operations.
3856+ * Store the data in the table's hashtable entry.
38273857 */
3828- dbentry = pgstat_get_db_entry (msg -> m_databaseid , false);
3829- if (dbentry == NULL )
3830- return ;
3858+ dbentry = pgstat_get_db_entry (msg -> m_databaseid , true);
38313859
3832- tabentry = hash_search (dbentry -> tables , & (msg -> m_tableoid ),
3833- HASH_FIND , NULL );
3834- if (tabentry == NULL )
3835- return ;
3860+ tabentry = pgstat_get_tab_entry (dbentry , msg -> m_tableoid , true);
38363861
38373862 if (msg -> m_autovacuum )
38383863 tabentry -> autovac_analyze_timestamp = msg -> m_analyzetime ;
0 commit comments