~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/miscutil/misc_utils.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * misc_utils.c --
 
3
 *
 
4
 * This file defines miscellaneous PostgreSQL utility functions.
 
5
 *
 
6
 * Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it>
 
7
 *
 
8
 * This file is distributed under the GNU General Public License
 
9
 * either version 2, or (at your option) any later version.
 
10
 */
 
11
 
 
12
#include "postgres.h"
 
13
 
 
14
#include <unistd.h>
 
15
#include <signal.h>
 
16
 
 
17
#include "access/heapam.h"
 
18
#include "access/htup.h"
 
19
#include "access/relscan.h"
 
20
#include "access/skey.h"
 
21
#include "access/tupdesc.h"
 
22
#include "catalog/catname.h"
 
23
#include "catalog/pg_listener.h"
 
24
#include "commands/async.h"
 
25
#include "fmgr.h"
 
26
#include "storage/lmgr.h"
 
27
#include "utils/fmgroids.h"
 
28
#include "utils/rel.h"
 
29
#include "utils/tqual.h"
 
30
 
 
31
#include "misc_utils.h"
 
32
 
 
33
 
 
34
int
 
35
backend_pid(void)
 
36
{
 
37
        return getpid();
 
38
}
 
39
 
 
40
int
 
41
unlisten(char *relname)
 
42
{
 
43
        Async_Unlisten(relname, getpid());
 
44
        return 0;
 
45
}
 
46
 
 
47
int
 
48
int4max(int x, int y)
 
49
{
 
50
        return Max(x, y);
 
51
}
 
52
 
 
53
int
 
54
int4min(int x, int y)
 
55
{
 
56
        return Min(x, y);
 
57
}
 
58
 
 
59
/*
 
60
 * Return the number of active listeners on a relation name.
 
61
 */
 
62
int
 
63
active_listeners(text *relname)
 
64
{
 
65
        HeapTuple       lTuple;
 
66
        Relation        lRel;
 
67
        HeapScanDesc sRel;
 
68
        TupleDesc       tdesc;
 
69
        ScanKeyData key;
 
70
        Datum           d;
 
71
        bool            isnull;
 
72
        int                     len,
 
73
                                pid;
 
74
        int                     count = 0;
 
75
        int                     ourpid = getpid();
 
76
        char            listen_name[NAMEDATALEN];
 
77
 
 
78
        lRel = heap_openr(ListenerRelationName, AccessShareLock);
 
79
        tdesc = RelationGetDescr(lRel);
 
80
 
 
81
        if (relname && (VARSIZE(relname) > VARHDRSZ))
 
82
        {
 
83
                MemSet(listen_name, 0, NAMEDATALEN);
 
84
                len = Min(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
 
85
                memcpy(listen_name, VARDATA(relname), len);
 
86
                ScanKeyInit(&key,
 
87
                                        Anum_pg_listener_relname,
 
88
                                        BTEqualStrategyNumber, F_NAMEEQ,
 
89
                                        PointerGetDatum(listen_name));
 
90
                sRel = heap_beginscan(lRel, SnapshotNow, 1, &key);
 
91
        }
 
92
        else
 
93
                sRel = heap_beginscan(lRel, SnapshotNow, 0, (ScanKey) NULL);
 
94
 
 
95
        while ((lTuple = heap_getnext(sRel, ForwardScanDirection)) != NULL)
 
96
        {
 
97
                d = heap_getattr(lTuple, Anum_pg_listener_pid, tdesc, &isnull);
 
98
                pid = DatumGetInt32(d);
 
99
                if ((pid == ourpid) || (kill(pid, 0) == 0))
 
100
                        count++;
 
101
        }
 
102
        heap_endscan(sRel);
 
103
 
 
104
        heap_close(lRel, AccessShareLock);
 
105
 
 
106
        return count;
 
107
}