*
* Copyright (c) 2001-2004, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.83 2004/10/25 06:27:21 neilc Exp $
+ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.84 2004/10/28 01:38:41 neilc Exp $
* ----------
*/
#include "postgres.h"
#include "miscadmin.h"
#include "postmaster/postmaster.h"
#include "storage/backendid.h"
+#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
/* ----------
* pgstat_report_activity() -
*
- * Called in tcop/postgres.c to tell the collector what the backend
+ * Called from tcop/postgres.c to tell the collector what the backend
* is actually doing (usually "<IDLE>" or the start of the query to
* be executed).
* ----------
/*
* Create or enlarge the pgStatTabstatMessages array
*/
-static bool
+static void
more_tabstat_space(void)
{
PgStat_MsgTabstat *newMessages;
/* Create (another) quantum of message buffers */
newMessages = (PgStat_MsgTabstat *)
- malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
- if (newMessages == NULL)
- {
- ereport(LOG,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
- return false;
- }
+ MemoryContextAllocZero(TopMemoryContext,
+ sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
/* Create or enlarge the pointer array */
if (pgStatTabstatMessages == NULL)
msgArray = (PgStat_MsgTabstat **)
- malloc(sizeof(PgStat_MsgTabstat *) * newAlloc);
+ MemoryContextAlloc(TopMemoryContext,
+ sizeof(PgStat_MsgTabstat *) * newAlloc);
else
msgArray = (PgStat_MsgTabstat **)
- realloc(pgStatTabstatMessages,
- sizeof(PgStat_MsgTabstat *) * newAlloc);
- if (msgArray == NULL)
- {
- free(newMessages);
- ereport(LOG,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory")));
- return false;
- }
+ repalloc(pgStatTabstatMessages,
+ sizeof(PgStat_MsgTabstat *) * newAlloc);
- MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
for (i = 0; i < TABSTAT_QUANTUM; i++)
msgArray[pgStatTabstatAlloc + i] = newMessages++;
pgStatTabstatMessages = msgArray;
pgStatTabstatAlloc = newAlloc;
- return true;
+ Assert(pgStatTabstatUsed < pgStatTabstatAlloc);
}
/* ----------
* If we ran out of message buffers, we just allocate more.
*/
if (pgStatTabstatUsed >= pgStatTabstatAlloc)
- {
- if (!more_tabstat_space())
- {
- stats->no_stats = TRUE;
- return;
- }
- Assert(pgStatTabstatUsed < pgStatTabstatAlloc);
- }
+ more_tabstat_space();
/*
* Use the first entry of the next message buffer.
* new xact-counters.
*/
if (pgStatTabstatAlloc == 0)
- {
- if (!more_tabstat_space())
- return;
- }
+ more_tabstat_space();
+
if (pgStatTabstatUsed == 0)
{
pgStatTabstatUsed++;
* new xact-counters.
*/
if (pgStatTabstatAlloc == 0)
- {
- if (!more_tabstat_space())
- return;
- }
+ more_tabstat_space();
+
if (pgStatTabstatUsed == 0)
{
pgStatTabstatUsed++;
/*
* Create the known backends table
*/
- pgStatBeTable = (PgStat_StatBeEntry *) malloc(
+ pgStatBeTable = (PgStat_StatBeEntry *) palloc0(
sizeof(PgStat_StatBeEntry) * MaxBackends);
- if (pgStatBeTable == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory in statistics collector --- abort")));
- memset(pgStatBeTable, 0, sizeof(PgStat_StatBeEntry) * MaxBackends);
readPipe = pgStatPipe[0];
/*
* Allocate the message buffer
*/
- msgbuffer = (char *) malloc(PGSTAT_RECVBUFFERSZ);
- if (msgbuffer == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_OUT_OF_MEMORY),
- errmsg("out of memory in statistics collector --- abort")));
+ msgbuffer = (char *) palloc(PGSTAT_RECVBUFFERSZ);
/*
* Loop forever
* simply return zero for anything and the collector simply starts
* from scratch with empty counters.
*/
- if ((fpin = fopen(pgStat_fname, PG_BINARY_R)) == NULL)
+ if ((fpin = AllocateFile(pgStat_fname, PG_BINARY_R)) == NULL)
return;
/*
{
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
/*
&found);
if (dbentry == NULL)
{
- fclose(fpin);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
{
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
hash_ctl.hash = tag_hash;
hash_ctl.hcxt = use_mcxt;
- PG_TRY();
- {
- dbentry->tables = hash_create("Per-database table",
- PGSTAT_TAB_HASH_SIZE,
- &hash_ctl,
- HASH_ELEM | HASH_FUNCTION | mcxt_flags);
- }
- PG_CATCH();
- {
- fclose(fpin);
- PG_RE_THROW();
- }
- PG_END_TRY();
+ dbentry->tables = hash_create("Per-database table",
+ PGSTAT_TAB_HASH_SIZE,
+ &hash_ctl,
+ HASH_ELEM | HASH_FUNCTION | mcxt_flags);
/*
* Arrange that following 'T's add entries to this
{
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
/*
(void *) &tabbuf.tableid,
HASH_ENTER, &found);
if (tabentry == NULL)
- {
- fclose(fpin);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
- }
if (found)
{
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
memcpy(tabentry, &tabbuf, sizeof(tabbuf));
*/
case 'M':
if (betab == NULL || numbackends == NULL)
- {
- fclose(fpin);
- return;
- }
+ goto done;
if (fread(&maxbackends, 1, sizeof(maxbackends), fpin) !=
sizeof(maxbackends))
{
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
if (maxbackends == 0)
- {
- fclose(fpin);
- return;
- }
+ goto done;
/*
* Allocate space (in TopTransactionContext too) for the
* backend table.
*/
if (use_mcxt == NULL)
- *betab = (PgStat_StatBeEntry *) malloc(
+ *betab = (PgStat_StatBeEntry *) palloc(
sizeof(PgStat_StatBeEntry) * maxbackends);
else
*betab = (PgStat_StatBeEntry *) MemoryContextAlloc(
* 'B' A PgStat_StatBeEntry follows.
*/
case 'B':
- if (betab == NULL || numbackends == NULL)
- {
- fclose(fpin);
- return;
- }
- if (*betab == NULL)
- {
- fclose(fpin);
- return;
- }
+ if (betab == NULL || numbackends == NULL || *betab == NULL)
+ goto done;
/*
* Read it directly into the table.
{
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
/*
if (numbackends != 0)
*numbackends = havebackends;
if (havebackends >= maxbackends)
- {
- fclose(fpin);
- return;
- }
+ goto done;
+
break;
/*
* 'E' The EOF marker of a complete stats file.
*/
case 'E':
- fclose(fpin);
- return;
+ goto done;
default:
ereport(pgStatRunningInCollector ? LOG : WARNING,
(errmsg("corrupted pgstat.stat file")));
- fclose(fpin);
- return;
+ goto done;
}
}
- fclose(fpin);
+done:
+ FreeFile(fpin);
}
/*