]> granicus.if.org Git - postgresql/blob - src/backend/tcop/postgres.c
Remove 576 references of include files that were not needed.
[postgresql] / src / backend / tcop / postgres.c
1 /*-------------------------------------------------------------------------
2  *
3  * postgres.c
4  *        POSTGRES C Backend Interface
5  *
6  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.492 2006/07/14 14:52:23 momjian Exp $
12  *
13  * NOTES
14  *        this is the "main" module of the postgres backend and
15  *        hence the main module of the "traffic cop".
16  *
17  *-------------------------------------------------------------------------
18  */
19
20 #include "postgres.h"
21
22 #include <unistd.h>
23 #include <signal.h>
24 #include <fcntl.h>
25 #include <sys/socket.h>
26 #if HAVE_SYS_SELECT_H
27 #include <sys/select.h>
28 #endif
29 #ifdef HAVE_GETOPT_H
30 #include <getopt.h>
31 #endif
32
33 #include "access/printtup.h"
34 #include "access/xact.h"
35 #include "catalog/pg_type.h"
36 #include "commands/async.h"
37 #include "commands/prepare.h"
38 #include "libpq/libpq.h"
39 #include "libpq/pqformat.h"
40 #include "libpq/pqsignal.h"
41 #include "miscadmin.h"
42 #include "nodes/print.h"
43 #include "optimizer/planner.h"
44 #include "parser/analyze.h"
45 #include "parser/parser.h"
46 #include "rewrite/rewriteHandler.h"
47 #include "storage/freespace.h"
48 #include "storage/ipc.h"
49 #include "storage/proc.h"
50 #include "storage/sinval.h"
51 #include "tcop/fastpath.h"
52 #include "tcop/pquery.h"
53 #include "tcop/tcopprot.h"
54 #include "tcop/utility.h"
55 #include "utils/flatfiles.h"
56 #include "utils/lsyscache.h"
57 #include "utils/memutils.h"
58 #include "utils/ps_status.h"
59 #include "mb/pg_wchar.h"
60
61 #include "pgstat.h"
62
63 extern int      optind;
64 extern char *optarg;
65
66 /* ----------------
67  *              global variables
68  * ----------------
69  */
70 const char *debug_query_string; /* for pgmonitor and log_min_error_statement */
71
72 /* Note: whereToSendOutput is initialized for the bootstrap/standalone case */
73 CommandDest whereToSendOutput = DestDebug;
74
75 /* flag for logging end of session */
76 bool            Log_disconnections = false;
77
78 LogStmtLevel log_statement = LOGSTMT_NONE;
79
80 /* GUC variable for maximum stack depth (measured in kilobytes) */
81 int                     max_stack_depth = 2048;
82
83 /* wait N seconds to allow attach from a debugger */
84 int                     PostAuthDelay = 0;
85
86
87
88 /* ----------------
89  *              private variables
90  * ----------------
91  */
92
93 /* max_stack_depth converted to bytes for speed of checking */
94 static int      max_stack_depth_bytes = 2048 * 1024;
95
96 /*
97  * Stack base pointer -- initialized by PostgresMain. This is not static
98  * so that PL/Java can modify it.
99  */
100 char       *stack_base_ptr = NULL;
101
102
103 /*
104  * Flag to mark SIGHUP. Whenever the main loop comes around it
105  * will reread the configuration file. (Better than doing the
106  * reading in the signal handler, ey?)
107  */
108 static volatile sig_atomic_t got_SIGHUP = false;
109
110 /*
111  * Flag to keep track of whether we have started a transaction.
112  * For extended query protocol this has to be remembered across messages.
113  */
114 static bool xact_started = false;
115
116 /*
117  * Flag to indicate that we are doing the outer loop's read-from-client,
118  * as opposed to any random read from client that might happen within
119  * commands like COPY FROM STDIN.
120  */
121 static bool DoingCommandRead = false;
122
123 /*
124  * Flags to implement skip-till-Sync-after-error behavior for messages of
125  * the extended query protocol.
126  */
127 static bool doing_extended_query_message = false;
128 static bool ignore_till_sync = false;
129
130 /*
131  * If an unnamed prepared statement exists, it's stored here.
132  * We keep it separate from the hashtable kept by commands/prepare.c
133  * in order to reduce overhead for short-lived queries.
134  */
135 static MemoryContext unnamed_stmt_context = NULL;
136 static PreparedStatement *unnamed_stmt_pstmt = NULL;
137
138
139 static bool EchoQuery = false;  /* default don't echo */
140
141 /*
142  * people who want to use EOF should #define DONTUSENEWLINE in
143  * tcop/tcopdebug.h
144  */
145 #ifndef TCOP_DONTUSENEWLINE
146 static int      UseNewLine = 1;         /* Use newlines query delimiters (the default) */
147 #else
148 static int      UseNewLine = 0;         /* Use EOF as query delimiters */
149 #endif   /* TCOP_DONTUSENEWLINE */
150
151
152 /* ----------------------------------------------------------------
153  *              decls for routines only used in this file
154  * ----------------------------------------------------------------
155  */
156 static int      InteractiveBackend(StringInfo inBuf);
157 static int      SocketBackend(StringInfo inBuf);
158 static int      ReadCommand(StringInfo inBuf);
159 static bool log_after_parse(List *raw_parsetree_list,
160                                 const char *query_string, char **prepare_string);
161 static List *pg_rewrite_queries(List *querytree_list);
162 static void start_xact_command(void);
163 static void finish_xact_command(void);
164 static bool IsTransactionExitStmt(Node *parsetree);
165 static bool IsTransactionExitStmtList(List *parseTrees);
166 static bool IsTransactionStmtList(List *parseTrees);
167 static void SigHupHandler(SIGNAL_ARGS);
168 static void log_disconnections(int code, Datum arg);
169
170
171 /* ----------------------------------------------------------------
172  *              routines to obtain user input
173  * ----------------------------------------------------------------
174  */
175
176 /* ----------------
177  *      InteractiveBackend() is called for user interactive connections
178  *
179  *      the string entered by the user is placed in its parameter inBuf,
180  *      and we act like a Q message was received.
181  *
182  *      EOF is returned if end-of-file input is seen; time to shut down.
183  * ----------------
184  */
185
186 static int
187 InteractiveBackend(StringInfo inBuf)
188 {
189         int                     c;                              /* character read from getc() */
190         bool            end = false;    /* end-of-input flag */
191         bool            backslashSeen = false;  /* have we seen a \ ? */
192
193         /*
194          * display a prompt and obtain input from the user
195          */
196         printf("backend> ");
197         fflush(stdout);
198
199         /* Reset inBuf to empty */
200         inBuf->len = 0;
201         inBuf->data[0] = '\0';
202         inBuf->cursor = 0;
203
204         for (;;)
205         {
206                 if (UseNewLine)
207                 {
208                         /*
209                          * if we are using \n as a delimiter, then read characters until
210                          * the \n.
211                          */
212                         while ((c = getc(stdin)) != EOF)
213                         {
214                                 if (c == '\n')
215                                 {
216                                         if (backslashSeen)
217                                         {
218                                                 /* discard backslash from inBuf */
219                                                 inBuf->data[--inBuf->len] = '\0';
220                                                 backslashSeen = false;
221                                                 continue;
222                                         }
223                                         else
224                                         {
225                                                 /* keep the newline character */
226                                                 appendStringInfoChar(inBuf, '\n');
227                                                 break;
228                                         }
229                                 }
230                                 else if (c == '\\')
231                                         backslashSeen = true;
232                                 else
233                                         backslashSeen = false;
234
235                                 appendStringInfoChar(inBuf, (char) c);
236                         }
237
238                         if (c == EOF)
239                                 end = true;
240                 }
241                 else
242                 {
243                         /*
244                          * otherwise read characters until EOF.
245                          */
246                         while ((c = getc(stdin)) != EOF)
247                                 appendStringInfoChar(inBuf, (char) c);
248
249                         if (inBuf->len == 0)
250                                 end = true;
251                 }
252
253                 if (end)
254                         return EOF;
255
256                 /*
257                  * otherwise we have a user query so process it.
258                  */
259                 break;
260         }
261
262         /* Add '\0' to make it look the same as message case. */
263         appendStringInfoChar(inBuf, (char) '\0');
264
265         /*
266          * if the query echo flag was given, print the query..
267          */
268         if (EchoQuery)
269                 printf("statement: %s\n", inBuf->data);
270         fflush(stdout);
271
272         return 'Q';
273 }
274
275 /* ----------------
276  *      SocketBackend()         Is called for frontend-backend connections
277  *
278  *      Returns the message type code, and loads message body data into inBuf.
279  *
280  *      EOF is returned if the connection is lost.
281  * ----------------
282  */
283 static int
284 SocketBackend(StringInfo inBuf)
285 {
286         int                     qtype;
287
288         /*
289          * Get message type code from the frontend.
290          */
291         qtype = pq_getbyte();
292
293         if (qtype == EOF)                       /* frontend disconnected */
294         {
295                 ereport(COMMERROR,
296                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
297                                  errmsg("unexpected EOF on client connection")));
298                 return qtype;
299         }
300
301         /*
302          * Validate message type code before trying to read body; if we have lost
303          * sync, better to say "command unknown" than to run out of memory because
304          * we used garbage as a length word.
305          *
306          * This also gives us a place to set the doing_extended_query_message flag
307          * as soon as possible.
308          */
309         switch (qtype)
310         {
311                 case 'Q':                               /* simple query */
312                         doing_extended_query_message = false;
313                         if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
314                         {
315                                 /* old style without length word; convert */
316                                 if (pq_getstring(inBuf))
317                                 {
318                                         ereport(COMMERROR,
319                                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
320                                                          errmsg("unexpected EOF on client connection")));
321                                         return EOF;
322                                 }
323                         }
324                         break;
325
326                 case 'F':                               /* fastpath function call */
327                         /* we let fastpath.c cope with old-style input of this */
328                         doing_extended_query_message = false;
329                         break;
330
331                 case 'X':                               /* terminate */
332                         doing_extended_query_message = false;
333                         ignore_till_sync = false;
334                         break;
335
336                 case 'B':                               /* bind */
337                 case 'C':                               /* close */
338                 case 'D':                               /* describe */
339                 case 'E':                               /* execute */
340                 case 'H':                               /* flush */
341                 case 'P':                               /* parse */
342                         doing_extended_query_message = true;
343                         /* these are only legal in protocol 3 */
344                         if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
345                                 ereport(FATAL,
346                                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
347                                                  errmsg("invalid frontend message type %d", qtype)));
348                         break;
349
350                 case 'S':                               /* sync */
351                         /* stop any active skip-till-Sync */
352                         ignore_till_sync = false;
353                         /* mark not-extended, so that a new error doesn't begin skip */
354                         doing_extended_query_message = false;
355                         /* only legal in protocol 3 */
356                         if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
357                                 ereport(FATAL,
358                                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
359                                                  errmsg("invalid frontend message type %d", qtype)));
360                         break;
361
362                 case 'd':                               /* copy data */
363                 case 'c':                               /* copy done */
364                 case 'f':                               /* copy fail */
365                         doing_extended_query_message = false;
366                         /* these are only legal in protocol 3 */
367                         if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
368                                 ereport(FATAL,
369                                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
370                                                  errmsg("invalid frontend message type %d", qtype)));
371                         break;
372
373                 default:
374
375                         /*
376                          * Otherwise we got garbage from the frontend.  We treat this as
377                          * fatal because we have probably lost message boundary sync, and
378                          * there's no good way to recover.
379                          */
380                         ereport(FATAL,
381                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
382                                          errmsg("invalid frontend message type %d", qtype)));
383                         break;
384         }
385
386         /*
387          * In protocol version 3, all frontend messages have a length word next
388          * after the type code; we can read the message contents independently of
389          * the type.
390          */
391         if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
392         {
393                 if (pq_getmessage(inBuf, 0))
394                         return EOF;                     /* suitable message already logged */
395         }
396
397         return qtype;
398 }
399
400 /* ----------------
401  *              ReadCommand reads a command from either the frontend or
402  *              standard input, places it in inBuf, and returns the
403  *              message type code (first byte of the message).
404  *              EOF is returned if end of file.
405  * ----------------
406  */
407 static int
408 ReadCommand(StringInfo inBuf)
409 {
410         int                     result;
411
412         if (whereToSendOutput == DestRemote)
413                 result = SocketBackend(inBuf);
414         else
415                 result = InteractiveBackend(inBuf);
416         return result;
417 }
418
419 /*
420  * prepare_for_client_read -- set up to possibly block on client input
421  *
422  * This must be called immediately before any low-level read from the
423  * client connection.  It is necessary to do it at a sufficiently low level
424  * that there won't be any other operations except the read kernel call
425  * itself between this call and the subsequent client_read_ended() call.
426  * In particular there mustn't be use of malloc() or other potentially
427  * non-reentrant libc functions.  This restriction makes it safe for us
428  * to allow interrupt service routines to execute nontrivial code while
429  * we are waiting for input.
430  */
431 void
432 prepare_for_client_read(void)
433 {
434         if (DoingCommandRead)
435         {
436                 /* Enable immediate processing of asynchronous signals */
437                 EnableNotifyInterrupt();
438                 EnableCatchupInterrupt();
439
440                 /* Allow "die" interrupt to be processed while waiting */
441                 ImmediateInterruptOK = true;
442
443                 /* And don't forget to detect one that already arrived */
444                 QueryCancelPending = false;
445                 CHECK_FOR_INTERRUPTS();
446         }
447 }
448
449 /*
450  * client_read_ended -- get out of the client-input state
451  */
452 void
453 client_read_ended(void)
454 {
455         if (DoingCommandRead)
456         {
457                 ImmediateInterruptOK = false;
458                 QueryCancelPending = false;             /* forget any CANCEL signal */
459
460                 DisableNotifyInterrupt();
461                 DisableCatchupInterrupt();
462         }
463 }
464
465
466 /*
467  * Parse a query string and pass it through the rewriter.
468  *
469  * A list of Query nodes is returned, since the string might contain
470  * multiple queries and/or the rewriter might expand one query to several.
471  *
472  * NOTE: this routine is no longer used for processing interactive queries,
473  * but it is still needed for parsing of SQL function bodies.
474  */
475 List *
476 pg_parse_and_rewrite(const char *query_string,  /* string to execute */
477                                          Oid *paramTypes,       /* parameter types */
478                                          int numParams)         /* number of parameters */
479 {
480         List       *raw_parsetree_list;
481         List       *querytree_list;
482         ListCell   *list_item;
483
484         /*
485          * (1) parse the request string into a list of raw parse trees.
486          */
487         raw_parsetree_list = pg_parse_query(query_string);
488
489         /*
490          * (2) Do parse analysis and rule rewrite.
491          */
492         querytree_list = NIL;
493         foreach(list_item, raw_parsetree_list)
494         {
495                 Node       *parsetree = (Node *) lfirst(list_item);
496
497                 querytree_list = list_concat(querytree_list,
498                                                                          pg_analyze_and_rewrite(parsetree,
499                                                                                                                         query_string,
500                                                                                                                         paramTypes,
501                                                                                                                         numParams));
502         }
503
504         return querytree_list;
505 }
506
507 /*
508  * Do raw parsing (only).
509  *
510  * A list of parsetrees is returned, since there might be multiple
511  * commands in the given string.
512  *
513  * NOTE: for interactive queries, it is important to keep this routine
514  * separate from the analysis & rewrite stages.  Analysis and rewriting
515  * cannot be done in an aborted transaction, since they require access to
516  * database tables.  So, we rely on the raw parser to determine whether
517  * we've seen a COMMIT or ABORT command; when we are in abort state, other
518  * commands are not processed any further than the raw parse stage.
519  */
520 List *
521 pg_parse_query(const char *query_string)
522 {
523         List       *raw_parsetree_list;
524
525         if (log_parser_stats)
526                 ResetUsage();
527
528         raw_parsetree_list = raw_parser(query_string);
529
530         if (log_parser_stats)
531                 ShowUsage("PARSER STATISTICS");
532
533         return raw_parsetree_list;
534 }
535
536 static bool
537 log_after_parse(List *raw_parsetree_list, const char *query_string,
538                                 char **prepare_string)
539 {
540         ListCell   *parsetree_item;
541         bool            log_this_statement = (log_statement == LOGSTMT_ALL);
542
543         *prepare_string = NULL;
544
545         /* Check if we need to log the statement, and get prepare_string. */
546         foreach(parsetree_item, raw_parsetree_list)
547         {
548                 Node       *parsetree = (Node *) lfirst(parsetree_item);
549                 const char *commandTag;
550
551                 if (IsA(parsetree, ExplainStmt) &&
552                         ((ExplainStmt *) parsetree)->analyze)
553                         parsetree = (Node *) (((ExplainStmt *) parsetree)->query);
554
555                 if (IsA(parsetree, PrepareStmt))
556                         parsetree = (Node *) (((PrepareStmt *) parsetree)->query);
557
558                 if (IsA(parsetree, SelectStmt) &&
559                         ((SelectStmt *) parsetree)->into == NULL)
560                         continue;                       /* optimization for frequent command */
561
562                 if (log_statement == LOGSTMT_MOD &&
563                         (IsA(parsetree, InsertStmt) ||
564                          IsA(parsetree, UpdateStmt) ||
565                          IsA(parsetree, DeleteStmt) ||
566                          IsA(parsetree, TruncateStmt) ||
567                          (IsA(parsetree, CopyStmt) &&
568                           ((CopyStmt *) parsetree)->is_from)))          /* COPY FROM */
569                         log_this_statement = true;
570
571                 commandTag = CreateCommandTag(parsetree);
572                 if ((log_statement == LOGSTMT_MOD ||
573                          log_statement == LOGSTMT_DDL) &&
574                         (strncmp(commandTag, "CREATE ", strlen("CREATE ")) == 0 ||
575                          IsA(parsetree, SelectStmt) ||          /* SELECT INTO, CREATE AS */
576                          strncmp(commandTag, "ALTER ", strlen("ALTER ")) == 0 ||
577                          strncmp(commandTag, "DROP ", strlen("DROP ")) == 0 ||
578                          IsA(parsetree, GrantStmt) ||           /* GRANT or REVOKE */
579                          IsA(parsetree, CommentStmt)))
580                         log_this_statement = true;
581
582                 /*
583                  * For the first EXECUTE we find, record the client statement used by
584                  * the PREPARE.  PREPARE doesn't save the parse tree so we have no
585                  * way to conditionally output based on the type of query prepared.
586                  */
587                 if (IsA(parsetree, ExecuteStmt))
588                 {
589                         ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
590                         PreparedStatement *entry;
591
592                         if (*prepare_string == NULL &&
593                                 (entry = FetchPreparedStatement(stmt->name, false)) != NULL &&
594                                 entry->query_string)
595                         {
596                                 *prepare_string = palloc(strlen(entry->query_string) +
597                                                                           strlen("  [PREPARE:  %s]") - 2 + 1);
598                                 sprintf(*prepare_string, "  [PREPARE:  %s]",
599                                                 entry->query_string);
600                         }
601                 }
602         }
603
604         if (log_this_statement)
605         {
606                 ereport(LOG,
607                                 (errmsg("statement: %s%s", query_string,
608                                                 *prepare_string ? *prepare_string : "")));
609                 return true;
610         }
611         else
612                 return false;
613 }
614
615
616 /*
617  * Given a raw parsetree (gram.y output), and optionally information about
618  * types of parameter symbols ($n), perform parse analysis and rule rewriting.
619  *
620  * A list of Query nodes is returned, since either the analyzer or the
621  * rewriter might expand one query to several.
622  *
623  * NOTE: for reasons mentioned above, this must be separate from raw parsing.
624  */
625 List *
626 pg_analyze_and_rewrite(Node *parsetree, const char *query_string,
627                                            Oid *paramTypes, int numParams)
628 {
629         List       *querytree_list;
630
631         /*
632          * (1) Perform parse analysis.
633          */
634         if (log_parser_stats)
635                 ResetUsage();
636
637         querytree_list = parse_analyze(parsetree, query_string,
638                                                                    paramTypes, numParams);
639
640         if (log_parser_stats)
641                 ShowUsage("PARSE ANALYSIS STATISTICS");
642
643         /*
644          * (2) Rewrite the queries, as necessary
645          */
646         querytree_list = pg_rewrite_queries(querytree_list);
647
648         return querytree_list;
649 }
650
651 /*
652  * Perform rewriting of a list of queries produced by parse analysis.
653  *
654  * Note: queries must just have come from the parser, because we do not do
655  * AcquireRewriteLocks() on them.
656  */
657 static List *
658 pg_rewrite_queries(List *querytree_list)
659 {
660         List       *new_list = NIL;
661         ListCell   *list_item;
662
663         if (log_parser_stats)
664                 ResetUsage();
665
666         /*
667          * rewritten queries are collected in new_list.  Note there may be more or
668          * fewer than in the original list.
669          */
670         foreach(list_item, querytree_list)
671         {
672                 Query      *querytree = (Query *) lfirst(list_item);
673
674                 if (Debug_print_parse)
675                         elog_node_display(DEBUG1, "parse tree", querytree,
676                                                           Debug_pretty_print);
677
678                 if (querytree->commandType == CMD_UTILITY)
679                 {
680                         /* don't rewrite utilities, just dump 'em into new_list */
681                         new_list = lappend(new_list, querytree);
682                 }
683                 else
684                 {
685                         /* rewrite regular queries */
686                         List       *rewritten = QueryRewrite(querytree);
687
688                         new_list = list_concat(new_list, rewritten);
689                 }
690         }
691
692         querytree_list = new_list;
693
694         if (log_parser_stats)
695                 ShowUsage("REWRITER STATISTICS");
696
697 #ifdef COPY_PARSE_PLAN_TREES
698
699         /*
700          * Optional debugging check: pass querytree output through copyObject()
701          */
702         new_list = (List *) copyObject(querytree_list);
703         /* This checks both copyObject() and the equal() routines... */
704         if (!equal(new_list, querytree_list))
705                 elog(WARNING, "copyObject() failed to produce an equal parse tree");
706         else
707                 querytree_list = new_list;
708 #endif
709
710         if (Debug_print_rewritten)
711                 elog_node_display(DEBUG1, "rewritten parse tree", querytree_list,
712                                                   Debug_pretty_print);
713
714         return querytree_list;
715 }
716
717
718 /* Generate a plan for a single already-rewritten query. */
719 Plan *
720 pg_plan_query(Query *querytree, ParamListInfo boundParams)
721 {
722         Plan       *plan;
723
724         /* Utility commands have no plans. */
725         if (querytree->commandType == CMD_UTILITY)
726                 return NULL;
727
728         if (log_planner_stats)
729                 ResetUsage();
730
731         /* call the optimizer */
732         plan = planner(querytree, false, 0, boundParams);
733
734         if (log_planner_stats)
735                 ShowUsage("PLANNER STATISTICS");
736
737 #ifdef COPY_PARSE_PLAN_TREES
738         /* Optional debugging check: pass plan output through copyObject() */
739         {
740                 Plan       *new_plan = (Plan *) copyObject(plan);
741
742                 /*
743                  * equal() currently does not have routines to compare Plan nodes, so
744                  * don't try to test equality here.  Perhaps fix someday?
745                  */
746 #ifdef NOT_USED
747                 /* This checks both copyObject() and the equal() routines... */
748                 if (!equal(new_plan, plan))
749                         elog(WARNING, "copyObject() failed to produce an equal plan tree");
750                 else
751 #endif
752                         plan = new_plan;
753         }
754 #endif
755
756         /*
757          * Print plan if debugging.
758          */
759         if (Debug_print_plan)
760                 elog_node_display(DEBUG1, "plan", plan, Debug_pretty_print);
761
762         return plan;
763 }
764
765 /*
766  * Generate plans for a list of already-rewritten queries.
767  *
768  * If needSnapshot is TRUE, we haven't yet set a snapshot for the current
769  * query.  A snapshot must be set before invoking the planner, since it
770  * might try to evaluate user-defined functions.  But we must not set a
771  * snapshot if the list contains only utility statements, because some
772  * utility statements depend on not having frozen the snapshot yet.
773  * (We assume that such statements cannot appear together with plannable
774  * statements in the rewriter's output.)
775  */
776 List *
777 pg_plan_queries(List *querytrees, ParamListInfo boundParams,
778                                 bool needSnapshot)
779 {
780         List       *plan_list = NIL;
781         ListCell   *query_list;
782
783         foreach(query_list, querytrees)
784         {
785                 Query      *query = (Query *) lfirst(query_list);
786                 Plan       *plan;
787
788                 if (query->commandType == CMD_UTILITY)
789                 {
790                         /* Utility commands have no plans. */
791                         plan = NULL;
792                 }
793                 else
794                 {
795                         if (needSnapshot)
796                         {
797                                 ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
798                                 needSnapshot = false;
799                         }
800                         plan = pg_plan_query(query, boundParams);
801                 }
802
803                 plan_list = lappend(plan_list, plan);
804         }
805
806         return plan_list;
807 }
808
809
810 /*
811  * exec_simple_query
812  *
813  * Execute a "simple Query" protocol message.
814  */
815 static void
816 exec_simple_query(const char *query_string)
817 {
818         CommandDest dest = whereToSendOutput;
819         MemoryContext oldcontext;
820         List       *parsetree_list;
821         ListCell   *parsetree_item;
822         bool            save_log_statement_stats = log_statement_stats;
823         char       *prepare_string = NULL;
824         bool            was_logged = false;
825
826         /*
827          * Report query to various monitoring facilities.
828          */
829         debug_query_string = query_string;
830
831         pgstat_report_activity(query_string);
832
833         /*
834          * We use save_log_statement_stats so ShowUsage doesn't report incorrect
835          * results because ResetUsage wasn't called.
836          */
837         if (save_log_statement_stats)
838                 ResetUsage();
839
840         /*
841          * Start up a transaction command.      All queries generated by the
842          * query_string will be in this same command block, *unless* we find a
843          * BEGIN/COMMIT/ABORT statement; we have to force a new xact command after
844          * one of those, else bad things will happen in xact.c. (Note that this
845          * will normally change current memory context.)
846          */
847         start_xact_command();
848
849         /*
850          * Zap any pre-existing unnamed statement.      (While not strictly necessary,
851          * it seems best to define simple-Query mode as if it used the unnamed
852          * statement and portal; this ensures we recover any storage used by prior
853          * unnamed operations.)
854          */
855         unnamed_stmt_pstmt = NULL;
856         if (unnamed_stmt_context)
857         {
858                 DropDependentPortals(unnamed_stmt_context);
859                 MemoryContextDelete(unnamed_stmt_context);
860         }
861         unnamed_stmt_context = NULL;
862
863         /*
864          * Switch to appropriate context for constructing parsetrees.
865          */
866         oldcontext = MemoryContextSwitchTo(MessageContext);
867
868         QueryContext = CurrentMemoryContext;
869
870         /*
871          * Do basic parsing of the query or queries (this should be safe even if
872          * we are in aborted transaction state!)
873          */
874         parsetree_list = pg_parse_query(query_string);
875
876         /* Log immediately if dictated by log_statement */
877         if (log_statement != LOGSTMT_NONE)
878                 was_logged = log_after_parse(parsetree_list, query_string,
879                                                                          &prepare_string);
880
881         /*
882          * Switch back to transaction context to enter the loop.
883          */
884         MemoryContextSwitchTo(oldcontext);
885
886         /*
887          * Run through the raw parsetree(s) and process each one.
888          */
889         foreach(parsetree_item, parsetree_list)
890         {
891                 Node       *parsetree = (Node *) lfirst(parsetree_item);
892                 const char *commandTag;
893                 char            completionTag[COMPLETION_TAG_BUFSIZE];
894                 List       *querytree_list,
895                                    *plantree_list;
896                 Portal          portal;
897                 DestReceiver *receiver;
898                 int16           format;
899
900                 /*
901                  * Get the command name for use in status display (it also becomes the
902                  * default completion tag, down inside PortalRun).      Set ps_status and
903                  * do any special start-of-SQL-command processing needed by the
904                  * destination.
905                  */
906                 commandTag = CreateCommandTag(parsetree);
907
908                 set_ps_display(commandTag, false);
909
910                 BeginCommand(commandTag, dest);
911
912                 /*
913                  * If we are in an aborted transaction, reject all commands except
914                  * COMMIT/ABORT.  It is important that this test occur before we try
915                  * to do parse analysis, rewrite, or planning, since all those phases
916                  * try to do database accesses, which may fail in abort state. (It
917                  * might be safe to allow some additional utility commands in this
918                  * state, but not many...)
919                  */
920                 if (IsAbortedTransactionBlockState() &&
921                         !IsTransactionExitStmt(parsetree))
922                         ereport(ERROR,
923                                         (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
924                                          errmsg("current transaction is aborted, "
925                                                 "commands ignored until end of transaction block")));
926
927                 /* Make sure we are in a transaction command */
928                 start_xact_command();
929
930                 /* If we got a cancel signal in parsing or prior command, quit */
931                 CHECK_FOR_INTERRUPTS();
932
933                 /*
934                  * OK to analyze, rewrite, and plan this query.
935                  *
936                  * Switch to appropriate context for constructing querytrees (again,
937                  * these must outlive the execution context).
938                  */
939                 oldcontext = MemoryContextSwitchTo(MessageContext);
940
941                 querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
942                                                                                                 NULL, 0);
943
944                 plantree_list = pg_plan_queries(querytree_list, NULL, true);
945
946                 /* If we got a cancel signal in analysis or planning, quit */
947                 CHECK_FOR_INTERRUPTS();
948
949                 /*
950                  * Create unnamed portal to run the query or queries in. If there
951                  * already is one, silently drop it.
952                  */
953                 portal = CreatePortal("", true, true);
954                 /* Don't display the portal in pg_cursors */
955                 portal->visible = false;
956
957                 PortalDefineQuery(portal,
958                                                   query_string,
959                                                   commandTag,
960                                                   querytree_list,
961                                                   plantree_list,
962                                                   MessageContext);
963
964                 /*
965                  * Start the portal.  No parameters here.
966                  */
967                 PortalStart(portal, NULL, InvalidSnapshot);
968
969                 /*
970                  * Select the appropriate output format: text unless we are doing a
971                  * FETCH from a binary cursor.  (Pretty grotty to have to do this here
972                  * --- but it avoids grottiness in other places.  Ah, the joys of
973                  * backward compatibility...)
974                  */
975                 format = 0;                             /* TEXT is default */
976                 if (IsA(parsetree, FetchStmt))
977                 {
978                         FetchStmt  *stmt = (FetchStmt *) parsetree;
979
980                         if (!stmt->ismove)
981                         {
982                                 Portal          fportal = GetPortalByName(stmt->portalname);
983
984                                 if (PortalIsValid(fportal) &&
985                                         (fportal->cursorOptions & CURSOR_OPT_BINARY))
986                                         format = 1; /* BINARY */
987                         }
988                 }
989                 PortalSetResultFormat(portal, 1, &format);
990
991                 /*
992                  * Now we can create the destination receiver object.
993                  */
994                 receiver = CreateDestReceiver(dest, portal);
995
996                 /*
997                  * Switch back to transaction context for execution.
998                  */
999                 MemoryContextSwitchTo(oldcontext);
1000
1001                 /*
1002                  * Run the portal to completion, and then drop it (and the receiver).
1003                  */
1004                 (void) PortalRun(portal,
1005                                                  FETCH_ALL,
1006                                                  receiver,
1007                                                  receiver,
1008                                                  completionTag);
1009
1010                 (*receiver->rDestroy) (receiver);
1011
1012                 PortalDrop(portal, false);
1013
1014                 if (IsA(parsetree, TransactionStmt))
1015                 {
1016                         /*
1017                          * If this was a transaction control statement, commit it. We will
1018                          * start a new xact command for the next command (if any).
1019                          */
1020                         finish_xact_command();
1021                 }
1022                 else if (lnext(parsetree_item) == NULL)
1023                 {
1024                         /*
1025                          * If this is the last parsetree of the query string, close down
1026                          * transaction statement before reporting command-complete.  This
1027                          * is so that any end-of-transaction errors are reported before
1028                          * the command-complete message is issued, to avoid confusing
1029                          * clients who will expect either a command-complete message or an
1030                          * error, not one and then the other.  But for compatibility with
1031                          * historical Postgres behavior, we do not force a transaction
1032                          * boundary between queries appearing in a single query string.
1033                          */
1034                         finish_xact_command();
1035                 }
1036                 else
1037                 {
1038                         /*
1039                          * We need a CommandCounterIncrement after every query, except
1040                          * those that start or end a transaction block.
1041                          */
1042                         CommandCounterIncrement();
1043                 }
1044
1045                 /*
1046                  * Tell client that we're done with this query.  Note we emit exactly
1047                  * one EndCommand report for each raw parsetree, thus one for each SQL
1048                  * command the client sent, regardless of rewriting. (But a command
1049                  * aborted by error will not send an EndCommand report at all.)
1050                  */
1051                 EndCommand(completionTag, dest);
1052         }                                                       /* end loop over parsetrees */
1053
1054         /*
1055          * Close down transaction statement, if one is open.
1056          */
1057         finish_xact_command();
1058
1059         /*
1060          * If there were no parsetrees, return EmptyQueryResponse message.
1061          */
1062         if (!parsetree_list)
1063                 NullCommand(dest);
1064
1065         QueryContext = NULL;
1066
1067         /*
1068          * Emit duration logging if appropriate.
1069          */
1070         if (log_duration || log_min_duration_statement >= 0)
1071         {
1072                 long            secs;
1073                 int                     usecs;
1074                 int                     msecs;
1075
1076                 TimestampDifference(GetCurrentStatementStartTimestamp(),
1077                                                         GetCurrentTimestamp(),
1078                                                         &secs, &usecs);
1079                 msecs = usecs / 1000;
1080
1081                 /*
1082                  * The odd-looking test for log_min_duration_statement being
1083                  * exceeded is designed to avoid integer overflow with very
1084                  * long durations: don't compute secs * 1000 until we've
1085                  * verified it will fit in int.
1086                  */
1087                 if (log_duration ||
1088                         log_min_duration_statement == 0 ||
1089                         (log_min_duration_statement > 0 &&
1090                          (secs > log_min_duration_statement / 1000 ||
1091                           secs * 1000 + msecs >= log_min_duration_statement)))
1092                 {
1093                         if (was_logged)
1094                                 ereport(LOG,
1095                                                 (errmsg("duration: %ld.%03d ms",
1096                                                                 secs, msecs)));
1097                         else
1098                                 ereport(LOG,
1099                                                 (errmsg("duration: %ld.%03d ms  statement: %s%s",
1100                                                                 secs, msecs,
1101                                                                 query_string,
1102                                                                 prepare_string ? prepare_string : "")));
1103                 }
1104         }
1105
1106         if (save_log_statement_stats)
1107                 ShowUsage("QUERY STATISTICS");
1108
1109         if (prepare_string != NULL)
1110                 pfree(prepare_string);
1111
1112         debug_query_string = NULL;
1113 }
1114
1115 /*
1116  * exec_parse_message
1117  *
1118  * Execute a "Parse" protocol message.
1119  */
1120 static void
1121 exec_parse_message(const char *query_string,    /* string to execute */
1122                                    const char *stmt_name,               /* name for prepared stmt */
1123                                    Oid *paramTypes,             /* parameter types */
1124                                    int numParams)               /* number of parameters */
1125 {
1126         MemoryContext oldcontext;
1127         List       *parsetree_list;
1128         const char *commandTag;
1129         List       *querytree_list,
1130                            *plantree_list,
1131                            *param_list;
1132         bool            is_named;
1133         bool            save_log_statement_stats = log_statement_stats;
1134
1135         /*
1136          * Report query to various monitoring facilities.
1137          */
1138         debug_query_string = query_string;
1139
1140         pgstat_report_activity(query_string);
1141
1142         set_ps_display("PARSE", false);
1143
1144         if (save_log_statement_stats)
1145                 ResetUsage();
1146
1147         if (log_statement == LOGSTMT_ALL)
1148                 ereport(LOG,
1149                                 (errmsg("statement: [protocol] PREPARE %s AS %s",
1150                                                 (*stmt_name != '\0') ? stmt_name : "<unnamed>",
1151                                                 query_string)));
1152
1153         /*
1154          * Start up a transaction command so we can run parse analysis etc. (Note
1155          * that this will normally change current memory context.) Nothing happens
1156          * if we are already in one.
1157          */
1158         start_xact_command();
1159
1160         /*
1161          * Switch to appropriate context for constructing parsetrees.
1162          *
1163          * We have two strategies depending on whether the prepared statement is
1164          * named or not.  For a named prepared statement, we do parsing in
1165          * MessageContext and copy the finished trees into the prepared
1166          * statement's private context; then the reset of MessageContext releases
1167          * temporary space used by parsing and planning.  For an unnamed prepared
1168          * statement, we assume the statement isn't going to hang around long, so
1169          * getting rid of temp space quickly is probably not worth the costs of
1170          * copying parse/plan trees.  So in this case, we set up a special context
1171          * for the unnamed statement, and do all the parsing/planning therein.
1172          */
1173         is_named = (stmt_name[0] != '\0');
1174         if (is_named)
1175         {
1176                 /* Named prepared statement --- parse in MessageContext */
1177                 oldcontext = MemoryContextSwitchTo(MessageContext);
1178         }
1179         else
1180         {
1181                 /* Unnamed prepared statement --- release any prior unnamed stmt */
1182                 unnamed_stmt_pstmt = NULL;
1183                 if (unnamed_stmt_context)
1184                 {
1185                         DropDependentPortals(unnamed_stmt_context);
1186                         MemoryContextDelete(unnamed_stmt_context);
1187                 }
1188                 unnamed_stmt_context = NULL;
1189                 /* create context for parsing/planning */
1190                 unnamed_stmt_context =
1191                         AllocSetContextCreate(TopMemoryContext,
1192                                                                   "unnamed prepared statement",
1193                                                                   ALLOCSET_DEFAULT_MINSIZE,
1194                                                                   ALLOCSET_DEFAULT_INITSIZE,
1195                                                                   ALLOCSET_DEFAULT_MAXSIZE);
1196                 oldcontext = MemoryContextSwitchTo(unnamed_stmt_context);
1197         }
1198
1199         QueryContext = CurrentMemoryContext;
1200
1201         /*
1202          * Do basic parsing of the query or queries (this should be safe even if
1203          * we are in aborted transaction state!)
1204          */
1205         parsetree_list = pg_parse_query(query_string);
1206
1207         /*
1208          * We only allow a single user statement in a prepared statement. This is
1209          * mainly to keep the protocol simple --- otherwise we'd need to worry
1210          * about multiple result tupdescs and things like that.
1211          */
1212         if (list_length(parsetree_list) > 1)
1213                 ereport(ERROR,
1214                                 (errcode(ERRCODE_SYNTAX_ERROR),
1215                 errmsg("cannot insert multiple commands into a prepared statement")));
1216
1217         if (parsetree_list != NIL)
1218         {
1219                 Node       *parsetree = (Node *) linitial(parsetree_list);
1220                 int                     i;
1221
1222                 /*
1223                  * Get the command name for possible use in status display.
1224                  */
1225                 commandTag = CreateCommandTag(parsetree);
1226
1227                 /*
1228                  * If we are in an aborted transaction, reject all commands except
1229                  * COMMIT/ROLLBACK.  It is important that this test occur before we
1230                  * try to do parse analysis, rewrite, or planning, since all those
1231                  * phases try to do database accesses, which may fail in abort state.
1232                  * (It might be safe to allow some additional utility commands in this
1233                  * state, but not many...)
1234                  */
1235                 if (IsAbortedTransactionBlockState() &&
1236                         !IsTransactionExitStmt(parsetree))
1237                         ereport(ERROR,
1238                                         (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1239                                          errmsg("current transaction is aborted, "
1240                                                 "commands ignored until end of transaction block")));
1241
1242                 /*
1243                  * OK to analyze, rewrite, and plan this query.  Note that the
1244                  * originally specified parameter set is not required to be complete,
1245                  * so we have to use parse_analyze_varparams().
1246                  */
1247                 if (log_parser_stats)
1248                         ResetUsage();
1249
1250                 querytree_list = parse_analyze_varparams(parsetree,
1251                                                                                                  query_string,
1252                                                                                                  &paramTypes,
1253                                                                                                  &numParams);
1254
1255                 /*
1256                  * Check all parameter types got determined, and convert array
1257                  * representation to a list for storage.
1258                  */
1259                 param_list = NIL;
1260                 for (i = 0; i < numParams; i++)
1261                 {
1262                         Oid                     ptype = paramTypes[i];
1263
1264                         if (ptype == InvalidOid || ptype == UNKNOWNOID)
1265                                 ereport(ERROR,
1266                                                 (errcode(ERRCODE_INDETERMINATE_DATATYPE),
1267                                          errmsg("could not determine data type of parameter $%d",
1268                                                         i + 1)));
1269                         param_list = lappend_oid(param_list, ptype);
1270                 }
1271
1272                 if (log_parser_stats)
1273                         ShowUsage("PARSE ANALYSIS STATISTICS");
1274
1275                 querytree_list = pg_rewrite_queries(querytree_list);
1276
1277                 /*
1278                  * If this is the unnamed statement and it has parameters, defer query
1279                  * planning until Bind.  Otherwise do it now.
1280                  */
1281                 if (!is_named && numParams > 0)
1282                         plantree_list = NIL;
1283                 else
1284                         plantree_list = pg_plan_queries(querytree_list, NULL, true);
1285         }
1286         else
1287         {
1288                 /* Empty input string.  This is legal. */
1289                 commandTag = NULL;
1290                 querytree_list = NIL;
1291                 plantree_list = NIL;
1292                 param_list = NIL;
1293         }
1294
1295         /* If we got a cancel signal in analysis or planning, quit */
1296         CHECK_FOR_INTERRUPTS();
1297
1298         /*
1299          * Store the query as a prepared statement.  See above comments.
1300          */
1301         if (is_named)
1302         {
1303                 StorePreparedStatement(stmt_name,
1304                                                            query_string,
1305                                                            commandTag,
1306                                                            querytree_list,
1307                                                            plantree_list,
1308                                                            param_list,
1309                                                            false);
1310         }
1311         else
1312         {
1313                 PreparedStatement *pstmt;
1314
1315                 pstmt = (PreparedStatement *) palloc0(sizeof(PreparedStatement));
1316                 /* query_string needs to be copied into unnamed_stmt_context */
1317                 pstmt->query_string = pstrdup(query_string);
1318                 /* the rest is there already */
1319                 pstmt->commandTag = commandTag;
1320                 pstmt->query_list = querytree_list;
1321                 pstmt->plan_list = plantree_list;
1322                 pstmt->argtype_list = param_list;
1323                 pstmt->from_sql = false;
1324                 pstmt->context = unnamed_stmt_context;
1325                 /* Now the unnamed statement is complete and valid */
1326                 unnamed_stmt_pstmt = pstmt;
1327         }
1328
1329         MemoryContextSwitchTo(oldcontext);
1330
1331         QueryContext = NULL;
1332
1333         /*
1334          * We do NOT close the open transaction command here; that only happens
1335          * when the client sends Sync.  Instead, do CommandCounterIncrement just
1336          * in case something happened during parse/plan.
1337          */
1338         CommandCounterIncrement();
1339
1340         /*
1341          * Send ParseComplete.
1342          */
1343         if (whereToSendOutput == DestRemote)
1344                 pq_putemptymessage('1');
1345
1346         if (save_log_statement_stats)
1347                 ShowUsage("PARSE MESSAGE STATISTICS");
1348
1349         debug_query_string = NULL;
1350 }
1351
1352 /*
1353  * exec_bind_message
1354  *
1355  * Process a "Bind" message to create a portal from a prepared statement
1356  */
1357 static void
1358 exec_bind_message(StringInfo input_message)
1359 {
1360         const char *portal_name;
1361         const char *stmt_name;
1362         int                     numPFormats;
1363         int16      *pformats = NULL;
1364         int                     numParams;
1365         int                     numRFormats;
1366         int16      *rformats = NULL;
1367         int                     i;
1368         PreparedStatement *pstmt;
1369         Portal          portal;
1370         ParamListInfo params;
1371
1372         pgstat_report_activity("<BIND>");
1373
1374         set_ps_display("BIND", false);
1375
1376         /*
1377          * Start up a transaction command so we can call functions etc. (Note that
1378          * this will normally change current memory context.) Nothing happens if
1379          * we are already in one.
1380          */
1381         start_xact_command();
1382
1383         /* Switch back to message context */
1384         MemoryContextSwitchTo(MessageContext);
1385
1386         /* Get the fixed part of the message */
1387         portal_name = pq_getmsgstring(input_message);
1388         stmt_name = pq_getmsgstring(input_message);
1389
1390         /* Get the parameter format codes */
1391         numPFormats = pq_getmsgint(input_message, 2);
1392         if (numPFormats > 0)
1393         {
1394                 pformats = (int16 *) palloc(numPFormats * sizeof(int16));
1395                 for (i = 0; i < numPFormats; i++)
1396                         pformats[i] = pq_getmsgint(input_message, 2);
1397         }
1398
1399         /* Get the parameter value count */
1400         numParams = pq_getmsgint(input_message, 2);
1401
1402         if (numPFormats > 1 && numPFormats != numParams)
1403                 ereport(ERROR,
1404                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1405                         errmsg("bind message has %d parameter formats but %d parameters",
1406                                    numPFormats, numParams)));
1407
1408         /* Find prepared statement */
1409         if (stmt_name[0] != '\0')
1410                 pstmt = FetchPreparedStatement(stmt_name, true);
1411         else
1412         {
1413                 /* special-case the unnamed statement */
1414                 pstmt = unnamed_stmt_pstmt;
1415                 if (!pstmt)
1416                         ereport(ERROR,
1417                                         (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
1418                                          errmsg("unnamed prepared statement does not exist")));
1419         }
1420
1421         if (numParams != list_length(pstmt->argtype_list))
1422                 ereport(ERROR,
1423                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1424                                  errmsg("bind message supplies %d parameters, but prepared statement \"%s\" requires %d",
1425                                    numParams, stmt_name, list_length(pstmt->argtype_list))));
1426
1427         /*
1428          * If we are in aborted transaction state, the only portals we can
1429          * actually run are those containing COMMIT or ROLLBACK commands. We
1430          * disallow binding anything else to avoid problems with infrastructure
1431          * that expects to run inside a valid transaction.      We also disallow
1432          * binding any parameters, since we can't risk calling user-defined I/O
1433          * functions.
1434          */
1435         if (IsAbortedTransactionBlockState() &&
1436                 (!IsTransactionExitStmtList(pstmt->query_list) ||
1437                  numParams != 0))
1438                 ereport(ERROR,
1439                                 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1440                                  errmsg("current transaction is aborted, "
1441                                                 "commands ignored until end of transaction block")));
1442
1443         /*
1444          * Create the portal.  Allow silent replacement of an existing portal only
1445          * if the unnamed portal is specified.
1446          */
1447         if (portal_name[0] == '\0')
1448                 portal = CreatePortal(portal_name, true, true);
1449         else
1450                 portal = CreatePortal(portal_name, false, false);
1451
1452         /* We need to output the parameter values someday */
1453         if (log_statement == LOGSTMT_ALL)
1454                 ereport(LOG,
1455                                 (errmsg("statement: [protocol] <BIND> %s", portal_name)));
1456
1457         /*
1458          * Fetch parameters, if any, and store in the portal's memory context.
1459          */
1460         if (numParams > 0)
1461         {
1462                 ListCell   *l;
1463                 MemoryContext oldContext;
1464
1465                 oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
1466
1467                 /* sizeof(ParamListInfoData) includes the first array element */
1468                 params = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
1469                                                                 (numParams - 1) * sizeof(ParamExternData));
1470                 params->numParams = numParams;
1471
1472                 i = 0;
1473                 foreach(l, pstmt->argtype_list)
1474                 {
1475                         Oid                     ptype = lfirst_oid(l);
1476                         int32           plength;
1477                         bool            isNull;
1478                         StringInfoData pbuf;
1479                         char            csave;
1480                         int16           pformat;
1481
1482                         plength = pq_getmsgint(input_message, 4);
1483                         isNull = (plength == -1);
1484
1485                         if (!isNull)
1486                         {
1487                                 const char *pvalue = pq_getmsgbytes(input_message, plength);
1488
1489                                 /*
1490                                  * Rather than copying data around, we just set up a phony
1491                                  * StringInfo pointing to the correct portion of the message
1492                                  * buffer.      We assume we can scribble on the message buffer so
1493                                  * as to maintain the convention that StringInfos have a
1494                                  * trailing null.  This is grotty but is a big win when
1495                                  * dealing with very large parameter strings.
1496                                  */
1497                                 pbuf.data = (char *) pvalue;
1498                                 pbuf.maxlen = plength + 1;
1499                                 pbuf.len = plength;
1500                                 pbuf.cursor = 0;
1501
1502                                 csave = pbuf.data[plength];
1503                                 pbuf.data[plength] = '\0';
1504                         }
1505                         else
1506                         {
1507                                 pbuf.data = NULL;               /* keep compiler quiet */
1508                                 csave = 0;
1509                         }
1510
1511                         if (numPFormats > 1)
1512                                 pformat = pformats[i];
1513                         else if (numPFormats > 0)
1514                                 pformat = pformats[0];
1515                         else
1516                                 pformat = 0;    /* default = text */
1517
1518                         if (pformat == 0)
1519                         {
1520                                 Oid                     typinput;
1521                                 Oid                     typioparam;
1522                                 char       *pstring;
1523
1524                                 getTypeInputInfo(ptype, &typinput, &typioparam);
1525
1526                                 /*
1527                                  * We have to do encoding conversion before calling the
1528                                  * typinput routine.
1529                                  */
1530                                 if (isNull)
1531                                         pstring = NULL;
1532                                 else
1533                                         pstring = pg_client_to_server(pbuf.data, plength);
1534
1535                                 params->params[i].value = OidInputFunctionCall(typinput,
1536                                                                                                                            pstring,
1537                                                                                                                            typioparam,
1538                                                                                                                            -1);
1539                                 /* Free result of encoding conversion, if any */
1540                                 if (pstring && pstring != pbuf.data)
1541                                         pfree(pstring);
1542                         }
1543                         else if (pformat == 1)
1544                         {
1545                                 Oid                     typreceive;
1546                                 Oid                     typioparam;
1547                                 StringInfo      bufptr;
1548
1549                                 /*
1550                                  * Call the parameter type's binary input converter
1551                                  */
1552                                 getTypeBinaryInputInfo(ptype, &typreceive, &typioparam);
1553
1554                                 if (isNull)
1555                                         bufptr = NULL;
1556                                 else
1557                                         bufptr = &pbuf;
1558
1559                                 params->params[i].value = OidReceiveFunctionCall(typreceive,
1560                                                                                                                                  bufptr,
1561                                                                                                                                  typioparam,
1562                                                                                                                                  -1);
1563
1564                                 /* Trouble if it didn't eat the whole buffer */
1565                                 if (!isNull && pbuf.cursor != pbuf.len)
1566                                         ereport(ERROR,
1567                                                         (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1568                                                          errmsg("incorrect binary data format in bind parameter %d",
1569                                                                         i + 1)));
1570                         }
1571                         else
1572                         {
1573                                 ereport(ERROR,
1574                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1575                                                  errmsg("unsupported format code: %d",
1576                                                                 pformat)));
1577                         }
1578
1579                         /* Restore message buffer contents */
1580                         if (!isNull)
1581                                 pbuf.data[plength] = csave;
1582
1583                         params->params[i].isnull = isNull;
1584                         params->params[i].ptype = ptype;
1585
1586                         i++;
1587                 }
1588
1589                 MemoryContextSwitchTo(oldContext);
1590         }
1591         else
1592                 params = NULL;
1593
1594         /* Get the result format codes */
1595         numRFormats = pq_getmsgint(input_message, 2);
1596         if (numRFormats > 0)
1597         {
1598                 rformats = (int16 *) palloc(numRFormats * sizeof(int16));
1599                 for (i = 0; i < numRFormats; i++)
1600                         rformats[i] = pq_getmsgint(input_message, 2);
1601         }
1602
1603         pq_getmsgend(input_message);
1604
1605         /*
1606          * If we didn't plan the query before, do it now.  This allows the planner
1607          * to make use of the concrete parameter values we now have.
1608          *
1609          * This happens only for unnamed statements, and so switching into the
1610          * statement context for planning is correct (see notes in
1611          * exec_parse_message).
1612          */
1613         if (pstmt->plan_list == NIL && pstmt->query_list != NIL)
1614         {
1615                 MemoryContext oldContext = MemoryContextSwitchTo(pstmt->context);
1616
1617                 pstmt->plan_list = pg_plan_queries(pstmt->query_list, params, true);
1618                 MemoryContextSwitchTo(oldContext);
1619         }
1620
1621         /*
1622          * Define portal and start execution.
1623          */
1624         PortalDefineQuery(portal,
1625                                           pstmt->query_string,
1626                                           pstmt->commandTag,
1627                                           pstmt->query_list,
1628                                           pstmt->plan_list,
1629                                           pstmt->context);
1630
1631         PortalStart(portal, params, InvalidSnapshot);
1632
1633         /*
1634          * Apply the result format requests to the portal.
1635          */
1636         PortalSetResultFormat(portal, numRFormats, rformats);
1637
1638         /*
1639          * Send BindComplete.
1640          */
1641         if (whereToSendOutput == DestRemote)
1642                 pq_putemptymessage('2');
1643 }
1644
1645 /*
1646  * exec_execute_message
1647  *
1648  * Process an "Execute" message for a portal
1649  */
1650 static void
1651 exec_execute_message(const char *portal_name, long max_rows)
1652 {
1653         CommandDest dest;
1654         DestReceiver *receiver;
1655         Portal          portal;
1656         bool            completed;
1657         char            completionTag[COMPLETION_TAG_BUFSIZE];
1658         bool            save_log_statement_stats = log_statement_stats;
1659         bool            execute_is_fetch = false;
1660
1661         /* Adjust destination to tell printtup.c what to do */
1662         dest = whereToSendOutput;
1663         if (dest == DestRemote)
1664                 dest = DestRemoteExecute;
1665
1666         portal = GetPortalByName(portal_name);
1667         if (!PortalIsValid(portal))
1668                 ereport(ERROR,
1669                                 (errcode(ERRCODE_UNDEFINED_CURSOR),
1670                                  errmsg("portal \"%s\" does not exist", portal_name)));
1671
1672         /*
1673          * If we re-issue an Execute protocol request against an existing portal,
1674          * then we are only fetching more rows rather than completely re-executing
1675          * the query from the start. atStart is never reset for a v3 portal, so we
1676          * are safe to use this check.
1677          */
1678         if (!portal->atStart)
1679                 execute_is_fetch = true;
1680
1681         /*
1682          * If the original query was a null string, just return
1683          * EmptyQueryResponse.
1684          */
1685         if (portal->commandTag == NULL)
1686         {
1687                 Assert(portal->parseTrees == NIL);
1688                 NullCommand(dest);
1689                 return;
1690         }
1691
1692         /* Should we display the portal names here? */
1693         if (execute_is_fetch)
1694         {
1695                 debug_query_string = "fetch message";
1696                 pgstat_report_activity("<FETCH>");
1697         }
1698         else if (portal->sourceText)
1699         {
1700                 debug_query_string = portal->sourceText;
1701                 pgstat_report_activity(portal->sourceText);
1702         }
1703         else
1704         {
1705                 debug_query_string = "execute message";
1706                 pgstat_report_activity("<EXECUTE>");
1707         }
1708
1709         set_ps_display(portal->commandTag, false);
1710
1711         /*
1712          * We use save_log_statement_stats so ShowUsage doesn't report incorrect
1713          * results because ResetUsage wasn't called.
1714          */
1715         if (save_log_statement_stats)
1716                 ResetUsage();
1717
1718         if (log_statement == LOGSTMT_ALL)
1719                 /* We have the portal, so output the source query. */
1720                 ereport(LOG,
1721                                 (errmsg("statement: [protocol] %sEXECUTE %s  [PREPARE:  %s]",
1722                                                 execute_is_fetch ? "FETCH from " : "",
1723                                                 (*portal_name) ? portal_name : "<unnamed>",
1724                                                 portal->sourceText ? portal->sourceText : "")));
1725
1726         BeginCommand(portal->commandTag, dest);
1727
1728         /*
1729          * Create dest receiver in MessageContext (we don't want it in transaction
1730          * context, because that may get deleted if portal contains VACUUM).
1731          */
1732         receiver = CreateDestReceiver(dest, portal);
1733
1734         /*
1735          * Ensure we are in a transaction command (this should normally be the
1736          * case already due to prior BIND).
1737          */
1738         start_xact_command();
1739
1740         /*
1741          * If we are in aborted transaction state, the only portals we can
1742          * actually run are those containing COMMIT or ROLLBACK commands.
1743          */
1744         if (IsAbortedTransactionBlockState() &&
1745                 !IsTransactionExitStmtList(portal->parseTrees))
1746                 ereport(ERROR,
1747                                 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1748                                  errmsg("current transaction is aborted, "
1749                                                 "commands ignored until end of transaction block")));
1750
1751         /* Check for cancel signal before we start execution */
1752         CHECK_FOR_INTERRUPTS();
1753
1754         /*
1755          * Okay to run the portal.
1756          */
1757         if (max_rows <= 0)
1758                 max_rows = FETCH_ALL;
1759
1760         completed = PortalRun(portal,
1761                                                   max_rows,
1762                                                   receiver,
1763                                                   receiver,
1764                                                   completionTag);
1765
1766         (*receiver->rDestroy) (receiver);
1767
1768         if (completed)
1769         {
1770                 if (IsTransactionStmtList(portal->parseTrees))
1771                 {
1772                         /*
1773                          * If this was a transaction control statement, commit it.      We
1774                          * will start a new xact command for the next command (if any).
1775                          */
1776                         finish_xact_command();
1777                 }
1778                 else
1779                 {
1780                         /*
1781                          * We need a CommandCounterIncrement after every query, except
1782                          * those that start or end a transaction block.
1783                          */
1784                         CommandCounterIncrement();
1785                 }
1786
1787                 /* Send appropriate CommandComplete to client */
1788                 EndCommand(completionTag, dest);
1789         }
1790         else
1791         {
1792                 /* Portal run not complete, so send PortalSuspended */
1793                 if (whereToSendOutput == DestRemote)
1794                         pq_putemptymessage('s');
1795         }
1796
1797         /*
1798          * Emit duration logging if appropriate.
1799          */
1800         if (log_duration || log_min_duration_statement >= 0)
1801         {
1802                 long            secs;
1803                 int                     usecs;
1804                 int                     msecs;
1805
1806                 TimestampDifference(GetCurrentStatementStartTimestamp(),
1807                                                         GetCurrentTimestamp(),
1808                                                         &secs, &usecs);
1809                 msecs = usecs / 1000;
1810
1811                 /*
1812                  * The odd-looking test for log_min_duration_statement being
1813                  * exceeded is designed to avoid integer overflow with very
1814                  * long durations: don't compute secs * 1000 until we've
1815                  * verified it will fit in int.
1816                  */
1817                 if (log_duration ||
1818                         log_min_duration_statement == 0 ||
1819                         (log_min_duration_statement > 0 &&
1820                          (secs > log_min_duration_statement / 1000 ||
1821                           secs * 1000 + msecs >= log_min_duration_statement)))
1822                 {
1823                         if (log_statement == LOGSTMT_ALL)       /* already logged? */
1824                                 ereport(LOG,
1825                                                 (errmsg("duration: %ld.%03d ms",
1826                                                                 secs, msecs)));
1827                         else
1828                                 ereport(LOG,
1829                                                 (errmsg("duration: %ld.%03d ms  statement: [protocol] %sEXECUTE %s  [PREPARE:  %s]",
1830                                                                 secs, msecs,
1831                                                                 execute_is_fetch ? "FETCH from " : "",
1832                                                                 (*portal_name) ? portal_name : "<unnamed>",
1833                                                         portal->sourceText ? portal->sourceText : "")));
1834                 }
1835         }
1836
1837         if (save_log_statement_stats)
1838                 ShowUsage("QUERY STATISTICS");
1839
1840         debug_query_string = NULL;
1841 }
1842
1843 /*
1844  * exec_describe_statement_message
1845  *
1846  * Process a "Describe" message for a prepared statement
1847  */
1848 static void
1849 exec_describe_statement_message(const char *stmt_name)
1850 {
1851         PreparedStatement *pstmt;
1852         TupleDesc       tupdesc;
1853         ListCell   *l;
1854         StringInfoData buf;
1855
1856         /*
1857          * Start up a transaction command. (Note that this will normally change
1858          * current memory context.) Nothing happens if we are already in one.
1859          */
1860         start_xact_command();
1861
1862         /* Switch back to message context */
1863         MemoryContextSwitchTo(MessageContext);
1864
1865         /* Find prepared statement */
1866         if (stmt_name[0] != '\0')
1867                 pstmt = FetchPreparedStatement(stmt_name, true);
1868         else
1869         {
1870                 /* special-case the unnamed statement */
1871                 pstmt = unnamed_stmt_pstmt;
1872                 if (!pstmt)
1873                         ereport(ERROR,
1874                                         (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
1875                                          errmsg("unnamed prepared statement does not exist")));
1876         }
1877
1878         /*
1879          * If we are in aborted transaction state, we can't safely create a result
1880          * tupledesc, because that needs catalog accesses.  Hence, refuse to
1881          * Describe statements that return data.  (We shouldn't just refuse all
1882          * Describes, since that might break the ability of some clients to issue
1883          * COMMIT or ROLLBACK commands, if they use code that blindly Describes
1884          * whatever it does.)  We can Describe parameters without doing anything
1885          * dangerous, so we don't restrict that.
1886          */
1887         if (IsAbortedTransactionBlockState() &&
1888                 PreparedStatementReturnsTuples(pstmt))
1889                 ereport(ERROR,
1890                                 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1891                                  errmsg("current transaction is aborted, "
1892                                                 "commands ignored until end of transaction block")));
1893
1894         if (whereToSendOutput != DestRemote)
1895                 return;                                 /* can't actually do anything... */
1896
1897         /*
1898          * First describe the parameters...
1899          */
1900         pq_beginmessage(&buf, 't'); /* parameter description message type */
1901         pq_sendint(&buf, list_length(pstmt->argtype_list), 2);
1902
1903         foreach(l, pstmt->argtype_list)
1904         {
1905                 Oid                     ptype = lfirst_oid(l);
1906
1907                 pq_sendint(&buf, (int) ptype, 4);
1908         }
1909         pq_endmessage(&buf);
1910
1911         /*
1912          * Next send RowDescription or NoData to describe the result...
1913          */
1914         tupdesc = FetchPreparedStatementResultDesc(pstmt);
1915         if (tupdesc)
1916                 SendRowDescriptionMessage(tupdesc,
1917                                                                   FetchPreparedStatementTargetList(pstmt),
1918                                                                   NULL);
1919         else
1920                 pq_putemptymessage('n');        /* NoData */
1921
1922 }
1923
1924 /*
1925  * exec_describe_portal_message
1926  *
1927  * Process a "Describe" message for a portal
1928  */
1929 static void
1930 exec_describe_portal_message(const char *portal_name)
1931 {
1932         Portal          portal;
1933
1934         /*
1935          * Start up a transaction command. (Note that this will normally change
1936          * current memory context.) Nothing happens if we are already in one.
1937          */
1938         start_xact_command();
1939
1940         /* Switch back to message context */
1941         MemoryContextSwitchTo(MessageContext);
1942
1943         portal = GetPortalByName(portal_name);
1944         if (!PortalIsValid(portal))
1945                 ereport(ERROR,
1946                                 (errcode(ERRCODE_UNDEFINED_CURSOR),
1947                                  errmsg("portal \"%s\" does not exist", portal_name)));
1948
1949         /*
1950          * If we are in aborted transaction state, we can't run
1951          * SendRowDescriptionMessage(), because that needs catalog accesses.
1952          * Hence, refuse to Describe portals that return data.  (We shouldn't just
1953          * refuse all Describes, since that might break the ability of some
1954          * clients to issue COMMIT or ROLLBACK commands, if they use code that
1955          * blindly Describes whatever it does.)
1956          */
1957         if (IsAbortedTransactionBlockState() &&
1958                 portal->tupDesc)
1959                 ereport(ERROR,
1960                                 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1961                                  errmsg("current transaction is aborted, "
1962                                                 "commands ignored until end of transaction block")));
1963
1964         if (whereToSendOutput != DestRemote)
1965                 return;                                 /* can't actually do anything... */
1966
1967         if (portal->tupDesc)
1968                 SendRowDescriptionMessage(portal->tupDesc,
1969                                                                   FetchPortalTargetList(portal),
1970                                                                   portal->formats);
1971         else
1972                 pq_putemptymessage('n');        /* NoData */
1973 }
1974
1975
1976 /*
1977  * Convenience routines for starting/committing a single command.
1978  */
1979 static void
1980 start_xact_command(void)
1981 {
1982         if (!xact_started)
1983         {
1984                 ereport(DEBUG3,
1985                                 (errmsg_internal("StartTransactionCommand")));
1986                 StartTransactionCommand();
1987
1988                 /* Set statement timeout running, if any */
1989                 /* NB: this mustn't be enabled until we are within an xact */
1990                 if (StatementTimeout > 0)
1991                         enable_sig_alarm(StatementTimeout, true);
1992                 else
1993                         cancel_from_timeout = false;
1994
1995                 xact_started = true;
1996         }
1997 }
1998
1999 static void
2000 finish_xact_command(void)
2001 {
2002         if (xact_started)
2003         {
2004                 /* Cancel any active statement timeout before committing */
2005                 disable_sig_alarm(true);
2006
2007                 /* Now commit the command */
2008                 ereport(DEBUG3,
2009                                 (errmsg_internal("CommitTransactionCommand")));
2010
2011                 CommitTransactionCommand();
2012
2013 #ifdef MEMORY_CONTEXT_CHECKING
2014                 /* Check all memory contexts that weren't freed during commit */
2015                 /* (those that were, were checked before being deleted) */
2016                 MemoryContextCheck(TopMemoryContext);
2017 #endif
2018
2019 #ifdef SHOW_MEMORY_STATS
2020                 /* Print mem stats after each commit for leak tracking */
2021                 if (ShowStats)
2022                         MemoryContextStats(TopMemoryContext);
2023 #endif
2024
2025                 xact_started = false;
2026         }
2027 }
2028
2029
2030 /*
2031  * Convenience routines for checking whether a statement is one of the
2032  * ones that we allow in transaction-aborted state.
2033  */
2034
2035 static bool
2036 IsTransactionExitStmt(Node *parsetree)
2037 {
2038         if (parsetree && IsA(parsetree, TransactionStmt))
2039         {
2040                 TransactionStmt *stmt = (TransactionStmt *) parsetree;
2041
2042                 if (stmt->kind == TRANS_STMT_COMMIT ||
2043                         stmt->kind == TRANS_STMT_PREPARE ||
2044                         stmt->kind == TRANS_STMT_ROLLBACK ||
2045                         stmt->kind == TRANS_STMT_ROLLBACK_TO)
2046                         return true;
2047         }
2048         return false;
2049 }
2050
2051 static bool
2052 IsTransactionExitStmtList(List *parseTrees)
2053 {
2054         if (list_length(parseTrees) == 1)
2055         {
2056                 Query      *query = (Query *) linitial(parseTrees);
2057
2058                 if (query->commandType == CMD_UTILITY &&
2059                         IsTransactionExitStmt(query->utilityStmt))
2060                         return true;
2061         }
2062         return false;
2063 }
2064
2065 static bool
2066 IsTransactionStmtList(List *parseTrees)
2067 {
2068         if (list_length(parseTrees) == 1)
2069         {
2070                 Query      *query = (Query *) linitial(parseTrees);
2071
2072                 if (query->commandType == CMD_UTILITY &&
2073                         query->utilityStmt && IsA(query->utilityStmt, TransactionStmt))
2074                         return true;
2075         }
2076         return false;
2077 }
2078
2079
2080 /* --------------------------------
2081  *              signal handler routines used in PostgresMain()
2082  * --------------------------------
2083  */
2084
2085 /*
2086  * quickdie() occurs when signalled SIGQUIT by the postmaster.
2087  *
2088  * Some backend has bought the farm,
2089  * so we need to stop what we're doing and exit.
2090  */
2091 void
2092 quickdie(SIGNAL_ARGS)
2093 {
2094         PG_SETMASK(&BlockSig);
2095
2096         /*
2097          * Ideally this should be ereport(FATAL), but then we'd not get control
2098          * back...
2099          */
2100         ereport(WARNING,
2101                         (errcode(ERRCODE_CRASH_SHUTDOWN),
2102                          errmsg("terminating connection because of crash of another server process"),
2103         errdetail("The postmaster has commanded this server process to roll back"
2104                           " the current transaction and exit, because another"
2105                           " server process exited abnormally and possibly corrupted"
2106                           " shared memory."),
2107                          errhint("In a moment you should be able to reconnect to the"
2108                                          " database and repeat your command.")));
2109
2110         /*
2111          * DO NOT proc_exit() -- we're here because shared memory may be
2112          * corrupted, so we don't want to try to clean up our transaction. Just
2113          * nail the windows shut and get out of town.
2114          *
2115          * Note we do exit(1) not exit(0).      This is to force the postmaster into a
2116          * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
2117          * backend.  This is necessary precisely because we don't clean up our
2118          * shared memory state.
2119          */
2120         exit(1);
2121 }
2122
2123 /*
2124  * Shutdown signal from postmaster: abort transaction and exit
2125  * at soonest convenient time
2126  */
2127 void
2128 die(SIGNAL_ARGS)
2129 {
2130         int                     save_errno = errno;
2131
2132         /* Don't joggle the elbow of proc_exit */
2133         if (!proc_exit_inprogress)
2134         {
2135                 InterruptPending = true;
2136                 ProcDiePending = true;
2137
2138                 /*
2139                  * If it's safe to interrupt, and we're waiting for input or a lock,
2140                  * service the interrupt immediately
2141                  */
2142                 if (ImmediateInterruptOK && InterruptHoldoffCount == 0 &&
2143                         CritSectionCount == 0)
2144                 {
2145                         /* bump holdoff count to make ProcessInterrupts() a no-op */
2146                         /* until we are done getting ready for it */
2147                         InterruptHoldoffCount++;
2148                         DisableNotifyInterrupt();
2149                         DisableCatchupInterrupt();
2150                         /* Make sure CheckDeadLock won't run while shutting down... */
2151                         LockWaitCancel();
2152                         InterruptHoldoffCount--;
2153                         ProcessInterrupts();
2154                 }
2155         }
2156
2157         errno = save_errno;
2158 }
2159
2160 /*
2161  * Timeout or shutdown signal from postmaster during client authentication.
2162  * Simply exit(0).
2163  *
2164  * XXX: possible future improvement: try to send a message indicating
2165  * why we are disconnecting.  Problem is to be sure we don't block while
2166  * doing so, nor mess up the authentication message exchange.
2167  */
2168 void
2169 authdie(SIGNAL_ARGS)
2170 {
2171         exit(0);
2172 }
2173
2174 /*
2175  * Query-cancel signal from postmaster: abort current transaction
2176  * at soonest convenient time
2177  */
2178 void
2179 StatementCancelHandler(SIGNAL_ARGS)
2180 {
2181         int                     save_errno = errno;
2182
2183         /*
2184          * Don't joggle the elbow of proc_exit
2185          */
2186         if (!proc_exit_inprogress)
2187         {
2188                 InterruptPending = true;
2189                 QueryCancelPending = true;
2190
2191                 /*
2192                  * If it's safe to interrupt, and we're waiting for a lock, service
2193                  * the interrupt immediately.  No point in interrupting if we're
2194                  * waiting for input, however.
2195                  */
2196                 if (ImmediateInterruptOK && InterruptHoldoffCount == 0 &&
2197                         CritSectionCount == 0)
2198                 {
2199                         /* bump holdoff count to make ProcessInterrupts() a no-op */
2200                         /* until we are done getting ready for it */
2201                         InterruptHoldoffCount++;
2202                         if (LockWaitCancel())
2203                         {
2204                                 DisableNotifyInterrupt();
2205                                 DisableCatchupInterrupt();
2206                                 InterruptHoldoffCount--;
2207                                 ProcessInterrupts();
2208                         }
2209                         else
2210                                 InterruptHoldoffCount--;
2211                 }
2212         }
2213
2214         errno = save_errno;
2215 }
2216
2217 /* signal handler for floating point exception */
2218 void
2219 FloatExceptionHandler(SIGNAL_ARGS)
2220 {
2221         ereport(ERROR,
2222                         (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
2223                          errmsg("floating-point exception"),
2224                          errdetail("An invalid floating-point operation was signaled. "
2225                                            "This probably means an out-of-range result or an "
2226                                            "invalid operation, such as division by zero.")));
2227 }
2228
2229 /* SIGHUP: set flag to re-read config file at next convenient time */
2230 static void
2231 SigHupHandler(SIGNAL_ARGS)
2232 {
2233         got_SIGHUP = true;
2234 }
2235
2236
2237 /*
2238  * ProcessInterrupts: out-of-line portion of CHECK_FOR_INTERRUPTS() macro
2239  *
2240  * If an interrupt condition is pending, and it's safe to service it,
2241  * then clear the flag and accept the interrupt.  Called only when
2242  * InterruptPending is true.
2243  */
2244 void
2245 ProcessInterrupts(void)
2246 {
2247         /* OK to accept interrupt now? */
2248         if (InterruptHoldoffCount != 0 || CritSectionCount != 0)
2249                 return;
2250         InterruptPending = false;
2251         if (ProcDiePending)
2252         {
2253                 ProcDiePending = false;
2254                 QueryCancelPending = false;             /* ProcDie trumps QueryCancel */
2255                 ImmediateInterruptOK = false;   /* not idle anymore */
2256                 DisableNotifyInterrupt();
2257                 DisableCatchupInterrupt();
2258                 ereport(FATAL,
2259                                 (errcode(ERRCODE_ADMIN_SHUTDOWN),
2260                          errmsg("terminating connection due to administrator command")));
2261         }
2262         if (QueryCancelPending)
2263         {
2264                 QueryCancelPending = false;
2265                 ImmediateInterruptOK = false;   /* not idle anymore */
2266                 DisableNotifyInterrupt();
2267                 DisableCatchupInterrupt();
2268                 if (cancel_from_timeout)
2269                         ereport(ERROR,
2270                                         (errcode(ERRCODE_QUERY_CANCELED),
2271                                          errmsg("canceling statement due to statement timeout")));
2272                 else
2273                         ereport(ERROR,
2274                                         (errcode(ERRCODE_QUERY_CANCELED),
2275                                          errmsg("canceling statement due to user request")));
2276         }
2277         /* If we get here, do nothing (probably, QueryCancelPending was reset) */
2278 }
2279
2280
2281 /*
2282  * check_stack_depth: check for excessively deep recursion
2283  *
2284  * This should be called someplace in any recursive routine that might possibly
2285  * recurse deep enough to overflow the stack.  Most Unixen treat stack
2286  * overflow as an unrecoverable SIGSEGV, so we want to error out ourselves
2287  * before hitting the hardware limit.  Unfortunately we have no direct way
2288  * to detect the hardware limit, so we have to rely on the admin to set a
2289  * GUC variable for it ...
2290  */
2291 void
2292 check_stack_depth(void)
2293 {
2294         char            stack_top_loc;
2295         int                     stack_depth;
2296
2297         /*
2298          * Compute distance from PostgresMain's local variables to my own
2299          *
2300          * Note: in theory stack_depth should be ptrdiff_t or some such, but since
2301          * the whole point of this code is to bound the value to something much
2302          * less than integer-sized, int should work fine.
2303          */
2304         stack_depth = (int) (stack_base_ptr - &stack_top_loc);
2305
2306         /*
2307          * Take abs value, since stacks grow up on some machines, down on others
2308          */
2309         if (stack_depth < 0)
2310                 stack_depth = -stack_depth;
2311
2312         /*
2313          * Trouble?
2314          *
2315          * The test on stack_base_ptr prevents us from erroring out if called
2316          * during process setup or in a non-backend process.  Logically it should
2317          * be done first, but putting it here avoids wasting cycles during normal
2318          * cases.
2319          */
2320         if (stack_depth > max_stack_depth_bytes &&
2321                 stack_base_ptr != NULL)
2322         {
2323                 ereport(ERROR,
2324                                 (errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
2325                                  errmsg("stack depth limit exceeded"),
2326                                  errhint("Increase the configuration parameter \"max_stack_depth\".")));
2327         }
2328 }
2329
2330 /* GUC assign hook to update max_stack_depth_bytes from max_stack_depth */
2331 bool
2332 assign_max_stack_depth(int newval, bool doit, GucSource source)
2333 {
2334         /* Range check was already handled by guc.c */
2335         if (doit)
2336                 max_stack_depth_bytes = newval * 1024;
2337         return true;
2338 }
2339
2340
2341 /*
2342  * set_debug_options --- apply "-d N" command line option
2343  *
2344  * -d is not quite the same as setting log_min_messages because it enables
2345  * other output options.
2346  */
2347 void
2348 set_debug_options(int debug_flag, GucContext context, GucSource source)
2349 {
2350         if (debug_flag > 0)
2351         {
2352                 char            debugstr[64];
2353
2354                 sprintf(debugstr, "debug%d", debug_flag);
2355                 SetConfigOption("log_min_messages", debugstr, context, source);
2356         }
2357         else
2358                 SetConfigOption("log_min_messages", "notice", context, source);
2359
2360         if (debug_flag >= 1 && context == PGC_POSTMASTER)
2361         {
2362                 SetConfigOption("log_connections", "true", context, source);
2363                 SetConfigOption("log_disconnections", "true", context, source);
2364         }
2365         if (debug_flag >= 2)
2366                 SetConfigOption("log_statement", "all", context, source);
2367         if (debug_flag >= 3)
2368                 SetConfigOption("debug_print_parse", "true", context, source);
2369         if (debug_flag >= 4)
2370                 SetConfigOption("debug_print_plan", "true", context, source);
2371         if (debug_flag >= 5)
2372                 SetConfigOption("debug_print_rewritten", "true", context, source);
2373 }
2374
2375
2376 bool
2377 set_plan_disabling_options(const char *arg, GucContext context, GucSource source)
2378 {
2379         char *tmp = NULL;
2380
2381         switch (arg[0])
2382         {
2383                 case 's':       /* seqscan */
2384                         tmp = "enable_seqscan";
2385                         break;
2386                 case 'i':       /* indexscan */
2387                         tmp = "enable_indexscan";
2388                         break;
2389                 case 'b':       /* bitmapscan */
2390                         tmp = "enable_bitmapscan";
2391                         break;
2392                 case 't':       /* tidscan */
2393                         tmp = "enable_tidscan";
2394                         break;
2395                 case 'n':       /* nestloop */
2396                         tmp = "enable_nestloop";
2397                         break;
2398                 case 'm':       /* mergejoin */
2399                         tmp = "enable_mergejoin";
2400                         break;
2401                 case 'h':       /* hashjoin */
2402                         tmp = "enable_hashjoin";
2403                         break;
2404         }
2405         if (tmp)
2406         {
2407                 SetConfigOption(tmp, "false", context, source);
2408                 return true;
2409         }
2410         else
2411                 return false;
2412 }
2413
2414
2415 const char *
2416 get_stats_option_name(const char *arg)
2417 {
2418         switch (arg[0])
2419         {
2420                 case 'p':
2421                         if (optarg[1] == 'a') /* "parser" */
2422                                 return "log_parser_stats";
2423                         else if (optarg[1] == 'l') /* "planner" */
2424                                 return "log_planner_stats";
2425                         break;
2426
2427                 case 'e':       /* "executor" */
2428                         return "log_executor_stats";
2429                         break;
2430         }
2431
2432         return NULL;
2433 }
2434
2435
2436 /* ----------------------------------------------------------------
2437  * PostgresMain
2438  *         postgres main loop -- all backends, interactive or otherwise start here
2439  *
2440  * argc/argv are the command line arguments to be used.  (When being forked
2441  * by the postmaster, these are not the original argv array of the process.)
2442  * username is the (possibly authenticated) PostgreSQL user name to be used
2443  * for the session.
2444  * ----------------------------------------------------------------
2445  */
2446 int
2447 PostgresMain(int argc, char *argv[], const char *username)
2448 {
2449         int                     flag;
2450         const char *dbname = NULL;
2451         char       *userDoption = NULL;
2452         bool            secure;
2453         int                     errs = 0;
2454         int                     debug_flag = -1;        /* -1 means not given */
2455         List       *guc_names = NIL;    /* for SUSET options */
2456         List       *guc_values = NIL;
2457         GucContext      ctx;
2458         GucSource       gucsource;
2459         bool            am_superuser;
2460         int                     firstchar;
2461         char            stack_base;
2462         StringInfoData input_message;
2463         sigjmp_buf      local_sigjmp_buf;
2464         volatile bool send_ready_for_query = true;
2465
2466 #define PendingConfigOption(name,val) \
2467         (guc_names = lappend(guc_names, pstrdup(name)), \
2468          guc_values = lappend(guc_values, pstrdup(val)))
2469
2470         /*
2471          * initialize globals (already done if under postmaster, but not if
2472          * standalone; cheap enough to do over)
2473          */
2474         MyProcPid = getpid();
2475
2476         /*
2477          * Fire up essential subsystems: error and memory management
2478          *
2479          * If we are running under the postmaster, this is done already.
2480          */
2481         if (!IsUnderPostmaster)
2482                 MemoryContextInit();
2483
2484         set_ps_display("startup", false);
2485
2486         SetProcessingMode(InitProcessing);
2487
2488         /* Set up reference point for stack depth checking */
2489         stack_base_ptr = &stack_base;
2490
2491         /* Compute paths, if we didn't inherit them from postmaster */
2492         if (my_exec_path[0] == '\0')
2493         {
2494                 if (find_my_exec(argv[0], my_exec_path) < 0)
2495                         elog(FATAL, "%s: could not locate my own executable path",
2496                                  argv[0]);
2497         }
2498
2499         if (pkglib_path[0] == '\0')
2500                 get_pkglib_path(my_exec_path, pkglib_path);
2501
2502         /*
2503          * Set default values for command-line options.
2504          */
2505         EchoQuery = false;
2506
2507         if (!IsUnderPostmaster)
2508                 InitializeGUCOptions();
2509
2510         /* ----------------
2511          *      parse command line arguments
2512          *
2513          *      There are now two styles of command line layout for the backend:
2514          *
2515          *      For interactive use (not started from postmaster) the format is
2516          *              postgres [switches] [databasename]
2517          *      If the databasename is omitted it is taken to be the user name.
2518          *
2519          *      When started from the postmaster, the format is
2520          *              postgres [secure switches] -p databasename [insecure switches]
2521          *      Switches appearing after -p came from the client (via "options"
2522          *      field of connection request).  For security reasons we restrict
2523          *      what these switches can do.
2524          * ----------------
2525          */
2526
2527         /* Ignore the initial --single argument, if present */
2528         if (argc > 1 && strcmp(argv[1], "--single") == 0)
2529         {
2530                 argv++;
2531                 argc--;
2532         }
2533
2534         /* all options are allowed until '-p' */
2535         secure = true;
2536         ctx = PGC_POSTMASTER;
2537         gucsource = PGC_S_ARGV;         /* initial switches came from command line */
2538
2539         while ((flag = getopt(argc, argv, "A:B:c:D:d:EeFf:h:ijk:lN:nOo:Pp:r:S:sTt:v:W:y:-:")) != -1)
2540         {
2541                 switch (flag)
2542                 {
2543                         case 'A':
2544                                 SetConfigOption("debug_assertions", optarg, ctx, gucsource);
2545                                 break;
2546
2547                         case 'B':
2548                                 SetConfigOption("shared_buffers", optarg, ctx, gucsource);
2549                                 break;
2550
2551                         case 'D':
2552                                 if (secure)
2553                                         userDoption = optarg;
2554                                 break;
2555
2556                         case 'd':
2557                                 debug_flag = atoi(optarg);
2558                                 break;
2559
2560                         case 'E':
2561                                 EchoQuery = true;
2562                                 break;
2563
2564                         case 'e':
2565                                 SetConfigOption("datestyle", "euro", ctx, gucsource);
2566                                 break;
2567
2568                         case 'F':
2569                                 SetConfigOption("fsync", "false", ctx, gucsource);
2570                                 break;
2571
2572                         case 'f':
2573                                 if (!set_plan_disabling_options(optarg, ctx, gucsource))
2574                                         errs++;
2575                                 break;
2576
2577                         case 'h':
2578                                 SetConfigOption("listen_addresses", optarg, ctx, gucsource);
2579                                 break;
2580
2581                         case 'i':
2582                                 SetConfigOption("listen_addresses", "*", ctx, gucsource);
2583                                 break;
2584
2585                         case 'j':
2586                                 UseNewLine = 0;
2587                                 break;
2588
2589                         case 'k':
2590                                 SetConfigOption("unix_socket_directory", optarg, ctx, gucsource);
2591                                 break;
2592
2593                         case 'l':
2594                                 SetConfigOption("ssl", "true", ctx, gucsource);
2595                                 break;
2596
2597                         case 'N':
2598                                 SetConfigOption("max_connections", optarg, ctx, gucsource);
2599                                 break;
2600
2601                         case 'n':
2602                                 /* ignored for consistency with postmaster */
2603                                 break;
2604
2605                         case 'O':
2606                                 SetConfigOption("allow_system_table_mods", "true", ctx, gucsource);
2607                                 break;
2608
2609                         case 'o':
2610                                 errs++;
2611                                 break;
2612
2613                         case 'P':
2614                                 SetConfigOption("ignore_system_indexes", "true", ctx, gucsource);
2615                                 break;
2616
2617                         case 'p':
2618                                 SetConfigOption("port", optarg, ctx, gucsource);
2619                                 break;
2620
2621                         case 'r':
2622                                 /* send output (stdout and stderr) to the given file */
2623                                 if (secure)
2624                                         StrNCpy(OutputFileName, optarg, MAXPGPATH);
2625                                 break;
2626
2627                         case 'S':
2628                                 SetConfigOption("work_mem", optarg, ctx, gucsource);
2629                                 break;
2630
2631                         case 's':
2632                                 /*
2633                                  * Since log options are SUSET, we need to postpone unless
2634                                  * still in secure context
2635                                  */
2636                                 if (ctx == PGC_BACKEND)
2637                                         PendingConfigOption("log_statement_stats", "true");
2638                                 else
2639                                         SetConfigOption("log_statement_stats", "true",
2640                                                                         ctx, gucsource);
2641                                 break;
2642
2643                         case 'T':
2644                                 /* ignored for consistency with postmaster */
2645                                 break;
2646
2647                         case 't':
2648                         {
2649                                 const char *tmp = get_stats_option_name(optarg);
2650                                 if (tmp)
2651                                 {
2652                                         if (ctx == PGC_BACKEND)
2653                                                 PendingConfigOption(tmp, "true");
2654                                         else
2655                                                 SetConfigOption(tmp, "true", ctx, gucsource);
2656                                 }
2657                                 else
2658                                         errs++;
2659                                 break;
2660                         }
2661
2662                         case 'v':
2663                                 if (secure)
2664                                         FrontendProtocol = (ProtocolVersion) atoi(optarg);
2665                                 break;
2666
2667                         case 'W':
2668                                 SetConfigOption("post_auth_delay", optarg, ctx, gucsource);
2669                                 break;
2670
2671
2672                         case 'y':
2673                                 /*
2674                                  * y - special flag passed if backend was forked by a
2675                                  * postmaster.
2676                                  */
2677                                 if (secure)
2678                                 {
2679                                         dbname = strdup(optarg);
2680
2681                                         secure = false;         /* subsequent switches are NOT secure */
2682                                         ctx = PGC_BACKEND;
2683                                         gucsource = PGC_S_CLIENT;
2684                                 }
2685                                 break;
2686
2687                         case 'c':
2688                         case '-':
2689                                 {
2690                                         char       *name,
2691                                                            *value;
2692
2693                                         ParseLongOption(optarg, &name, &value);
2694                                         if (!value)
2695                                         {
2696                                                 if (flag == '-')
2697                                                         ereport(ERROR,
2698                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
2699                                                                          errmsg("--%s requires a value",
2700                                                                                         optarg)));
2701                                                 else
2702                                                         ereport(ERROR,
2703                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
2704                                                                          errmsg("-c %s requires a value",
2705                                                                                         optarg)));
2706                                         }
2707
2708                                         /*
2709                                          * If a SUSET option, must postpone evaluation, unless we
2710                                          * are still reading secure switches.
2711                                          */
2712                                         if (ctx == PGC_BACKEND && IsSuperuserConfigOption(name))
2713                                                 PendingConfigOption(name, value);
2714                                         else
2715                                                 SetConfigOption(name, value, ctx, gucsource);
2716                                         free(name);
2717                                         if (value)
2718                                                 free(value);
2719                                         break;
2720                                 }
2721
2722                         default:
2723                                 errs++;
2724                                 break;
2725                 }
2726         }
2727
2728         /*
2729          * Process any additional GUC variable settings passed in startup packet.
2730          * These are handled exactly like command-line variables.
2731          */
2732         if (MyProcPort != NULL)
2733         {
2734                 ListCell   *gucopts = list_head(MyProcPort->guc_options);
2735
2736                 while (gucopts)
2737                 {
2738                         char       *name;
2739                         char       *value;
2740
2741                         name = lfirst(gucopts);
2742                         gucopts = lnext(gucopts);
2743
2744                         value = lfirst(gucopts);
2745                         gucopts = lnext(gucopts);
2746
2747                         if (IsSuperuserConfigOption(name))
2748                                 PendingConfigOption(name, value);
2749                         else
2750                                 SetConfigOption(name, value, PGC_BACKEND, PGC_S_CLIENT);
2751                 }
2752         }
2753
2754         /* Acquire configuration parameters, unless inherited from postmaster */
2755         if (!IsUnderPostmaster)
2756         {
2757                 if (!SelectConfigFiles(userDoption, argv[0]))
2758                         proc_exit(1);
2759                 /* If timezone is not set, determine what the OS uses */
2760                 pg_timezone_initialize();
2761         }
2762
2763         if (PostAuthDelay)
2764                 pg_usleep(PostAuthDelay * 1000000L);
2765
2766         /*
2767          * Set up signal handlers and masks.
2768          *
2769          * Note that postmaster blocked all signals before forking child process,
2770          * so there is no race condition whereby we might receive a signal before
2771          * we have set up the handler.
2772          *
2773          * Also note: it's best not to use any signals that are SIG_IGNored in the
2774          * postmaster.  If such a signal arrives before we are able to change the
2775          * handler to non-SIG_IGN, it'll get dropped.  Instead, make a dummy
2776          * handler in the postmaster to reserve the signal. (Of course, this isn't
2777          * an issue for signals that are locally generated, such as SIGALRM and
2778          * SIGPIPE.)
2779          */
2780         pqsignal(SIGHUP, SigHupHandler);        /* set flag to read config file */
2781         pqsignal(SIGINT, StatementCancelHandler);       /* cancel current query */
2782         pqsignal(SIGTERM, die);         /* cancel current query and exit */
2783         pqsignal(SIGQUIT, quickdie);    /* hard crash time */
2784         pqsignal(SIGALRM, handle_sig_alarm);            /* timeout conditions */
2785
2786         /*
2787          * Ignore failure to write to frontend. Note: if frontend closes
2788          * connection, we will notice it and exit cleanly when control next
2789          * returns to outer loop.  This seems safer than forcing exit in the midst
2790          * of output during who-knows-what operation...
2791          */
2792         pqsignal(SIGPIPE, SIG_IGN);
2793         pqsignal(SIGUSR1, CatchupInterruptHandler);
2794         pqsignal(SIGUSR2, NotifyInterruptHandler);
2795         pqsignal(SIGFPE, FloatExceptionHandler);
2796
2797         /*
2798          * Reset some signals that are accepted by postmaster but not by backend
2799          */
2800         pqsignal(SIGCHLD, SIG_DFL); /* system() requires this on some platforms */
2801
2802         pqinitmask();
2803
2804         /* We allow SIGQUIT (quickdie) at all times */
2805 #ifdef HAVE_SIGPROCMASK
2806         sigdelset(&BlockSig, SIGQUIT);
2807 #else
2808         BlockSig &= ~(sigmask(SIGQUIT));
2809 #endif
2810
2811         PG_SETMASK(&BlockSig);          /* block everything except SIGQUIT */
2812
2813         if (IsUnderPostmaster)
2814         {
2815                 /* noninteractive case: nothing should be left after switches */
2816                 if (errs || argc != optind || dbname == NULL)
2817                 {
2818                         ereport(FATAL,
2819                                         (errcode(ERRCODE_SYNTAX_ERROR),
2820                                  errmsg("invalid command-line arguments for server process"),
2821                            errhint("Try \"%s --help\" for more information.", argv[0])));
2822                 }
2823
2824                 BaseInit();
2825         }
2826         else
2827         {
2828                 /* interactive case: database name can be last arg on command line */
2829                 if (errs || argc - optind > 1)
2830                 {
2831                         ereport(FATAL,
2832                                         (errcode(ERRCODE_SYNTAX_ERROR),
2833                                          errmsg("%s: invalid command-line arguments",
2834                                                         argv[0]),
2835                            errhint("Try \"%s --help\" for more information.", argv[0])));
2836                 }
2837                 else if (argc - optind == 1)
2838                         dbname = argv[optind];
2839                 else if ((dbname = username) == NULL)
2840                 {
2841                         ereport(FATAL,
2842                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2843                                          errmsg("%s: no database nor user name specified",
2844                                                         argv[0])));
2845                 }
2846
2847                 /*
2848                  * Validate we have been given a reasonable-looking DataDir (if under
2849                  * postmaster, assume postmaster did this already).
2850                  */
2851                 Assert(DataDir);
2852                 ValidatePgVersion(DataDir);
2853
2854                 /* Change into DataDir (if under postmaster, was done already) */
2855                 ChangeToDataDir();
2856
2857                 /*
2858                  * Create lockfile for data directory.
2859                  */
2860                 CreateDataDirLockFile(false);
2861
2862                 BaseInit();
2863
2864                 /*
2865                  * Start up xlog for standalone backend, and register to have it
2866                  * closed down at exit.
2867                  */
2868                 StartupXLOG();
2869                 on_shmem_exit(ShutdownXLOG, 0);
2870
2871                 /*
2872                  * Read any existing FSM cache file, and register to write one out at
2873                  * exit.
2874                  */
2875                 LoadFreeSpaceMap();
2876                 on_shmem_exit(DumpFreeSpaceMap, 0);
2877
2878                 /*
2879                  * We have to build the flat file for pg_database, but not for the
2880                  * user and group tables, since we won't try to do authentication.
2881                  */
2882                 BuildFlatFiles(true);
2883         }
2884
2885         /*
2886          * Create a per-backend PGPROC struct in shared memory, except in
2887          * the EXEC_BACKEND case where this was done in SubPostmasterMain.
2888          * We must do this before we can use LWLocks (and in the EXEC_BACKEND
2889          * case we already had to do some stuff with LWLocks).
2890          */
2891 #ifdef EXEC_BACKEND
2892         if (!IsUnderPostmaster)
2893                 InitProcess();
2894 #else
2895         InitProcess();
2896 #endif
2897
2898         /*
2899          * General initialization.
2900          *
2901          * NOTE: if you are tempted to add code in this vicinity, consider putting
2902          * it inside InitPostgres() instead.  In particular, anything that
2903          * involves database access should be there, not here.
2904          */
2905         ereport(DEBUG3,
2906                         (errmsg_internal("InitPostgres")));
2907         am_superuser = InitPostgres(dbname, username);
2908
2909         SetProcessingMode(NormalProcessing);
2910
2911         /*
2912          * Now that we know if client is a superuser, we can try to apply SUSET
2913          * GUC options that came from the client.
2914          */
2915         ctx = am_superuser ? PGC_SUSET : PGC_USERSET;
2916
2917         if (debug_flag >= 0)
2918                 set_debug_options(debug_flag, ctx, PGC_S_CLIENT);
2919
2920         if (guc_names != NIL)
2921         {
2922                 ListCell   *namcell,
2923                                    *valcell;
2924
2925                 forboth(namcell, guc_names, valcell, guc_values)
2926                 {
2927                         char       *name = (char *) lfirst(namcell);
2928                         char       *value = (char *) lfirst(valcell);
2929
2930                         SetConfigOption(name, value, ctx, PGC_S_CLIENT);
2931                         pfree(name);
2932                         pfree(value);
2933                 }
2934         }
2935
2936         /*
2937          * Now all GUC states are fully set up.  Report them to client if
2938          * appropriate.
2939          */
2940         BeginReportingGUCOptions();
2941
2942         /*
2943          * Also set up handler to log session end; we have to wait till now to be
2944          * sure Log_disconnections has its final value.
2945          */
2946         if (IsUnderPostmaster && Log_disconnections)
2947                 on_proc_exit(log_disconnections, 0);
2948
2949         /*
2950          * Send this backend's cancellation info to the frontend.
2951          */
2952         if (whereToSendOutput == DestRemote &&
2953                 PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
2954         {
2955                 StringInfoData buf;
2956
2957                 pq_beginmessage(&buf, 'K');
2958                 pq_sendint(&buf, (int32) MyProcPid, sizeof(int32));
2959                 pq_sendint(&buf, (int32) MyCancelKey, sizeof(int32));
2960                 pq_endmessage(&buf);
2961                 /* Need not flush since ReadyForQuery will do it. */
2962         }
2963
2964         /* Welcome banner for standalone case */
2965         if (whereToSendOutput == DestDebug)
2966                 printf("\nPostgreSQL stand-alone backend %s\n", PG_VERSION);
2967
2968         /*
2969          * Create the memory context we will use in the main loop.
2970          *
2971          * MessageContext is reset once per iteration of the main loop, ie, upon
2972          * completion of processing of each command message from the client.
2973          */
2974         MessageContext = AllocSetContextCreate(TopMemoryContext,
2975                                                                                    "MessageContext",
2976                                                                                    ALLOCSET_DEFAULT_MINSIZE,
2977                                                                                    ALLOCSET_DEFAULT_INITSIZE,
2978                                                                                    ALLOCSET_DEFAULT_MAXSIZE);
2979
2980         /*
2981          * Remember stand-alone backend startup time
2982          */
2983         if (!IsUnderPostmaster)
2984                 PgStartTime = GetCurrentTimestamp();
2985
2986         /*
2987          * POSTGRES main processing loop begins here
2988          *
2989          * If an exception is encountered, processing resumes here so we abort the
2990          * current transaction and start a new one.
2991          *
2992          * You might wonder why this isn't coded as an infinite loop around a
2993          * PG_TRY construct.  The reason is that this is the bottom of the
2994          * exception stack, and so with PG_TRY there would be no exception handler
2995          * in force at all during the CATCH part.  By leaving the outermost setjmp
2996          * always active, we have at least some chance of recovering from an error
2997          * during error recovery.  (If we get into an infinite loop thereby, it
2998          * will soon be stopped by overflow of elog.c's internal state stack.)
2999          */
3000
3001         if (sigsetjmp(local_sigjmp_buf, 1) != 0)
3002         {
3003                 /*
3004                  * NOTE: if you are tempted to add more code in this if-block,
3005                  * consider the high probability that it should be in
3006                  * AbortTransaction() instead.  The only stuff done directly here
3007                  * should be stuff that is guaranteed to apply *only* for outer-level
3008                  * error recovery, such as adjusting the FE/BE protocol status.
3009                  */
3010
3011                 /* Since not using PG_TRY, must reset error stack by hand */
3012                 error_context_stack = NULL;
3013
3014                 /* Prevent interrupts while cleaning up */
3015                 HOLD_INTERRUPTS();
3016
3017                 /*
3018                  * Forget any pending QueryCancel request, since we're returning to
3019                  * the idle loop anyway, and cancel the statement timer if running.
3020                  */
3021                 QueryCancelPending = false;
3022                 disable_sig_alarm(true);
3023                 QueryCancelPending = false;             /* again in case timeout occurred */
3024
3025                 /*
3026                  * Turn off these interrupts too.  This is only needed here and not in
3027                  * other exception-catching places since these interrupts are only
3028                  * enabled while we wait for client input.
3029                  */
3030                 DoingCommandRead = false;
3031                 DisableNotifyInterrupt();
3032                 DisableCatchupInterrupt();
3033
3034                 /* Make sure libpq is in a good state */
3035                 pq_comm_reset();
3036
3037                 /* Report the error to the client and/or server log */
3038                 EmitErrorReport();
3039
3040                 /*
3041                  * Make sure debug_query_string gets reset before we possibly clobber
3042                  * the storage it points at.
3043                  */
3044                 debug_query_string = NULL;
3045
3046                 /*
3047                  * Abort the current transaction in order to recover.
3048                  */
3049                 AbortCurrentTransaction();
3050
3051                 /*
3052                  * Now return to normal top-level context and clear ErrorContext for
3053                  * next time.
3054                  */
3055                 MemoryContextSwitchTo(TopMemoryContext);
3056                 FlushErrorState();
3057                 QueryContext = NULL;
3058
3059                 /*
3060                  * If we were handling an extended-query-protocol message, initiate
3061                  * skip till next Sync.  This also causes us not to issue
3062                  * ReadyForQuery (until we get Sync).
3063                  */
3064                 if (doing_extended_query_message)
3065                         ignore_till_sync = true;
3066
3067                 /* We don't have a transaction command open anymore */
3068                 xact_started = false;
3069
3070                 /* Now we can allow interrupts again */
3071                 RESUME_INTERRUPTS();
3072         }
3073
3074         /* We can now handle ereport(ERROR) */
3075         PG_exception_stack = &local_sigjmp_buf;
3076
3077         PG_SETMASK(&UnBlockSig);
3078
3079         if (!ignore_till_sync)
3080                 send_ready_for_query = true;            /* initially, or after error */
3081
3082         /*
3083          * Non-error queries loop here.
3084          */
3085
3086         for (;;)
3087         {
3088                 /*
3089                  * At top of loop, reset extended-query-message flag, so that any
3090                  * errors encountered in "idle" state don't provoke skip.
3091                  */
3092                 doing_extended_query_message = false;
3093
3094                 /*
3095                  * Release storage left over from prior query cycle, and create a new
3096                  * query input buffer in the cleared MessageContext.
3097                  */
3098                 MemoryContextSwitchTo(MessageContext);
3099                 MemoryContextResetAndDeleteChildren(MessageContext);
3100
3101                 initStringInfo(&input_message);
3102
3103                 /*
3104                  * (1) If we've reached idle state, tell the frontend we're ready for
3105                  * a new query.
3106                  *
3107                  * Note: this includes fflush()'ing the last of the prior output.
3108                  *
3109                  * This is also a good time to send collected statistics to the
3110                  * collector, and to update the PS stats display.  We avoid doing
3111                  * those every time through the message loop because it'd slow down
3112                  * processing of batched messages, and because we don't want to report
3113                  * uncommitted updates (that confuses autovacuum).
3114                  */
3115                 if (send_ready_for_query)
3116                 {
3117                         if (IsTransactionOrTransactionBlock())
3118                         {
3119                                 set_ps_display("idle in transaction", false);
3120                                 pgstat_report_activity("<IDLE> in transaction");
3121                         }
3122                         else
3123                         {
3124                                 pgstat_report_tabstat();
3125
3126                                 set_ps_display("idle", false);
3127                                 pgstat_report_activity("<IDLE>");
3128                         }
3129
3130                         ReadyForQuery(whereToSendOutput);
3131                         send_ready_for_query = false;
3132                 }
3133
3134                 /*
3135                  * (2) Allow asynchronous signals to be executed immediately if they
3136                  * come in while we are waiting for client input. (This must be
3137                  * conditional since we don't want, say, reads on behalf of COPY FROM
3138                  * STDIN doing the same thing.)
3139                  */
3140                 QueryCancelPending = false;             /* forget any earlier CANCEL signal */
3141                 DoingCommandRead = true;
3142
3143                 /*
3144                  * (3) read a command (loop blocks here)
3145                  */
3146                 firstchar = ReadCommand(&input_message);
3147
3148                 /*
3149                  * (4) disable async signal conditions again.
3150                  */
3151                 DoingCommandRead = false;
3152
3153                 /*
3154                  * (5) check for any other interesting events that happened while we
3155                  * slept.
3156                  */
3157                 if (got_SIGHUP)
3158                 {
3159                         got_SIGHUP = false;
3160                         ProcessConfigFile(PGC_SIGHUP);
3161                 }
3162
3163                 /*
3164                  * (6) process the command.  But ignore it if we're skipping till
3165                  * Sync.
3166                  */
3167                 if (ignore_till_sync && firstchar != EOF)
3168                         continue;
3169
3170                 switch (firstchar)
3171                 {
3172                         case 'Q':                       /* simple query */
3173                                 {
3174                                         const char *query_string;
3175
3176                                         /* Set statement_timestamp() */
3177                                         SetCurrentStatementStartTimestamp();
3178
3179                                         query_string = pq_getmsgstring(&input_message);
3180                                         pq_getmsgend(&input_message);
3181
3182                                         exec_simple_query(query_string);
3183
3184                                         send_ready_for_query = true;
3185                                 }
3186                                 break;
3187
3188                         case 'P':                       /* parse */
3189                                 {
3190                                         const char *stmt_name;
3191                                         const char *query_string;
3192                                         int                     numParams;
3193                                         Oid                *paramTypes = NULL;
3194
3195                                         /* Set statement_timestamp() */
3196                                         SetCurrentStatementStartTimestamp();
3197
3198                                         stmt_name = pq_getmsgstring(&input_message);
3199                                         query_string = pq_getmsgstring(&input_message);
3200                                         numParams = pq_getmsgint(&input_message, 2);
3201                                         if (numParams > 0)
3202                                         {
3203                                                 int                     i;
3204
3205                                                 paramTypes = (Oid *) palloc(numParams * sizeof(Oid));
3206                                                 for (i = 0; i < numParams; i++)
3207                                                         paramTypes[i] = pq_getmsgint(&input_message, 4);
3208                                         }
3209                                         pq_getmsgend(&input_message);
3210
3211                                         exec_parse_message(query_string, stmt_name,
3212                                                                            paramTypes, numParams);
3213                                 }
3214                                 break;
3215
3216                         case 'B':                       /* bind */
3217                                 /* Set statement_timestamp() */
3218                                 SetCurrentStatementStartTimestamp();
3219
3220                                 /*
3221                                  * this message is complex enough that it seems best to put
3222                                  * the field extraction out-of-line
3223                                  */
3224                                 exec_bind_message(&input_message);
3225                                 break;
3226
3227                         case 'E':                       /* execute */
3228                                 {
3229                                         const char *portal_name;
3230                                         int                     max_rows;
3231
3232                                         /* Set statement_timestamp() */
3233                                         SetCurrentStatementStartTimestamp();
3234
3235                                         portal_name = pq_getmsgstring(&input_message);
3236                                         max_rows = pq_getmsgint(&input_message, 4);
3237                                         pq_getmsgend(&input_message);
3238
3239                                         exec_execute_message(portal_name, max_rows);
3240                                 }
3241                                 break;
3242
3243                         case 'F':                       /* fastpath function call */
3244                                 /* Set statement_timestamp() */
3245                                 SetCurrentStatementStartTimestamp();
3246
3247                                 /* Tell the collector what we're doing */
3248                                 pgstat_report_activity("<FASTPATH> function call");
3249
3250                                 /* start an xact for this function invocation */
3251                                 start_xact_command();
3252
3253                                 /*
3254                                  * Note: we may at this point be inside an aborted
3255                                  * transaction.  We can't throw error for that until
3256                                  * we've finished reading the function-call message, so
3257                                  * HandleFunctionRequest() must check for it after doing so.
3258                                  * Be careful not to do anything that assumes we're inside a
3259                                  * valid transaction here.
3260                                  */
3261
3262                                 /* switch back to message context */
3263                                 MemoryContextSwitchTo(MessageContext);
3264
3265                                 if (HandleFunctionRequest(&input_message) == EOF)
3266                                 {
3267                                         /* lost frontend connection during F message input */
3268
3269                                         /*
3270                                          * Reset whereToSendOutput to prevent ereport from
3271                                          * attempting to send any more messages to client.
3272                                          */
3273                                         if (whereToSendOutput == DestRemote)
3274                                                 whereToSendOutput = DestNone;
3275
3276                                         proc_exit(0);
3277                                 }
3278
3279                                 /* commit the function-invocation transaction */
3280                                 finish_xact_command();
3281
3282                                 send_ready_for_query = true;
3283                                 break;
3284
3285                         case 'C':                       /* close */
3286                                 {
3287                                         int                     close_type;
3288                                         const char *close_target;
3289
3290                                         close_type = pq_getmsgbyte(&input_message);
3291                                         close_target = pq_getmsgstring(&input_message);
3292                                         pq_getmsgend(&input_message);
3293
3294                                         switch (close_type)
3295                                         {
3296                                                 case 'S':
3297                                                         if (close_target[0] != '\0')
3298                                                                 DropPreparedStatement(close_target, false);
3299                                                         else
3300                                                         {
3301                                                                 /* special-case the unnamed statement */
3302                                                                 unnamed_stmt_pstmt = NULL;
3303                                                                 if (unnamed_stmt_context)
3304                                                                 {
3305                                                                         DropDependentPortals(unnamed_stmt_context);
3306                                                                         MemoryContextDelete(unnamed_stmt_context);
3307                                                                 }
3308                                                                 unnamed_stmt_context = NULL;
3309                                                         }
3310                                                         break;
3311                                                 case 'P':
3312                                                         {
3313                                                                 Portal          portal;
3314
3315                                                                 portal = GetPortalByName(close_target);
3316                                                                 if (PortalIsValid(portal))
3317                                                                         PortalDrop(portal, false);
3318                                                         }
3319                                                         break;
3320                                                 default:
3321                                                         ereport(ERROR,
3322                                                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
3323                                                                    errmsg("invalid CLOSE message subtype %d",
3324                                                                                   close_type)));
3325                                                         break;
3326                                         }
3327
3328                                         if (whereToSendOutput == DestRemote)
3329                                                 pq_putemptymessage('3');                /* CloseComplete */
3330                                 }
3331                                 break;
3332
3333                         case 'D':                       /* describe */
3334                                 {
3335                                         int                     describe_type;
3336                                         const char *describe_target;
3337
3338                                         /* Set statement_timestamp() (needed for xact) */
3339                                         SetCurrentStatementStartTimestamp();
3340
3341                                         describe_type = pq_getmsgbyte(&input_message);
3342                                         describe_target = pq_getmsgstring(&input_message);
3343                                         pq_getmsgend(&input_message);
3344
3345                                         switch (describe_type)
3346                                         {
3347                                                 case 'S':
3348                                                         exec_describe_statement_message(describe_target);
3349                                                         break;
3350                                                 case 'P':
3351                                                         exec_describe_portal_message(describe_target);
3352                                                         break;
3353                                                 default:
3354                                                         ereport(ERROR,
3355                                                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
3356                                                                 errmsg("invalid DESCRIBE message subtype %d",
3357                                                                            describe_type)));
3358                                                         break;
3359                                         }
3360                                 }
3361                                 break;
3362
3363                         case 'H':                       /* flush */
3364                                 pq_getmsgend(&input_message);
3365                                 if (whereToSendOutput == DestRemote)
3366                                         pq_flush();
3367                                 break;
3368
3369                         case 'S':                       /* sync */
3370                                 pq_getmsgend(&input_message);
3371                                 finish_xact_command();
3372                                 send_ready_for_query = true;
3373                                 break;
3374
3375                                 /*
3376                                  * 'X' means that the frontend is closing down the socket. EOF
3377                                  * means unexpected loss of frontend connection. Either way,
3378                                  * perform normal shutdown.
3379                                  */
3380                         case 'X':
3381                         case EOF:
3382
3383                                 /*
3384                                  * Reset whereToSendOutput to prevent ereport from attempting
3385                                  * to send any more messages to client.
3386                                  */
3387                                 if (whereToSendOutput == DestRemote)
3388                                         whereToSendOutput = DestNone;
3389
3390                                 /*
3391                                  * NOTE: if you are tempted to add more code here, DON'T!
3392                                  * Whatever you had in mind to do should be set up as an
3393                                  * on_proc_exit or on_shmem_exit callback, instead. Otherwise
3394                                  * it will fail to be called during other backend-shutdown
3395                                  * scenarios.
3396                                  */
3397                                 proc_exit(0);
3398
3399                         case 'd':                       /* copy data */
3400                         case 'c':                       /* copy done */
3401                         case 'f':                       /* copy fail */
3402
3403                                 /*
3404                                  * Accept but ignore these messages, per protocol spec; we
3405                                  * probably got here because a COPY failed, and the frontend
3406                                  * is still sending data.
3407                                  */
3408                                 break;
3409
3410                         default:
3411                                 ereport(FATAL,
3412                                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
3413                                                  errmsg("invalid frontend message type %d",
3414                                                                 firstchar)));
3415                 }
3416         }                                                       /* end of input-reading loop */
3417
3418         /* can't get here because the above loop never exits */
3419         Assert(false);
3420
3421         return 1;                                       /* keep compiler quiet */
3422 }
3423
3424 #ifndef HAVE_GETRUSAGE
3425 #include "rusagestub.h"
3426 #else
3427 #include <sys/resource.h>
3428 #endif   /* HAVE_GETRUSAGE */
3429
3430 static struct rusage Save_r;
3431 static struct timeval Save_t;
3432
3433 void
3434 ResetUsage(void)
3435 {
3436         getrusage(RUSAGE_SELF, &Save_r);
3437         gettimeofday(&Save_t, NULL);
3438         ResetBufferUsage();
3439         /* ResetTupleCount(); */
3440 }
3441
3442 void
3443 ShowUsage(const char *title)
3444 {
3445         StringInfoData str;
3446         struct timeval user,
3447                                 sys;
3448         struct timeval elapse_t;
3449         struct rusage r;
3450         char       *bufusage;
3451
3452         getrusage(RUSAGE_SELF, &r);
3453         gettimeofday(&elapse_t, NULL);
3454         memcpy((char *) &user, (char *) &r.ru_utime, sizeof(user));
3455         memcpy((char *) &sys, (char *) &r.ru_stime, sizeof(sys));
3456         if (elapse_t.tv_usec < Save_t.tv_usec)
3457         {
3458                 elapse_t.tv_sec--;
3459                 elapse_t.tv_usec += 1000000;
3460         }
3461         if (r.ru_utime.tv_usec < Save_r.ru_utime.tv_usec)
3462         {
3463                 r.ru_utime.tv_sec--;
3464                 r.ru_utime.tv_usec += 1000000;
3465         }
3466         if (r.ru_stime.tv_usec < Save_r.ru_stime.tv_usec)
3467         {
3468                 r.ru_stime.tv_sec--;
3469                 r.ru_stime.tv_usec += 1000000;
3470         }
3471
3472         /*
3473          * the only stats we don't show here are for memory usage -- i can't
3474          * figure out how to interpret the relevant fields in the rusage struct,
3475          * and they change names across o/s platforms, anyway. if you can figure
3476          * out what the entries mean, you can somehow extract resident set size,
3477          * shared text size, and unshared data and stack sizes.
3478          */
3479         initStringInfo(&str);
3480
3481         appendStringInfo(&str, "! system usage stats:\n");
3482         appendStringInfo(&str,
3483                                 "!\t%ld.%06ld elapsed %ld.%06ld user %ld.%06ld system sec\n",
3484                                          (long) (elapse_t.tv_sec - Save_t.tv_sec),
3485                                          (long) (elapse_t.tv_usec - Save_t.tv_usec),
3486                                          (long) (r.ru_utime.tv_sec - Save_r.ru_utime.tv_sec),
3487                                          (long) (r.ru_utime.tv_usec - Save_r.ru_utime.tv_usec),
3488                                          (long) (r.ru_stime.tv_sec - Save_r.ru_stime.tv_sec),
3489                                          (long) (r.ru_stime.tv_usec - Save_r.ru_stime.tv_usec));
3490         appendStringInfo(&str,
3491                                          "!\t[%ld.%06ld user %ld.%06ld sys total]\n",
3492                                          (long) user.tv_sec,
3493                                          (long) user.tv_usec,
3494                                          (long) sys.tv_sec,
3495                                          (long) sys.tv_usec);
3496 #if defined(HAVE_GETRUSAGE)
3497         appendStringInfo(&str,
3498                                          "!\t%ld/%ld [%ld/%ld] filesystem blocks in/out\n",
3499                                          r.ru_inblock - Save_r.ru_inblock,
3500         /* they only drink coffee at dec */
3501                                          r.ru_oublock - Save_r.ru_oublock,
3502                                          r.ru_inblock, r.ru_oublock);
3503         appendStringInfo(&str,
3504                           "!\t%ld/%ld [%ld/%ld] page faults/reclaims, %ld [%ld] swaps\n",
3505                                          r.ru_majflt - Save_r.ru_majflt,
3506                                          r.ru_minflt - Save_r.ru_minflt,
3507                                          r.ru_majflt, r.ru_minflt,
3508                                          r.ru_nswap - Save_r.ru_nswap,
3509                                          r.ru_nswap);
3510         appendStringInfo(&str,
3511                  "!\t%ld [%ld] signals rcvd, %ld/%ld [%ld/%ld] messages rcvd/sent\n",
3512                                          r.ru_nsignals - Save_r.ru_nsignals,
3513                                          r.ru_nsignals,
3514                                          r.ru_msgrcv - Save_r.ru_msgrcv,
3515                                          r.ru_msgsnd - Save_r.ru_msgsnd,
3516                                          r.ru_msgrcv, r.ru_msgsnd);
3517         appendStringInfo(&str,
3518                          "!\t%ld/%ld [%ld/%ld] voluntary/involuntary context switches\n",
3519                                          r.ru_nvcsw - Save_r.ru_nvcsw,
3520                                          r.ru_nivcsw - Save_r.ru_nivcsw,
3521                                          r.ru_nvcsw, r.ru_nivcsw);
3522 #endif   /* HAVE_GETRUSAGE */
3523
3524         bufusage = ShowBufferUsage();
3525         appendStringInfo(&str, "! buffer usage stats:\n%s", bufusage);
3526         pfree(bufusage);
3527
3528         /* remove trailing newline */
3529         if (str.data[str.len - 1] == '\n')
3530                 str.data[--str.len] = '\0';
3531
3532         ereport(LOG,
3533                         (errmsg_internal("%s", title),
3534                          errdetail("%s", str.data)));
3535
3536         pfree(str.data);
3537 }
3538
3539 /*
3540  * on_proc_exit handler to log end of session
3541  */
3542 static void
3543 log_disconnections(int code, Datum arg)
3544 {
3545         Port       *port = MyProcPort;
3546         long            secs;
3547         int                     usecs;
3548         int                     msecs;
3549         int                     hours,
3550                                 minutes,
3551                                 seconds;
3552
3553         TimestampDifference(port->SessionStartTime,
3554                                                 GetCurrentTimestamp(),
3555                                                 &secs, &usecs);
3556         msecs = usecs / 1000;
3557
3558         hours = secs / SECS_PER_HOUR;
3559         secs %= SECS_PER_HOUR;
3560         minutes = secs / SECS_PER_MINUTE;
3561         seconds = secs % SECS_PER_MINUTE;
3562
3563         ereport(LOG,
3564                         (errmsg("disconnection: session time: %d:%02d:%02d.%03d "
3565                                         "user=%s database=%s host=%s%s%s",
3566                                         hours, minutes, seconds, msecs,
3567                                         port->user_name, port->database_name, port->remote_host,
3568                                         port->remote_port[0] ? " port=" : "", port->remote_port)));
3569 }