postgresql/src/fe_utils/conditional.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

190 lines
3.7 KiB
C
Raw Permalink Normal View History

/*-------------------------------------------------------------------------
* A stack of automaton states to handle nested conditionals.
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
*
* Copyright (c) 2000-2024, PostgreSQL Global Development Group
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
*
* src/fe_utils/conditional.c
*
*-------------------------------------------------------------------------
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
*/
#include "postgres_fe.h"
#include "fe_utils/conditional.h"
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
/*
* create stack
*/
ConditionalStack
conditional_stack_create(void)
{
ConditionalStack cstack = pg_malloc(sizeof(ConditionalStackData));
cstack->head = NULL;
return cstack;
}
/*
Allow pgbench to retry in some cases. When serialization or deadlock errors are reported by backend, allow to retry and continue the benchmarking. For this purpose new options "--max-tries", "--failures-detailed" and "--verbose-errors" are added. Transactions with serialization errors or deadlock errors will be repeated after rollbacks until they complete successfully or reach the maximum number of tries (specified by the --max-tries option), or the maximum time of tries (specified by the --latency-limit option). These options can be specified at the same time. It is not possible to use an unlimited number of tries (--max-tries=0) without the --latency-limit option or the --time option. By default the option --max-tries is set to 1, which means transactions with serialization/deadlock errors are not retried. If the last try fails, this transaction will be reported as failed, and the client variables will be set as they were before the first run of this transaction. Statistics on retries and failures are printed in the progress, transaction / aggregation logs and in the end with other results (all and for each script). Also retries and failures are printed per-command with average latency by using option (--report-per-command, -r). Option --failures-detailed prints group failures by basic types (serialization failures / deadlock failures). Option --verbose-errors prints distinct reports on errors and failures (errors without retrying) by type with detailed information like which limit for retries was violated and how far it was exceeded for the serialization/deadlock failures. Patch originally written by Marina Polyakova then Yugo Nagata inherited the discussion and heavily modified the patch to make it commitable. Authors: Yugo Nagata, Marina Polyakova Reviewed-by: Fabien Coelho, Tatsuo Ishii, Alvaro Herrera, Kevin Grittner, Andres Freund, Arthur Zakirov, Alexander Korotkov, Teodor Sigaev, Ildus Kurbangaliev Discussion: https://postgr.es/m/flat/72a0d590d6ba06f242d75c2e641820ec%40postgrespro.ru
2022-03-23 10:52:37 +01:00
* Destroy all the elements from the stack. The stack itself is not freed.
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
*/
void
Allow pgbench to retry in some cases. When serialization or deadlock errors are reported by backend, allow to retry and continue the benchmarking. For this purpose new options "--max-tries", "--failures-detailed" and "--verbose-errors" are added. Transactions with serialization errors or deadlock errors will be repeated after rollbacks until they complete successfully or reach the maximum number of tries (specified by the --max-tries option), or the maximum time of tries (specified by the --latency-limit option). These options can be specified at the same time. It is not possible to use an unlimited number of tries (--max-tries=0) without the --latency-limit option or the --time option. By default the option --max-tries is set to 1, which means transactions with serialization/deadlock errors are not retried. If the last try fails, this transaction will be reported as failed, and the client variables will be set as they were before the first run of this transaction. Statistics on retries and failures are printed in the progress, transaction / aggregation logs and in the end with other results (all and for each script). Also retries and failures are printed per-command with average latency by using option (--report-per-command, -r). Option --failures-detailed prints group failures by basic types (serialization failures / deadlock failures). Option --verbose-errors prints distinct reports on errors and failures (errors without retrying) by type with detailed information like which limit for retries was violated and how far it was exceeded for the serialization/deadlock failures. Patch originally written by Marina Polyakova then Yugo Nagata inherited the discussion and heavily modified the patch to make it commitable. Authors: Yugo Nagata, Marina Polyakova Reviewed-by: Fabien Coelho, Tatsuo Ishii, Alvaro Herrera, Kevin Grittner, Andres Freund, Arthur Zakirov, Alexander Korotkov, Teodor Sigaev, Ildus Kurbangaliev Discussion: https://postgr.es/m/flat/72a0d590d6ba06f242d75c2e641820ec%40postgrespro.ru
2022-03-23 10:52:37 +01:00
conditional_stack_reset(ConditionalStack cstack)
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
{
Allow pgbench to retry in some cases. When serialization or deadlock errors are reported by backend, allow to retry and continue the benchmarking. For this purpose new options "--max-tries", "--failures-detailed" and "--verbose-errors" are added. Transactions with serialization errors or deadlock errors will be repeated after rollbacks until they complete successfully or reach the maximum number of tries (specified by the --max-tries option), or the maximum time of tries (specified by the --latency-limit option). These options can be specified at the same time. It is not possible to use an unlimited number of tries (--max-tries=0) without the --latency-limit option or the --time option. By default the option --max-tries is set to 1, which means transactions with serialization/deadlock errors are not retried. If the last try fails, this transaction will be reported as failed, and the client variables will be set as they were before the first run of this transaction. Statistics on retries and failures are printed in the progress, transaction / aggregation logs and in the end with other results (all and for each script). Also retries and failures are printed per-command with average latency by using option (--report-per-command, -r). Option --failures-detailed prints group failures by basic types (serialization failures / deadlock failures). Option --verbose-errors prints distinct reports on errors and failures (errors without retrying) by type with detailed information like which limit for retries was violated and how far it was exceeded for the serialization/deadlock failures. Patch originally written by Marina Polyakova then Yugo Nagata inherited the discussion and heavily modified the patch to make it commitable. Authors: Yugo Nagata, Marina Polyakova Reviewed-by: Fabien Coelho, Tatsuo Ishii, Alvaro Herrera, Kevin Grittner, Andres Freund, Arthur Zakirov, Alexander Korotkov, Teodor Sigaev, Ildus Kurbangaliev Discussion: https://postgr.es/m/flat/72a0d590d6ba06f242d75c2e641820ec%40postgrespro.ru
2022-03-23 10:52:37 +01:00
if (!cstack)
return; /* nothing to do here */
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
while (conditional_stack_pop(cstack))
continue;
Allow pgbench to retry in some cases. When serialization or deadlock errors are reported by backend, allow to retry and continue the benchmarking. For this purpose new options "--max-tries", "--failures-detailed" and "--verbose-errors" are added. Transactions with serialization errors or deadlock errors will be repeated after rollbacks until they complete successfully or reach the maximum number of tries (specified by the --max-tries option), or the maximum time of tries (specified by the --latency-limit option). These options can be specified at the same time. It is not possible to use an unlimited number of tries (--max-tries=0) without the --latency-limit option or the --time option. By default the option --max-tries is set to 1, which means transactions with serialization/deadlock errors are not retried. If the last try fails, this transaction will be reported as failed, and the client variables will be set as they were before the first run of this transaction. Statistics on retries and failures are printed in the progress, transaction / aggregation logs and in the end with other results (all and for each script). Also retries and failures are printed per-command with average latency by using option (--report-per-command, -r). Option --failures-detailed prints group failures by basic types (serialization failures / deadlock failures). Option --verbose-errors prints distinct reports on errors and failures (errors without retrying) by type with detailed information like which limit for retries was violated and how far it was exceeded for the serialization/deadlock failures. Patch originally written by Marina Polyakova then Yugo Nagata inherited the discussion and heavily modified the patch to make it commitable. Authors: Yugo Nagata, Marina Polyakova Reviewed-by: Fabien Coelho, Tatsuo Ishii, Alvaro Herrera, Kevin Grittner, Andres Freund, Arthur Zakirov, Alexander Korotkov, Teodor Sigaev, Ildus Kurbangaliev Discussion: https://postgr.es/m/flat/72a0d590d6ba06f242d75c2e641820ec%40postgrespro.ru
2022-03-23 10:52:37 +01:00
}
/*
* destroy stack
*/
void
conditional_stack_destroy(ConditionalStack cstack)
{
conditional_stack_reset(cstack);
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
free(cstack);
}
/*
* Create a new conditional branch.
*/
void
conditional_stack_push(ConditionalStack cstack, ifState new_state)
{
IfStackElem *p = (IfStackElem *) pg_malloc(sizeof(IfStackElem));
p->if_state = new_state;
p->query_len = -1;
p->paren_depth = -1;
p->next = cstack->head;
cstack->head = p;
}
/*
* Destroy the topmost conditional branch.
* Returns false if there was no branch to end.
*/
bool
conditional_stack_pop(ConditionalStack cstack)
{
IfStackElem *p = cstack->head;
if (!p)
return false;
cstack->head = cstack->head->next;
free(p);
return true;
}
/*
* Returns current stack depth, for debugging purposes.
*/
int
conditional_stack_depth(ConditionalStack cstack)
{
if (cstack == NULL)
return -1;
else
{
IfStackElem *p = cstack->head;
int depth = 0;
while (p != NULL)
{
depth++;
p = p->next;
}
return depth;
}
}
Support \if ... \elif ... \else ... \endif in psql scripting. This patch adds nestable conditional blocks to psql. The control structure feature per se is complete, but the boolean expressions understood by \if and \elif are pretty primitive; basically, after variable substitution and backtick expansion, the result has to be "true" or "false" or one of the other standard spellings of a boolean value. But that's enough for many purposes, since you can always do the heavy lifting on the server side; and we can extend it later. Along the way, pay down some of the technical debt that had built up around psql/command.c: * Refactor exec_command() into a function per command, instead of being a 1500-line monstrosity. This makes the file noticeably longer because of repetitive function header/trailer overhead, but it seems much more readable. * Teach psql_get_variable() and psqlscanslash.l to suppress variable substitution and backtick expansion on the basis of the conditional stack state, thereby allowing removal of the OT_NO_EVAL kluge. * Fix the no-doubt-once-expedient hack of sometimes silently substituting mainloop.c's previous_buf for query_buf when calling HandleSlashCmds. (It's a bit remarkable that commands like \r worked at all with that.) Recall of a previous query is now done explicitly in the slash commands where that should happen. Corey Huinker, reviewed by Fabien Coelho, further hacking by me Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
/*
* Fetch the current state of the top of the stack.
*/
ifState
conditional_stack_peek(ConditionalStack cstack)
{
if (conditional_stack_empty(cstack))
return IFSTATE_NONE;
return cstack->head->if_state;
}
/*
* Change the state of the topmost branch.
* Returns false if there was no branch state to set.
*/
bool
conditional_stack_poke(ConditionalStack cstack, ifState new_state)
{
if (conditional_stack_empty(cstack))
return false;
cstack->head->if_state = new_state;
return true;
}
/*
* True if there are no active \if-blocks.
*/
bool
conditional_stack_empty(ConditionalStack cstack)
{
return cstack->head == NULL;
}
/*
* True if we should execute commands normally; that is, the current
* conditional branch is active, or there is no open \if block.
*/
bool
conditional_active(ConditionalStack cstack)
{
ifState s = conditional_stack_peek(cstack);
return s == IFSTATE_NONE || s == IFSTATE_TRUE || s == IFSTATE_ELSE_TRUE;
}
/*
* Save current query buffer length in topmost stack entry.
*/
void
conditional_stack_set_query_len(ConditionalStack cstack, int len)
{
Assert(!conditional_stack_empty(cstack));
cstack->head->query_len = len;
}
/*
* Fetch last-recorded query buffer length from topmost stack entry.
* Will return -1 if no stack or it was never saved.
*/
int
conditional_stack_get_query_len(ConditionalStack cstack)
{
if (conditional_stack_empty(cstack))
return -1;
return cstack->head->query_len;
}
/*
* Save current parenthesis nesting depth in topmost stack entry.
*/
void
conditional_stack_set_paren_depth(ConditionalStack cstack, int depth)
{
Assert(!conditional_stack_empty(cstack));
cstack->head->paren_depth = depth;
}
/*
* Fetch last-recorded parenthesis nesting depth from topmost stack entry.
* Will return -1 if no stack or it was never saved.
*/
int
conditional_stack_get_paren_depth(ConditionalStack cstack)
{
if (conditional_stack_empty(cstack))
return -1;
return cstack->head->paren_depth;
}