forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.c
995 lines (823 loc) · 21.4 KB
/
alert.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#include "postgres.h"
#include "executor/spi.h"
#if PG_VERSION_NUM >= 90300
#include "access/htup_details.h"
#endif
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "string.h"
#include "storage/lwlock.h"
#include "utils/timestamp.h"
#include "orafce.h"
#include "builtins.h"
#include "pipe.h"
#include "shmmc.h"
#include "utils/rel.h"
PG_FUNCTION_INFO_V1(dbms_alert_register);
PG_FUNCTION_INFO_V1(dbms_alert_remove);
PG_FUNCTION_INFO_V1(dbms_alert_removeall);
PG_FUNCTION_INFO_V1(dbms_alert_set_defaults);
PG_FUNCTION_INFO_V1(dbms_alert_signal);
PG_FUNCTION_INFO_V1(dbms_alert_waitany);
PG_FUNCTION_INFO_V1(dbms_alert_waitone);
PG_FUNCTION_INFO_V1(dbms_alert_defered_signal);
extern unsigned int sid;
float8 sensitivity = 250.0;
extern LWLockId shmem_lock;
#ifndef _GetCurrentTimestamp
#define _GetCurrentTimestamp() GetCurrentTimestamp()
#endif
#ifndef GetNowFloat
#ifdef HAVE_INT64_TIMESTAMP
#define GetNowFloat() ((float8) _GetCurrentTimestamp() / 1000000.0)
#else
#define GetNowFloat() _GetCurrentTimestamp()
#endif
#endif
#define TDAYS (1000*24*3600)
/*
* There are maximum 30 events and 255 collaborating sessions
*
*/
alert_event *events;
alert_lock *locks;
alert_lock *session_lock = NULL;
#define NOT_FOUND -1
#define NOT_USED -1
/*
* Compare text and cstr
*/
static int
textcmpm(text *txt, char *str)
{
int retval;
char *p;
int len;
len = VARSIZE(txt) - VARHDRSZ;
p = VARDATA(txt);
while (len-- && *p != '\0')
{
if (0 != (retval = *p++ - *str++))
return retval;
}
if (len > 0)
return 1;
if (*str != '\0')
return -1;
return 0;
}
/*
* find or create event rec
*
*/
static alert_lock*
find_lock(int sid, bool create)
{
int i;
int first_free = NOT_FOUND;
if (session_lock != NULL)
return session_lock;
for (i = 0; i < MAX_LOCKS; i++)
{
if (locks[i].sid == sid)
return &locks[i];
else if (locks[i].sid == NOT_USED && first_free == NOT_FOUND)
first_free = i;
}
if (create)
{
if (first_free != NOT_FOUND)
{
locks[first_free].sid = sid;
locks[first_free].echo = NULL;
session_lock = &locks[first_free];
return &locks[first_free];
}
else
ereport(ERROR,
(errcode(ERRCODE_ORA_PACKAGES_LOCK_REQUEST_ERROR),
errmsg("lock request error"),
errdetail("Failed to create session lock."),
errhint("There are too many collaborating sessions. Increase MAX_LOCKS in 'pipe.h'.")));
}
return NULL;
}
static alert_event*
find_event(text *event_name, bool create, int *event_id)
{
int i;
for (i = 0; i < MAX_EVENTS;i++)
{
if (events[i].event_name != NULL && textcmpm(event_name,events[i].event_name) == 0)
{
if (event_id != NULL)
*event_id = i;
return &events[i];
}
}
if (create)
{
for (i=0; i < MAX_EVENTS; i++)
{
if (events[i].event_name == NULL)
{
events[i].event_name = ora_scstring(event_name);
events[i].max_receivers = 0;
events[i].receivers = NULL;
events[i].messages = NULL;
events[i].receivers_number = 0;
if (event_id != NULL)
*event_id = i;
return &events[i];
}
}
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("event registeration error"),
errdetail("Too many registered events."),
errhint("There are too many collaborating sessions. Increase MAX_EVENTS in 'pipe.h'.")));
}
return NULL;
}
static void
register_event(text *event_name)
{
alert_event *ev;
int *new_receivers;
int first_free;
int i;
find_lock(sid, true);
ev = find_event(event_name, true, NULL);
first_free = NOT_FOUND;
for (i = 0; i < ev->max_receivers; i++)
{
if (ev->receivers[i] == sid)
return; /* event is registered */
if (ev->receivers[i] == NOT_USED && first_free == NOT_FOUND)
first_free = i;
}
/*
* I can have a maximum of MAX_LOCKS receivers for one event.
* Array receivers is increased for 16 fields
*/
if (first_free == NOT_FOUND)
{
if (ev->max_receivers + 16 > MAX_LOCKS)
ereport(ERROR,
(errcode(ERRCODE_ORA_PACKAGES_LOCK_REQUEST_ERROR),
errmsg("lock request error"),
errdetail("Failed to create session lock."),
errhint("There are too many collaborating sessions. Increase MAX_LOCKS in 'pipe.h'.")));
/* increase receiver's array */
new_receivers = (int*)salloc((ev->max_receivers + 16)*sizeof(int));
for (i = 0; i < ev->max_receivers + 16; i++)
{
if (i < ev->max_receivers)
new_receivers[i] = ev->receivers[i];
else
new_receivers[i] = NOT_USED;
}
ev->max_receivers += 16;
if (ev->receivers)
ora_sfree(ev->receivers);
ev->receivers = new_receivers;
first_free = ev->max_receivers - 16;
}
ev->receivers_number += 1;
ev->receivers[first_free] = sid;
}
/*
* Remove receiver from default receivers of message,
* I expect clean all message_items
*/
static void
unregister_event(int event_id, int sid)
{
alert_event *ev;
int i;
ev = &events[event_id];
if (ev->receivers_number > 0)
{
for (i = 0; i < ev->max_receivers; i++)
{
if (ev->receivers[i] == sid)
{
ev->receivers[i] = NOT_USED;
ev->receivers_number -= 1;
break;
}
}
if (ev->receivers_number == 0)
{
ora_sfree(ev->receivers);
ora_sfree(ev->event_name);
ev->receivers = NULL;
ev->event_name = NULL;
}
}
}
/*
* remove receiver from list of receivers.
* Message has always minimal one receiver
* Return true, if exist other receiver
*/
static bool
remove_receiver(message_item *msg, int sid)
{
int i;
bool find_other = false;
bool found = false;
for (i = 0; i < msg->receivers_number; i++)
{
if (msg->receivers[i] == sid)
{
msg->receivers[i] = NOT_USED;
found = true;
}
else if (msg->receivers[i] != NOT_USED)
{
find_other = true;
}
if (found && find_other)
break;
}
return find_other;
}
/*
*
* Reads message message_id for user sid. If arg:all is true,
* then get any message. If arg:remove_all then remove all
* signaled messages for sid. If arg:filter_message then
* skip other messages than message_id, else read and remove
* all others messages than message_id.
*
*/
static char*
find_and_remove_message_item(int message_id, int sid,
bool all, bool remove_all,
bool filter_message,
int *sleep, char **event_name)
{
alert_lock *alck;
int _message_id;
char *result = NULL;
if (sleep != NULL)
*sleep = 0;
alck = find_lock(sid, false);
if (event_name)
*event_name = NULL;
if (alck != NULL && alck->echo != NULL)
{
/* if I have registered and created item */
struct _message_echo *echo, *last_echo;
echo = alck->echo;
last_echo = NULL;
while (echo != NULL)
{
char *message_text;
bool destroy_msg_item = false;
if (filter_message && echo->message_id != message_id)
{
last_echo = echo;
echo = echo->next_echo;
continue;
}
message_text = echo->message->message;
_message_id = echo->message_id;
if (!remove_receiver(echo->message, sid))
{
destroy_msg_item = true;
if (echo->message->prev_message != NULL)
echo->message->prev_message->next_message =
echo->message->next_message;
else
events[echo->message_id].messages =
echo->message->next_message;
if (echo->message->next_message != NULL)
echo->message->next_message->prev_message =
echo->message->prev_message;
ora_sfree(echo->message->receivers);
ora_sfree(echo->message);
}
if (last_echo == NULL)
{
alck->echo = echo->next_echo;
ora_sfree(echo);
echo = alck->echo;
}
else
{
last_echo->next_echo = echo->next_echo;
ora_sfree(echo);
echo = last_echo;
}
if (remove_all)
{
if (message_text != NULL && destroy_msg_item)
ora_sfree(message_text);
continue;
}
else if (_message_id == message_id || all)
{
/* I have to do local copy */
if (message_text)
{
result = pstrdup(message_text);
if (destroy_msg_item)
ora_sfree(message_text);
}
if (event_name != NULL)
*event_name = pstrdup(events[_message_id].event_name);
break;
}
}
}
return result;
}
/*
* Queue mustn't to contain duplicate messages
*/
static void
create_message(text *event_name, text *message)
{
int event_id;
alert_event *ev;
message_item *msg_item = NULL;
int i,j,k;
find_event(event_name, false, &event_id);
/* process event only when any recipient exitsts */
if (NULL != (ev = find_event(event_name, false, &event_id)))
{
if (ev->receivers_number > 0)
{
msg_item = ev->messages;
while (msg_item != NULL)
{
if (msg_item->message == NULL && message == NULL)
return;
if (msg_item->message != NULL && message != NULL)
if (0 == textcmpm(message,msg_item->message))
return;
msg_item = msg_item->next_message;
}
msg_item = salloc(sizeof(message_item));
msg_item->receivers = salloc( ev->receivers_number*sizeof(int));
msg_item->receivers_number = ev->receivers_number;
if (message != NULL)
msg_item->message = ora_scstring(message);
else
msg_item->message = NULL;
msg_item->message_id = event_id;
for (i = j = 0; j < ev->max_receivers; j++)
{
if (ev->receivers[j] != NOT_USED)
{
msg_item->receivers[i++] = ev->receivers[j];
for (k = 0; k < MAX_LOCKS; k++)
if (locks[k].sid == ev->receivers[j])
{
/* create echo */
message_echo *echo = salloc(sizeof(message_echo));
echo->message = msg_item;
echo->message_id = event_id;
echo->next_echo = NULL;
if (locks[k].echo == NULL)
locks[k].echo = echo;
else
{
message_echo *p;
p = locks[k].echo;
while (p->next_echo != NULL)
p = p->next_echo;
p->next_echo = echo;
}
}
}
}
msg_item->next_message = NULL;
if (ev->messages == NULL)
{
msg_item->prev_message = NULL;
ev->messages = msg_item;
}
else
{
message_item *p;
p = ev->messages;
while (p->next_message != NULL)
p = p->next_message;
p->next_message = msg_item;
msg_item->prev_message = p;
}
}
}
}
#define WATCH_PRE(t, et, c) \
et = GetNowFloat() + (float8)t; c = 0; \
do \
{ \
#define WATCH_POST(t,et,c) \
if (GetNowFloat() >= et) \
break; \
if (cycle++ % 100 == 0) \
CHECK_FOR_INTERRUPTS(); \
pg_usleep(10000L); \
} while(t != 0);
/*
*
* PROCEDURE DBMS_ALERT.REGISTER (name IN VARCHAR2);
*
* Registers the calling session to receive notification of alert name.
*
*/
Datum
dbms_alert_register(PG_FUNCTION_ARGS)
{
text *name = PG_GETARG_TEXT_P(0);
int cycle = 0;
float8 endtime;
float8 timeout = 2;
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
{
register_event(name);
LWLockRelease(shmem_lock);
PG_RETURN_VOID();
}
WATCH_POST(timeout, endtime, cycle);
LOCK_ERROR();
PG_RETURN_VOID();
}
/*
*
* PROCEDURE DBMS_ALERT.REMOVE(name IN VARCHAR2);
*
* Unregisters the calling session from receiving notification of alert name.
* Don't raise any exceptions.
*
*/
Datum
dbms_alert_remove(PG_FUNCTION_ARGS)
{
text *name = PG_GETARG_TEXT_P(0);
alert_event *ev;
int ev_id;
int cycle = 0;
float8 endtime;
float8 timeout = 2;
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES,MAX_EVENTS,MAX_LOCKS,false))
{
ev = find_event(name, false, &ev_id);
if (NULL != ev)
{
find_and_remove_message_item(ev_id, sid,
false, true, true, NULL, NULL);
unregister_event(ev_id, sid);
}
LWLockRelease(shmem_lock);
PG_RETURN_VOID();
}
WATCH_POST(timeout, endtime, cycle);
LOCK_ERROR();
PG_RETURN_VOID();
}
/*
*
* PROCEDURE DBMS_ALERT.REMOVEALL;
*
* Unregisters the calling session from notification of all alerts.
*
*/
Datum
dbms_alert_removeall(PG_FUNCTION_ARGS)
{
int i;
int cycle = 0;
float8 endtime;
float8 timeout = 2;
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES,MAX_EVENTS,MAX_LOCKS,false))
{
for (i = 0; i < MAX_EVENTS; i++)
if (events[i].event_name != NULL)
{
find_and_remove_message_item(i, sid,
false, true, true, NULL, NULL);
unregister_event(i, sid);
}
LWLockRelease(shmem_lock);
PG_RETURN_VOID();
}
WATCH_POST(timeout, endtime, cycle);
LOCK_ERROR();
PG_RETURN_VOID();
}
/*
*
* PROCEDURE DBMS_ALERT.WAITANY(name OUT VARCHAR2 ,message OUT VARCHAR2
* ,status OUT INTEGER
* ,timeout IN NUMBER DEFAULT MAXWAIT);
*
* Waits for up to timeout seconds to be notified of any alerts for which
* the session is registered. If status = 0 then name and message contain
* alert information. If status = 1 then timeout seconds elapsed without
* notification of any alert.
*
*/
Datum
dbms_alert_waitany(PG_FUNCTION_ARGS)
{
float8 timeout;
TupleDesc tupdesc;
AttInMetadata *attinmeta;
HeapTuple tuple;
Datum result;
char *str[3] = {NULL, NULL, "1"};
int cycle = 0;
float8 endtime;
TupleDesc btupdesc;
if (PG_ARGISNULL(0))
timeout = TDAYS;
else
timeout = PG_GETARG_FLOAT8(0);
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
{
str[1] = find_and_remove_message_item(-1, sid,
true, false, false, NULL, &str[0]);
if (str[0])
{
str[2] = "0";
LWLockRelease(shmem_lock);
break;
}
LWLockRelease(shmem_lock);
}
WATCH_POST(timeout, endtime, cycle);
get_call_result_type(fcinfo, NULL, &tupdesc);
btupdesc = BlessTupleDesc(tupdesc);
attinmeta = TupleDescGetAttInMetadata(btupdesc);
tuple = BuildTupleFromCStrings(attinmeta, str);
result = HeapTupleGetDatum(tuple);
if (str[0])
pfree(str[0]);
if (str[1])
pfree(str[1]);
return result;
}
/*
*
* PROCEDURE DBMS_ALERT.WAITONE(name IN VARCHAR2, message OUT VARCHAR2
* ,status OUT INTEGER
* ,timeout IN NUMBER DEFAULT MAXWAIT);
*
* Waits for up to timeout seconds for notification of alert name. If status = 0
* then message contains alert information. If status = 1 then timeout
* seconds elapsed without notification.
*
*/
Datum
dbms_alert_waitone(PG_FUNCTION_ARGS)
{
text *name;
float8 timeout;
TupleDesc tupdesc;
AttInMetadata *attinmeta;
HeapTuple tuple;
Datum result;
int message_id;
char *str[2] = {NULL,"1"};
char *event_name;
int cycle = 0;
float8 endtime;
TupleDesc btupdesc;
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("event name is NULL"),
errdetail("Eventname may not be NULL.")));
if (PG_ARGISNULL(1))
timeout = TDAYS;
else
timeout = PG_GETARG_FLOAT8(1);
name = PG_GETARG_TEXT_P(0);
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
{
if (NULL != find_event(name, false, &message_id))
{
str[0] = find_and_remove_message_item(message_id, sid,
false, false, false, NULL, &event_name);
if (event_name != NULL)
{
str[1] = "0";
pfree(event_name);
LWLockRelease(shmem_lock);
break;
}
}
LWLockRelease(shmem_lock);
}
WATCH_POST(timeout, endtime, cycle);
get_call_result_type(fcinfo, NULL, &tupdesc);
btupdesc = BlessTupleDesc(tupdesc);
attinmeta = TupleDescGetAttInMetadata(btupdesc);
tuple = BuildTupleFromCStrings(attinmeta, str);
result = HeapTupleGetDatum(tuple);
if (str[0])
pfree(str[0]);
return result;
}
/*
*
* PROCEDURE DBMS_ALERT.SET_DEFAULTS(sensitivity IN NUMBER);
*
* The SET_DEFAULTS procedure is used to set session configurable settings
* used by the DBMS_ALERT package. Currently, the polling loop interval sleep time
* is the only session setting that can be modified using this procedure. The
* header for this procedure is,
*
*/
Datum
dbms_alert_set_defaults(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("feature not supported"),
errdetail("Sensitivity isn't supported.")));
PG_RETURN_VOID();
}
/*
* This code was originally in plpgsql
*
*/
/*
CREATE OR REPLACE FUNCTION dbms_alert._defered_signal() RETURNS trigger AS $$
BEGIN
PERFORM dbms_alert._signal(NEW.event, NEW.message);
DELETE FROM ora_alerts WHERE oid=NEW.oid;
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER VOLATILE;
*/
#define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
#define ItemPointerGetDatum(X) PointerGetDatum(X)
Datum
dbms_alert_defered_signal(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
TupleDesc tupdesc;
HeapTuple rettuple;
char *relname;
text *name;
text *message;
int event_col;
int message_col;
Datum datum;
bool isnull;
int cycle = 0;
float8 endtime;
float8 timeout = 2;
if (!CALLED_AS_TRIGGER(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("not called by trigger manager")));
if (!TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("not called on valid event")));
if (SPI_connect() < 0)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("SPI_connect failed")));
if (strcmp((relname = SPI_getrelname(trigdata->tg_relation)), "ora_alerts") != 0)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("not called with valid relation")));
rettuple = trigdata->tg_trigtuple;
tupdesc = trigdata->tg_relation->rd_att;
if (SPI_ERROR_NOATTRIBUTE == (event_col = SPI_fnumber(tupdesc, "event")))
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("attribute event not found")));
if (SPI_ERROR_NOATTRIBUTE == (message_col = SPI_fnumber(tupdesc, "message")))
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("attribute message not found")));
datum = SPI_getbinval(rettuple, tupdesc, event_col, &isnull);
if (isnull)
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("event name is NULL"),
errdetail("Eventname may not be NULL.")));
name = DatumGetTextP(datum);
datum = SPI_getbinval(rettuple, tupdesc, message_col, &isnull);
if (isnull)
message = NULL;
else
message = DatumGetTextP(datum);
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
{
ItemPointer tid;
Oid argtypes[1] = {TIDOID};
char nulls[1] = {' '};
Datum values[1];
void *plan;
create_message(name, message);
LWLockRelease(shmem_lock);
tid = &rettuple->t_data->t_ctid;
if (!(plan = SPI_prepare("DELETE FROM ora_alerts WHERE ctid = $1", 1, argtypes)))
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("SPI_prepare failed")));
values[0] = ItemPointerGetDatum(tid);
if (SPI_OK_DELETE != SPI_execute_plan(plan, values, nulls, false, 1))
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("can't execute sql")));
SPI_finish();
return PointerGetDatum(rettuple);
}
WATCH_POST(timeout, endtime, cycle);
LOCK_ERROR();
PG_RETURN_NULL();
}
/*
*
* PROCEDURE DBMS_ALERT.SIGNAL(name IN VARCHAR2,message IN VARCHAR2);
*
* Signals the occurrence of alert name and attaches message. (Sessions
* registered for alert name are notified only when the signaling transaction
* commits.)
*
*/
/*
CREATE OR REPLACE FUNCTION dbms_alert.signal(_event text, _message text) RETURNS void AS $$
BEGIN
PERFORM 1 FROM pg_catalog.pg_class c
WHERE pg_catalog.pg_table_is_visible(c.oid)
AND c.relkind='r' AND c.relname = 'ora_alerts';
IF NOT FOUND THEN
CREATE TEMP TABLE ora_alerts(event text, message text) WITH OIDS;
REVOKE ALL ON TABLE ora_alerts FROM PUBLIC;
CREATE CONSTRAINT TRIGGER ora_alert_signal AFTER INSERT ON ora_alerts
INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE dbms_alert._defered_signal();
END IF;
INSERT INTO ora_alerts(event, message) VALUES(_event, _message);
END;
$$ LANGUAGE plpgsql VOLATILE SECURITY DEFINER;
*/
#define SPI_EXEC(cmd,_type_) \
if (SPI_OK_##_type_ != SPI_exec(cmd, 1)) \
ereport(ERROR, \
(errcode(ERRCODE_INTERNAL_ERROR), \
errmsg("SPI execute error"), \
errdetail("Can't execute %s.", cmd)));
Datum
dbms_alert_signal(PG_FUNCTION_ARGS)
{
void *plan;
Oid argtypes[] = {TEXTOID, TEXTOID};
Datum values[2];
char nulls[2] = {' ',' '};
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("event name is NULL"),
errdetail("Eventname may not be NULL.")));
if (PG_ARGISNULL(1))
nulls[1] = 'n';
values[0] = PG_GETARG_DATUM(0);
values[1] = PG_GETARG_DATUM(1);
if (SPI_connect() < 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("SPI_connect failed")));
SPI_EXEC("SELECT 1 FROM pg_catalog.pg_class c "
"WHERE pg_catalog.pg_table_is_visible(c.oid) "
"AND c.relkind='r' AND c.relname = 'ora_alerts'", SELECT);
if (0 == SPI_processed)
{
SPI_EXEC("CREATE TEMP TABLE ora_alerts(event text, message text)", UTILITY);
SPI_EXEC("REVOKE ALL ON TABLE ora_alerts FROM PUBLIC", UTILITY);
SPI_EXEC("CREATE CONSTRAINT TRIGGER ora_alert_signal AFTER INSERT ON ora_alerts "
"INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE dbms_alert.defered_signal()", UTILITY);
}
if (!(plan = SPI_prepare(
"INSERT INTO ora_alerts(event,message) VALUES($1, $2)",
2,
argtypes)))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("SPI_prepare failed")));
if (SPI_OK_INSERT != SPI_execute_plan(plan, values, nulls, false, 1))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("can't execute sql")));
SPI_finish();
PG_RETURN_VOID();
}