misc_utils.c 2.7 KB
Newer Older
1
/*
M
 
Marc G. Fournier 已提交
2
 * misc_utils.c --
3
 *
M
 
Marc G. Fournier 已提交
4
 * This file defines miscellaneous PostgreSQL utility functions.
5
 *
M
 
Marc G. Fournier 已提交
6 7 8 9
 * Copyright (c) 1998, 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.
10 11 12
 */

#include <unistd.h>
B
Hi,  
Bruce Momjian 已提交
13 14 15
#include <signal.h>
#include <string.h>
#include <errno.h>
16 17

#include "postgres.h"
B
Hi,  
Bruce Momjian 已提交
18 19 20 21 22 23 24 25 26
#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"
27
#include "utils/palloc.h"
B
Hi,  
Bruce Momjian 已提交
28 29
#include "utils/rel.h"
#include "utils/tqual.h"
30 31

#include "misc_utils.h"
B
Hi,  
Bruce Momjian 已提交
32 33

#define MIN(x,y)	((x)<=(y) ? (x) : (y))
34

35
extern int	ExecutorLimit(int limit);
36
extern void Async_Unlisten(char *relname, int pid);
M
 
Marc G. Fournier 已提交
37 38 39 40 41
extern int	assertTest(int val);

#ifdef ASSERT_CHECKING_TEST
extern int	assertEnable(int val);
#endif
42 43 44 45

int
query_limit(int limit)
{
46
	return ExecutorLimit(limit);
47 48 49 50 51
}

int
backend_pid()
{
52
	return getpid();
53 54 55 56 57
}

int
unlisten(char *relname)
{
58 59
	Async_Unlisten(relname, getpid());
	return 0;
60 61 62 63 64
}

int
max(int x, int y)
{
65
	return ((x > y) ? x : y);
66 67 68 69 70
}

int
min(int x, int y)
{
71
	return ((x < y) ? x : y);
72 73
}

B
Hi,  
Bruce Momjian 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*
 * 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];

92
	lRel = heap_openr(ListenerRelationName, AccessShareLock);
B
Hi,  
Bruce Momjian 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	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);

119
	heap_close(lRel, AccessShareLock);
B
Hi,  
Bruce Momjian 已提交
120 121 122 123

	return count;
}

M
 
Marc G. Fournier 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137
int
assert_enable(int val)
{
	return assertEnable(val);
}

#ifdef ASSERT_CHECKING_TEST
int
assert_test(int val)
{
	return assertTest(val);
}
#endif

138
/* end of file */
M
 
Marc G. Fournier 已提交
139 140

/*
B
Hi,  
Bruce Momjian 已提交
141 142 143 144
 * Local Variables:
 *  tab-width: 4
 *  c-indent-level: 4
 *  c-basic-offset: 4
M
 
Marc G. Fournier 已提交
145 146
 * End:
 */