1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-09 13:09:39 +03:00

Back-patch fix for bugs in pgstat_initstats.

This commit is contained in:
Tom Lane
2003-07-22 19:00:36 +00:00
parent fd9397c0e7
commit 3196738793

View File

@@ -16,7 +16,7 @@
* *
* Copyright (c) 2001, PostgreSQL Global Development Group * Copyright (c) 2001, PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.31 2002/10/24 23:19:13 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.31.2.1 2003/07/22 19:00:36 tgl Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
@@ -75,13 +75,15 @@ static int pgStatPid;
static long pgStatNumMessages = 0; static long pgStatNumMessages = 0;
static bool pgStatRunningInCollector = FALSE; static bool pgStatRunningInCollector = FALSE;
static int pgStatTabstatAlloc = 0; static int pgStatTabstatAlloc = 0;
static int pgStatTabstatUsed = 0; static int pgStatTabstatUsed = 0;
static PgStat_MsgTabstat **pgStatTabstatMessages = NULL; static PgStat_MsgTabstat **pgStatTabstatMessages = NULL;
#define TABSTAT_QUANTUM 4 /* we alloc this many at a time */
static int pgStatXactCommit = 0; static int pgStatXactCommit = 0;
static int pgStatXactRollback = 0; static int pgStatXactRollback = 0;
static TransactionId pgStatDBHashXact = InvalidTransactionId; static TransactionId pgStatDBHashXact = InvalidTransactionId;
static HTAB *pgStatDBHash = NULL; static HTAB *pgStatDBHash = NULL;
static HTAB *pgStatBeDead = NULL; static HTAB *pgStatBeDead = NULL;
@@ -752,32 +754,37 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
} }
/* /*
* On the first of all calls initialize the message buffers. * On the first of all calls create some message buffers.
*/ */
if (pgStatTabstatAlloc == 0)
{
pgStatTabstatAlloc = 4;
pgStatTabstatMessages = (PgStat_MsgTabstat **)
malloc(sizeof(PgStat_MsgTabstat *) * pgStatTabstatAlloc);
if (pgStatTabstatMessages == NULL) if (pgStatTabstatMessages == NULL)
{ {
elog(LOG, "PGSTATBE: malloc() failed"); PgStat_MsgTabstat *newMessages;
PgStat_MsgTabstat **msgArray;
newMessages = (PgStat_MsgTabstat *)
malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
if (newMessages == NULL)
{
elog(LOG, "out of memory");
return; return;
} }
for (i = 0; i < pgStatTabstatAlloc; i++) msgArray = (PgStat_MsgTabstat **)
malloc(sizeof(PgStat_MsgTabstat *) * TABSTAT_QUANTUM);
if (msgArray == NULL)
{ {
pgStatTabstatMessages[i] = (PgStat_MsgTabstat *) free(newMessages);
malloc(sizeof(PgStat_MsgTabstat)); elog(LOG, "out of memory");
if (pgStatTabstatMessages[i] == NULL)
{
elog(LOG, "PGSTATBE: malloc() failed");
return; return;
} }
} MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
for (i = 0; i < TABSTAT_QUANTUM; i++)
msgArray[i] = newMessages++;
pgStatTabstatMessages = msgArray;
pgStatTabstatAlloc = TABSTAT_QUANTUM;
} }
/* /*
* Lookup the so far used table slots for this relation. * Search the already-used message slots for this relation.
*/ */
for (mb = 0; mb < pgStatTabstatUsed; mb++) for (mb = 0; mb < pgStatTabstatUsed; mb++)
{ {
@@ -799,7 +806,6 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
*/ */
i = pgStatTabstatMessages[mb]->m_nentries++; i = pgStatTabstatMessages[mb]->m_nentries++;
useent = &pgStatTabstatMessages[mb]->m_entry[i]; useent = &pgStatTabstatMessages[mb]->m_entry[i];
memset(useent, 0, sizeof(PgStat_TableEntry));
useent->t_id = rel_id; useent->t_id = rel_id;
stats->tabentry = (void *) useent; stats->tabentry = (void *) useent;
return; return;
@@ -810,27 +816,31 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
*/ */
if (pgStatTabstatUsed >= pgStatTabstatAlloc) if (pgStatTabstatUsed >= pgStatTabstatAlloc)
{ {
pgStatTabstatAlloc += 4; int newAlloc = pgStatTabstatAlloc + TABSTAT_QUANTUM;
pgStatTabstatMessages = (PgStat_MsgTabstat **) PgStat_MsgTabstat *newMessages;
PgStat_MsgTabstat **msgArray;
newMessages = (PgStat_MsgTabstat *)
malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
if (newMessages == NULL)
{
elog(LOG, "out of memory");
return;
}
msgArray = (PgStat_MsgTabstat **)
realloc(pgStatTabstatMessages, realloc(pgStatTabstatMessages,
sizeof(PgStat_MsgTabstat *) * pgStatTabstatAlloc); sizeof(PgStat_MsgTabstat *) * newAlloc);
if (pgStatTabstatMessages == NULL) if (msgArray == NULL)
{ {
pgStatTabstatAlloc -= 4; free(newMessages);
elog(LOG, "PGSTATBE: malloc() failed"); elog(LOG, "out of memory");
return; return;
} }
for (i = pgStatTabstatUsed; i < pgStatTabstatAlloc; i++) MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
{ for (i = 0; i < TABSTAT_QUANTUM; i++)
pgStatTabstatMessages[i] = (PgStat_MsgTabstat *) msgArray[pgStatTabstatAlloc + i] = newMessages++;
malloc(sizeof(PgStat_MsgTabstat)); pgStatTabstatMessages = msgArray;
if (pgStatTabstatMessages[i] == NULL) pgStatTabstatAlloc = newAlloc;
{
pgStatTabstatAlloc -= 4;
elog(LOG, "PGSTATBE: malloc() failed");
return;
}
}
} }
/* /*
@@ -839,10 +849,8 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
mb = pgStatTabstatUsed++; mb = pgStatTabstatUsed++;
pgStatTabstatMessages[mb]->m_nentries = 1; pgStatTabstatMessages[mb]->m_nentries = 1;
useent = &pgStatTabstatMessages[mb]->m_entry[0]; useent = &pgStatTabstatMessages[mb]->m_entry[0];
memset(useent, 0, sizeof(PgStat_TableEntry));
useent->t_id = rel_id; useent->t_id = rel_id;
stats->tabentry = (void *) useent; stats->tabentry = (void *) useent;
return;
} }