postgresql/contrib/miscutil/misc_utils.c

128 lines
2.3 KiB
C
Raw Normal View History

1997-11-06 05:38:25 +08:00
/*
* misc_utils.c --
1997-11-06 05:38:25 +08:00
*
* This file defines miscellaneous PostgreSQL utility functions.
1997-11-06 05:38:25 +08:00
*
1999-09-28 04:04:14 +08:00
* Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it>
*
* This file is distributed under the GNU General Public License
* either version 2, or (at your option) any later version.
1997-11-06 05:38:25 +08:00
*/
#include "postgres.h"
1997-11-06 05:38:25 +08:00
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
1997-11-06 05:38:25 +08:00
#include "access/heapam.h"
#include "access/htup.h"
#include "access/relscan.h"
#include "access/skey.h"
#include "access/tupdesc.h"
#include "catalog/catname.h"
#include "catalog/pg_listener.h"
#include "commands/async.h"
#include "fmgr.h"
#include "storage/lmgr.h"
#include "utils/fmgroids.h"
1997-11-06 05:38:25 +08:00
#include "utils/palloc.h"
#include "utils/rel.h"
#include "utils/tqual.h"
1997-11-06 05:38:25 +08:00
#include "misc_utils.h"
#undef MIN
#define MIN(x,y) ((x)<=(y) ? (x) : (y))
1997-11-06 05:38:25 +08:00
int
backend_pid()
{
return getpid();
1997-11-06 05:38:25 +08:00
}
int
unlisten(char *relname)
{
Async_Unlisten(relname, getpid());
return 0;
1997-11-06 05:38:25 +08:00
}
int
max(int x, int y)
{
return ((x > y) ? x : y);
1997-11-06 05:38:25 +08:00
}
int
min(int x, int y)
{
return ((x < y) ? x : y);
1997-11-06 05:38:25 +08:00
}
/*
* Return the number of active listeners on a relation name.
*/
int
active_listeners(text *relname)
{
HeapTuple lTuple;
Relation lRel;
HeapScanDesc sRel;
TupleDesc tdesc;
ScanKeyData key;
Datum d;
bool isnull;
int len,
pid;
int count = 0;
int ourpid = getpid();
char listen_name[NAMEDATALEN];
lRel = heap_openr(ListenerRelationName, AccessShareLock);
tdesc = RelationGetDescr(lRel);
if (relname && (VARSIZE(relname) > VARHDRSZ))
{
MemSet(listen_name, 0, NAMEDATALEN);
len = MIN(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
memcpy(listen_name, VARDATA(relname), len);
ScanKeyEntryInitialize(&key, 0,
Anum_pg_listener_relname,
F_NAMEEQ,
PointerGetDatum(listen_name));
sRel = heap_beginscan(lRel, 0, SnapshotNow, 1, &key);
}
else
sRel = heap_beginscan(lRel, 0, SnapshotNow, 0, (ScanKey) NULL);
while (HeapTupleIsValid(lTuple = heap_getnext(sRel, 0)))
{
d = heap_getattr(lTuple, Anum_pg_listener_pid, tdesc, &isnull);
pid = DatumGetInt32(d);
if ((pid == ourpid) || (kill(pid, SIGTSTP) == 0))
{
/* elog(NOTICE, "%d ok", pid); */
count++;
}
}
heap_endscan(sRel);
heap_close(lRel, AccessShareLock);
return count;
}
1997-11-06 05:38:25 +08:00
/* end of file */
/*
* Local Variables:
* tab-width: 4
* c-indent-level: 4
* c-basic-offset: 4
* End:
*/