From fcd810c69adf11b6ec1cff35359be0dd27662eff Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Fri, 14 Jan 2011 16:30:33 +0100 Subject: [PATCH] Use a lexer and grammar for parsing walsender commands 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. --- doc/src/sgml/protocol.sgml | 21 ++- src/backend/replication/Makefile | 25 ++- src/backend/replication/basebackup.c | 17 +- src/backend/replication/repl_gram.y | 143 +++++++++++++++++ src/backend/replication/repl_scanner.l | 168 ++++++++++++++++++++ src/backend/replication/walsender.c | 276 ++++++++++++++++++++------------- src/include/replication/basebackup.h | 2 +- src/include/replication/replnodes.h | 63 ++++++++ src/include/replication/walsender.h | 13 ++ src/tools/msvc/Mkvcbuild.pm | 1 + src/tools/msvc/pgbison.bat | 1 + src/tools/msvc/pgflex.bat | 1 + 12 files changed, 603 insertions(+), 128 deletions(-) create mode 100644 src/backend/replication/repl_gram.y create mode 100644 src/backend/replication/repl_scanner.l create mode 100644 src/include/replication/replnodes.h diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml index 80c14fb74c..76c062fd51 100644 --- a/doc/src/sgml/protocol.sgml +++ b/doc/src/sgml/protocol.sgml @@ -1460,16 +1460,27 @@ The commands accepted in walsender mode are: - BASE_BACKUP options;label + BASE_BACKUP [LABEL 'label'] [PROGRESS] Instructs the server to start streaming a base backup. - The system will automatically be put in backup mode with the label - specified in label before the backup is started, and - taken out of it when the backup is complete. The following options - are accepted: + The system will automatically be put in backup mode before the backup + is started, and taken out of it when the backup is complete. The + following options are accepted: + LABEL 'label' + + + Sets the label of the backup. If none is specified, a backup label + of base backup will be used. The quoting rules + for the label are the same as a standard SQL string with + turned on. + + + + + PROGRESS diff --git a/src/backend/replication/Makefile b/src/backend/replication/Makefile index 21fc096df3..42c6eaf26c 100644 --- a/src/backend/replication/Makefile +++ b/src/backend/replication/Makefile @@ -12,6 +12,29 @@ subdir = src/backend/replication top_builddir = ../../.. include $(top_builddir)/src/Makefile.global -OBJS = walsender.o walreceiverfuncs.o walreceiver.o basebackup.o +OBJS = walsender.o walreceiverfuncs.o walreceiver.o basebackup.o \ + repl_gram.o include $(top_srcdir)/src/backend/common.mk + +# repl_scanner is compiled as part of repl_gram +repl_gram.o: repl_scanner.c + +# See notes in src/backend/parser/Makefile about the following two rules + +repl_gram.c: repl_gram.y +ifdef BISON + $(BISON) -d $(BISONFLAGS) -o $@ $< +else + @$(missing) bison $< $@ +endif + +repl_scanner.c: repl_scanner.l +ifdef FLEX + $(FLEX) $(FLEXFLAGS) -o'$@' $< +else + @$(missing) flex $< $@ +endif + +# repl_gram.c and repl_scanner.c are in the distribution tarball, so +# they are not cleaned here. diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c index 7929f855f6..1ed5e2a6c9 100644 --- a/src/backend/replication/basebackup.c +++ b/src/backend/replication/basebackup.c @@ -98,12 +98,10 @@ perform_base_backup(const char *backup_label, List *tablespaces) * pg_stop_backup() for the user. */ void -SendBaseBackup(const char *options) +SendBaseBackup(const char *backup_label, bool progress) { DIR *dir; struct dirent *de; - char *backup_label = strchr(options, ';'); - bool progress = false; List *tablespaces = NIL; tablespaceinfo *ti; MemoryContext backup_context; @@ -119,18 +117,7 @@ SendBaseBackup(const char *options) WalSndSetState(WALSNDSTATE_BACKUP); if (backup_label == NULL) - ereport(FATAL, - (errcode(ERRCODE_PROTOCOL_VIOLATION), - errmsg("invalid base backup options: %s", options))); - backup_label++; /* Walk past the semicolon */ - - /* Currently the only option string supported is PROGRESS */ - if (strncmp(options, "PROGRESS", 8) == 0) - progress = true; - else if (options[0] != ';') - ereport(FATAL, - (errcode(ERRCODE_PROTOCOL_VIOLATION), - errmsg("invalid base backup options: %s", options))); + backup_label = "base backup"; if (update_process_title) { diff --git a/src/backend/replication/repl_gram.y b/src/backend/replication/repl_gram.y new file mode 100644 index 0000000000..0ef33ddb4f --- /dev/null +++ b/src/backend/replication/repl_gram.y @@ -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 SCONST +%token RECPTR + +/* Keyword tokens. */ +%token K_BASE_BACKUP +%token K_IDENTIFY_SYSTEM +%token K_LABEL +%token K_PROGRESS +%token K_START_REPLICATION + +%type command +%type base_backup start_replication identify_system +%type opt_progress +%type 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