-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify_now.c
executable file
·76 lines (62 loc) · 1.76 KB
/
notify_now.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*-------------------------------------------------------------------------
*
* notify_now.c
* extenstion for PostgreSQL
*
* IDENTIFICATION
* notify_now/notify_now.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/json.h"
#include "utils/builtins.h"
#include <limits.h>
#include "access/parallel.h"
#include "catalog/pg_database.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#define NOTIFY_NOW_PAYLOAD_MAX_LENGTH 2000000000
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(notify_now);
Datum
notify_now(PG_FUNCTION_ARGS)
{
size_t channel_len;
size_t payload_len;
const char *channel;
const char *payload;
StringInfoData buf;
if (IsParallelWorker())
elog(ERROR, "cannot send notifications from a parallel worker");
if (PG_ARGISNULL(0))
channel = "";
else
channel = text_to_cstring(PG_GETARG_TEXT_P(0));
if (PG_ARGISNULL(1))
payload = "";
else
payload = text_to_cstring(PG_GETARG_TEXT_P(1));
channel_len = strlen(channel);
payload_len = strlen(payload);
if (channel_len == 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("channel name cannot be empty")));
if (channel_len >= NAMEDATALEN)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("channel name too long")));
if (payload_len >= NOTIFY_NOW_PAYLOAD_MAX_LENGTH)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("payload string too long")));
pq_beginmessage(&buf, 'A'); // NOTIFY
pq_sendint32(&buf, MyProcPid);
pq_sendstring(&buf, channel);
pq_sendstring(&buf, payload);
pq_endmessage(&buf);
pq_flush(); // send as soon as possible
PG_RETURN_VOID();
}