7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
* NOTES
13
13
* this is the "main" module of the postgres backend and
@@ -158,9 +158,9 @@ int _exec_repeat_ = 1;
158
158
* decls for routines only used in this file
159
159
* ----------------------------------------------------------------
160
160
*/
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 );
164
164
static void pg_exec_query (char * query_string );
165
165
166
166
@@ -178,9 +178,8 @@ static void pg_exec_query(char *query_string);
178
178
*/
179
179
180
180
static int
181
- InteractiveBackend (char * inBuf )
181
+ InteractiveBackend (StringInfo inBuf )
182
182
{
183
- char * stuff = inBuf ; /* current place in input buffer */
184
183
int c ; /* character read from getc() */
185
184
bool end = false; /* end-of-input flag */
186
185
bool backslashSeen = false; /* have we seen a \ ? */
@@ -192,6 +191,10 @@ InteractiveBackend(char *inBuf)
192
191
printf ("backend> " );
193
192
fflush (stdout );
194
193
194
+ /* Reset inBuf to empty */
195
+ inBuf -> len = 0 ;
196
+ inBuf -> data [0 ] = '\0' ;
197
+
195
198
for (;;)
196
199
{
197
200
if (UseNewLine )
@@ -207,14 +210,15 @@ InteractiveBackend(char *inBuf)
207
210
{
208
211
if (backslashSeen )
209
212
{
210
- stuff -- ;
213
+ /* discard backslash from inBuf */
214
+ inBuf -> data [-- inBuf -> len ] = '\0' ;
215
+ backslashSeen = false;
211
216
continue ;
212
217
}
213
218
else
214
219
{
215
220
/* keep the newline character */
216
- * stuff ++ = '\n' ;
217
- * stuff ++ = '\0' ;
221
+ appendStringInfoChar (inBuf , '\n' );
218
222
break ;
219
223
}
220
224
}
@@ -223,7 +227,7 @@ InteractiveBackend(char *inBuf)
223
227
else
224
228
backslashSeen = false;
225
229
226
- * stuff ++ = (char ) c ;
230
+ appendStringInfoChar ( inBuf , (char ) c ) ;
227
231
}
228
232
229
233
if (c == EOF )
@@ -236,9 +240,9 @@ InteractiveBackend(char *inBuf)
236
240
* ----------------
237
241
*/
238
242
while ((c = getc (stdin )) != EOF )
239
- * stuff ++ = (char ) c ;
243
+ appendStringInfoChar ( inBuf , (char ) c ) ;
240
244
241
- if (stuff == inBuf )
245
+ if (inBuf -> len == 0 )
242
246
end = true;
243
247
}
244
248
@@ -261,7 +265,7 @@ InteractiveBackend(char *inBuf)
261
265
* ----------------
262
266
*/
263
267
if (EchoQuery )
264
- printf ("query: %s\n" , inBuf );
268
+ printf ("query: %s\n" , inBuf -> data );
265
269
fflush (stdout );
266
270
267
271
return 'Q' ;
@@ -274,15 +278,15 @@ InteractiveBackend(char *inBuf)
274
278
* the user is placed in its parameter inBuf.
275
279
*
276
280
* 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()
278
282
* (now called from PostgresMain()).
279
283
*
280
284
* EOF is returned if the connection is lost.
281
285
* ----------------
282
286
*/
283
287
284
288
static int
285
- SocketBackend (char * inBuf )
289
+ SocketBackend (StringInfo inBuf )
286
290
{
287
291
char qtype ;
288
292
char result = '\0' ;
@@ -302,7 +306,7 @@ SocketBackend(char *inBuf)
302
306
* ----------------
303
307
*/
304
308
case 'Q' :
305
- if (pq_getstr (inBuf , MAX_PARSE_BUFFER ))
309
+ if (pq_getstr (inBuf ))
306
310
return EOF ;
307
311
result = 'Q' ;
308
312
break ;
@@ -312,7 +316,7 @@ SocketBackend(char *inBuf)
312
316
* ----------------
313
317
*/
314
318
case 'F' :
315
- if (pq_getstr (inBuf , MAX_PARSE_BUFFER ))
319
+ if (pq_getstr (inBuf ))
316
320
return EOF ; /* ignore "string" at start of F message */
317
321
result = 'F' ;
318
322
break ;
@@ -347,12 +351,21 @@ SocketBackend(char *inBuf)
347
351
* ----------------
348
352
*/
349
353
static int
350
- ReadCommand (char * inBuf )
354
+ ReadCommand (StringInfo inBuf )
351
355
{
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 );
352
363
if (IsUnderPostmaster )
353
- return SocketBackend (inBuf );
364
+ result = SocketBackend (inBuf );
354
365
else
355
- return InteractiveBackend (inBuf );
366
+ result = InteractiveBackend (inBuf );
367
+ MemoryContextSwitchTo (oldcontext );
368
+ return result ;
356
369
}
357
370
358
371
List *
@@ -374,45 +387,7 @@ pg_parse_and_plan(char *query_string, /* string to execute */
374
387
375
388
if (DebugPrintQuery )
376
389
{
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 );
416
391
}
417
392
418
393
/* ----------------
@@ -889,7 +864,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
889
864
int errs = 0 ;
890
865
891
866
int firstchar ;
892
- char parser_input [ MAX_PARSE_BUFFER ] ;
867
+ StringInfo parser_input ;
893
868
char * userName ;
894
869
895
870
/* Used if verbose is set, must be initialized */
@@ -1452,6 +1427,8 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
1452
1427
1453
1428
on_shmem_exit (remove_all_temp_relations , NULL );
1454
1429
1430
+ parser_input = makeStringInfo (); /* initialize input buffer */
1431
+
1455
1432
/* ----------------
1456
1433
* Set up handler for cancel-request signal, and
1457
1434
* send this backend's cancellation info to the frontend.
@@ -1492,7 +1469,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
1492
1469
if (!IsUnderPostmaster )
1493
1470
{
1494
1471
puts ("\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" );
1496
1473
}
1497
1474
1498
1475
/* ----------------
@@ -1548,8 +1525,6 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
1548
1525
* (3) read a command.
1549
1526
* ----------------
1550
1527
*/
1551
- MemSet (parser_input , 0 , MAX_PARSE_BUFFER );
1552
-
1553
1528
firstchar = ReadCommand (parser_input );
1554
1529
1555
1530
QueryCancel = false; /* forget any earlier CANCEL signal */
@@ -1592,7 +1567,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
1592
1567
* ----------------
1593
1568
*/
1594
1569
case 'Q' :
1595
- if (strspn (parser_input , " \t\n" ) == strlen ( parser_input ) )
1570
+ if (strspn (parser_input -> data , " \t\n" ) == parser_input -> len )
1596
1571
{
1597
1572
/* ----------------
1598
1573
* 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[])
1616
1591
TPRINTF (TRACE_VERBOSE , "StartTransactionCommand" );
1617
1592
StartTransactionCommand ();
1618
1593
1619
- pg_exec_query (parser_input );
1594
+ pg_exec_query (parser_input -> data );
1620
1595
1621
1596
if (ShowStats )
1622
1597
ShowUsage ();
0 commit comments