8000
We read every piece of feedback, and take your input very seriously.
1 parent e3d97d7 commit 1c6702fCopy full SHA for 1c6702f
src/backend/postmaster/pgstat.c
@@ -13,7 +13,7 @@
13
*
14
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
15
16
- * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.42 2003/08/04 00:43:21 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.43 2003/08/12 16:21:18 tgl Exp $
17
* ----------
18
*/
19
#include "postgres.h"
@@ -156,7 +156,8 @@ pgstat_init(void)
156
/*
157
* Force start of collector daemon if something to collect
158
159
- if (pgstat_collect_querystring || pgstat_collect_tuplelevel ||
+ if (pgstat_collect_querystring ||
160
+ pgstat_collect_tuplelevel ||
161
pgstat_collect_blocklevel)
162
pgstat_collect_startcollector = true;
163
@@ -536,34 +537,38 @@ void
536
537
pgstat_report_tabstat(void)
538
{
539
int i;
- int n;
540
- int len;
541
-
542
- if (!pgstat_collect_querystring && !pgstat_collect_tuplelevel &&
543
- !pgstat_collect_blocklevel)
544
- return;
545
546
- if (pgStatSock < 0)
+ if (pgStatSock < 0 ||
+ !(pgstat_collect_querystring ||
+ pgstat_collect_blocklevel))
+ {
+ /* Not reporting stats, so just flush whatever we have */
547
+ pgStatTabstatUsed = 0;
548
return;
549
+ }
550
551
552
* For each message buffer used during the last query set the header
553
* fields and send it out.
554
555
for (i = 0; i < pgStatTabstatUsed; i++)
556
- n = pgStatTabstatMessages[i]->m_nentries;
557
+ PgStat_MsgTabstat *tsmsg = pgStatTabstatMessages[i];
558
+ int n;
559
+ int len;
560
+
561
+ n = tsmsg->m_nentries;
562
len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
563
n * sizeof(PgStat_TableEntry);
564
- pgStatTabstatMessages[i]->m_xact_commit = pgStatXactCommit;
- pgStatTabstatMessages[i]->m_xact_rollback = pgStatXactRollback;
565
+ tsmsg->m_xact_commit = pgStatXactCommit;
566
+ tsmsg->m_xact_rollback = pgStatXactRollback;
567
pgStatXactCommit = 0;
568
pgStatXactRollback = 0;
569
- pgstat_setheader(&pgStatTabstatMessages[i]->m_hdr,
- PGSTAT_MTYPE_TABSTAT);
- pgstat_send(pgStatTabstatMessages[i], len);
570
+ pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
571
+ pgstat_send(tsmsg, len);
572
}
573
574
pgStatTabstatUsed = 0;
@@ -802,6 +807,53 @@ pgstat_ping(void)
802
807
pgstat_send(&msg, sizeof(msg));
803
808
804
809
810
+/*
811
+ * Create or enlarge the pgStatTabstatMessages array
812
+ */
813
+static bool
814
+more_tabstat_space(void)
815
+{
816
+ PgStat_MsgTabstat *newMessages;
817
+ PgStat_MsgTabstat **msgArray;
818
+ int newAlloc = pgStatTabstatAlloc + TABSTAT_QUANTUM;
819
+ int i;
820
821
+ /* Create (another) quantum of message buffers */
822
+ newMessages = (PgStat_MsgTabstat *)
823
+ malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
824
+ if (newMessages == NULL)
825
826
+ ereport(LOG,
827
+ (errcode(ERRCODE_OUT_OF_MEMORY),
828
+ errmsg("out of memory")));
829
+ return false;
830
831
832
+ /* Create or enlarge the pointer array */
833
+ if (pgStatTabstatMessages == NULL)
834
+ msgArray = (PgStat_MsgTabstat **)
835
+ malloc(sizeof(PgStat_MsgTabstat *) * newAlloc);
836
+ else
837
838
+ realloc(pgStatTabstatMessages,
839
+ sizeof(PgStat_MsgTabstat *) * newAlloc);
840
+ if (msgArray == NULL)
841
842
+ free(newMessages);
843
844
845
846
847
848
849
+ MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
850
+ for (i = 0; i < TABSTAT_QUANTUM; i++)
851
+ msgArray[pgStatTabstatAlloc + i] = newMessages++;
852
+ pgStatTabstatMessages = msgArray;
853
+ pgStatTabstatAlloc = newAlloc;
854
855
+ return true;
856
+}
805
857
806
858
/* ----------
859
* pgstat_initstats() -
@@ -815,8 +867,9 @@ pgstat_ping(void)
867
void
868
pgstat_initstats(PgStat_Info *stats, Relation rel)
869
- PgStat_TableEntry *useent;
870
Oid rel_id = rel->rd_id;
871
+ PgStat_TableEntry *useent;
872
+ PgStat_MsgTabstat *tsmsg;
873
int mb;
874
875
@@ -828,69 +881,39 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
881
stats->heap_scan_counted = FALSE;
882
stats->index_scan_counted = FALSE;
883
884
885
+ !(pgstat_collect_tuplelevel ||
886
887
888
stats->no_stats = TRUE;
889
890
891
- /*
- * On the first of all calls create some message buffers.
- */
- if (pgStatTabstatMessages == NULL)
- {
- PgStat_MsgTabstat *newMessages;
- PgStat_MsgTabstat **msgArray;
- newMessages = (PgStat_MsgTabstat *)
- malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
- if (newMessages == NULL)
-< 10000 /span> {
- ereport(LOG,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
- }
- msgArray = (PgStat_MsgTabstat **)
- malloc(sizeof(PgStat_MsgTabstat *) * TABSTAT_QUANTUM);
- if (msgArray == NULL)
- free(newMessages);
860
861
862
863
864
- MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
865
- for (i = 0; i < TABSTAT_QUANTUM; i++)
866
- msgArray[i] = newMessages++;
- pgStatTabstatMessages = msgArray;
- pgStatTabstatAlloc = TABSTAT_QUANTUM;
892
893
* Search the already-used message slots for this relation.
894
895
for (mb = 0; mb < pgStatTabstatUsed; mb++)
896
876
- for (i = 0; i < pgStatTabstatMessages[mb]->m_nentries; i++)
897
+ tsmsg = pgStatTabstatMessages[mb];
898
899
+ for (i = tsmsg->m_nentries; --i >= 0; )
877
900
878
- if (pgStatTabstatMessages[mb]->m_entry[i].t_id == rel_id)
901
+ if (tsmsg->m_entry[i].t_id == rel_id)
879
902
880
- stats->tabentry = (void *) &(pgStatTabstatMessages[mb]->m_entry[i]);
903
+ stats->tabentry = (void *) &(tsmsg->m_entry[i]);
904
905
906
907
- if (pgStatTabstatMessages[mb]->m_nentries >= PGSTAT_NUM_TABENTRIES)
908
+ if (tsmsg->m_nentries >= PGSTAT_NUM_TABENTRIES)
909
continue;
910
911
912
* Not found, but found a message buffer with an empty slot
913
* instead. Fine, let's use this one.
914
- i = pgStatTabstatMessages[mb]->m_nentries++;
- useent = &pgStatTabstatMessages[mb]->m_entry[i];
915
+ i = tsmsg->m_nentries++;
916
+ useent = &tsmsg->m_entry[i];
917
MemSet(useent, 0, sizeof(PgStat_TableEntry));
918
useent->t_id = rel_id;
919
stats->tabentry = (void *) useent;
@@ -902,43 +925,21 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
925
926
if (pgStatTabstatUsed >= pgStatTabstatAlloc)
927
- int newAlloc = pgStatTabstatAlloc + TABSTAT_QUANTUM;
- realloc(pgStatTabstatMessages,
920
- sizeof(PgStat_MsgTabstat *) * newAlloc);
921
928
+ if (!more_tabstat_space())
922
929
923
924
930
+ stats->no_stats = TRUE;
931
932
- msgArray[pgStatTabstatAlloc + i] = newMessages++;
933
- pgStatTabstatAlloc = newAlloc;
+ Assert(pgStatTabstatUsed < pgStatTabstatAlloc);
934
935
936
937
* Use the first entry of the next message buffer.
938
939
mb = pgStatTabstatUsed++;
940
- pgStatTabstatMessages[mb]->m_nentries = 1;
941
- useent = &pgStatTabstatMessages[mb]->m_entry[0];
+ tsmsg->m_nentries = 1;
942
+ useent = &tsmsg->m_entry[0];
943
944
945
@@ -954,8 +955,9 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
954
955
956
pgstat_count_xact_commit(void)
957
958
+ if (!(pgstat_collect_querystring ||
959
960
961
962
963
pgStatXactCommit++;
@@ -965,13 +967,15 @@ pgstat_count_xact_commit(void)
965
967
* message buffer used without slots, causing the next report to tell
966
968
* new xact-counters.
969
- if (pgStatTabstatAlloc > 0)
970
+ if (pgStatTabstatAlloc == 0)
971
- if (pgStatTabstatUsed == 0)
972
- pgStatTabstatUsed++;
973
- pgStatTabstatMessages[0]->m_nentries = 0;
974
+ return;
975
+ if (pgStatTabstatUsed == 0)
976
977
+ pgStatTabstatUsed++;
978
+ pgStatTabstatMessages[0]->m_nentries = 0;
979
980
981
@@ -985,8 +989,9 @@ pgstat_count_xact_commit(void)
985
989
986
990
pgstat_count_xact_rollback(void)
987
991
988
992
993
994
995
996
997
pgStatXactRollback++;
@@ -996,13 +1001,15 @@ pgstat_count_xact_rollback(void)
1001
1002
998
1003
999
1004
1000
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015