postgresql/contrib/miscutil/misc_utils.c

154 lines
2.7 KiB
C
Raw Normal View History

1997-11-05 22:38:25 +01:00
/*
* misc_utils.c --
1997-11-05 22:38:25 +01:00
*
* This file defines miscellaneous PostgreSQL utility functions.
1997-11-05 22:38:25 +01:00
*
1999-09-27 22:04:14 +02: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-05 22:38:25 +01:00
*/
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
1997-11-05 22:38:25 +01:00
#include "postgres.h"
#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 "storage/lmgr.h"
#include "utils/fmgr.h"
1997-11-05 22:38:25 +01:00
#include "utils/palloc.h"
#include "utils/rel.h"
#include "utils/tqual.h"
1997-11-05 22:38:25 +01:00
#include "misc_utils.h"
#define MIN(x,y) ((x)<=(y) ? (x) : (y))
1997-11-05 22:38:25 +01:00
extern int ExecutorLimit(int limit);
1997-11-05 22:38:25 +01:00
extern void Async_Unlisten(char *relname, int pid);
extern int assertTest(int val);
#ifdef ASSERT_CHECKING_TEST
extern int assertEnable(int val);
#endif
1997-11-05 22:38:25 +01:00
int
query_limit(int limit)
{
return ExecutorLimit(limit);
1997-11-05 22:38:25 +01:00
}
int
backend_pid()
{
return getpid();
1997-11-05 22:38:25 +01:00
}
int
unlisten(char *relname)
{
Async_Unlisten(relname, getpid());
return 0;
1997-11-05 22:38:25 +01:00
}
int
max(int x, int y)
{
return ((x > y) ? x : y);
1997-11-05 22:38:25 +01:00
}
int
min(int x, int y)
{
return ((x < y) ? x : y);
1997-11-05 22:38:25 +01: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))
{
len = MIN(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
strncpy(listen_name, VARDATA(relname), len);
listen_name[len] = '\0';
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;
}
contrib-array.patch this is an old patch which I have already submitted and never seen in the sources. It corrects the datatype oids used in some iterator functions. This bug has been reported to me by many other people. contrib-datetime.patch some code contributed by Reiner Dassing <dassing@wettzell.ifag.de> contrib-makefiles.patch fixes all my contrib makefiles which don't work with some compilers, as reported to me by another user. contrib-miscutil.patch an old patch for one of my old contribs. contrib-string.patch a small change to the c-like text output functions. Now the '{' is escaped only at the beginning of the string to distinguish it from arrays, and the '}' is no more escaped. elog-lineno.patch adds the current lineno of CopyFrom to elog messages. This is very useful when you load a 1 million tuples table from an external file and there is a bad value somehere. Currently you get an error message but you can't know where is the bad data. The patch uses a variable which was declared static in copy.c. The variable is now exported and initialized to 0. It is always cleared at the end of the copy or at the first elog message or when the copy is canceled. I know this is very ugly but I can't find any better way of knowing where the copy fails and I have this problem quite often. plperl-makefile.patch fixes a typo in a makefile, but the error must be elsewhere because it is a file generated automatically. Please have a look. tprintf-timestamp.patch restores the original 2-digit year format, assuming that the two century digits don't carry much information and that '000202' is easier to read than 20000202. Being only a log file it shouldn't break anything. Please apply the patches before the next scheduled code freeze. I also noticed that some of the contribs don't compile correcly. Should we ask people to fix their code or rename their makefiles so that they are ignored by the top makefile? -- Massimo Dal Zotto
2000-02-13 19:59:53 +01:00
#ifdef USE_ASSERT_CHECKING
int
assert_enable(int val)
{
return assertEnable(val);
}
#ifdef ASSERT_CHECKING_TEST
int
assert_test(int val)
{
return assertTest(val);
}
#endif
contrib-array.patch this is an old patch which I have already submitted and never seen in the sources. It corrects the datatype oids used in some iterator functions. This bug has been reported to me by many other people. contrib-datetime.patch some code contributed by Reiner Dassing <dassing@wettzell.ifag.de> contrib-makefiles.patch fixes all my contrib makefiles which don't work with some compilers, as reported to me by another user. contrib-miscutil.patch an old patch for one of my old contribs. contrib-string.patch a small change to the c-like text output functions. Now the '{' is escaped only at the beginning of the string to distinguish it from arrays, and the '}' is no more escaped. elog-lineno.patch adds the current lineno of CopyFrom to elog messages. This is very useful when you load a 1 million tuples table from an external file and there is a bad value somehere. Currently you get an error message but you can't know where is the bad data. The patch uses a variable which was declared static in copy.c. The variable is now exported and initialized to 0. It is always cleared at the end of the copy or at the first elog message or when the copy is canceled. I know this is very ugly but I can't find any better way of knowing where the copy fails and I have this problem quite often. plperl-makefile.patch fixes a typo in a makefile, but the error must be elsewhere because it is a file generated automatically. Please have a look. tprintf-timestamp.patch restores the original 2-digit year format, assuming that the two century digits don't carry much information and that '000202' is easier to read than 20000202. Being only a log file it shouldn't break anything. Please apply the patches before the next scheduled code freeze. I also noticed that some of the contribs don't compile correcly. Should we ask people to fix their code or rename their makefiles so that they are ignored by the top makefile? -- Massimo Dal Zotto
2000-02-13 19:59:53 +01:00
#endif
1997-11-05 22:38:25 +01:00
/* end of file */
/*
* Local Variables:
* tab-width: 4
* c-indent-level: 4
* c-basic-offset: 4
* End:
*/