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 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
|
/*--------------------------------------------------------------------
* Symbols referenced in this file:
* - errstart_cold
* - errstart
* - PG_exception_stack
* - write_stderr
* - in_error_recursion_trouble
* - error_context_stack
* - errordata_stack_depth
* - errordata
* - should_output_to_server
* - is_log_level_output
* - should_output_to_client
* - recursion_depth
* - get_error_stack_entry
* - set_stack_entry_domain
* - errmsg_internal
* - errcode
* - errmsg
* - errdetail
* - errhidestmt
* - errhidecontext
* - errfinish
* - pg_re_throw
* - EmitErrorReport
* - emit_log_hook
* - send_message_to_server_log
* - send_message_to_frontend
* - pgwin32_dispatch_queued_signals
* - set_stack_entry_location
* - matches_backtrace_functions
* - backtrace_symbol_list
* - set_backtrace
* - FreeErrorDataContents
* - geterrcode
* - errsave_start
* - errsave_finish
* - errhint
* - errposition
* - internalerrposition
* - internalerrquery
* - geterrposition
* - getinternalerrposition
* - set_errcontext_domain
* - errcontext_msg
* - CopyErrorData
* - FlushErrorState
* - pg_signal_queue
* - pg_signal_mask
* - pgwin32_dispatch_queued_signals
*--------------------------------------------------------------------
*/
/*-------------------------------------------------------------------------
*
* elog.c
* error logging and reporting
*
* Because of the extremely high rate at which log messages can be generated,
* we need to be mindful of the performance cost of obtaining any information
* that may be logged. Also, it's important to keep in mind that this code may
* get called from within an aborted transaction, in which case operations
* such as syscache lookups are unsafe.
*
* Some notes about recursion and errors during error processing:
*
* We need to be robust about recursive-error scenarios --- for example,
* if we run out of memory, it's important to be able to report that fact.
* There are a number of considerations that go into this.
*
* First, distinguish between re-entrant use and actual recursion. It
* is possible for an error or warning message to be emitted while the
* parameters for an error message are being computed. In this case
* errstart has been called for the outer message, and some field values
* may have already been saved, but we are not actually recursing. We handle
* this by providing a (small) stack of ErrorData records. The inner message
* can be computed and sent without disturbing the state of the outer message.
* (If the inner message is actually an error, this isn't very interesting
* because control won't come back to the outer message generator ... but
* if the inner message is only debug or log data, this is critical.)
*
* Second, actual recursion will occur if an error is reported by one of
* the elog.c routines or something they call. By far the most probable
* scenario of this sort is "out of memory"; and it's also the nastiest
* to handle because we'd likely also run out of memory while trying to
* report this error! Our escape hatch for this case is to reset the
* ErrorContext to empty before trying to process the inner error. Since
* ErrorContext is guaranteed to have at least 8K of space in it (see mcxt.c),
* we should be able to process an "out of memory" message successfully.
* Since we lose the prior error state due to the reset, we won't be able
* to return to processing the original error, but we wouldn't have anyway.
* (NOTE: the escape hatch is not used for recursive situations where the
* inner message is of less than ERROR severity; in that case we just
* try to process it and return normally. Usually this will work, but if
* it ends up in infinite recursion, we will PANIC due to error stack
* overflow.)
*
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/utils/error/elog.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#include "access/transam.h"
#include "access/xact.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "nodes/miscnodes.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/bgworker.h"
#include "postmaster/postmaster.h"
#include "postmaster/syslogger.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/guc_hooks.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/varlena.h"
/* In this module, access gettext() via err_gettext() */
#undef _
#define _(x) err_gettext(x)
/* Global variables */
__thread ErrorContextCallback *error_context_stack = NULL;
__thread sigjmp_buf *PG_exception_stack = NULL;
extern bool redirection_done;
/*
* Hook for intercepting messages before they are sent to the server log.
* Note that the hook will not get called for messages that are suppressed
* by log_min_messages. Also note that logging hooks implemented in preload
* libraries will miss any log messages that are generated before the
* library is loaded.
*/
__thread emit_log_hook_type emit_log_hook = NULL;
/* GUC parameters */
/* format for extra log line info */
/* Processed form of backtrace_symbols GUC */
static __thread char *backtrace_symbol_list;
#ifdef HAVE_SYSLOG
/*
* Max string length to send to syslog(). Note that this doesn't count the
* sequence-number prefix we add, and of course it doesn't count the prefix
* added by syslog itself. Solaris and sysklogd truncate the final message
* at 1024 bytes, so this value leaves 124 bytes for those prefixes. (Most
* other syslog implementations seem to have limits of 2KB or so.)
*/
#ifndef PG_SYSLOG_LIMIT
#define PG_SYSLOG_LIMIT 900
#endif
static void write_syslog(int level, const char *line);
#endif
#ifdef WIN32
extern char *event_source;
static void write_eventlog(int level, const char *line, int len);
#endif
/* We provide a small stack of ErrorData records for re-entrant cases */
#define ERRORDATA_STACK_SIZE 5
static __thread ErrorData errordata[ERRORDATA_STACK_SIZE];
static __thread int errordata_stack_depth = -1;
/* index of topmost active frame */
static __thread int recursion_depth = 0;
/* to detect actual recursion */
/*
* Saved timeval and buffers for formatted timestamps that might be used by
* both log_line_prefix and csv logs.
*/
#define FORMATTED_TS_LEN 128
/* Macro for checking errordata_stack_depth is reasonable */
#define CHECK_STACK_DEPTH() \
do { \
if (errordata_stack_depth < 0) \
{ \
errordata_stack_depth = -1; \
ereport(ERROR, (errmsg_internal("errstart was not called"))); \
} \
} while (0)
static const char *err_gettext(const char *str) pg_attribute_format_arg(1);
static ErrorData *get_error_stack_entry(void);
static void set_stack_entry_domain(ErrorData *edata, const char *domain);
static void set_stack_entry_location(ErrorData *edata,
const char *filename, int lineno,
const char *funcname);
static bool matches_backtrace_functions(const char *funcname);
static pg_noinline void set_backtrace(ErrorData *edata, int num_skip);
static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str);
static void FreeErrorDataContents(ErrorData *edata);
static void write_console(const char *line, int len);
static const char *process_log_prefix_padding(const char *p, int *ppadding);
static void log_line_prefix(StringInfo buf, ErrorData *edata);
static void send_message_to_server_log(ErrorData *edata);
static void send_message_to_frontend(ErrorData *edata);
static void append_with_tabs(StringInfo buf, const char *str);
/*
* is_log_level_output -- is elevel logically >= log_min_level?
*
* We use this for tests that should consider LOG to sort out-of-order,
* between ERROR and FATAL. Generally this is the right thing for testing
* whether a message should go to the postmaster log, whereas a simple >=
* test is correct for testing whether the message should go to the client.
*/
static inline bool
is_log_level_output(int elevel, int log_min_level)
{
if (elevel == LOG || elevel == LOG_SERVER_ONLY)
{
if (log_min_level == LOG || log_min_level <= ERROR)
return true;
}
else if (elevel == WARNING_CLIENT_ONLY)
{
/* never sent to log, regardless of log_min_level */
return false;
}
else if (log_min_level == LOG)
{
/* elevel != LOG */
if (elevel >= FATAL)
return true;
}
/* Neither is LOG */
else if (elevel >= log_min_level)
return true;
return false;
}
/*
* Policy-setting subroutines. These are fairly simple, but it seems wise
* to have the code in just one place.
*/
/*
* should_output_to_server --- should message of given elevel go to the log?
*/
static inline bool
should_output_to_server(int elevel)
{
return is_log_level_output(elevel, log_min_messages);
}
/*
* should_output_to_client --- should message of given elevel go to the client?
*/
static inline bool should_output_to_client(int elevel) { return false; }
/*
* message_level_is_interesting --- would ereport/elog do anything?
*
* Returns true if ereport/elog with this elevel will not be a no-op.
* This is useful to short-circuit any expensive preparatory work that
* might be needed for a logging message. There is no point in
* prepending this to a bare ereport/elog call, however.
*/
/*
* in_error_recursion_trouble --- are we at risk of infinite error recursion?
*
* This function exists to provide common control of various fallback steps
* that we take if we think we are facing infinite error recursion. See the
* callers for details.
*/
bool
in_error_recursion_trouble(void)
{
/* Pull the plug if recurse more than once */
return (recursion_depth > 2);
}
/*
* One of those fallback steps is to stop trying to localize the error
* message, since there's a significant probability that that's exactly
* what's causing the recursion.
*/
#ifdef ENABLE_NLS
#else
#endif
/*
* errstart_cold
* A simple wrapper around errstart, but hinted to be "cold". Supporting
* compilers are more likely to move code for branches containing this
* function into an area away from the calling function's code. This can
* result in more commonly executed code being more compact and fitting
* on fewer cache lines.
*/
pg_attribute_cold bool
errstart_cold(int elevel, const char *domain)
{
return errstart(elevel, domain);
}
/*
* errstart --- begin an error-reporting cycle
*
* Create and initialize error stack entry. Subsequently, errmsg() and
* perhaps other routines will be called to further populate the stack entry.
* Finally, errfinish() will be called to actually process the error report.
*
* Returns true in normal case. Returns false to short-circuit the error
* report (if it's a warning or lower and not to be reported anywhere).
*/
bool
errstart(int elevel, const char *domain)
{
ErrorData *edata;
bool output_to_server;
bool output_to_client = false;
int i;
/*
* Check some cases in which we want to promote an error into a more
* severe error. None of this logic applies for non-error messages.
*/
if (elevel >= ERROR)
{
/*
* If we are inside a critical section, all errors become PANIC
* errors. See miscadmin.h.
*/
if (CritSectionCount > 0)
elevel = PANIC;
/*
* Check reasons for treating ERROR as FATAL:
*
* 1. we have no handler to pass the error to (implies we are in the
* postmaster or in backend startup).
*
* 2. ExitOnAnyError mode switch is set (initdb uses this).
*
* 3. the error occurred after proc_exit has begun to run. (It's
* proc_exit's responsibility to see that this doesn't turn into
* infinite recursion!)
*/
if (elevel == ERROR)
{
if (PG_exception_stack == NULL ||
ExitOnAnyError ||
proc_exit_inprogress)
elevel = FATAL;
}
/*
* If the error level is ERROR or more, errfinish is not going to
* return to caller; therefore, if there is any stacked error already
* in progress it will be lost. This is more or less okay, except we
* do not want to have a FATAL or PANIC error downgraded because the
* reporting process was interrupted by a lower-grade error. So check
* the stack and make sure we panic if panic is warranted.
*/
for (i = 0; i <= errordata_stack_depth; i++)
elevel = Max(elevel, errordata[i].elevel);
}
/*
* Now decide whether we need to process this report at all; if it's
* warning or less and not enabled for logging, just return false without
* starting up any error logging machinery.
*/
output_to_server = should_output_to_server(elevel);
output_to_client = should_output_to_client(elevel);
if (elevel < ERROR && !output_to_server && !output_to_client)
return false;
/*
* We need to do some actual work. Make sure that memory context
* initialization has finished, else we can't do anything useful.
*/
if (ErrorContext == NULL)
{
/* Oops, hard crash time; very little we can do safely here */
write_stderr("error occurred before error message processing is available\n");
exit(2);
}
/*
* Okay, crank up a stack entry to store the info in.
*/
if (recursion_depth++ > 0 && elevel >= ERROR)
{
/*
* Oops, error during error processing. Clear ErrorContext as
* discussed at top of file. We will not return to the original
* error's reporter or handler, so we don't need it.
*/
MemoryContextReset(ErrorContext);
/*
* Infinite error recursion might be due to something broken in a
* context traceback routine. Abandon them too. We also abandon
* attempting to print the error statement (which, if long, could
* itself be the source of the recursive failure).
*/
if (in_error_recursion_trouble())
{
error_context_stack = NULL;
debug_query_string = NULL;
}
}
/* Initialize data for this error frame */
edata = get_error_stack_entry();
edata->elevel = elevel;
edata->output_to_server = output_to_server;
edata->output_to_client = output_to_client;
set_stack_entry_domain(edata, domain);
/* Select default errcode based on elevel */
if (elevel >= ERROR)
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
else if (elevel >= WARNING)
edata->sqlerrcode = ERRCODE_WARNING;
else
edata->sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION;
/*
* Any allocations for this error state level should go into ErrorContext
*/
edata->assoc_context = ErrorContext;
recursion_depth--;
return true;
}
/*
* errfinish --- end an error-reporting cycle
*
* Produce the appropriate error report(s) and pop the error stack.
*
* If elevel, as passed to errstart(), is ERROR or worse, control does not
* return to the caller. See elog.h for the error level definitions.
*/
void
errfinish(const char *filename, int lineno, const char *funcname)
{
ErrorData *edata = &errordata[errordata_stack_depth];
int elevel;
MemoryContext oldcontext;
ErrorContextCallback *econtext;
recursion_depth++;
CHECK_STACK_DEPTH();
/* Save the last few bits of error state into the stack entry */
set_stack_entry_location(edata, filename, lineno, funcname);
elevel = edata->elevel;
/*
* Do processing in ErrorContext, which we hope has enough reserved space
* to report an error.
*/
oldcontext = MemoryContextSwitchTo(ErrorContext);
/* Collect backtrace, if enabled and we didn't already */
if (!edata->backtrace &&
edata->funcname &&
backtrace_functions &&
matches_backtrace_functions(edata->funcname))
set_backtrace(edata, 2);
/*
* Call any context callback functions. Errors occurring in callback
* functions will be treated as recursive errors --- this ensures we will
* avoid infinite recursion (see errstart).
*/
for (econtext = error_context_stack;
econtext != NULL;
econtext = econtext->previous)
econtext->callback(econtext->arg);
/*
* If ERROR (not more nor less) we pass it off to the current handler.
* Printing it and popping the stack is the responsibility of the handler.
*/
if (elevel == ERROR)
{
/*
* We do some minimal cleanup before longjmp'ing so that handlers can
* execute in a reasonably sane state.
*
* Reset InterruptHoldoffCount in case we ereport'd from inside an
* interrupt holdoff section. (We assume here that no handler will
* itself be inside a holdoff section. If necessary, such a handler
* could save and restore InterruptHoldoffCount for itself, but this
* should make life easier for most.)
*/
InterruptHoldoffCount = 0;
QueryCancelHoldoffCount = 0;
CritSectionCount = 0; /* should be unnecessary, but... */
/*
* Note that we leave CurrentMemoryContext set to ErrorContext. The
* handler should reset it to something else soon.
*/
recursion_depth--;
PG_RE_THROW();
}
/* Emit the message to the right places */
EmitErrorReport();
/* Now free up subsidiary data attached to stack entry, and release it */
FreeErrorDataContents(edata);
errordata_stack_depth--;
/* Exit error-handling context */
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
/*
* Perform error recovery action as specified by elevel.
*/
if (elevel == FATAL)
{
/*
* For a FATAL error, we let proc_exit clean up and exit.
*
* If we just reported a startup failure, the client will disconnect
* on receiving it, so don't send any more to the client.
*/
if (PG_exception_stack == NULL && whereToSendOutput == DestRemote)
whereToSendOutput = DestNone;
/*
* fflush here is just to improve the odds that we get to see the
* error message, in case things are so hosed that proc_exit crashes.
* Any other code you might be tempted to add here should probably be
* in an on_proc_exit or on_shmem_exit callback instead.
*/
fflush(NULL);
/*
* Let the cumulative stats system know. Only mark the session as
* terminated by fatal error if there is no other known cause.
*/
if (pgStatSessionEndCause == DISCONNECT_NORMAL)
pgStatSessionEndCause = DISCONNECT_FATAL;
/*
* Do normal process-exit cleanup, then return exit code 1 to indicate
* FATAL termination. The postmaster may or may not consider this
* worthy of panic, depending on which subprocess returns it.
*/
proc_exit(1);
}
if (elevel >= PANIC)
{
/*
* Serious crash time. Postmaster will observe SIGABRT process exit
* status and kill the other backends too.
*
* XXX: what if we are *in* the postmaster? abort() won't kill our
* children...
*/
fflush(NULL);
abort();
}
/*
* Check for cancel/die interrupt first --- this is so that the user can
* stop a query emitting tons of notice or warning messages, even if it's
* in a loop that otherwise fails to check for interrupts.
*/
CHECK_FOR_INTERRUPTS();
}
/*
* errsave_start --- begin a "soft" error-reporting cycle
*
* If "context" isn't an ErrorSaveContext node, this behaves as
* errstart(ERROR, domain), and the errsave() macro ends up acting
* exactly like ereport(ERROR, ...).
*
* If "context" is an ErrorSaveContext node, but the node creator only wants
* notification of the fact of a soft error without any details, we just set
* the error_occurred flag in the ErrorSaveContext node and return false,
* which will cause us to skip the remaining error processing steps.
*
* Otherwise, create and initialize error stack entry and return true.
* Subsequently, errmsg() and perhaps other routines will be called to further
* populate the stack entry. Finally, errsave_finish() will be called to
* tidy up.
*/
bool
errsave_start(struct Node *context, const char *domain)
{
ErrorSaveContext *escontext;
ErrorData *edata;
/*
* Do we have a context for soft error reporting? If not, just punt to
* errstart().
*/
if (context == NULL || !IsA(context, ErrorSaveContext))
return errstart(ERROR, domain);
/* Report that a soft error was detected */
escontext = (ErrorSaveContext *) context;
escontext->error_occurred = true;
/* Nothing else to do if caller wants no further details */
if (!escontext->details_wanted)
return false;
/*
* Okay, crank up a stack entry to store the info in.
*/
recursion_depth++;
/* Initialize data for this error frame */
edata = get_error_stack_entry();
edata->elevel = LOG; /* signal all is well to errsave_finish */
set_stack_entry_domain(edata, domain);
/* Select default errcode based on the assumed elevel of ERROR */
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
/*
* Any allocations for this error state level should go into the caller's
* context. We don't need to pollute ErrorContext, or even require it to
* exist, in this code path.
*/
edata->assoc_context = CurrentMemoryContext;
recursion_depth--;
return true;
}
/*
* errsave_finish --- end a "soft" error-reporting cycle
*
* If errsave_start() decided this was a regular error, behave as
* errfinish(). Otherwise, package up the error details and save
* them in the ErrorSaveContext node.
*/
void
errsave_finish(struct Node *context, const char *filename, int lineno,
const char *funcname)
{
ErrorSaveContext *escontext = (ErrorSaveContext *) context;
ErrorData *edata = &errordata[errordata_stack_depth];
/* verify stack depth before accessing *edata */
CHECK_STACK_DEPTH();
/*
* If errsave_start punted to errstart, then elevel will be ERROR or
* perhaps even PANIC. Punt likewise to errfinish.
*/
if (edata->elevel >= ERROR)
{
errfinish(filename, lineno, funcname);
pg_unreachable();
}
/*
* Else, we should package up the stack entry contents and deliver them to
* the caller.
*/
recursion_depth++;
/* Save the last few bits of error state into the stack entry */
set_stack_entry_location(edata, filename, lineno, funcname);
/* Replace the LOG value that errsave_start inserted */
edata->elevel = ERROR;
/*
* We skip calling backtrace and context functions, which are more likely
* to cause trouble than provide useful context; they might act on the
* assumption that a transaction abort is about to occur.
*/
/*
* Make a copy of the error info for the caller. All the subsidiary
* strings are already in the caller's context, so it's sufficient to
* flat-copy the stack entry.
*/
escontext->error_data = palloc_object(ErrorData);
memcpy(escontext->error_data, edata, sizeof(ErrorData));
/* Exit error-handling context */
errordata_stack_depth--;
recursion_depth--;
}
/*
* get_error_stack_entry --- allocate and initialize a new stack entry
*
* The entry should be freed, when we're done with it, by calling
* FreeErrorDataContents() and then decrementing errordata_stack_depth.
*
* Returning the entry's address is just a notational convenience,
* since it had better be errordata[errordata_stack_depth].
*
* Although the error stack is not large, we don't expect to run out of space.
* Using more than one entry implies a new error report during error recovery,
* which is possible but already suggests we're in trouble. If we exhaust the
* stack, almost certainly we are in an infinite loop of errors during error
* recovery, so we give up and PANIC.
*
* (Note that this is distinct from the recursion_depth checks, which
* guard against recursion while handling a single stack entry.)
*/
static ErrorData *
get_error_stack_entry(void)
{
ErrorData *edata;
/* Allocate error frame */
errordata_stack_depth++;
if (unlikely(errordata_stack_depth >= ERRORDATA_STACK_SIZE))
{
/* Wups, stack not big enough */
errordata_stack_depth = -1; /* make room on stack */
ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
}
/* Initialize error frame to all zeroes/NULLs */
edata = &errordata[errordata_stack_depth];
memset(edata, 0, sizeof(ErrorData));
/* Save errno immediately to ensure error parameter eval can't change it */
edata->saved_errno = errno;
return edata;
}
/*
* set_stack_entry_domain --- fill in the internationalization domain
*/
static void
set_stack_entry_domain(ErrorData *edata, const char *domain)
{
/* the default text domain is the backend's */
edata->domain = domain ? domain : PG_TEXTDOMAIN("postgres");
/* initialize context_domain the same way (see set_errcontext_domain()) */
edata->context_domain = edata->domain;
}
/*
* set_stack_entry_location --- fill in code-location details
*
* Store the values of __FILE__, __LINE__, and __func__ from the call site.
* We make an effort to normalize __FILE__, since compilers are inconsistent
* about how much of the path they'll include, and we'd prefer that the
* behavior not depend on that (especially, that it not vary with build path).
*/
static void
set_stack_entry_location(ErrorData *edata,
const char *filename, int lineno,
const char *funcname)
{
if (filename)
{
const char *slash;
/* keep only base name, useful especially for vpath builds */
slash = strrchr(filename, '/');
if (slash)
filename = slash + 1;
/* Some Windows compilers use backslashes in __FILE__ strings */
slash = strrchr(filename, '\\');
if (slash)
filename = slash + 1;
}
edata->filename = filename;
edata->lineno = lineno;
edata->funcname = funcname;
}
/*
* matches_backtrace_functions --- checks whether the given funcname matches
* backtrace_functions
*
* See check_backtrace_functions.
*/
static bool
matches_backtrace_functions(const char *funcname)
{
const char *p;
if (!backtrace_symbol_list || funcname == NULL || funcname[0] == '\0')
return false;
p = backtrace_symbol_list;
for (;;)
{
if (*p == '\0') /* end of backtrace_symbol_list */
break;
if (strcmp(funcname, p) == 0)
return true;
p += strlen(p) + 1;
}
return false;
}
/*
* errcode --- add SQLSTATE error code to the current error
*
* The code is expected to be represented as per MAKE_SQLSTATE().
*/
int
errcode(int sqlerrcode)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
edata->sqlerrcode = sqlerrcode;
return 0; /* return value does not matter */
}
/*
* errcode_for_file_access --- add SQLSTATE error code to the current error
*
* The SQLSTATE code is chosen based on the saved errno value. We assume
* that the failing operation was some type of disk file access.
*
* NOTE: the primary error message string should generally include %m
* when this is used.
*/
#ifdef EROFS
#endif
#if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
#endif
/*
* errcode_for_socket_access --- add SQLSTATE error code to the current error
*
* The SQLSTATE code is chosen based on the saved errno value. We assume
* that the failing operation was some type of socket access.
*
* NOTE: the primary error message string should generally include %m
* when this is used.
*/
/*
* This macro handles expansion of a format string and associated parameters;
* it's common code for errmsg(), errdetail(), etc. Must be called inside
* a routine that is declared like "const char *fmt, ..." and has an edata
* pointer set up. The message is assigned to edata->targetfield, or
* appended to it if appendval is true. The message is subject to translation
* if translateit is true.
*
* Note: we pstrdup the buffer rather than just transferring its storage
* to the edata field because the buffer might be considerably larger than
* really necessary.
*/
#define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \
{ \
StringInfoData buf; \
/* Internationalize the error format string */ \
if ((translateit) && !in_error_recursion_trouble()) \
fmt = dgettext((domain), fmt); \
initStringInfo(&buf); \
if ((appendval) && edata->targetfield) { \
appendStringInfoString(&buf, edata->targetfield); \
appendStringInfoChar(&buf, '\n'); \
} \
/* Generate actual output --- have to use appendStringInfoVA */ \
for (;;) \
{ \
va_list args; \
int needed; \
errno = edata->saved_errno; \
va_start(args, fmt); \
needed = appendStringInfoVA(&buf, fmt, args); \
va_end(args); \
if (needed == 0) \
break; \
enlargeStringInfo(&buf, needed); \
} \
/* Save the completed message into the stack item */ \
if (edata->targetfield) \
pfree(edata->targetfield); \
edata->targetfield = pstrdup(buf.data); \
pfree(buf.data); \
}
/*
* Same as above, except for pluralized error messages. The calling routine
* must be declared like "const char *fmt_singular, const char *fmt_plural,
* unsigned long n, ...". Translation is assumed always wanted.
*/
#define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \
{ \
const char *fmt; \
StringInfoData buf; \
/* Internationalize the error format string */ \
if (!in_error_recursion_trouble()) \
fmt = dngettext((domain), fmt_singular, fmt_plural, n); \
else \
fmt = (n == 1 ? fmt_singular : fmt_plural); \
initStringInfo(&buf); \
if ((appendval) && edata->targetfield) { \
appendStringInfoString(&buf, edata->targetfield); \
appendStringInfoChar(&buf, '\n'); \
} \
/* Generate actual output --- have to use appendStringInfoVA */ \
for (;;) \
{ \
va_list args; \
int needed; \
errno = edata->saved_errno; \
va_start(args, n); \
needed = appendStringInfoVA(&buf, fmt, args); \
va_end(args); \
if (needed == 0) \
break; \
enlargeStringInfo(&buf, needed); \
} \
/* Save the completed message into the stack item */ \
if (edata->targetfield) \
pfree(edata->targetfield); \
edata->targetfield = pstrdup(buf.data); \
pfree(buf.data); \
}
/*
* errmsg --- add a primary error message text to the current error
*
* In addition to the usual %-escapes recognized by printf, "%m" in
* fmt is replaced by the error message for the caller's value of errno.
*
* Note: no newline is needed at the end of the fmt string, since
* ereport will provide one for the output methods that need it.
*/
int
errmsg(const char *fmt,...)
{
ErrorData *edata = &errordata[errordata_stack_depth];
MemoryContext oldcontext;
recursion_depth++;
CHECK_STACK_DEPTH();
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
edata->message_id = fmt;
EVALUATE_MESSAGE(edata->domain, message, false, true);
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
return 0; /* return value does not matter */
}
/*
* Add a backtrace to the containing ereport() call. This is intended to be
* added temporarily during debugging.
*/
/*
* Compute backtrace data and add it to the supplied ErrorData. num_skip
* specifies how many inner frames to skip. Use this to avoid showing the
* internal backtrace support functions in the backtrace. This requires that
* this and related functions are not inlined.
*/
static void
set_backtrace(ErrorData *edata, int num_skip)
{
StringInfoData errtrace;
initStringInfo(&errtrace);
#ifdef HAVE_BACKTRACE_SYMBOLS
{
void *buf[100];
int nframes;
char **strfrms;
nframes = backtrace(buf, lengthof(buf));
strfrms = backtrace_symbols(buf, nframes);
if (strfrms == NULL)
return;
for (int i = num_skip; i < nframes; i++)
appendStringInfo(&errtrace, "\n%s", strfrms[i]);
free(strfrms);
}
#else
appendStringInfoString(&errtrace,
"backtrace generation is not supported by this installation");
#endif
edata->backtrace = errtrace.data;
}
/*
* errmsg_internal --- add a primary error message text to the current error
*
* This is exactly like errmsg() except that strings passed to errmsg_internal
* are not translated, and are customarily left out of the
* internationalization message dictionary. This should be used for "can't
* happen" cases that are probably not worth spending translation effort on.
* We also use this for certain cases where we *must* not try to translate
* the message because the translation would fail and result in infinite
* error recursion.
*/
int
errmsg_internal(const char *fmt,...)
{
ErrorData *edata = &errordata[errordata_stack_depth];
MemoryContext oldcontext;
recursion_depth++;
CHECK_STACK_DEPTH();
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
edata->message_id = fmt;
EVALUATE_MESSAGE(edata->domain, message, false, false);
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
return 0; /* return value does not matter */
}
/*
* errmsg_plural --- add a primary error message text to the current error,
* with support for pluralization of the message text
*/
/*
* errdetail --- add a detail error message text to the current error
*/
int
errdetail(const char *fmt,...)
{
ErrorData *edata = &errordata[errordata_stack_depth];
MemoryContext oldcontext;
recursion_depth++;
CHECK_STACK_DEPTH();
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
EVALUATE_MESSAGE(edata->domain, detail, false, true);
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
return 0; /* return value does not matter */
}
/*
* errdetail_internal --- add a detail error message text to the current error
*
* This is exactly like errdetail() except that strings passed to
* errdetail_internal are not translated, and are customarily left out of the
* internationalization message dictionary. This should be used for detail
* messages that seem not worth translating for one reason or another
* (typically, that they don't seem to be useful to average users).
*/
/*
* errdetail_log --- add a detail_log error message text to the current error
*/
/*
* errdetail_log_plural --- add a detail_log error message text to the current error
* with support for pluralization of the message text
*/
/*
* errdetail_plural --- add a detail error message text to the current error,
* with support for pluralization of the message text
*/
/*
* errhint --- add a hint error message text to the current error
*/
int
errhint(const char *fmt,...)
{
ErrorData *edata = &errordata[errordata_stack_depth];
MemoryContext oldcontext;
recursion_depth++;
CHECK_STACK_DEPTH();
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
EVALUATE_MESSAGE(edata->domain, hint, false, true);
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
return 0; /* return value does not matter */
}
/*
* errhint_plural --- add a hint error message text to the current error,
* with support for pluralization of the message text
*/
/*
* errcontext_msg --- add a context error message text to the current error
*
* Unlike other cases, multiple calls are allowed to build up a stack of
* context information. We assume earlier calls represent more-closely-nested
* states.
*/
int
errcontext_msg(const char *fmt,...)
{
ErrorData *edata = &errordata[errordata_stack_depth];
MemoryContext oldcontext;
recursion_depth++;
CHECK_STACK_DEPTH();
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
EVALUATE_MESSAGE(edata->context_domain, context, true, true);
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
return 0; /* return value does not matter */
}
/*
* set_errcontext_domain --- set message domain to be used by errcontext()
*
* errcontext_msg() can be called from a different module than the original
* ereport(), so we cannot use the message domain passed in errstart() to
* translate it. Instead, each errcontext_msg() call should be preceded by
* a set_errcontext_domain() call to specify the domain. This is usually
* done transparently by the errcontext() macro.
*/
int
set_errcontext_domain(const char *domain)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
/* the default text domain is the backend's */
edata->context_domain = domain ? domain : PG_TEXTDOMAIN("postgres");
return 0; /* return value does not matter */
}
/*
* errhidestmt --- optionally suppress STATEMENT: field of log entry
*
* This should be called if the message text already includes the statement.
*/
int
errhidestmt(bool hide_stmt)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
edata->hide_stmt = hide_stmt;
return 0; /* return value does not matter */
}
/*
* errhidecontext --- optionally suppress CONTEXT: field of log entry
*
* This should only be used for verbose debugging messages where the repeated
* inclusion of context would bloat the log volume too much.
*/
int
errhidecontext(bool hide_ctx)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
edata->hide_ctx = hide_ctx;
return 0; /* return value does not matter */
}
/*
* errposition --- add cursor position to the current error
*/
int
errposition(int cursorpos)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
edata->cursorpos = cursorpos;
return 0; /* return value does not matter */
}
/*
* internalerrposition --- add internal cursor position to the current error
*/
int
internalerrposition(int cursorpos)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
edata->internalpos = cursorpos;
return 0; /* return value does not matter */
}
/*
* internalerrquery --- add internal query text to the current error
*
* Can also pass NULL to drop the internal query text entry. This case
* is intended for use in error callback subroutines that are editorializing
* on the layout of the error report.
*/
int
internalerrquery(const char *query)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
if (edata->internalquery)
{
pfree(edata->internalquery);
edata->internalquery = NULL;
}
if (query)
edata->internalquery = MemoryContextStrdup(edata->assoc_context, query);
return 0; /* return value does not matter */
}
/*
* err_generic_string -- used to set individual ErrorData string fields
* identified by PG_DIAG_xxx codes.
*
* This intentionally only supports fields that don't use localized strings,
* so that there are no translation considerations.
*
* Most potential callers should not use this directly, but instead prefer
* higher-level abstractions, such as errtablecol() (see relcache.c).
*/
/*
* set_errdata_field --- set an ErrorData string field
*/
/*
* geterrcode --- return the currently set SQLSTATE error code
*
* This is only intended for use in error callback subroutines, since there
* is no other place outside elog.c where the concept is meaningful.
*/
int
geterrcode(void)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
return edata->sqlerrcode;
}
/*
* geterrposition --- return the currently set error position (0 if none)
*
* This is only intended for use in error callback subroutines, since there
* is no other place outside elog.c where the concept is meaningful.
*/
int
geterrposition(void)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
return edata->cursorpos;
}
/*
* getinternalerrposition --- same for internal error position
*
* This is only intended for use in error callback subroutines, since there
* is no other place outside elog.c where the concept is meaningful.
*/
int
getinternalerrposition(void)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
return edata->internalpos;
}
/*
* Functions to allow construction of error message strings separately from
* the ereport() call itself.
*
* The expected calling convention is
*
* pre_format_elog_string(errno, domain), var = format_elog_string(format,...)
*
* which can be hidden behind a macro such as GUC_check_errdetail(). We
* assume that any functions called in the arguments of format_elog_string()
* cannot result in re-entrant use of these functions --- otherwise the wrong
* text domain might be used, or the wrong errno substituted for %m. This is
* okay for the current usage with GUC check hooks, but might need further
* effort someday.
*
* The result of format_elog_string() is stored in ErrorContext, and will
* therefore survive until FlushErrorState() is called.
*/
/*
* Actual output of the top-of-stack error message
*
* In the ereport(ERROR) case this is called from PostgresMain (or not at all,
* if the error is caught by somebody). For all other severity levels this
* is called by errfinish.
*/
void
EmitErrorReport(void)
{
ErrorData *edata = &errordata[errordata_stack_depth];
MemoryContext oldcontext;
recursion_depth++;
CHECK_STACK_DEPTH();
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
/*
* Call hook before sending message to log. The hook function is allowed
* to turn off edata->output_to_server, so we must recheck that afterward.
* Making any other change in the content of edata is not considered
* supported.
*
* Note: the reason why the hook can only turn off output_to_server, and
* not turn it on, is that it'd be unreliable: we will never get here at
* all if errstart() deems the message uninteresting. A hook that could
* make decisions in that direction would have to hook into errstart(),
* where it would have much less information available. emit_log_hook is
* intended for custom log filtering and custom log message transmission
* mechanisms.
*
* The log hook has access to both the translated and original English
* error message text, which is passed through to allow it to be used as a
* message identifier. Note that the original text is not available for
* detail, detail_log, hint and context text elements.
*/
if (edata->output_to_server && emit_log_hook)
(*emit_log_hook) (edata);
/* Send to server log, if enabled */
if (edata->output_to_server)
send_message_to_server_log(edata);
/* Send to client, if enabled */
if (edata->output_to_client)
send_message_to_frontend(edata);
MemoryContextSwitchTo(oldcontext);
recursion_depth--;
}
/*
* CopyErrorData --- obtain a copy of the topmost error stack entry
*
* This is only for use in error handler code. The data is copied into the
* current memory context, so callers should always switch away from
* ErrorContext first; otherwise it will be lost when FlushErrorState is done.
*/
ErrorData *
CopyErrorData(void)
{
ErrorData *edata = &errordata[errordata_stack_depth];
ErrorData *newedata;
/*
* we don't increment recursion_depth because out-of-memory here does not
* indicate a problem within the error subsystem.
*/
CHECK_STACK_DEPTH();
Assert(CurrentMemoryContext != ErrorContext);
/* Copy the struct itself */
newedata = (ErrorData *) palloc(sizeof(ErrorData));
memcpy(newedata, edata, sizeof(ErrorData));
/* Make copies of separately-allocated fields */
if (newedata->message)
newedata->message = pstrdup(newedata->message);
if (newedata->detail)
newedata->detail = pstrdup(newedata->detail);
if (newedata->detail_log)
newedata->detail_log = pstrdup(newedata->detail_log);
if (newedata->hint)
newedata->hint = pstrdup(newedata->hint);
if (newedata->context)
newedata->context = pstrdup(newedata->context);
if (newedata->backtrace)
newedata->backtrace = pstrdup(newedata->backtrace);
if (newedata->schema_name)
newedata->schema_name = pstrdup(newedata->schema_name);
if (newedata->table_name)
newedata->table_name = pstrdup(newedata->table_name);
if (newedata->column_name)
newedata->column_name = pstrdup(newedata->column_name);
if (newedata->datatype_name)
newedata->datatype_name = pstrdup(newedata->datatype_name);
if (newedata->constraint_name)
newedata->constraint_name = pstrdup(newedata->constraint_name);
if (newedata->internalquery)
newedata->internalquery = pstrdup(newedata->internalquery);
/* Use the calling context for string allocation */
newedata->assoc_context = CurrentMemoryContext;
return newedata;
}
/*
* FreeErrorData --- free the structure returned by CopyErrorData.
*
* Error handlers should use this in preference to assuming they know all
* the separately-allocated fields.
*/
/*
* FreeErrorDataContents --- free the subsidiary data of an ErrorData.
*
* This can be used on either an error stack entry or a copied ErrorData.
*/
static void
FreeErrorDataContents(ErrorData *edata)
{
if (edata->message)
pfree(edata->message);
if (edata->detail)
pfree(edata->detail);
if (edata->detail_log)
pfree(edata->detail_log);
if (edata->hint)
pfree(edata->hint);
if (edata->context)
pfree(edata->context);
if (edata->backtrace)
pfree(edata->backtrace);
if (edata->schema_name)
pfree(edata->schema_name);
if (edata->table_name)
pfree(edata->table_name);
if (edata->column_name)
pfree(edata->column_name);
if (edata->datatype_name)
pfree(edata->datatype_name);
if (edata->constraint_name)
pfree(edata->constraint_name);
if (edata->internalquery)
pfree(edata->internalquery);
}
/*
* FlushErrorState --- flush the error state after error recovery
*
* This should be called by an error handler after it's done processing
* the error; or as soon as it's done CopyErrorData, if it intends to
* do stuff that is likely to provoke another error. You are not "out" of
* the error subsystem until you have done this.
*/
void
FlushErrorState(void)
{
/*
* Reset stack to empty. The only case where it would be more than one
* deep is if we serviced an error that interrupted construction of
* another message. We assume control escaped out of that message
* construction and won't ever go back.
*/
errordata_stack_depth = -1;
recursion_depth = 0;
/* Delete all data in ErrorContext */
MemoryContextResetAndDeleteChildren(ErrorContext);
}
/*
* ThrowErrorData --- report an error described by an ErrorData structure
*
* This is somewhat like ReThrowError, but it allows elevels besides ERROR,
* and the boolean flags such as output_to_server are computed via the
* default rules rather than being copied from the given ErrorData.
* This is primarily used to re-report errors originally reported by
* background worker processes and then propagated (with or without
* modification) to the backend responsible for them.
*/
/*
* ReThrowError --- re-throw a previously copied error
*
* A handler can do CopyErrorData/FlushErrorState to get out of the error
* subsystem, then do some processing, and finally ReThrowError to re-throw
* the original error. This is slower than just PG_RE_THROW() but should
* be used if the "some processing" is likely to incur another error.
*/
/*
* pg_re_throw --- out-of-line implementation of PG_RE_THROW() macro
*/
void
pg_re_throw(void)
{
/* If possible, throw the error to the next outer setjmp handler */
if (PG_exception_stack != NULL)
siglongjmp(*PG_exception_stack, 1);
else
{
/*
* If we get here, elog(ERROR) was thrown inside a PG_TRY block, which
* we have now exited only to discover that there is no outer setjmp
* handler to pass the error to. Had the error been thrown outside
* the block to begin with, we'd have promoted the error to FATAL, so
* the correct behavior is to make it FATAL now; that is, emit it and
* then call proc_exit.
*/
ErrorData *edata = &errordata[errordata_stack_depth];
Assert(errordata_stack_depth >= 0);
Assert(edata->elevel == ERROR);
edata->elevel = FATAL;
/*
* At least in principle, the increase in severity could have changed
* where-to-output decisions, so recalculate.
*/
edata->output_to_server = should_output_to_server(FATAL);
edata->output_to_client = should_output_to_client(FATAL);
/*
* We can use errfinish() for the rest, but we don't want it to call
* any error context routines a second time. Since we know we are
* about to exit, it should be OK to just clear the context stack.
*/
error_context_stack = NULL;
errfinish(edata->filename, edata->lineno, edata->funcname);
}
/* Doesn't return ... */
ExceptionalCondition("pg_re_throw tried to return", __FILE__, __LINE__);
}
/*
* GetErrorContextStack - Return the context stack, for display/diags
*
* Returns a pstrdup'd string in the caller's context which includes the PG
* error call stack. It is the caller's responsibility to ensure this string
* is pfree'd (or its context cleaned up) when done.
*
* This information is collected by traversing the error contexts and calling
* each context's callback function, each of which is expected to call
* errcontext() to return a string which can be presented to the user.
*/
/*
* Initialization of error output file
*/
/*
* GUC check_hook for backtrace_functions
*
* We split the input string, where commas separate function names
* and certain whitespace chars are ignored, into a \0-separated (and
* \0\0-terminated) list of function names. This formulation allows
* easy scanning when an error is thrown while avoiding the use of
* non-reentrant strtok(), as well as keeping the output data in a
* single palloc() chunk.
*/
/*
* GUC assign_hook for backtrace_functions
*/
/*
* GUC check_hook for log_destination
*/
#ifdef HAVE_SYSLOG
#endif
#ifdef WIN32
#endif
/*
* GUC assign_hook for log_destination
*/
/*
* GUC assign_hook for syslog_ident
*/
#ifdef HAVE_SYSLOG
#endif
/*
* GUC assign_hook for syslog_facility
*/
#ifdef HAVE_SYSLOG
#endif
#ifdef HAVE_SYSLOG
/*
* Write a message line to syslog
*/
#endif /* HAVE_SYSLOG */
#ifdef WIN32
/*
* Get the PostgreSQL equivalent of the Windows ANSI code page. "ANSI" system
* interfaces (e.g. CreateFileA()) expect string arguments in this encoding.
* Every process in a given system will find the same value at all times.
*/
/*
* Write a message line to the windows event log
*/
#endif /* WIN32 */
#ifdef WIN32
#else
#endif
/*
* get_formatted_log_time -- compute and get the log timestamp.
*
* The timestamp is computed if not set yet, so as it is kept consistent
* among all the log destinations that require it to be consistent. Note
* that the computed timestamp is returned in a static buffer, not
* palloc()'d.
*/
/*
* reset_formatted_start_time -- reset the start timestamp
*/
/*
* get_formatted_start_time -- compute and get the start timestamp.
*
* The timestamp is computed if not set yet. Note that the computed
* timestamp is returned in a static buffer, not palloc()'d.
*/
/*
* check_log_of_query -- check if a query can be logged
*/
/*
* get_backend_type_for_log -- backend type for log entries
*
* Returns a pointer to a static buffer, not palloc()'d.
*/
/*
* process_log_prefix_padding --- helper function for processing the format
* string in log_line_prefix
*
* Note: This function returns NULL if it finds something which
* it deems invalid in the format string.
*/
/*
* Format log status information using Log_line_prefix.
*/
/*
* Format log status info; append to the provided buffer.
*/
/*
* Unpack MAKE_SQLSTATE code. Note that this returns a pointer to a
* static buffer.
*/
/*
* Write error report to server's log
*/
static void send_message_to_server_log(ErrorData *edata) {}
/*
* Send data to the syslogger using the chunked protocol
*
* Note: when there are multiple backends writing into the syslogger pipe,
* it's critical that each write go into the pipe indivisibly, and not
* get interleaved with data from other processes. Fortunately, the POSIX
* spec requires that writes to pipes be atomic so long as they are not
* more than PIPE_BUF bytes long. So we divide long messages into chunks
* that are no more than that length, and send one chunk per write() call.
* The collector process knows how to reassemble the chunks.
*
* Because of the atomic write requirement, there are only two possible
* results from write() here: -1 for failure, or the requested number of
* bytes. There is not really anything we can do about a failure; retry would
* probably be an infinite loop, and we can't even report the error usefully.
* (There is noplace else we could send it!) So we might as well just ignore
* the result from write(). However, on some platforms you get a compiler
* warning from ignoring write()'s result, so do a little dance with casting
* rc to void to shut up the compiler.
*/
/*
* Append a text string to the error report being built for the client.
*
* This is ordinarily identical to pq_sendstring(), but if we are in
* error recursion trouble we skip encoding conversion, because of the
* possibility that the problem is a failure in the encoding conversion
* subsystem itself. Code elsewhere should ensure that the passed-in
* strings will be plain 7-bit ASCII, and thus not in need of conversion,
* in such cases. (In particular, we disable localization of error messages
* to help ensure that's true.)
*/
/*
* Write error report to client
*/
static void send_message_to_frontend(ErrorData *edata) {}
/*
* Support routines for formatting error messages.
*/
/*
* error_severity --- get string representing elevel
*
* The string is not localized here, but we mark the strings for translation
* so that callers can invoke _() on the result.
*/
/*
* append_with_tabs
*
* Append the string to the StringInfo buffer, inserting a tab after any
* newline.
*/
/*
* Write errors to stderr (or by equal means when stderr is
* not available). Used before ereport/elog can be used
* safely (memory context, GUC load etc)
*/
void
write_stderr(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fflush(stderr);
va_end(ap);
}
/*
* Write a message to STDERR using only async-signal-safe functions. This can
* be used to safely emit a message from a signal handler.
*
* TODO: It is likely possible to safely do a limited amount of string
* interpolation (e.g., %s and %d), but that is not presently supported.
*/
/*
* Adjust the level of a recovery-related message per trace_recovery_messages.
*
* The argument is the default log level of the message, eg, DEBUG2. (This
* should only be applied to DEBUGn log messages, otherwise it's a no-op.)
* If the level is >= trace_recovery_messages, we return LOG, causing the
* message to be logged unconditionally (for most settings of
* log_min_messages). Otherwise, we return the argument unchanged.
* The message will then be shown based on the setting of log_min_messages.
*
* Intention is to keep this for at least the whole of the 9.0 production
* release, so we can more easily diagnose production problems in the field.
* It should go away eventually, though, because it's an ugly and
* hard-to-explain kluge.
*/
#ifdef WIN32
__thread volatile int pg_signal_queue;
__thread int pg_signal_mask;
void pgwin32_dispatch_queued_signals(void) {}
#endif
|