*** ./doc/src/sgml/ref/psql-ref.sgml.orig	2009-12-25 00:36:39.000000000 +0100
--- ./doc/src/sgml/ref/psql-ref.sgml	2010-01-04 10:45:39.350376804 +0100
***************
*** 2335,2340 ****
--- 2335,2361 ----
      </note>
  
      <para>
+     <application>psql</application> provides two additional syntax for 
+     retrieving the content of variable. This auxilary syntax ensure
+     necessary escaping when we would to use content as sql literal or
+     sql identifier.
+ <programlisting>
+ testdb=&gt; <userinput>\set foo 'hello world'</userinput>
+ testdb=&gt; <userinput>\echo :'foo'</userinput>
+ 'hello world'
+ 
+ testdb=&gt; <userinput>\echo :"foo"</userinput>
+ "hello world"
+ 
+ testdb=&gt; <userinput>SELECT :'foo' AS :"foo";</userinput>
+  hello world 
+  -------------
+  hello world
+ (1 row)
+ </programlisting>    
+     </para>
+ 
+     <para>
      If you call <command>\set</command> without a second argument, the
      variable is set, with an empty string as value. To unset (or delete) a
      variable, use the command <command>\unset</command>.
***************
*** 2722,2728 ****
      the variable is copied literally, so it can even contain unbalanced
      quotes or backslash commands. You must make sure that it makes sense
      where you put it. Variable interpolation will not be performed into
!     quoted <acronym>SQL</acronym> entities.
      </para>
  
      <para>
--- 2743,2755 ----
      the variable is copied literally, so it can even contain unbalanced
      quotes or backslash commands. You must make sure that it makes sense
      where you put it. Variable interpolation will not be performed into
!     quoted <acronym>SQL</acronym> entities. Identifiers with special chars
!     have to be inserted between double quotes. <application>psql</application>
!     ensure necessary quoting with additional syntax:
! <programlisting>
! testdb=&gt; <userinput>\set foo 'my tab'</userinput>
! testdb=&gt; <userinput>SELECT * FROM :"foo";</userinput>
! </programlisting>
      </para>
  
      <para>
***************
*** 2752,2757 ****
--- 2779,2793 ----
      at one point you thought it was great that all Unix commands use the
      same escape character.)
      </para>
+     
+     <para>
+     With alternative syntax for retrieving content of variables external
+     escaping are not necessary:
+ <programlisting>
+ testdb=&gt; <userinput>\set content `cat my_file.txt`</userinput>
+ testdb=&gt; <userinput>INSERT INTO my_table VALUES(:'content');</userinput>
+ </programlisting>
+     </para>
  
      <para>
      Since colons can legally appear in SQL commands, the following rule
*** ./src/bin/psql/psqlscan.l.orig	2010-01-02 17:57:59.000000000 +0100
--- ./src/bin/psql/psqlscan.l	2010-01-04 11:48:13.590501254 +0100
***************
*** 47,52 ****
--- 47,53 ----
  #include "settings.h"
  #include "variables.h"
  
+ #include "dumputils.h"
  
  /*
   * We use a stack of flex buffers to handle substitution of psql variables.
***************
*** 707,712 ****
--- 708,769 ----
  					}
  				}
  
+ :'[A-Za-z0-9_]+'	{
+ 					/* 
+ 					 * Possible psql variable substitution
+ 					 * with literal quoting.
+ 					 */
+ 					const char *value;
+ 
+ 					yytext[yyleng - 1] = '\0';
+ 					value = GetVariable(pset.vars, yytext + 2);
+ 
+ 					if (value)
+ 					{
+ 						/* It is a variable, perform substitution */
+ 						PQExpBufferData   buf;
+ 						
+ 						initPQExpBuffer(&buf);
+ 						appendStringLiteralConn(output_buf, value, pset.db);
+ 						push_new_buffer(buf.data);
+ 						termPQExpBuffer(&buf);
+ 						/* yy_scan_string already made buffer active */
+ 					}
+ 					else
+ 					{
+ 						/*
+ 						 * if the variable doesn't exist we'll copy the
+ 						 * string as is
+ 						 */
+ 						ECHO;
+ 					}
+ 				}
+ 
+ :\"[A-Za-z0-9_]+\"	{
+ 					/* Possible psql variable substitution */
+ 					const char *value;
+ 
+ 					/* remove dquotes */
+ 					yytext[yyleng - 1] = '\0';
+ 					value = GetVariable(pset.vars, yytext + 2);
+ 
+ 					if (value)
+ 					{
+ 						/* It is a variable, perform substitution */
+ 						push_new_buffer(fmtId(value));
+ 						/* yy_scan_string already made buffer active */
+ 					}
+ 					else
+ 					{
+ 						/*
+ 						 * if the variable doesn't exist we'll copy the
+ 						 * string as is
+ 						 */
+ 						ECHO;
+ 					}
+ 				}
+ 
+ 
  	/*
  	 * Back to backend-compatible rules.
  	 */
***************
*** 927,932 ****
--- 984,1036 ----
  					return LEXRES_OK;
  				}
  
+ :'[A-Za-z0-9_]*'	{
+ 					/* Possible psql variable substitution */
+ 					if (option_type == OT_VERBATIM)
+ 						ECHO;
+ 					else
+ 					{
+ 						const char *value;
+ 
+ 						yytext[yyleng - 1] = '\0';
+ 						value = GetVariable(pset.vars, yytext + 2);
+ 
+ 						/*
+ 						 * The variable value is just emitted without any
+ 						 * further examination.  This is consistent with the
+ 						 * pre-8.0 code behavior, if not with the way that
+ 						 * variables are handled outside backslash commands.
+ 						 */
+ 						if (value)
+ 							appendStringLiteralConn(output_buf, value, pset.db);
+ 					}
+ 
+ 					*option_quote = ':';
+ 
+ 					return LEXRES_OK;
+ 				}
+ 
+ :\"[A-Za-z0-9_]*\"	{
+ 					/* Possible psql variable substitution */
+ 					if (option_type == OT_VERBATIM)
+ 						ECHO;
+ 					else
+ 					{
+ 						const char *value;
+ 						
+ 						yytext[yyleng - 1] = '\0';
+ 						value = GetVariable(pset.vars, yytext + 2);
+ 
+ 						if (value)
+ 							appendPQExpBufferStr(output_buf, fmtId(value));
+ 					}
+ 
+ 					*option_quote = ':';
+ 
+ 					return LEXRES_OK;
+ 				}
+ 
+ 
  "|"				{
  					ECHO;
  					if (option_type == OT_FILEPIPE)
