<synopsis>
 ALTER DATABASE <replaceable class="PARAMETER">name</replaceable> [ [ WITH ] <replaceable class="PARAMETER">option</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">option</replaceable> can be:
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
 
     CONNECTION LIMIT <replaceable class="PARAMETER">connlimit</replaceable>
 
 
 ALTER FUNCTION <replaceable>name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
     SET SCHEMA <replaceable>new_schema</replaceable>
 
-where <replaceable class="PARAMETER">action</replaceable> is one of:
+<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
 
     CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
     IMMUTABLE | STABLE | VOLATILE
 
 <synopsis>
 ALTER ROLE <replaceable class="PARAMETER">name</replaceable> [ [ WITH ] <replaceable class="PARAMETER">option</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">option</replaceable> can be:
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
     
       SUPERUSER | NOSUPERUSER
     | CREATEDB | NOCREATEDB
 
 ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
     SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
 
-where <replaceable class="PARAMETER">action</replaceable> is one of:
+<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
 
     ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
     DROP [ COLUMN ] [ IF EXISTS ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
 
 <synopsis>
 ALTER USER <replaceable class="PARAMETER">name</replaceable> [ [ WITH ] <replaceable class="PARAMETER">option</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">option</replaceable> can be:
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
     
       SUPERUSER | NOSUPERUSER
     | CREATEDB | NOCREATEDB
 
 <synopsis>
 BEGIN [ WORK | TRANSACTION ] [ <replaceable class="parameter">transaction_mode</replaceable> [, ...] ]
 
-where <replaceable class="parameter">transaction_mode</replaceable> is one of:
+<phrase>where <replaceable class="parameter">transaction_mode</replaceable> is one of:</phrase>
 
     ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
     READ WRITE | READ ONLY
 
  <refsynopsisdiv>
 <synopsis>
 CREATE DOMAIN <replaceable class="parameter">name</replaceable> [ AS ] <replaceable class="parameter">data_type</replaceable>
-    [ DEFAULT <replaceable>expression</> ]
+    [ DEFAULT <replaceable>expression</replaceable> ]
     [ <replaceable class="PARAMETER">constraint</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">constraint</replaceable> is:
+<phrase>where <replaceable class="PARAMETER">constraint</replaceable> is:</phrase>
 
 [ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
 { NOT NULL | NULL | CHECK (<replaceable class="PARAMETER">expression</replaceable>) }
 
 <synopsis>
 CREATE GROUP <replaceable class="PARAMETER">name</replaceable> [ [ WITH ] <replaceable class="PARAMETER">option</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">option</replaceable> can be:
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
     
       SUPERUSER | NOSUPERUSER
     | CREATEDB | NOCREATEDB
 
 <synopsis>
 CREATE ROLE <replaceable class="PARAMETER">name</replaceable> [ [ WITH ] <replaceable class="PARAMETER">option</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">option</replaceable> can be:
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
     
       SUPERUSER | NOSUPERUSER
     | CREATEDB | NOCREATEDB
 
  <refsynopsisdiv>
 <synopsis>
 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable class="PARAMETER">table_name</replaceable> ( [
-  { <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ DEFAULT <replaceable>default_expr</> ] [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
+  { <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ DEFAULT <replaceable>default_expr</replaceable> ] [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
     | <replaceable>table_constraint</replaceable>
     | LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ] ... }
     [, ... ]
 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
 [ TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ]
 
-where <replaceable class="PARAMETER">column_constraint</replaceable> is:
+<phrase>where <replaceable class="PARAMETER">column_constraint</replaceable> is:</phrase>
 
 [ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
 { NOT NULL |
     [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ] }
 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
 
-and <replaceable class="PARAMETER">table_constraint</replaceable> is:
+<phrase>and <replaceable class="PARAMETER">table_constraint</replaceable> is:</phrase>
 
 [ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
 { UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) <replaceable class="PARAMETER">index_parameters</replaceable> |
     [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ] }
 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
 
-<replaceable class="PARAMETER">index_parameters</replaceable> in <literal>UNIQUE</> and <literal>PRIMARY KEY</> constraints are:
+<phrase><replaceable class="PARAMETER">index_parameters</replaceable> in <literal>UNIQUE</literal> and <literal>PRIMARY KEY</literal> constraints are:</phrase>
 
 [ WITH ( <replaceable class="PARAMETER">storage_parameter</replaceable> [= <replaceable class="PARAMETER">value</replaceable>] [, ... ] ) ]
 [ USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ]
 
 <synopsis>
 CREATE USER <replaceable class="PARAMETER">name</replaceable> [ [ WITH ] <replaceable class="PARAMETER">option</replaceable> [ ... ] ]
 
-where <replaceable class="PARAMETER">option</replaceable> can be:
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
     
       SUPERUSER | NOSUPERUSER
     | CREATEDB | NOCREATEDB
 
 <synopsis>
 FETCH [ <replaceable class="PARAMETER">direction</replaceable> { FROM | IN } ] <replaceable class="PARAMETER">cursorname</replaceable>
 
-where <replaceable class="PARAMETER">direction</replaceable> can be empty or one of:
+<phrase>where <replaceable class="PARAMETER">direction</replaceable> can be empty or one of:</phrase>
 
     NEXT
     PRIOR
 
     TO { [ GROUP ] <replaceable class="PARAMETER">rolename</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
 
 GRANT { USAGE | ALL [ PRIVILEGES ] }
-    ON FOREIGN DATA WRAPPER <replaceable>fdwname</> [, ...]
+    ON FOREIGN DATA WRAPPER <replaceable>fdwname</replaceable> [, ...]
     TO { [ GROUP ] <replaceable class="PARAMETER">rolename</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
 
 GRANT { USAGE | ALL [ PRIVILEGES ] }
-    ON FOREIGN SERVER <replaceable>servername</> [, ...]
+    ON FOREIGN SERVER <replaceable>servername</replaceable> [, ...]
     TO { [ GROUP ] <replaceable class="PARAMETER">rolename</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
 
 GRANT { EXECUTE | ALL [ PRIVILEGES ] }
     TO { [ GROUP ] <replaceable class="PARAMETER">rolename</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
 
 GRANT { CREATE | ALL [ PRIVILEGES ] }
-    ON TABLESPACE <replaceable>tablespacename</> [, ...]
+    ON TABLESPACE <replaceable>tablespacename</replaceable> [, ...]
     TO { [ GROUP ] <replaceable class="PARAMETER">rolename</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
 
 GRANT <replaceable class="PARAMETER">role</replaceable> [, ...] TO <replaceable class="PARAMETER">rolename</replaceable> [, ...] [ WITH ADMIN OPTION ]
 
 <synopsis>
 LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [, ...] [ IN <replaceable class="PARAMETER">lockmode</replaceable> MODE ] [ NOWAIT ]
 
-where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
+<phrase>where <replaceable class="PARAMETER">lockmode</replaceable> is one of:</phrase>
 
     ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
     | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
 
     [ FETCH { FIRST | NEXT } [ <replaceable class="parameter">count</replaceable> ] { ROW | ROWS } ONLY ]
     [ FOR { UPDATE | SHARE } [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ] [ NOWAIT ] [...] ]
 
-where <replaceable class="parameter">from_item</replaceable> can be one of:
+<phrase>where <replaceable class="parameter">from_item</replaceable> can be one of:</phrase>
 
     [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] [ [ AS ] <replaceable class="parameter">alias</replaceable> [ ( <replaceable class="parameter">column_alias</replaceable> [, ...] ) ] ]
     ( <replaceable class="parameter">select</replaceable> ) [ AS ] <replaceable class="parameter">alias</replaceable> [ ( <replaceable class="parameter">column_alias</replaceable> [, ...] ) ]
     <replaceable class="parameter">function_name</replaceable> ( [ <replaceable class="parameter">argument</replaceable> [, ...] ] ) AS ( <replaceable class="parameter">column_definition</replaceable> [, ...] )
     <replaceable class="parameter">from_item</replaceable> [ NATURAL ] <replaceable class="parameter">join_type</replaceable> <replaceable class="parameter">from_item</replaceable> [ ON <replaceable class="parameter">join_condition</replaceable> | USING ( <replaceable class="parameter">join_column</replaceable> [, ...] ) ]
 
-and <replaceable class="parameter">with_query</replaceable> is:
+<phrase>and <replaceable class="parameter">with_query</replaceable> is:</phrase>
 
     <replaceable class="parameter">with_query_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] AS ( <replaceable class="parameter">select</replaceable> )
 
 
 SET TRANSACTION <replaceable class="parameter">transaction_mode</replaceable> [, ...]
 SET SESSION CHARACTERISTICS AS TRANSACTION <replaceable class="parameter">transaction_mode</replaceable> [, ...]
 
-where <replaceable class="parameter">transaction_mode</replaceable> is one of:
+<phrase>where <replaceable class="parameter">transaction_mode</replaceable> is one of:</phrase>
 
     ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
     READ WRITE | READ ONLY
 
 <synopsis>
 START TRANSACTION [ <replaceable class="parameter">transaction_mode</replaceable> [, ...] ]
 
-where <replaceable class="parameter">transaction_mode</replaceable> is one of:
+<phrase>where <replaceable class="parameter">transaction_mode</replaceable> is one of:</phrase>
 
     ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
     READ WRITE | READ ONLY
 
 OBJS=  command.o common.o help.o input.o stringutils.o mainloop.o copy.o \
        startup.o prompt.o variables.o large_obj.o print.o describe.o \
        psqlscan.o tab-complete.o mbprint.o dumputils.o keywords.o kwlookup.o \
+       sql_help.o \
        $(WIN32RES)
 
 FLEXFLAGS = -Cfe
 kwlookup.c: % : $(top_srcdir)/src/backend/parser/%
        rm -f $@ && $(LN_S) $< .
 
+sql_help.c: sql_help.h ;
 sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
-       $(PERL) $< $(REFDOCDIR) $@
+       $(PERL) $< $(REFDOCDIR) $*
 
 psqlscan.c: psqlscan.l
 ifdef FLEX
        rm -f psql$(X) $(OBJS) dumputils.c keywords.c kwlookup.c
 
 maintainer-clean: distclean
-       rm -f sql_help.h psqlscan.c
+       rm -f sql_help.h sql_help.c psqlscan.c
 
 # enough that this worked, but this here is by no means an SGML
 # parser.
 #
-# Call: perl create_help.pl docdir sql_help.h
+# Call: perl create_help.pl docdir sql_help
 # The name of the header file doesn't matter to this script, but it
 # sure does matter to the rest of the source.
 #
 use strict;
 
 my $docdir = $ARGV[0] or die "$0: missing required argument: docdir\n";
-my $outputfile = $ARGV[1] or die "$0: missing required argument: output file\n";
+my $hfile = $ARGV[1] . '.h' or die "$0: missing required argument: output file\n";
+my $cfile = $ARGV[1] . '.c';
 
-my $outputfilebasename;
-if ($outputfile =~ m!.*/([^/]+)$!) {
-    $outputfilebasename = $1;
+my $hfilebasename;
+if ($hfile =~ m!.*/([^/]+)$!) {
+    $hfilebasename = $1;
 }
 else {
-    $outputfilebasename = $outputfile;
+    $hfilebasename = $hfile;
 }
 
-my $define = $outputfilebasename;
+my $define = $hfilebasename;
 $define =~ tr/a-z/A-Z/;
 $define =~ s/\W/_/g;
 
 opendir(DIR, $docdir)
     or die "$0: could not open documentation source dir '$docdir': $!\n";
-open(OUT, ">$outputfile")
-    or die "$0: could not open output file '$outputfile': $!\n";
+open(HFILE, ">$hfile")
+    or die "$0: could not open output file '$hfile': $!\n";
+open(CFILE, ">$cfile")
+    or die "$0: could not open output file '$cfile': $!\n";
 
-print OUT
+print HFILE
 "/*
  * *** Do not change this file by hand. It is automatically
  * *** generated from the DocBook documentation.
 
 #define N_(x) (x)                              /* gettext noop */
 
+#include \"postgres_fe.h\"
+#include \"pqexpbuffer.h\"
+
 struct _helpStruct
 {
        const char         *cmd;                /* the command name */
        const char         *help;               /* the help associated with it */
-       const char         *syntax;             /* the syntax associated with it */
+       void (*syntaxfunc)(PQExpBuffer);        /* function that prints the syntax associated with it */
+       int                             nl_count;       /* number of newlines in syntax (for pager) */
 };
 
+";
+
+print CFILE
+"/*
+ * *** Do not change this file by hand. It is automatically
+ * *** generated from the DocBook documentation.
+ *
+ * generated by
+ *     $^X $0 @ARGV
+ *
+ */
+
+#include \"$hfile\"
 
-static const struct _helpStruct QL_HELP[] = {
 ";
 
 my $maxlen = 0;
        $cmddesc =~ s/\s+/ /g;
         $cmddesc =~ s/\"/\\"/g;
 
-       $cmdsynopsis =~ s/<[^>]+>//g;
+        my @params = ();
+
+        my $nl_count = () = $cmdsynopsis =~ /\n/g;
+
+        $cmdsynopsis =~ m!</>! and die "$0:$file: null end tag not supported in synopsis\n";
+        $cmdsynopsis =~ s/%/%%/g;
+
+        while ($cmdsynopsis =~ m!<(\w+)[^>]*>(.+?)</\1[^>]*>!) {
+            my $match = $2;
+           $match =~ s/<[^>]+>//g;
+           $match =~ s/%%/%/g;
+            push @params, $match;
+            $cmdsynopsis =~ s!<(\w+)[^>]*>.+?</\1[^>]*>!%s!;
+        }
        $cmdsynopsis =~ s/\r?\n/\\n/g;
         $cmdsynopsis =~ s/\"/\\"/g;
 
         foreach my $cmdname (@cmdnames) {
-           $entries{$cmdname} = { cmddesc => $cmddesc, cmdsynopsis => $cmdsynopsis };
+           $entries{$cmdname} = { cmddesc => $cmddesc, cmdsynopsis => $cmdsynopsis, params => \@params, nl_count => $nl_count };
            $maxlen = ($maxlen >= length $cmdname) ? $maxlen : length $cmdname;
        }
     }
     }
 }
 
-print OUT "    { \"$_\",\n      N_(\"".$entries{$_}{cmddesc}."\"),\n      N_(\"".$entries{$_}{cmdsynopsis}."\") },\n\n" foreach (sort keys %entries);
+foreach (sort keys %entries) {
+    my $prefix = "\t"x5 . '  ';
+    my $id = $_;
+    $id =~ s/ /_/g;
+    my $synopsis = "\"$entries{$_}{cmdsynopsis}\"";
+    $synopsis =~ s/\\n/\\n"\n$prefix"/g;
+    my @args = ("buf",
+                $synopsis,
+                map("_(\"$_\")", @{$entries{$_}{params}}));
+    print HFILE "extern void sql_help_$id(PQExpBuffer buf);\n";
+    print CFILE "void
+sql_help_$id(PQExpBuffer buf)
+{
+\tappendPQExpBuffer(".join(",\n$prefix", @args).");
+}
+
+";
+}
+
+print HFILE "
+
+static const struct _helpStruct QL_HELP[] = {
+";
+foreach (sort keys %entries) {
+    my $id = $_;
+    $id =~ s/ /_/g;
+    print HFILE "    { \"$_\",
+      N_(\"$entries{$_}{cmddesc}\"),
+      sql_help_$id,
+      $entries{$_}{nl_count} },
+
+";
+}
 
-print OUT "
+print HFILE "
     { NULL, NULL, NULL }    /* End of list marker */
 };
 
 #endif /* $define */
 ";
 
-close OUT;
+close CFILE;
+close HFILE;
 closedir DIR;
 
                size_t          len,
                                        wordlen;
                int                     nl_count = 0;
-               const char *ch;
 
                /* User gets two chances: exact match, then the first word */
 
                                if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
                                        strcmp(topic, "*") == 0)
                                {
-                                       nl_count += 5;
-                                       for (ch = QL_HELP[i].syntax; *ch != '\0'; ch++)
-                                               if (*ch == '\n')
-                                                       nl_count++;
+                                       nl_count += 5 + QL_HELP[i].nl_count;
+
                                        /* If we have an exact match, exit.  Fixes \h SELECT */
                                        if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
                                                break;
                                if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
                                        strcmp(topic, "*") == 0)
                                {
+                                       PQExpBufferData buffer;
+
+                                       initPQExpBuffer(&buffer);
+                                       QL_HELP[i].syntaxfunc(&buffer);
                                        help_found = true;
                                        fprintf(output, _("Command:     %s\n"
                                                                          "Description: %s\n"
                                                                          "Syntax:\n%s\n\n"),
                                                        QL_HELP[i].cmd,
                                                        _(QL_HELP[i].help),
-                                                       _(QL_HELP[i].syntax));
+                                                       buffer.data);
                                        /* If we have an exact match, exit.  Fixes \h SELECT */
                                        if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
                                                break;
 
 CATALOG_NAME   := psql
 AVAIL_LANGUAGES        := cs de es fr ja pt_BR sv tr
 GETTEXT_FILES  := command.c common.c copy.c help.c input.c large_obj.c \
-                   mainloop.c print.c startup.c describe.c sql_help.h \
+                   mainloop.c print.c startup.c describe.c sql_help.h sql_help.c \
                    ../../port/exec.c
 GETTEXT_TRIGGERS:= _ N_ psql_error simple_prompt