mirror of https://github.com/postgres/postgres
Makes it easier to parse mainly the BASE_BACKUP command with it's options, and avoids having to manually deal with quoted identifiers in the label (previously broken), and makes it easier to add new commands and options in the future. In passing, refactor the case statement in the walsender to put each command in it's own function.pull/1/head
parent
688423d004
commit
fcd810c69a
@ -0,0 +1,143 @@ |
||||
%{ |
||||
/*------------------------------------------------------------------------- |
||||
* |
||||
* repl_gram.y - Parser for the replication commands |
||||
* |
||||
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/replication/repl_gram.y |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#include "postgres.h" |
||||
|
||||
#include "replication/replnodes.h" |
||||
#include "replication/walsender.h" |
||||
|
||||
/* Result of the parsing is returned here */ |
||||
Node *replication_parse_result; |
||||
|
||||
/* Location tracking support --- simpler than bison's default */ |
||||
#define YYLLOC_DEFAULT(Current, Rhs, N) \ |
||||
do { \ |
||||
if (N) \ |
||||
(Current) = (Rhs)[1]; \ |
||||
else \ |
||||
(Current) = (Rhs)[0]; \ |
||||
} while (0) |
||||
|
||||
/* |
||||
* Bison doesn't allocate anything that needs to live across parser calls, |
||||
* so we can easily have it use palloc instead of malloc. This prevents |
||||
* memory leaks if we error out during parsing. Note this only works with |
||||
* bison >= 2.0. However, in bison 1.875 the default is to use alloca() |
||||
* if possible, so there's not really much problem anyhow, at least if |
||||
* you're building with gcc. |
||||
*/ |
||||
#define YYMALLOC palloc |
||||
#define YYFREE pfree |
||||
|
||||
#define parser_yyerror(msg) replication_yyerror(msg, yyscanner) |
||||
#define parser_errposition(pos) replication_scanner_errposition(pos) |
||||
|
||||
%} |
||||
|
||||
%expect 0 |
||||
%name-prefix="replication_yy" |
||||
|
||||
%union { |
||||
char *str; |
||||
bool boolval; |
||||
|
||||
XLogRecPtr recptr; |
||||
Node *node; |
||||
} |
||||
|
||||
/* Non-keyword tokens */ |
||||
%token <str> SCONST |
||||
%token <recptr> RECPTR |
||||
|
||||
/* Keyword tokens. */ |
||||
%token K_BASE_BACKUP |
||||
%token K_IDENTIFY_SYSTEM |
||||
%token K_LABEL |
||||
%token K_PROGRESS |
||||
%token K_START_REPLICATION |
||||
|
||||
%type <node> command |
||||
%type <node> base_backup start_replication identify_system |
||||
%type <boolval> opt_progress |
||||
%type <str> opt_label |
||||
|
||||
%% |
||||
|
||||
firstcmd: command opt_semicolon |
||||
{ |
||||
replication_parse_result = $1; |
||||
} |
||||
; |
||||
|
||||
opt_semicolon: ';' |
||||
| /* EMPTY */ |
||||
; |
||||
|
||||
command: |
||||
identify_system |
||||
| base_backup |
||||
| start_replication |
||||
; |
||||
|
||||
/* |
||||
* IDENTIFY_SYSTEM |
||||
*/ |
||||
identify_system: |
||||
K_IDENTIFY_SYSTEM |
||||
{ |
||||
$$ = (Node *) makeNode(IdentifySystemCmd); |
||||
} |
||||
; |
||||
|
||||
/* |
||||
* BASE_BACKUP [LABEL <label>] [PROGRESS] |
||||
*/ |
||||
base_backup: |
||||
K_BASE_BACKUP opt_label opt_progress |
||||
{ |
||||
BaseBackupCmd *cmd = (BaseBackupCmd *) makeNode(BaseBackupCmd); |
||||
|
||||
cmd->label = $2; |
||||
cmd->progress = $3; |
||||
|
||||
$$ = (Node *) cmd; |
||||
} |
||||
; |
||||
|
||||
opt_label: K_LABEL SCONST { $$ = $2; } |
||||
| /* EMPTY */ { $$ = NULL; } |
||||
; |
||||
|
||||
opt_progress: K_PROGRESS { $$ = true; } |
||||
| /* EMPTY */ { $$ = false; } |
||||
; |
||||
|
||||
/* |
||||
* START_REPLICATION %X/%X |
||||
*/ |
||||
start_replication: |
||||
K_START_REPLICATION RECPTR |
||||
{ |
||||
StartReplicationCmd *cmd; |
||||
|
||||
cmd = makeNode(StartReplicationCmd); |
||||
cmd->startpoint = $2; |
||||
|
||||
$$ = (Node *) cmd; |
||||
} |
||||
; |
||||
%% |
||||
|
||||
#include "repl_scanner.c" |
||||
@ -0,0 +1,168 @@ |
||||
%{ |
||||
/*------------------------------------------------------------------------- |
||||
* |
||||
* repl_scanner.l |
||||
* a lexical scanner for the replication commands |
||||
* |
||||
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/replication/repl_scanner.l |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */ |
||||
#undef fprintf |
||||
#define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg))) |
||||
|
||||
/* Handle to the buffer that the lexer uses internally */ |
||||
static YY_BUFFER_STATE scanbufhandle; |
||||
|
||||
static StringInfoData litbuf; |
||||
|
||||
static void startlit(void); |
||||
static char *litbufdup(void); |
||||
static void addlit(char *ytext, int yleng); |
||||
static void addlitchar(unsigned char ychar); |
||||
|
||||
%} |
||||
|
||||
%option 8bit |
||||
%option never-interactive |
||||
%option nodefault |
||||
%option noinput |
||||
%option nounput |
||||
%option noyywrap |
||||
%option warn |
||||
%option prefix="replication_yy" |
||||
|
||||
%x xq |
||||
|
||||
/* Extended quote |
||||
* xqdouble implements embedded quote, '''' |
||||
*/ |
||||
xqstart {quote} |
||||
xqdouble {quote}{quote} |
||||
xqinside [^']+ |
||||
|
||||
hexdigit [0-9A-Za-z]+ |
||||
|
||||
quote ' |
||||
quotestop {quote} |
||||
|
||||
%% |
||||
|
||||
BASE_BACKUP { return K_BASE_BACKUP; } |
||||
IDENTIFY_SYSTEM { return K_IDENTIFY_SYSTEM; } |
||||
LABEL { return K_LABEL; } |
||||
PROGRESS { return K_PROGRESS; } |
||||
START_REPLICATION { return K_START_REPLICATION; } |
||||
"," { return ','; } |
||||
";" { return ';'; } |
||||
|
||||
[\n] ; |
||||
[\t] ; |
||||
" " ; |
||||
|
||||
{hexdigit}+\/{hexdigit}+ { |
||||
if (sscanf(yytext, "%X/%X", &yylval.recptr.xlogid, &yylval.recptr.xrecoff) != 2) |
||||
yyerror("invalid streaming start location"); |
||||
return RECPTR; |
||||
} |
||||
|
||||
{xqstart} { |
||||
BEGIN(xq); |
||||
startlit(); |
||||
} |
||||
<xq>{quotestop} { |
||||
yyless(1); |
||||
BEGIN(INITIAL); |
||||
yylval.str = litbufdup(); |
||||
return SCONST; |
||||
} |
||||
<xq>{xqdouble} { |
||||
addlitchar('\''); |
||||
} |
||||
<xq>{xqinside} { |
||||
addlit(yytext, yyleng); |
||||
} |
||||
|
||||
<xq><<EOF>> { yyerror("unterminated quoted string"); } |
||||
|
||||
|
||||
<<EOF>> { |
||||
yyterminate(); |
||||
} |
||||
|
||||
. { |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_SYNTAX_ERROR), |
||||
errmsg("syntax error: unexpected character \"%s\"", yytext))); |
||||
} |
||||
%% |
||||
|
||||
|
||||
static void |
||||
startlit(void) |
||||
{ |
||||
initStringInfo(&litbuf); |
||||
} |
||||
|
||||
static char * |
||||
litbufdup(void) |
||||
{ |
||||
return litbuf.data; |
||||
} |
||||
|
||||
static void |
||||
addlit(char *ytext, int yleng) |
||||
{ |
||||
appendBinaryStringInfo(&litbuf, ytext, yleng); |
||||
} |
||||
|
||||
static void |
||||
addlitchar(unsigned char ychar) |
||||
{ |
||||
appendStringInfoChar(&litbuf, ychar); |
||||
} |
||||
|
||||
void |
||||
yyerror(const char *message) |
||||
{ |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_SYNTAX_ERROR), |
||||
errmsg_internal("%s", message))); |
||||
} |
||||
|
||||
|
||||
void |
||||
replication_scanner_init(const char *str) |
||||
{ |
||||
Size slen = strlen(str); |
||||
char *scanbuf; |
||||
|
||||
/* |
||||
* Might be left over after ereport() |
||||
*/ |
||||
if (YY_CURRENT_BUFFER) |
||||
yy_delete_buffer(YY_CURRENT_BUFFER); |
||||
|
||||
/* |
||||
* Make a scan buffer with special termination needed by flex. |
||||
*/ |
||||
scanbuf = (char *) palloc(slen + 2); |
||||
memcpy(scanbuf, str, slen); |
||||
scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR; |
||||
scanbufhandle = yy_scan_buffer(scanbuf, slen + 2); |
||||
} |
||||
|
||||
void |
||||
replication_scanner_finish() |
||||
{ |
||||
yy_delete_buffer(scanbufhandle); |
||||
scanbufhandle = NULL; |
||||
} |
||||
@ -0,0 +1,63 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* replnodes.h |
||||
* definitions for replication grammar parse nodes |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/replication/replnodes.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef REPLNODES_H |
||||
#define REPLNODES_H |
||||
|
||||
#include "access/xlogdefs.h" |
||||
#include "nodes/primnodes.h" |
||||
#include "nodes/value.h" |
||||
|
||||
/*
|
||||
* NodeTags for replication parser |
||||
*/ |
||||
typedef enum ReplNodeTag |
||||
{ |
||||
T_IdentifySystemCmd = 10, |
||||
T_BaseBackupCmd, |
||||
T_StartReplicationCmd |
||||
} ReplNodeTag; |
||||
|
||||
/* ----------------------
|
||||
* IDENTIFY_SYSTEM command |
||||
* ---------------------- |
||||
*/ |
||||
typedef struct IdentifySystemCmd |
||||
{ |
||||
NodeTag type; |
||||
} IdentifySystemCmd; |
||||
|
||||
|
||||
/* ----------------------
|
||||
* BASE_BACKUP command |
||||
* ---------------------- |
||||
*/ |
||||
typedef struct BaseBackupCmd |
||||
{ |
||||
NodeTag type; |
||||
char *label; |
||||
bool progress; |
||||
} BaseBackupCmd; |
||||
|
||||
|
||||
/* ----------------------
|
||||
* START_REPLICATION command |
||||
* ---------------------- |
||||
*/ |
||||
typedef struct StartReplicationCmd |
||||
{ |
||||
NodeTag type; |
||||
XLogRecPtr startpoint; |
||||
} StartReplicationCmd; |
||||
|
||||
#endif /* REPLNODES_H */ |
||||
Loading…
Reference in new issue