77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.127 1999/07/22 02:40:07 tgl Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.128 1999/08/31 04:26:40 tgl Exp $
1111 *
1212 * NOTES
1313 * this is the "main" module of the postgres backend and
@@ -158,9 +158,9 @@ int_exec_repeat_ = 1;
158158 *decls for routines only used in this file
159159 * ----------------------------------------------------------------
160160 */
161- static int InteractiveBackend (char * inBuf );
162- static int SocketBackend (char * inBuf );
163- static int ReadCommand (char * inBuf );
161+ static int InteractiveBackend (StringInfo inBuf );
162+ static int SocketBackend (StringInfo inBuf );
163+ static int ReadCommand (StringInfo inBuf );
164164static void pg_exec_query (char * query_string );
165165
166166
@@ -178,9 +178,8 @@ static void pg_exec_query(char *query_string);
178178 */
179179
180180static int
181- InteractiveBackend (char * inBuf )
181+ InteractiveBackend (StringInfo inBuf )
182182{
183- char * stuff = inBuf ;/* current place in input buffer */
184183int c ;/* character read from getc() */
185184bool end = false;/* end-of-input flag */
186185bool backslashSeen = false;/* have we seen a \ ? */
@@ -192,6 +191,10 @@ InteractiveBackend(char *inBuf)
192191printf ("backend> " );
193192fflush (stdout );
194193
194+ /* Reset inBuf to empty */
195+ inBuf -> len = 0 ;
196+ inBuf -> data [0 ]= '\0' ;
197+
195198for (;;)
196199{
197200if (UseNewLine )
@@ -207,14 +210,15 @@ InteractiveBackend(char *inBuf)
207210{
208211if (backslashSeen )
209212{
210- stuff -- ;
213+ /* discard backslash from inBuf */
214+ inBuf -> data [-- inBuf -> len ]= '\0' ;
215+ backslashSeen = false;
211216continue ;
212217}
213218else
214219{
215220/* keep the newline character */
216- * stuff ++ = '\n' ;
217- * stuff ++ = '\0' ;
221+ appendStringInfoChar (inBuf ,'\n' );
218222break ;
219223}
220224}
@@ -223,7 +227,7 @@ InteractiveBackend(char *inBuf)
223227else
224228backslashSeen = false;
225229
226- * stuff ++ = (char )c ;
230+ appendStringInfoChar ( inBuf , (char )c ) ;
227231}
228232
229233if (c == EOF )
@@ -236,9 +240,9 @@ InteractiveBackend(char *inBuf)
236240 * ----------------
237241 */
238242while ((c = getc (stdin ))!= EOF )
239- * stuff ++ = (char )c ;
243+ appendStringInfoChar ( inBuf , (char )c ) ;
240244
241- if (stuff == inBuf )
245+ if (inBuf -> len == 0 )
242246end = true;
243247}
244248
@@ -261,7 +265,7 @@ InteractiveBackend(char *inBuf)
261265 * ----------------
262266 */
263267if (EchoQuery )
264- printf ("query: %s\n" ,inBuf );
268+ printf ("query: %s\n" ,inBuf -> data );
265269fflush (stdout );
266270
267271return 'Q' ;
@@ -274,15 +278,15 @@ InteractiveBackend(char *inBuf)
274278 *the user is placed in its parameter inBuf.
275279 *
276280 *If the input is a fastpath function call (case 'F') then
277- *the function call is processed in HandleFunctionRequest().
281+ *the function call is processed in HandleFunctionRequest()
278282 *(now called from PostgresMain()).
279283 *
280284 * EOF is returned if the connection is lost.
281285 * ----------------
282286 */
283287
284288static int
285- SocketBackend (char * inBuf )
289+ SocketBackend (StringInfo inBuf )
286290{
287291char qtype ;
288292char result = '\0' ;
@@ -302,7 +306,7 @@ SocketBackend(char *inBuf)
302306 * ----------------
303307 */
304308case 'Q' :
305- if (pq_getstr (inBuf , MAX_PARSE_BUFFER ))
309+ if (pq_getstr (inBuf ))
306310return EOF ;
307311result = 'Q' ;
308312break ;
@@ -312,7 +316,7 @@ SocketBackend(char *inBuf)
312316 * ----------------
313317 */
314318case 'F' :
315- if (pq_getstr (inBuf , MAX_PARSE_BUFFER ))
319+ if (pq_getstr (inBuf ))
316320return EOF ;/* ignore "string" at start of F message */
317321result = 'F' ;
318322break ;
@@ -347,12 +351,21 @@ SocketBackend(char *inBuf)
347351 * ----------------
348352 */
349353static int
350- ReadCommand (char * inBuf )
354+ ReadCommand (StringInfo inBuf )
351355{
356+ MemoryContext oldcontext ;
357+ int result ;
358+
359+ /* Make sure any expansion of inBuf happens in permanent memory context,
360+ * so that we can keep using it for future command cycles.
361+ */
362+ oldcontext = MemoryContextSwitchTo (TopMemoryContext );
352363if (IsUnderPostmaster )
353- return SocketBackend (inBuf );
364+ result = SocketBackend (inBuf );
354365else
355- return InteractiveBackend (inBuf );
366+ result = InteractiveBackend (inBuf );
367+ MemoryContextSwitchTo (oldcontext );
368+ return result ;
356369}
357370
358371List *
@@ -374,45 +387,7 @@ pg_parse_and_plan(char *query_string,/* string to execute */
374387
375388if (DebugPrintQuery )
376389{
377- if (DebugPrintQuery > 3 )
378- {
379- /* Print the query string as is if query debug level > 3 */
380- TPRINTF (TRACE_QUERY ,"query: %s" ,query_string );
381- }
382- else
383- {
384- /* Print condensed query string to fit in one log line */
385- char buff [MAX_QUERY_SIZE + 1 ];
386- char c ,
387- * s ,
388- * d ;
389- int n ,
390- is_space = 1 ;
391-
392- for (s = query_string ,d = buff ,n = 0 ; (c = * s )&& (n < MAX_QUERY_SIZE );s ++ )
393- {
394- switch (c )
395- {
396- case '\r' :
397- case '\n' :
398- case '\t' :
399- c = ' ' ;
400- /* fall through */
401- case ' ' :
402- if (is_space )
403- continue ;
404- is_space = 1 ;
405- break ;
406- default :
407- is_space = 0 ;
408- break ;
409- }
410- * d ++ = c ;
411- n ++ ;
412- }
413- * d = '\0' ;
414- TPRINTF (TRACE_QUERY ,"query: %s" ,buff );
415- }
390+ TPRINTF (TRACE_QUERY ,"query: %s" ,query_string );
416391}
417392
418393/* ----------------
@@ -889,7 +864,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
889864int errs = 0 ;
890865
891866int firstchar ;
892- char parser_input [ MAX_PARSE_BUFFER ] ;
867+ StringInfo parser_input ;
893868char * userName ;
894869
895870/* Used if verbose is set, must be initialized */
@@ -1452,6 +1427,8 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
14521427
14531428on_shmem_exit (remove_all_temp_relations ,NULL );
14541429
1430+ parser_input = makeStringInfo ();/* initialize input buffer */
1431+
14551432/* ----------------
14561433 *Set up handler for cancel-request signal, and
14571434 *send this backend's cancellation info to the frontend.
@@ -1492,7 +1469,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
14921469if (!IsUnderPostmaster )
14931470{
14941471puts ("\nPOSTGRES backend interactive interface " );
1495- puts ("$Revision: 1.127 $ $Date: 1999/07/22 02:40:07 $\n" );
1472+ puts ("$Revision: 1.128 $ $Date: 1999/08/31 04:26:40 $\n" );
14961473}
14971474
14981475/* ----------------
@@ -1548,8 +1525,6 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
15481525 * (3) read a command.
15491526 * ----------------
15501527 */
1551- MemSet (parser_input ,0 ,MAX_PARSE_BUFFER );
1552-
15531528firstchar = ReadCommand (parser_input );
15541529
15551530QueryCancel = false;/* forget any earlier CANCEL signal */
@@ -1592,7 +1567,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
15921567 * ----------------
15931568 */
15941569case 'Q' :
1595- if (strspn (parser_input ," \t\n" )== strlen ( parser_input ) )
1570+ if (strspn (parser_input -> data ," \t\n" )== parser_input -> len )
15961571{
15971572/* ----------------
15981573 *if there is nothing in the input buffer, don't bother
@@ -1616,7 +1591,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
16161591TPRINTF (TRACE_VERBOSE ,"StartTransactionCommand" );
16171592StartTransactionCommand ();
16181593
1619- pg_exec_query (parser_input );
1594+ pg_exec_query (parser_input -> data );
16201595
16211596if (ShowStats )
16221597ShowUsage ();