Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e12f416

Browse files
committed
Clean up some minor bugs concerning what was inside the main loop
and what wasn't. Also try to improve the comments so that doesn't happen again. Changed SIGPIPE handling to SIG_IGN so that if frontend quits, we will finish out the current command and return to main loop before quitting. This seems much safer than a forced abort mid-command.
1 parent c17b2d1 commit e12f416

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

src/backend/tcop/postgres.c

+46-39
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.89 1998/09/01 04:32:13 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.90 1998/10/02 01:14:14 tgl Exp $
1111
*
1212
* NOTES
1313
* this is the "main" module of the postgres backend and
@@ -223,6 +223,7 @@ InteractiveBackend(char *inBuf)
223223
* ----------------
224224
*/
225225
printf("> ");
226+
fflush(stdout);
226227

227228
for (;;)
228229
{
@@ -295,6 +296,7 @@ InteractiveBackend(char *inBuf)
295296
*/
296297
if (EchoQuery)
297298
printf("query: %s\n", inBuf);
299+
fflush(stdout);
298300

299301
return 'Q';
300302
}
@@ -1398,7 +1400,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
13981400
{
13991401
PS_INIT_STATUS(real_argc, real_argv, argv[0],
14001402
remote_info, userName, DBName);
1401-
PS_SET_STATUS("idle");
1403+
PS_SET_STATUS("startup");
14021404
}
14031405

14041406
/* ----------------
@@ -1465,18 +1467,33 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
14651467
puts("\treset_client_encoding() done.");
14661468
#endif
14671469

1470+
/* ----------------
1471+
* if stable main memory is assumed (-S(old) flag is set), it is necessary
1472+
* to flush all dirty shared buffers before exit
1473+
* plai 8/7/90
1474+
* this used to be done further down, causing an additional entry in
1475+
* the shmem exit list for every error :-( ... tgl 10/1/98
1476+
* ----------------
1477+
*/
1478+
if (!TransactionFlushEnabled())
1479+
on_shmem_exit(FlushBufferPool, NULL);
1480+
14681481
/* ----------------
14691482
* Set up handler for cancel-request signal, and
14701483
* send this backend's cancellation info to the frontend.
14711484
* This should not be done until we are sure startup is successful.
14721485
* ----------------
14731486
*/
14741487

1475-
pqsignal(SIGHUP, read_pg_options); /* upate pg_options from file */
1488+
pqsignal(SIGHUP, read_pg_options); /* update pg_options from file */
14761489
pqsignal(SIGINT, QueryCancelHandler); /* cancel current query */
14771490
pqsignal(SIGQUIT, handle_warn); /* handle error */
14781491
pqsignal(SIGTERM, die);
1479-
pqsignal(SIGPIPE, die);
1492+
pqsignal(SIGPIPE, SIG_IGN); /* ignore failure to write to frontend */
1493+
/* Note: if frontend closes connection, we will notice it and exit cleanly
1494+
* when control next returns to outer loop. This seems safer than forcing
1495+
* exit in the midst of output during who-knows-what operation...
1496+
*/
14801497
pqsignal(SIGUSR1, quickdie);
14811498
pqsignal(SIGUSR2, Async_NotifyHandler); /* flush also sinval cache */
14821499
pqsignal(SIGCHLD, SIG_IGN); /* ignored, sent by LockOwners */
@@ -1491,7 +1508,15 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
14911508
/* Need not flush since ReadyForQuery will do it. */
14921509
}
14931510

1511+
if (!IsUnderPostmaster)
1512+
{
1513+
puts("\nPOSTGRES backend interactive interface ");
1514+
puts("$Revision: 1.90 $ $Date: 1998/10/02 01:14:14 $\n");
1515+
}
1516+
14941517
/* ----------------
1518+
* POSTGRES main processing loop begins here
1519+
*
14951520
* if an exception is encountered, processing resumes here
14961521
* so we abort the current transaction and start a new one.
14971522
* This must be done after we initialize the slave backends
@@ -1512,52 +1537,41 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
15121537
if (Verbose)
15131538
TPRINTF(TRACE_VERBOSE, "AbortCurrentTransaction");
15141539

1515-
MemSet(parser_input, 0, MAX_PARSE_BUFFER);
1516-
15171540
AbortCurrentTransaction();
1518-
15191541
}
15201542

15211543
InError = false;
15221544

1523-
/* ----------------
1524-
* POSTGRES main processing loop begins here
1525-
* ----------------
1526-
*/
1527-
if (!IsUnderPostmaster)
1528-
{
1529-
puts("\nPOSTGRES backend interactive interface");
1530-
puts("$Revision: 1.89 $ $Date: 1998/09/01 04:32:13 $");
1531-
}
1532-
1533-
/* ----------------
1534-
* if stable main memory is assumed (-S(old) flag is set), it is necessary
1535-
* to flush all dirty shared buffers before exit
1536-
* plai 8/7/90
1537-
* ----------------
1545+
/*
1546+
* Non-error queries loop here.
15381547
*/
1539-
if (!TransactionFlushEnabled())
1540-
on_shmem_exit(FlushBufferPool, NULL);
15411548

15421549
for (;;)
15431550
{
1551+
PS_SET_STATUS("idle");
1552+
15441553
/* ----------------
1545-
* (0) tell the frontend we're ready for a new query.
1554+
* (1) tell the frontend we're ready for a new query.
1555+
*
1556+
* Note: this includes fflush()'ing the last of the prior output.
15461557
* ----------------
15471558
*/
15481559
ReadyForQuery(whereToSendOutput);
15491560

15501561
/* ----------------
1551-
* (1) read a command.
1562+
* (2) read a command.
15521563
* ----------------
15531564
*/
15541565
MemSet(parser_input, 0, MAX_PARSE_BUFFER);
15551566

15561567
firstchar = ReadCommand(parser_input);
15571568

1558-
QueryCancel = false;
1569+
QueryCancel = false; /* forget any earlier CANCEL signal */
15591570

1560-
/* process the command */
1571+
/* ----------------
1572+
* (3) process the command.
1573+
* ----------------
1574+
*/
15611575
switch (firstchar)
15621576
{
15631577
/* ----------------
@@ -1571,19 +1585,16 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
15711585
/* start an xact for this function invocation */
15721586
if (Verbose)
15731587
TPRINTF(TRACE_VERBOSE, "StartTransactionCommand");
1574-
15751588
StartTransactionCommand();
1589+
15761590
HandleFunctionRequest();
1577-
PS_SET_STATUS("idle");
15781591
break;
15791592

15801593
/* ----------------
15811594
* 'Q' indicates a user query
15821595
* ----------------
15831596
*/
15841597
case 'Q':
1585-
fflush(stdout);
1586-
15871598
if (strspn(parser_input, " \t\n") == strlen(parser_input))
15881599
{
15891600
/* ----------------
@@ -1610,8 +1621,6 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
16101621

16111622
pg_exec_query(parser_input);
16121623

1613-
PS_SET_STATUS("idle");
1614-
16151624
if (ShowStats)
16161625
ShowUsage();
16171626
}
@@ -1631,7 +1640,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
16311640
}
16321641

16331642
/* ----------------
1634-
* (3) commit the current transaction
1643+
* (4) commit the current transaction
16351644
*
16361645
* Note: if we had an empty input buffer, then we didn't
16371646
* call pg_exec_query, so we don't bother to commit this transaction.
@@ -1643,17 +1652,15 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
16431652
TPRINTF(TRACE_VERBOSE, "CommitTransactionCommand");
16441653
PS_SET_STATUS("commit");
16451654
CommitTransactionCommand();
1646-
PS_SET_STATUS("idle");
1647-
16481655
}
16491656
else
16501657
{
16511658
if (IsUnderPostmaster)
16521659
NullCommand(Remote);
16531660
}
1654-
16551661
} /* infinite for-loop */
1656-
proc_exit(0);
1662+
1663+
proc_exit(0); /* shouldn't get here... */
16571664
return 1;
16581665
}
16591666

0 commit comments

Comments
 (0)