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 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
|
#include "postgres.h"
#include "access/xact.h"
#include "executor/spi.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
#include "utils/timestamp.h"
#if PG_VERSION_NUM >= 140000
#include "utils/wait_event.h"
#elif PG_VERSION_NUM >= 130000
#include "pgstat.h"
#endif
#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_waitany_maxwait);
PG_FUNCTION_INFO_V1(dbms_alert_waitone_maxwait);
#if PG_VERSION_NUM >= 130000
extern ConditionVariable *alert_cv;
#endif
typedef struct alert_signal_data
{
text *event;
text *message;
struct alert_signal_data *next;
} alert_signal_data;
static MemoryContext local_buf_cxt;
static LocalTransactionId local_buf_lxid = InvalidTransactionId;
static alert_signal_data *signals;
#ifndef _GetCurrentTimestamp
#define _GetCurrentTimestamp() GetCurrentTimestamp()
#endif
#ifndef GetNowFloat
#ifdef HAVE_INT64_TIMESTAMP
#define GetNowFloat() ((float8) _GetCurrentTimestamp() / 1000000.0)
#else
#define GetNowFloat() _GetCurrentTimestamp()
#endif
#endif
/* in sec 1000 days */
#define MAXWAIT 86400000
#if PG_VERSION_NUM >= 170000
#define CURRENT_LXID (MyProc->vxid.lxid)
#else
#define CURRENT_LXID (MyProc->lxid)
#endif
static void unregister_event(int event_id, int sid);
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);
/*
* There are maximum 30 events and 255 collaborating sessions
*
*/
static alert_lock * session_lock = NULL;
#define NOT_FOUND -1
#define NOT_USED -1
/*
* Compare text and cstr
*/
static int
textcmpm(text *txt, char *str)
{
char *p;
int len;
len = VARSIZE(txt) - VARHDRSZ;
p = VARDATA(txt);
while (len-- && *p != '\0')
{
int retval;
if (0 != (retval = *p++ - *str++))
return retval;
}
if (len > 0)
return 1;
if (*str != '\0')
return -1;
return 0;
}
/*
* this function is called when we know so session with sid is not valid
* anymore.
*/
static void
purge_shared_alert_mem()
{
int i;
LWLockAcquire(ProcArrayLock, LW_SHARED);
for (i = 0; i < MAX_LOCKS; i++)
{
PGPROC *proc;
if (locks[i].sid == NOT_USED)
continue;
proc = BackendPidGetProcWithLock(locks[i].pid);
if (proc == NULL)
{
int j;
int invalid_sid = locks[i].sid;
for (j = 0; j < MAX_EVENTS; j++)
{
if (events[j].event_name != NULL)
{
find_and_remove_message_item(j, invalid_sid,
false, true, true, NULL, NULL);
unregister_event(j, invalid_sid);
}
}
locks[i].sid = NOT_USED;
}
}
LWLockRelease(ProcArrayLock);
}
/*
* 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)
{
purge_shared_alert_mem();
for (i = 0; i < MAX_LOCKS; i++)
{
if (locks[i].sid == NOT_USED)
{
first_free = i;
break;
}
}
}
if (first_free != NOT_FOUND)
{
locks[first_free].sid = sid;
locks[first_free].echo = NULL;
locks[first_free].pid = MyProcPid;
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 registration 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;
ev = &events[event_id];
if (ev->receivers_number > 0)
{
int i;
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;
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;
int _message_id;
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;
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)
{
int i,
j,
k;
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_lockid);
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);
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))
{
alert_event *ev;
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_lockid);
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 cycle = 0;
float8 endtime;
float8 timeout = 2;
WATCH_PRE(timeout, endtime, cycle);
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
{
alert_lock *alck;
int i;
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);
}
}
alck = find_lock(sid, false);
if (alck)
{
/* After all events unregistration, an echo field should NULL */
Assert(alck->echo == NULL);
alck->sid = NOT_USED;
session_lock = NULL;
}
LWLockRelease(shmem_lockid);
PG_RETURN_VOID();
}
WATCH_POST(timeout, endtime, cycle);
LOCK_ERROR();
PG_RETURN_VOID();
}
/*
* workhorse for dbms_alert_waitany and dbms_alert_waitany_maxwait
*/
static Datum
_dbms_alert_waitany(int timeout, FunctionCallInfo fcinfo)
{
TupleDesc tupdesc;
AttInMetadata *attinmeta;
HeapTuple tuple;
Datum result;
char *str[3] = {NULL, NULL, "1"};
instr_time start_time;
TupleDesc btupdesc;
#if PG_VERSION_NUM < 130000
long cycle = 0;
#endif
INSTR_TIME_SET_CURRENT(start_time);
for (;;)
{
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_lockid);
break;
}
LWLockRelease(shmem_lockid);
}
if (timeout > 0)
{
instr_time cur_time;
long cur_timeout;
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout * 1000L - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
break;
#if PG_VERSION_NUM >= 130000
if (cur_timeout > 1000)
cur_timeout = 1000;
if (ConditionVariableTimedSleep(alert_cv, cur_timeout, PG_WAIT_EXTENSION))
{
/* exit on timeout */
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout * 1000L - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
break;
}
#else
if (cycle++ % 10)
CHECK_FOR_INTERRUPTS();
pg_usleep(10000L);
/* exit on timeout */
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout * 1000L - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
break;
#endif
}
else
break;
}
#if PG_VERSION_NUM >= 130000
ConditionVariableCancelSleep();
#endif
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.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)
{
int timeout;
if (!PG_ARGISNULL(0))
{
/*
* cannot to change SQL API now, so use not well choosed float.
*/
timeout = (int) PG_GETARG_FLOAT8(0);
if (timeout < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("negative timeout is not allowed")));
if (timeout > MAXWAIT)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timeout is too large (maximum: %d)", MAXWAIT)));
}
else
timeout = MAXWAIT;
return _dbms_alert_waitany(timeout, fcinfo);
}
Datum
dbms_alert_waitany_maxwait(PG_FUNCTION_ARGS)
{
return _dbms_alert_waitany(MAXWAIT, fcinfo);
}
/*
* common part of dbms_alert_waitone and dbms_alert_waitone_maxwait
*/
static Datum
_dbms_alert_waitone(text *name, int timeout, FunctionCallInfo fcinfo)
{
TupleDesc tupdesc;
AttInMetadata *attinmeta;
HeapTuple tuple;
Datum result;
int message_id;
char *str[2] = {NULL, "1"};
char *event_name;
instr_time start_time;
TupleDesc btupdesc;
#if PG_VERSION_NUM < 130000
long cycle = 0;
#endif
INSTR_TIME_SET_CURRENT(start_time);
for (;;)
{
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_lockid);
break;
}
}
LWLockRelease(shmem_lockid);
}
if (timeout > 0)
{
instr_time cur_time;
long cur_timeout;
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout * 1000L - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
break;
#if PG_VERSION_NUM >= 130000
if (cur_timeout > 1000)
cur_timeout = 1000;
if (ConditionVariableTimedSleep(alert_cv, cur_timeout, PG_WAIT_EXTENSION))
{
/* exit on timeout */
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout * 1000L - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
break;
}
#else
if (cycle++ % 10)
CHECK_FOR_INTERRUPTS();
pg_usleep(10000L);
/* exit on timeout */
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout * 1000L - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
break;
#endif
}
else
break;
}
#if PG_VERSION_NUM >= 130000
ConditionVariableCancelSleep();
#endif
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.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;
int timeout;
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))
{
/*
* cannot to change SQL API now, so use not well choosed float.
*/
timeout = (int) PG_GETARG_FLOAT8(1);
if (timeout < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("negative timeout is not allowed")));
if (timeout > MAXWAIT)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timeout is too large (maximum: %d)", MAXWAIT)));
}
else
timeout = MAXWAIT;
name = PG_GETARG_TEXT_P(0);
return _dbms_alert_waitone(name, timeout, fcinfo);
}
Datum
dbms_alert_waitone_maxwait(PG_FUNCTION_ARGS)
{
text *name;
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("event name is NULL"),
errdetail("Eventname may not be NULL.")));
name = PG_GETARG_TEXT_P(0);
return _dbms_alert_waitone(name, MAXWAIT, fcinfo);
}
/*
*
* 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();
}
void
orafce_xact_cb(XactEvent event, void *arg)
{
if (event == XACT_EVENT_PRE_COMMIT)
{
/*
* In this time we have valid MyProc->lxid, in ACT_EVENT_COMMIT is
* MyProc->lxid already invalided. So we need to invalid pointer to
* obsolete buffer here.
*/
if (local_buf_lxid != CURRENT_LXID)
signals = NULL;
}
else if (event == XACT_EVENT_COMMIT && signals)
{
if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
{
alert_signal_data *signal = signals;
while (signal)
{
create_message(signal->event, signal->message);
signal = signal->next;
}
signals = NULL;
LWLockRelease(shmem_lockid);
#if PG_VERSION_NUM >= 130000
ConditionVariableBroadcast(alert_cv);
#endif
}
}
}
static bool
text_eq(const text *p1, const text *p2)
{
int len1,
len2;
Assert(p1 && p2);
len1 = VARSIZE_ANY_EXHDR(p1);
len2 = VARSIZE_ANY_EXHDR(p2);
if (len1 == len2)
{
return memcmp(VARDATA_ANY(p1), VARDATA_ANY(p2), len1) == 0;
}
else
return false;
}
/*
*
* 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.)
*
*/
Datum
dbms_alert_signal(PG_FUNCTION_ARGS)
{
text *event;
text *message;
alert_signal_data *new_signal;
alert_signal_data *last_signal = NULL;
MemoryContext oldcxt;
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("event name is NULL"),
errdetail("Eventname may not be NULL.")));
event = PG_GETARG_TEXT_P(0);
message = (!PG_ARGISNULL(1)) ? PG_GETARG_TEXT_P(1) : NULL;
if (local_buf_lxid != CURRENT_LXID)
{
local_buf_cxt = AllocSetContextCreate(TopTransactionContext,
"dbms_alert local buffer",
ALLOCSET_START_SMALL_SIZES);
local_buf_lxid = CURRENT_LXID;
signals = NULL;
last_signal = NULL;
}
if (signals)
{
alert_signal_data *s = signals;
while (s)
{
last_signal = s;
if (text_eq(s->event, event) == 0)
{
if (!message && !s->message)
PG_RETURN_VOID();
if (message && s->message &&
text_eq(message, s->message) == 0)
PG_RETURN_VOID();
}
s = s->next;
}
}
oldcxt = MemoryContextSwitchTo(local_buf_cxt);
new_signal = palloc(sizeof(alert_signal_data));
new_signal->event = TextPCopy(event);
new_signal->message = message ? TextPCopy(message) : NULL;
new_signal->next = NULL;
MemoryContextSwitchTo(oldcxt);
if (signals)
last_signal->next = new_signal;
else
signals = new_signal;
PG_RETURN_VOID();
}
/*
* removed by Orafce 9.6, header is necessary to allow upgrade
*
*/
Datum
dbms_alert_defered_signal(PG_FUNCTION_ARGS)
{
PG_RETURN_NULL();
}
|