1 /*-------------------------------------------------------------------------
4 * POSTGRES C Backend Interface
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.492 2006/07/14 14:52:23 momjian Exp $
14 * this is the "main" module of the postgres backend and
15 * hence the main module of the "traffic cop".
17 *-------------------------------------------------------------------------
25 #include <sys/socket.h>
27 #include <sys/select.h>
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"
70 const char *debug_query_string; /* for pgmonitor and log_min_error_statement */
72 /* Note: whereToSendOutput is initialized for the bootstrap/standalone case */
73 CommandDest whereToSendOutput = DestDebug;
75 /* flag for logging end of session */
76 bool Log_disconnections = false;
78 LogStmtLevel log_statement = LOGSTMT_NONE;
80 /* GUC variable for maximum stack depth (measured in kilobytes) */
81 int max_stack_depth = 2048;
83 /* wait N seconds to allow attach from a debugger */
84 int PostAuthDelay = 0;
93 /* max_stack_depth converted to bytes for speed of checking */
94 static int max_stack_depth_bytes = 2048 * 1024;
97 * Stack base pointer -- initialized by PostgresMain. This is not static
98 * so that PL/Java can modify it.
100 char *stack_base_ptr = NULL;
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?)
108 static volatile sig_atomic_t got_SIGHUP = false;
111 * Flag to keep track of whether we have started a transaction.
112 * For extended query protocol this has to be remembered across messages.
114 static bool xact_started = false;
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.
121 static bool DoingCommandRead = false;
124 * Flags to implement skip-till-Sync-after-error behavior for messages of
125 * the extended query protocol.
127 static bool doing_extended_query_message = false;
128 static bool ignore_till_sync = false;
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.
135 static MemoryContext unnamed_stmt_context = NULL;
136 static PreparedStatement *unnamed_stmt_pstmt = NULL;
139 static bool EchoQuery = false; /* default don't echo */
142 * people who want to use EOF should #define DONTUSENEWLINE in
145 #ifndef TCOP_DONTUSENEWLINE
146 static int UseNewLine = 1; /* Use newlines query delimiters (the default) */
148 static int UseNewLine = 0; /* Use EOF as query delimiters */
149 #endif /* TCOP_DONTUSENEWLINE */
152 /* ----------------------------------------------------------------
153 * decls for routines only used in this file
154 * ----------------------------------------------------------------
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);
171 /* ----------------------------------------------------------------
172 * routines to obtain user input
173 * ----------------------------------------------------------------
177 * InteractiveBackend() is called for user interactive connections
179 * the string entered by the user is placed in its parameter inBuf,
180 * and we act like a Q message was received.
182 * EOF is returned if end-of-file input is seen; time to shut down.
187 InteractiveBackend(StringInfo inBuf)
189 int c; /* character read from getc() */
190 bool end = false; /* end-of-input flag */
191 bool backslashSeen = false; /* have we seen a \ ? */
194 * display a prompt and obtain input from the user
199 /* Reset inBuf to empty */
201 inBuf->data[0] = '\0';
209 * if we are using \n as a delimiter, then read characters until
212 while ((c = getc(stdin)) != EOF)
218 /* discard backslash from inBuf */
219 inBuf->data[--inBuf->len] = '\0';
220 backslashSeen = false;
225 /* keep the newline character */
226 appendStringInfoChar(inBuf, '\n');
231 backslashSeen = true;
233 backslashSeen = false;
235 appendStringInfoChar(inBuf, (char) c);
244 * otherwise read characters until EOF.
246 while ((c = getc(stdin)) != EOF)
247 appendStringInfoChar(inBuf, (char) c);
257 * otherwise we have a user query so process it.
262 /* Add '\0' to make it look the same as message case. */
263 appendStringInfoChar(inBuf, (char) '\0');
266 * if the query echo flag was given, print the query..
269 printf("statement: %s\n", inBuf->data);
276 * SocketBackend() Is called for frontend-backend connections
278 * Returns the message type code, and loads message body data into inBuf.
280 * EOF is returned if the connection is lost.
284 SocketBackend(StringInfo inBuf)
289 * Get message type code from the frontend.
291 qtype = pq_getbyte();
293 if (qtype == EOF) /* frontend disconnected */
296 (errcode(ERRCODE_PROTOCOL_VIOLATION),
297 errmsg("unexpected EOF on client connection")));
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.
306 * This also gives us a place to set the doing_extended_query_message flag
307 * as soon as possible.
311 case 'Q': /* simple query */
312 doing_extended_query_message = false;
313 if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
315 /* old style without length word; convert */
316 if (pq_getstring(inBuf))
319 (errcode(ERRCODE_PROTOCOL_VIOLATION),
320 errmsg("unexpected EOF on client connection")));
326 case 'F': /* fastpath function call */
327 /* we let fastpath.c cope with old-style input of this */
328 doing_extended_query_message = false;
331 case 'X': /* terminate */
332 doing_extended_query_message = false;
333 ignore_till_sync = false;
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)
346 (errcode(ERRCODE_PROTOCOL_VIOLATION),
347 errmsg("invalid frontend message type %d", qtype)));
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)
358 (errcode(ERRCODE_PROTOCOL_VIOLATION),
359 errmsg("invalid frontend message type %d", qtype)));
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)
369 (errcode(ERRCODE_PROTOCOL_VIOLATION),
370 errmsg("invalid frontend message type %d", qtype)));
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.
381 (errcode(ERRCODE_PROTOCOL_VIOLATION),
382 errmsg("invalid frontend message type %d", qtype)));
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
391 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
393 if (pq_getmessage(inBuf, 0))
394 return EOF; /* suitable message already logged */
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.
408 ReadCommand(StringInfo inBuf)
412 if (whereToSendOutput == DestRemote)
413 result = SocketBackend(inBuf);
415 result = InteractiveBackend(inBuf);
420 * prepare_for_client_read -- set up to possibly block on client input
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.
432 prepare_for_client_read(void)
434 if (DoingCommandRead)
436 /* Enable immediate processing of asynchronous signals */
437 EnableNotifyInterrupt();
438 EnableCatchupInterrupt();
440 /* Allow "die" interrupt to be processed while waiting */
441 ImmediateInterruptOK = true;
443 /* And don't forget to detect one that already arrived */
444 QueryCancelPending = false;
445 CHECK_FOR_INTERRUPTS();
450 * client_read_ended -- get out of the client-input state
453 client_read_ended(void)
455 if (DoingCommandRead)
457 ImmediateInterruptOK = false;
458 QueryCancelPending = false; /* forget any CANCEL signal */
460 DisableNotifyInterrupt();
461 DisableCatchupInterrupt();
467 * Parse a query string and pass it through the rewriter.
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.
472 * NOTE: this routine is no longer used for processing interactive queries,
473 * but it is still needed for parsing of SQL function bodies.
476 pg_parse_and_rewrite(const char *query_string, /* string to execute */
477 Oid *paramTypes, /* parameter types */
478 int numParams) /* number of parameters */
480 List *raw_parsetree_list;
481 List *querytree_list;
485 * (1) parse the request string into a list of raw parse trees.
487 raw_parsetree_list = pg_parse_query(query_string);
490 * (2) Do parse analysis and rule rewrite.
492 querytree_list = NIL;
493 foreach(list_item, raw_parsetree_list)
495 Node *parsetree = (Node *) lfirst(list_item);
497 querytree_list = list_concat(querytree_list,
498 pg_analyze_and_rewrite(parsetree,
504 return querytree_list;
508 * Do raw parsing (only).
510 * A list of parsetrees is returned, since there might be multiple
511 * commands in the given string.
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.
521 pg_parse_query(const char *query_string)
523 List *raw_parsetree_list;
525 if (log_parser_stats)
528 raw_parsetree_list = raw_parser(query_string);
530 if (log_parser_stats)
531 ShowUsage("PARSER STATISTICS");
533 return raw_parsetree_list;
537 log_after_parse(List *raw_parsetree_list, const char *query_string,
538 char **prepare_string)
540 ListCell *parsetree_item;
541 bool log_this_statement = (log_statement == LOGSTMT_ALL);
543 *prepare_string = NULL;
545 /* Check if we need to log the statement, and get prepare_string. */
546 foreach(parsetree_item, raw_parsetree_list)
548 Node *parsetree = (Node *) lfirst(parsetree_item);
549 const char *commandTag;
551 if (IsA(parsetree, ExplainStmt) &&
552 ((ExplainStmt *) parsetree)->analyze)
553 parsetree = (Node *) (((ExplainStmt *) parsetree)->query);
555 if (IsA(parsetree, PrepareStmt))
556 parsetree = (Node *) (((PrepareStmt *) parsetree)->query);
558 if (IsA(parsetree, SelectStmt) &&
559 ((SelectStmt *) parsetree)->into == NULL)
560 continue; /* optimization for frequent command */
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;
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;
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.
587 if (IsA(parsetree, ExecuteStmt))
589 ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
590 PreparedStatement *entry;
592 if (*prepare_string == NULL &&
593 (entry = FetchPreparedStatement(stmt->name, false)) != NULL &&
596 *prepare_string = palloc(strlen(entry->query_string) +
597 strlen(" [PREPARE: %s]") - 2 + 1);
598 sprintf(*prepare_string, " [PREPARE: %s]",
599 entry->query_string);
604 if (log_this_statement)
607 (errmsg("statement: %s%s", query_string,
608 *prepare_string ? *prepare_string : "")));
617 * Given a raw parsetree (gram.y output), and optionally information about
618 * types of parameter symbols ($n), perform parse analysis and rule rewriting.
620 * A list of Query nodes is returned, since either the analyzer or the
621 * rewriter might expand one query to several.
623 * NOTE: for reasons mentioned above, this must be separate from raw parsing.
626 pg_analyze_and_rewrite(Node *parsetree, const char *query_string,
627 Oid *paramTypes, int numParams)
629 List *querytree_list;
632 * (1) Perform parse analysis.
634 if (log_parser_stats)
637 querytree_list = parse_analyze(parsetree, query_string,
638 paramTypes, numParams);
640 if (log_parser_stats)
641 ShowUsage("PARSE ANALYSIS STATISTICS");
644 * (2) Rewrite the queries, as necessary
646 querytree_list = pg_rewrite_queries(querytree_list);
648 return querytree_list;
652 * Perform rewriting of a list of queries produced by parse analysis.
654 * Note: queries must just have come from the parser, because we do not do
655 * AcquireRewriteLocks() on them.
658 pg_rewrite_queries(List *querytree_list)
660 List *new_list = NIL;
663 if (log_parser_stats)
667 * rewritten queries are collected in new_list. Note there may be more or
668 * fewer than in the original list.
670 foreach(list_item, querytree_list)
672 Query *querytree = (Query *) lfirst(list_item);
674 if (Debug_print_parse)
675 elog_node_display(DEBUG1, "parse tree", querytree,
678 if (querytree->commandType == CMD_UTILITY)
680 /* don't rewrite utilities, just dump 'em into new_list */
681 new_list = lappend(new_list, querytree);
685 /* rewrite regular queries */
686 List *rewritten = QueryRewrite(querytree);
688 new_list = list_concat(new_list, rewritten);
692 querytree_list = new_list;
694 if (log_parser_stats)
695 ShowUsage("REWRITER STATISTICS");
697 #ifdef COPY_PARSE_PLAN_TREES
700 * Optional debugging check: pass querytree output through copyObject()
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");
707 querytree_list = new_list;
710 if (Debug_print_rewritten)
711 elog_node_display(DEBUG1, "rewritten parse tree", querytree_list,
714 return querytree_list;
718 /* Generate a plan for a single already-rewritten query. */
720 pg_plan_query(Query *querytree, ParamListInfo boundParams)
724 /* Utility commands have no plans. */
725 if (querytree->commandType == CMD_UTILITY)
728 if (log_planner_stats)
731 /* call the optimizer */
732 plan = planner(querytree, false, 0, boundParams);
734 if (log_planner_stats)
735 ShowUsage("PLANNER STATISTICS");
737 #ifdef COPY_PARSE_PLAN_TREES
738 /* Optional debugging check: pass plan output through copyObject() */
740 Plan *new_plan = (Plan *) copyObject(plan);
743 * equal() currently does not have routines to compare Plan nodes, so
744 * don't try to test equality here. Perhaps fix someday?
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");
757 * Print plan if debugging.
759 if (Debug_print_plan)
760 elog_node_display(DEBUG1, "plan", plan, Debug_pretty_print);
766 * Generate plans for a list of already-rewritten queries.
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.)
777 pg_plan_queries(List *querytrees, ParamListInfo boundParams,
780 List *plan_list = NIL;
781 ListCell *query_list;
783 foreach(query_list, querytrees)
785 Query *query = (Query *) lfirst(query_list);
788 if (query->commandType == CMD_UTILITY)
790 /* Utility commands have no plans. */
797 ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
798 needSnapshot = false;
800 plan = pg_plan_query(query, boundParams);
803 plan_list = lappend(plan_list, plan);
813 * Execute a "simple Query" protocol message.
816 exec_simple_query(const char *query_string)
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;
827 * Report query to various monitoring facilities.
829 debug_query_string = query_string;
831 pgstat_report_activity(query_string);
834 * We use save_log_statement_stats so ShowUsage doesn't report incorrect
835 * results because ResetUsage wasn't called.
837 if (save_log_statement_stats)
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.)
847 start_xact_command();
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.)
855 unnamed_stmt_pstmt = NULL;
856 if (unnamed_stmt_context)
858 DropDependentPortals(unnamed_stmt_context);
859 MemoryContextDelete(unnamed_stmt_context);
861 unnamed_stmt_context = NULL;
864 * Switch to appropriate context for constructing parsetrees.
866 oldcontext = MemoryContextSwitchTo(MessageContext);
868 QueryContext = CurrentMemoryContext;
871 * Do basic parsing of the query or queries (this should be safe even if
872 * we are in aborted transaction state!)
874 parsetree_list = pg_parse_query(query_string);
876 /* Log immediately if dictated by log_statement */
877 if (log_statement != LOGSTMT_NONE)
878 was_logged = log_after_parse(parsetree_list, query_string,
882 * Switch back to transaction context to enter the loop.
884 MemoryContextSwitchTo(oldcontext);
887 * Run through the raw parsetree(s) and process each one.
889 foreach(parsetree_item, parsetree_list)
891 Node *parsetree = (Node *) lfirst(parsetree_item);
892 const char *commandTag;
893 char completionTag[COMPLETION_TAG_BUFSIZE];
894 List *querytree_list,
897 DestReceiver *receiver;
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
906 commandTag = CreateCommandTag(parsetree);
908 set_ps_display(commandTag, false);
910 BeginCommand(commandTag, dest);
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...)
920 if (IsAbortedTransactionBlockState() &&
921 !IsTransactionExitStmt(parsetree))
923 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
924 errmsg("current transaction is aborted, "
925 "commands ignored until end of transaction block")));
927 /* Make sure we are in a transaction command */
928 start_xact_command();
930 /* If we got a cancel signal in parsing or prior command, quit */
931 CHECK_FOR_INTERRUPTS();
934 * OK to analyze, rewrite, and plan this query.
936 * Switch to appropriate context for constructing querytrees (again,
937 * these must outlive the execution context).
939 oldcontext = MemoryContextSwitchTo(MessageContext);
941 querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
944 plantree_list = pg_plan_queries(querytree_list, NULL, true);
946 /* If we got a cancel signal in analysis or planning, quit */
947 CHECK_FOR_INTERRUPTS();
950 * Create unnamed portal to run the query or queries in. If there
951 * already is one, silently drop it.
953 portal = CreatePortal("", true, true);
954 /* Don't display the portal in pg_cursors */
955 portal->visible = false;
957 PortalDefineQuery(portal,
965 * Start the portal. No parameters here.
967 PortalStart(portal, NULL, InvalidSnapshot);
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...)
975 format = 0; /* TEXT is default */
976 if (IsA(parsetree, FetchStmt))
978 FetchStmt *stmt = (FetchStmt *) parsetree;
982 Portal fportal = GetPortalByName(stmt->portalname);
984 if (PortalIsValid(fportal) &&
985 (fportal->cursorOptions & CURSOR_OPT_BINARY))
986 format = 1; /* BINARY */
989 PortalSetResultFormat(portal, 1, &format);
992 * Now we can create the destination receiver object.
994 receiver = CreateDestReceiver(dest, portal);
997 * Switch back to transaction context for execution.
999 MemoryContextSwitchTo(oldcontext);
1002 * Run the portal to completion, and then drop it (and the receiver).
1004 (void) PortalRun(portal,
1010 (*receiver->rDestroy) (receiver);
1012 PortalDrop(portal, false);
1014 if (IsA(parsetree, TransactionStmt))
1017 * If this was a transaction control statement, commit it. We will
1018 * start a new xact command for the next command (if any).
1020 finish_xact_command();
1022 else if (lnext(parsetree_item) == NULL)
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.
1034 finish_xact_command();
1039 * We need a CommandCounterIncrement after every query, except
1040 * those that start or end a transaction block.
1042 CommandCounterIncrement();
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.)
1051 EndCommand(completionTag, dest);
1052 } /* end loop over parsetrees */
1055 * Close down transaction statement, if one is open.
1057 finish_xact_command();
1060 * If there were no parsetrees, return EmptyQueryResponse message.
1062 if (!parsetree_list)
1065 QueryContext = NULL;
1068 * Emit duration logging if appropriate.
1070 if (log_duration || log_min_duration_statement >= 0)
1076 TimestampDifference(GetCurrentStatementStartTimestamp(),
1077 GetCurrentTimestamp(),
1079 msecs = usecs / 1000;
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.
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)))
1095 (errmsg("duration: %ld.%03d ms",
1099 (errmsg("duration: %ld.%03d ms statement: %s%s",
1102 prepare_string ? prepare_string : "")));
1106 if (save_log_statement_stats)
1107 ShowUsage("QUERY STATISTICS");
1109 if (prepare_string != NULL)
1110 pfree(prepare_string);
1112 debug_query_string = NULL;
1116 * exec_parse_message
1118 * Execute a "Parse" protocol message.
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 */
1126 MemoryContext oldcontext;
1127 List *parsetree_list;
1128 const char *commandTag;
1129 List *querytree_list,
1133 bool save_log_statement_stats = log_statement_stats;
1136 * Report query to various monitoring facilities.
1138 debug_query_string = query_string;
1140 pgstat_report_activity(query_string);
1142 set_ps_display("PARSE", false);
1144 if (save_log_statement_stats)
1147 if (log_statement == LOGSTMT_ALL)
1149 (errmsg("statement: [protocol] PREPARE %s AS %s",
1150 (*stmt_name != '\0') ? stmt_name : "<unnamed>",
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.
1158 start_xact_command();
1161 * Switch to appropriate context for constructing parsetrees.
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.
1173 is_named = (stmt_name[0] != '\0');
1176 /* Named prepared statement --- parse in MessageContext */
1177 oldcontext = MemoryContextSwitchTo(MessageContext);
1181 /* Unnamed prepared statement --- release any prior unnamed stmt */
1182 unnamed_stmt_pstmt = NULL;
1183 if (unnamed_stmt_context)
1185 DropDependentPortals(unnamed_stmt_context);
1186 MemoryContextDelete(unnamed_stmt_context);
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);
1199 QueryContext = CurrentMemoryContext;
1202 * Do basic parsing of the query or queries (this should be safe even if
1203 * we are in aborted transaction state!)
1205 parsetree_list = pg_parse_query(query_string);
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.
1212 if (list_length(parsetree_list) > 1)
1214 (errcode(ERRCODE_SYNTAX_ERROR),
1215 errmsg("cannot insert multiple commands into a prepared statement")));
1217 if (parsetree_list != NIL)
1219 Node *parsetree = (Node *) linitial(parsetree_list);
1223 * Get the command name for possible use in status display.
1225 commandTag = CreateCommandTag(parsetree);
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...)
1235 if (IsAbortedTransactionBlockState() &&
1236 !IsTransactionExitStmt(parsetree))
1238 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1239 errmsg("current transaction is aborted, "
1240 "commands ignored until end of transaction block")));
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().
1247 if (log_parser_stats)
1250 querytree_list = parse_analyze_varparams(parsetree,
1256 * Check all parameter types got determined, and convert array
1257 * representation to a list for storage.
1260 for (i = 0; i < numParams; i++)
1262 Oid ptype = paramTypes[i];
1264 if (ptype == InvalidOid || ptype == UNKNOWNOID)
1266 (errcode(ERRCODE_INDETERMINATE_DATATYPE),
1267 errmsg("could not determine data type of parameter $%d",
1269 param_list = lappend_oid(param_list, ptype);
1272 if (log_parser_stats)
1273 ShowUsage("PARSE ANALYSIS STATISTICS");
1275 querytree_list = pg_rewrite_queries(querytree_list);
1278 * If this is the unnamed statement and it has parameters, defer query
1279 * planning until Bind. Otherwise do it now.
1281 if (!is_named && numParams > 0)
1282 plantree_list = NIL;
1284 plantree_list = pg_plan_queries(querytree_list, NULL, true);
1288 /* Empty input string. This is legal. */
1290 querytree_list = NIL;
1291 plantree_list = NIL;
1295 /* If we got a cancel signal in analysis or planning, quit */
1296 CHECK_FOR_INTERRUPTS();
1299 * Store the query as a prepared statement. See above comments.
1303 StorePreparedStatement(stmt_name,
1313 PreparedStatement *pstmt;
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;
1329 MemoryContextSwitchTo(oldcontext);
1331 QueryContext = NULL;
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.
1338 CommandCounterIncrement();
1341 * Send ParseComplete.
1343 if (whereToSendOutput == DestRemote)
1344 pq_putemptymessage('1');
1346 if (save_log_statement_stats)
1347 ShowUsage("PARSE MESSAGE STATISTICS");
1349 debug_query_string = NULL;
1355 * Process a "Bind" message to create a portal from a prepared statement
1358 exec_bind_message(StringInfo input_message)
1360 const char *portal_name;
1361 const char *stmt_name;
1363 int16 *pformats = NULL;
1366 int16 *rformats = NULL;
1368 PreparedStatement *pstmt;
1370 ParamListInfo params;
1372 pgstat_report_activity("<BIND>");
1374 set_ps_display("BIND", false);
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.
1381 start_xact_command();
1383 /* Switch back to message context */
1384 MemoryContextSwitchTo(MessageContext);
1386 /* Get the fixed part of the message */
1387 portal_name = pq_getmsgstring(input_message);
1388 stmt_name = pq_getmsgstring(input_message);
1390 /* Get the parameter format codes */
1391 numPFormats = pq_getmsgint(input_message, 2);
1392 if (numPFormats > 0)
1394 pformats = (int16 *) palloc(numPFormats * sizeof(int16));
1395 for (i = 0; i < numPFormats; i++)
1396 pformats[i] = pq_getmsgint(input_message, 2);
1399 /* Get the parameter value count */
1400 numParams = pq_getmsgint(input_message, 2);
1402 if (numPFormats > 1 && numPFormats != numParams)
1404 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1405 errmsg("bind message has %d parameter formats but %d parameters",
1406 numPFormats, numParams)));
1408 /* Find prepared statement */
1409 if (stmt_name[0] != '\0')
1410 pstmt = FetchPreparedStatement(stmt_name, true);
1413 /* special-case the unnamed statement */
1414 pstmt = unnamed_stmt_pstmt;
1417 (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
1418 errmsg("unnamed prepared statement does not exist")));
1421 if (numParams != list_length(pstmt->argtype_list))
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))));
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
1435 if (IsAbortedTransactionBlockState() &&
1436 (!IsTransactionExitStmtList(pstmt->query_list) ||
1439 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1440 errmsg("current transaction is aborted, "
1441 "commands ignored until end of transaction block")));
1444 * Create the portal. Allow silent replacement of an existing portal only
1445 * if the unnamed portal is specified.
1447 if (portal_name[0] == '\0')
1448 portal = CreatePortal(portal_name, true, true);
1450 portal = CreatePortal(portal_name, false, false);
1452 /* We need to output the parameter values someday */
1453 if (log_statement == LOGSTMT_ALL)
1455 (errmsg("statement: [protocol] <BIND> %s", portal_name)));
1458 * Fetch parameters, if any, and store in the portal's memory context.
1463 MemoryContext oldContext;
1465 oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
1467 /* sizeof(ParamListInfoData) includes the first array element */
1468 params = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
1469 (numParams - 1) * sizeof(ParamExternData));
1470 params->numParams = numParams;
1473 foreach(l, pstmt->argtype_list)
1475 Oid ptype = lfirst_oid(l);
1478 StringInfoData pbuf;
1482 plength = pq_getmsgint(input_message, 4);
1483 isNull = (plength == -1);
1487 const char *pvalue = pq_getmsgbytes(input_message, plength);
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.
1497 pbuf.data = (char *) pvalue;
1498 pbuf.maxlen = plength + 1;
1502 csave = pbuf.data[plength];
1503 pbuf.data[plength] = '\0';
1507 pbuf.data = NULL; /* keep compiler quiet */
1511 if (numPFormats > 1)
1512 pformat = pformats[i];
1513 else if (numPFormats > 0)
1514 pformat = pformats[0];
1516 pformat = 0; /* default = text */
1524 getTypeInputInfo(ptype, &typinput, &typioparam);
1527 * We have to do encoding conversion before calling the
1533 pstring = pg_client_to_server(pbuf.data, plength);
1535 params->params[i].value = OidInputFunctionCall(typinput,
1539 /* Free result of encoding conversion, if any */
1540 if (pstring && pstring != pbuf.data)
1543 else if (pformat == 1)
1550 * Call the parameter type's binary input converter
1552 getTypeBinaryInputInfo(ptype, &typreceive, &typioparam);
1559 params->params[i].value = OidReceiveFunctionCall(typreceive,
1564 /* Trouble if it didn't eat the whole buffer */
1565 if (!isNull && pbuf.cursor != pbuf.len)
1567 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1568 errmsg("incorrect binary data format in bind parameter %d",
1574 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1575 errmsg("unsupported format code: %d",
1579 /* Restore message buffer contents */
1581 pbuf.data[plength] = csave;
1583 params->params[i].isnull = isNull;
1584 params->params[i].ptype = ptype;
1589 MemoryContextSwitchTo(oldContext);
1594 /* Get the result format codes */
1595 numRFormats = pq_getmsgint(input_message, 2);
1596 if (numRFormats > 0)
1598 rformats = (int16 *) palloc(numRFormats * sizeof(int16));
1599 for (i = 0; i < numRFormats; i++)
1600 rformats[i] = pq_getmsgint(input_message, 2);
1603 pq_getmsgend(input_message);
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.
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).
1613 if (pstmt->plan_list == NIL && pstmt->query_list != NIL)
1615 MemoryContext oldContext = MemoryContextSwitchTo(pstmt->context);
1617 pstmt->plan_list = pg_plan_queries(pstmt->query_list, params, true);
1618 MemoryContextSwitchTo(oldContext);
1622 * Define portal and start execution.
1624 PortalDefineQuery(portal,
1625 pstmt->query_string,
1631 PortalStart(portal, params, InvalidSnapshot);
1634 * Apply the result format requests to the portal.
1636 PortalSetResultFormat(portal, numRFormats, rformats);
1639 * Send BindComplete.
1641 if (whereToSendOutput == DestRemote)
1642 pq_putemptymessage('2');
1646 * exec_execute_message
1648 * Process an "Execute" message for a portal
1651 exec_execute_message(const char *portal_name, long max_rows)
1654 DestReceiver *receiver;
1657 char completionTag[COMPLETION_TAG_BUFSIZE];
1658 bool save_log_statement_stats = log_statement_stats;
1659 bool execute_is_fetch = false;
1661 /* Adjust destination to tell printtup.c what to do */
1662 dest = whereToSendOutput;
1663 if (dest == DestRemote)
1664 dest = DestRemoteExecute;
1666 portal = GetPortalByName(portal_name);
1667 if (!PortalIsValid(portal))
1669 (errcode(ERRCODE_UNDEFINED_CURSOR),
1670 errmsg("portal \"%s\" does not exist", portal_name)));
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.
1678 if (!portal->atStart)
1679 execute_is_fetch = true;
1682 * If the original query was a null string, just return
1683 * EmptyQueryResponse.
1685 if (portal->commandTag == NULL)
1687 Assert(portal->parseTrees == NIL);
1692 /* Should we display the portal names here? */
1693 if (execute_is_fetch)
1695 debug_query_string = "fetch message";
1696 pgstat_report_activity("<FETCH>");
1698 else if (portal->sourceText)
1700 debug_query_string = portal->sourceText;
1701 pgstat_report_activity(portal->sourceText);
1705 debug_query_string = "execute message";
1706 pgstat_report_activity("<EXECUTE>");
1709 set_ps_display(portal->commandTag, false);
1712 * We use save_log_statement_stats so ShowUsage doesn't report incorrect
1713 * results because ResetUsage wasn't called.
1715 if (save_log_statement_stats)
1718 if (log_statement == LOGSTMT_ALL)
1719 /* We have the portal, so output the source query. */
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 : "")));
1726 BeginCommand(portal->commandTag, dest);
1729 * Create dest receiver in MessageContext (we don't want it in transaction
1730 * context, because that may get deleted if portal contains VACUUM).
1732 receiver = CreateDestReceiver(dest, portal);
1735 * Ensure we are in a transaction command (this should normally be the
1736 * case already due to prior BIND).
1738 start_xact_command();
1741 * If we are in aborted transaction state, the only portals we can
1742 * actually run are those containing COMMIT or ROLLBACK commands.
1744 if (IsAbortedTransactionBlockState() &&
1745 !IsTransactionExitStmtList(portal->parseTrees))
1747 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1748 errmsg("current transaction is aborted, "
1749 "commands ignored until end of transaction block")));
1751 /* Check for cancel signal before we start execution */
1752 CHECK_FOR_INTERRUPTS();
1755 * Okay to run the portal.
1758 max_rows = FETCH_ALL;
1760 completed = PortalRun(portal,
1766 (*receiver->rDestroy) (receiver);
1770 if (IsTransactionStmtList(portal->parseTrees))
1773 * If this was a transaction control statement, commit it. We
1774 * will start a new xact command for the next command (if any).
1776 finish_xact_command();
1781 * We need a CommandCounterIncrement after every query, except
1782 * those that start or end a transaction block.
1784 CommandCounterIncrement();
1787 /* Send appropriate CommandComplete to client */
1788 EndCommand(completionTag, dest);
1792 /* Portal run not complete, so send PortalSuspended */
1793 if (whereToSendOutput == DestRemote)
1794 pq_putemptymessage('s');
1798 * Emit duration logging if appropriate.
1800 if (log_duration || log_min_duration_statement >= 0)
1806 TimestampDifference(GetCurrentStatementStartTimestamp(),
1807 GetCurrentTimestamp(),
1809 msecs = usecs / 1000;
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.
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)))
1823 if (log_statement == LOGSTMT_ALL) /* already logged? */
1825 (errmsg("duration: %ld.%03d ms",
1829 (errmsg("duration: %ld.%03d ms statement: [protocol] %sEXECUTE %s [PREPARE: %s]",
1831 execute_is_fetch ? "FETCH from " : "",
1832 (*portal_name) ? portal_name : "<unnamed>",
1833 portal->sourceText ? portal->sourceText : "")));
1837 if (save_log_statement_stats)
1838 ShowUsage("QUERY STATISTICS");
1840 debug_query_string = NULL;
1844 * exec_describe_statement_message
1846 * Process a "Describe" message for a prepared statement
1849 exec_describe_statement_message(const char *stmt_name)
1851 PreparedStatement *pstmt;
1857 * Start up a transaction command. (Note that this will normally change
1858 * current memory context.) Nothing happens if we are already in one.
1860 start_xact_command();
1862 /* Switch back to message context */
1863 MemoryContextSwitchTo(MessageContext);
1865 /* Find prepared statement */
1866 if (stmt_name[0] != '\0')
1867 pstmt = FetchPreparedStatement(stmt_name, true);
1870 /* special-case the unnamed statement */
1871 pstmt = unnamed_stmt_pstmt;
1874 (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
1875 errmsg("unnamed prepared statement does not exist")));
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.
1887 if (IsAbortedTransactionBlockState() &&
1888 PreparedStatementReturnsTuples(pstmt))
1890 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1891 errmsg("current transaction is aborted, "
1892 "commands ignored until end of transaction block")));
1894 if (whereToSendOutput != DestRemote)
1895 return; /* can't actually do anything... */
1898 * First describe the parameters...
1900 pq_beginmessage(&buf, 't'); /* parameter description message type */
1901 pq_sendint(&buf, list_length(pstmt->argtype_list), 2);
1903 foreach(l, pstmt->argtype_list)
1905 Oid ptype = lfirst_oid(l);
1907 pq_sendint(&buf, (int) ptype, 4);
1909 pq_endmessage(&buf);
1912 * Next send RowDescription or NoData to describe the result...
1914 tupdesc = FetchPreparedStatementResultDesc(pstmt);
1916 SendRowDescriptionMessage(tupdesc,
1917 FetchPreparedStatementTargetList(pstmt),
1920 pq_putemptymessage('n'); /* NoData */
1925 * exec_describe_portal_message
1927 * Process a "Describe" message for a portal
1930 exec_describe_portal_message(const char *portal_name)
1935 * Start up a transaction command. (Note that this will normally change
1936 * current memory context.) Nothing happens if we are already in one.
1938 start_xact_command();
1940 /* Switch back to message context */
1941 MemoryContextSwitchTo(MessageContext);
1943 portal = GetPortalByName(portal_name);
1944 if (!PortalIsValid(portal))
1946 (errcode(ERRCODE_UNDEFINED_CURSOR),
1947 errmsg("portal \"%s\" does not exist", portal_name)));
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.)
1957 if (IsAbortedTransactionBlockState() &&
1960 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1961 errmsg("current transaction is aborted, "
1962 "commands ignored until end of transaction block")));
1964 if (whereToSendOutput != DestRemote)
1965 return; /* can't actually do anything... */
1967 if (portal->tupDesc)
1968 SendRowDescriptionMessage(portal->tupDesc,
1969 FetchPortalTargetList(portal),
1972 pq_putemptymessage('n'); /* NoData */
1977 * Convenience routines for starting/committing a single command.
1980 start_xact_command(void)
1985 (errmsg_internal("StartTransactionCommand")));
1986 StartTransactionCommand();
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);
1993 cancel_from_timeout = false;
1995 xact_started = true;
2000 finish_xact_command(void)
2004 /* Cancel any active statement timeout before committing */
2005 disable_sig_alarm(true);
2007 /* Now commit the command */
2009 (errmsg_internal("CommitTransactionCommand")));
2011 CommitTransactionCommand();
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);
2019 #ifdef SHOW_MEMORY_STATS
2020 /* Print mem stats after each commit for leak tracking */
2022 MemoryContextStats(TopMemoryContext);
2025 xact_started = false;
2031 * Convenience routines for checking whether a statement is one of the
2032 * ones that we allow in transaction-aborted state.
2036 IsTransactionExitStmt(Node *parsetree)
2038 if (parsetree && IsA(parsetree, TransactionStmt))
2040 TransactionStmt *stmt = (TransactionStmt *) parsetree;
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)
2052 IsTransactionExitStmtList(List *parseTrees)
2054 if (list_length(parseTrees) == 1)
2056 Query *query = (Query *) linitial(parseTrees);
2058 if (query->commandType == CMD_UTILITY &&
2059 IsTransactionExitStmt(query->utilityStmt))
2066 IsTransactionStmtList(List *parseTrees)
2068 if (list_length(parseTrees) == 1)
2070 Query *query = (Query *) linitial(parseTrees);
2072 if (query->commandType == CMD_UTILITY &&
2073 query->utilityStmt && IsA(query->utilityStmt, TransactionStmt))
2080 /* --------------------------------
2081 * signal handler routines used in PostgresMain()
2082 * --------------------------------
2086 * quickdie() occurs when signalled SIGQUIT by the postmaster.
2088 * Some backend has bought the farm,
2089 * so we need to stop what we're doing and exit.
2092 quickdie(SIGNAL_ARGS)
2094 PG_SETMASK(&BlockSig);
2097 * Ideally this should be ereport(FATAL), but then we'd not get control
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"
2107 errhint("In a moment you should be able to reconnect to the"
2108 " database and repeat your command.")));
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.
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.
2124 * Shutdown signal from postmaster: abort transaction and exit
2125 * at soonest convenient time
2130 int save_errno = errno;
2132 /* Don't joggle the elbow of proc_exit */
2133 if (!proc_exit_inprogress)
2135 InterruptPending = true;
2136 ProcDiePending = true;
2139 * If it's safe to interrupt, and we're waiting for input or a lock,
2140 * service the interrupt immediately
2142 if (ImmediateInterruptOK && InterruptHoldoffCount == 0 &&
2143 CritSectionCount == 0)
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... */
2152 InterruptHoldoffCount--;
2153 ProcessInterrupts();
2161 * Timeout or shutdown signal from postmaster during client authentication.
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.
2169 authdie(SIGNAL_ARGS)
2175 * Query-cancel signal from postmaster: abort current transaction
2176 * at soonest convenient time
2179 StatementCancelHandler(SIGNAL_ARGS)
2181 int save_errno = errno;
2184 * Don't joggle the elbow of proc_exit
2186 if (!proc_exit_inprogress)
2188 InterruptPending = true;
2189 QueryCancelPending = true;
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.
2196 if (ImmediateInterruptOK && InterruptHoldoffCount == 0 &&
2197 CritSectionCount == 0)
2199 /* bump holdoff count to make ProcessInterrupts() a no-op */
2200 /* until we are done getting ready for it */
2201 InterruptHoldoffCount++;
2202 if (LockWaitCancel())
2204 DisableNotifyInterrupt();
2205 DisableCatchupInterrupt();
2206 InterruptHoldoffCount--;
2207 ProcessInterrupts();
2210 InterruptHoldoffCount--;
2217 /* signal handler for floating point exception */
2219 FloatExceptionHandler(SIGNAL_ARGS)
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.")));
2229 /* SIGHUP: set flag to re-read config file at next convenient time */
2231 SigHupHandler(SIGNAL_ARGS)
2238 * ProcessInterrupts: out-of-line portion of CHECK_FOR_INTERRUPTS() macro
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.
2245 ProcessInterrupts(void)
2247 /* OK to accept interrupt now? */
2248 if (InterruptHoldoffCount != 0 || CritSectionCount != 0)
2250 InterruptPending = false;
2253 ProcDiePending = false;
2254 QueryCancelPending = false; /* ProcDie trumps QueryCancel */
2255 ImmediateInterruptOK = false; /* not idle anymore */
2256 DisableNotifyInterrupt();
2257 DisableCatchupInterrupt();
2259 (errcode(ERRCODE_ADMIN_SHUTDOWN),
2260 errmsg("terminating connection due to administrator command")));
2262 if (QueryCancelPending)
2264 QueryCancelPending = false;
2265 ImmediateInterruptOK = false; /* not idle anymore */
2266 DisableNotifyInterrupt();
2267 DisableCatchupInterrupt();
2268 if (cancel_from_timeout)
2270 (errcode(ERRCODE_QUERY_CANCELED),
2271 errmsg("canceling statement due to statement timeout")));
2274 (errcode(ERRCODE_QUERY_CANCELED),
2275 errmsg("canceling statement due to user request")));
2277 /* If we get here, do nothing (probably, QueryCancelPending was reset) */
2282 * check_stack_depth: check for excessively deep recursion
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 ...
2292 check_stack_depth(void)
2298 * Compute distance from PostgresMain's local variables to my own
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.
2304 stack_depth = (int) (stack_base_ptr - &stack_top_loc);
2307 * Take abs value, since stacks grow up on some machines, down on others
2309 if (stack_depth < 0)
2310 stack_depth = -stack_depth;
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
2320 if (stack_depth > max_stack_depth_bytes &&
2321 stack_base_ptr != NULL)
2324 (errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
2325 errmsg("stack depth limit exceeded"),
2326 errhint("Increase the configuration parameter \"max_stack_depth\".")));
2330 /* GUC assign hook to update max_stack_depth_bytes from max_stack_depth */
2332 assign_max_stack_depth(int newval, bool doit, GucSource source)
2334 /* Range check was already handled by guc.c */
2336 max_stack_depth_bytes = newval * 1024;
2342 * set_debug_options --- apply "-d N" command line option
2344 * -d is not quite the same as setting log_min_messages because it enables
2345 * other output options.
2348 set_debug_options(int debug_flag, GucContext context, GucSource source)
2354 sprintf(debugstr, "debug%d", debug_flag);
2355 SetConfigOption("log_min_messages", debugstr, context, source);
2358 SetConfigOption("log_min_messages", "notice", context, source);
2360 if (debug_flag >= 1 && context == PGC_POSTMASTER)
2362 SetConfigOption("log_connections", "true", context, source);
2363 SetConfigOption("log_disconnections", "true", context, source);
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);
2377 set_plan_disabling_options(const char *arg, GucContext context, GucSource source)
2383 case 's': /* seqscan */
2384 tmp = "enable_seqscan";
2386 case 'i': /* indexscan */
2387 tmp = "enable_indexscan";
2389 case 'b': /* bitmapscan */
2390 tmp = "enable_bitmapscan";
2392 case 't': /* tidscan */
2393 tmp = "enable_tidscan";
2395 case 'n': /* nestloop */
2396 tmp = "enable_nestloop";
2398 case 'm': /* mergejoin */
2399 tmp = "enable_mergejoin";
2401 case 'h': /* hashjoin */
2402 tmp = "enable_hashjoin";
2407 SetConfigOption(tmp, "false", context, source);
2416 get_stats_option_name(const char *arg)
2421 if (optarg[1] == 'a') /* "parser" */
2422 return "log_parser_stats";
2423 else if (optarg[1] == 'l') /* "planner" */
2424 return "log_planner_stats";
2427 case 'e': /* "executor" */
2428 return "log_executor_stats";
2436 /* ----------------------------------------------------------------
2438 * postgres main loop -- all backends, interactive or otherwise start here
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
2444 * ----------------------------------------------------------------
2447 PostgresMain(int argc, char *argv[], const char *username)
2450 const char *dbname = NULL;
2451 char *userDoption = NULL;
2454 int debug_flag = -1; /* -1 means not given */
2455 List *guc_names = NIL; /* for SUSET options */
2456 List *guc_values = NIL;
2458 GucSource gucsource;
2462 StringInfoData input_message;
2463 sigjmp_buf local_sigjmp_buf;
2464 volatile bool send_ready_for_query = true;
2466 #define PendingConfigOption(name,val) \
2467 (guc_names = lappend(guc_names, pstrdup(name)), \
2468 guc_values = lappend(guc_values, pstrdup(val)))
2471 * initialize globals (already done if under postmaster, but not if
2472 * standalone; cheap enough to do over)
2474 MyProcPid = getpid();
2477 * Fire up essential subsystems: error and memory management
2479 * If we are running under the postmaster, this is done already.
2481 if (!IsUnderPostmaster)
2482 MemoryContextInit();
2484 set_ps_display("startup", false);
2486 SetProcessingMode(InitProcessing);
2488 /* Set up reference point for stack depth checking */
2489 stack_base_ptr = &stack_base;
2491 /* Compute paths, if we didn't inherit them from postmaster */
2492 if (my_exec_path[0] == '\0')
2494 if (find_my_exec(argv[0], my_exec_path) < 0)
2495 elog(FATAL, "%s: could not locate my own executable path",
2499 if (pkglib_path[0] == '\0')
2500 get_pkglib_path(my_exec_path, pkglib_path);
2503 * Set default values for command-line options.
2507 if (!IsUnderPostmaster)
2508 InitializeGUCOptions();
2511 * parse command line arguments
2513 * There are now two styles of command line layout for the backend:
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.
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.
2527 /* Ignore the initial --single argument, if present */
2528 if (argc > 1 && strcmp(argv[1], "--single") == 0)
2534 /* all options are allowed until '-p' */
2536 ctx = PGC_POSTMASTER;
2537 gucsource = PGC_S_ARGV; /* initial switches came from command line */
2539 while ((flag = getopt(argc, argv, "A:B:c:D:d:EeFf:h:ijk:lN:nOo:Pp:r:S:sTt:v:W:y:-:")) != -1)
2544 SetConfigOption("debug_assertions", optarg, ctx, gucsource);
2548 SetConfigOption("shared_buffers", optarg, ctx, gucsource);
2553 userDoption = optarg;
2557 debug_flag = atoi(optarg);
2565 SetConfigOption("datestyle", "euro", ctx, gucsource);
2569 SetConfigOption("fsync", "false", ctx, gucsource);
2573 if (!set_plan_disabling_options(optarg, ctx, gucsource))
2578 SetConfigOption("listen_addresses", optarg, ctx, gucsource);
2582 SetConfigOption("listen_addresses", "*", ctx, gucsource);
2590 SetConfigOption("unix_socket_directory", optarg, ctx, gucsource);
2594 SetConfigOption("ssl", "true", ctx, gucsource);
2598 SetConfigOption("max_connections", optarg, ctx, gucsource);
2602 /* ignored for consistency with postmaster */
2606 SetConfigOption("allow_system_table_mods", "true", ctx, gucsource);
2614 SetConfigOption("ignore_system_indexes", "true", ctx, gucsource);
2618 SetConfigOption("port", optarg, ctx, gucsource);
2622 /* send output (stdout and stderr) to the given file */
2624 StrNCpy(OutputFileName, optarg, MAXPGPATH);
2628 SetConfigOption("work_mem", optarg, ctx, gucsource);
2633 * Since log options are SUSET, we need to postpone unless
2634 * still in secure context
2636 if (ctx == PGC_BACKEND)
2637 PendingConfigOption("log_statement_stats", "true");
2639 SetConfigOption("log_statement_stats", "true",
2644 /* ignored for consistency with postmaster */
2649 const char *tmp = get_stats_option_name(optarg);
2652 if (ctx == PGC_BACKEND)
2653 PendingConfigOption(tmp, "true");
2655 SetConfigOption(tmp, "true", ctx, gucsource);
2664 FrontendProtocol = (ProtocolVersion) atoi(optarg);
2668 SetConfigOption("post_auth_delay", optarg, ctx, gucsource);
2674 * y - special flag passed if backend was forked by a
2679 dbname = strdup(optarg);
2681 secure = false; /* subsequent switches are NOT secure */
2683 gucsource = PGC_S_CLIENT;
2693 ParseLongOption(optarg, &name, &value);
2698 (errcode(ERRCODE_SYNTAX_ERROR),
2699 errmsg("--%s requires a value",
2703 (errcode(ERRCODE_SYNTAX_ERROR),
2704 errmsg("-c %s requires a value",
2709 * If a SUSET option, must postpone evaluation, unless we
2710 * are still reading secure switches.
2712 if (ctx == PGC_BACKEND && IsSuperuserConfigOption(name))
2713 PendingConfigOption(name, value);
2715 SetConfigOption(name, value, ctx, gucsource);
2729 * Process any additional GUC variable settings passed in startup packet.
2730 * These are handled exactly like command-line variables.
2732 if (MyProcPort != NULL)
2734 ListCell *gucopts = list_head(MyProcPort->guc_options);
2741 name = lfirst(gucopts);
2742 gucopts = lnext(gucopts);
2744 value = lfirst(gucopts);
2745 gucopts = lnext(gucopts);
2747 if (IsSuperuserConfigOption(name))
2748 PendingConfigOption(name, value);
2750 SetConfigOption(name, value, PGC_BACKEND, PGC_S_CLIENT);
2754 /* Acquire configuration parameters, unless inherited from postmaster */
2755 if (!IsUnderPostmaster)
2757 if (!SelectConfigFiles(userDoption, argv[0]))
2759 /* If timezone is not set, determine what the OS uses */
2760 pg_timezone_initialize();
2764 pg_usleep(PostAuthDelay * 1000000L);
2767 * Set up signal handlers and masks.
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.
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
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 */
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...
2792 pqsignal(SIGPIPE, SIG_IGN);
2793 pqsignal(SIGUSR1, CatchupInterruptHandler);
2794 pqsignal(SIGUSR2, NotifyInterruptHandler);
2795 pqsignal(SIGFPE, FloatExceptionHandler);
2798 * Reset some signals that are accepted by postmaster but not by backend
2800 pqsignal(SIGCHLD, SIG_DFL); /* system() requires this on some platforms */
2804 /* We allow SIGQUIT (quickdie) at all times */
2805 #ifdef HAVE_SIGPROCMASK
2806 sigdelset(&BlockSig, SIGQUIT);
2808 BlockSig &= ~(sigmask(SIGQUIT));
2811 PG_SETMASK(&BlockSig); /* block everything except SIGQUIT */
2813 if (IsUnderPostmaster)
2815 /* noninteractive case: nothing should be left after switches */
2816 if (errs || argc != optind || dbname == NULL)
2819 (errcode(ERRCODE_SYNTAX_ERROR),
2820 errmsg("invalid command-line arguments for server process"),
2821 errhint("Try \"%s --help\" for more information.", argv[0])));
2828 /* interactive case: database name can be last arg on command line */
2829 if (errs || argc - optind > 1)
2832 (errcode(ERRCODE_SYNTAX_ERROR),
2833 errmsg("%s: invalid command-line arguments",
2835 errhint("Try \"%s --help\" for more information.", argv[0])));
2837 else if (argc - optind == 1)
2838 dbname = argv[optind];
2839 else if ((dbname = username) == NULL)
2842 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2843 errmsg("%s: no database nor user name specified",
2848 * Validate we have been given a reasonable-looking DataDir (if under
2849 * postmaster, assume postmaster did this already).
2852 ValidatePgVersion(DataDir);
2854 /* Change into DataDir (if under postmaster, was done already) */
2858 * Create lockfile for data directory.
2860 CreateDataDirLockFile(false);
2865 * Start up xlog for standalone backend, and register to have it
2866 * closed down at exit.
2869 on_shmem_exit(ShutdownXLOG, 0);
2872 * Read any existing FSM cache file, and register to write one out at
2876 on_shmem_exit(DumpFreeSpaceMap, 0);
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.
2882 BuildFlatFiles(true);
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).
2892 if (!IsUnderPostmaster)
2899 * General initialization.
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.
2906 (errmsg_internal("InitPostgres")));
2907 am_superuser = InitPostgres(dbname, username);
2909 SetProcessingMode(NormalProcessing);
2912 * Now that we know if client is a superuser, we can try to apply SUSET
2913 * GUC options that came from the client.
2915 ctx = am_superuser ? PGC_SUSET : PGC_USERSET;
2917 if (debug_flag >= 0)
2918 set_debug_options(debug_flag, ctx, PGC_S_CLIENT);
2920 if (guc_names != NIL)
2925 forboth(namcell, guc_names, valcell, guc_values)
2927 char *name = (char *) lfirst(namcell);
2928 char *value = (char *) lfirst(valcell);
2930 SetConfigOption(name, value, ctx, PGC_S_CLIENT);
2937 * Now all GUC states are fully set up. Report them to client if
2940 BeginReportingGUCOptions();
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.
2946 if (IsUnderPostmaster && Log_disconnections)
2947 on_proc_exit(log_disconnections, 0);
2950 * Send this backend's cancellation info to the frontend.
2952 if (whereToSendOutput == DestRemote &&
2953 PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
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. */
2964 /* Welcome banner for standalone case */
2965 if (whereToSendOutput == DestDebug)
2966 printf("\nPostgreSQL stand-alone backend %s\n", PG_VERSION);
2969 * Create the memory context we will use in the main loop.
2971 * MessageContext is reset once per iteration of the main loop, ie, upon
2972 * completion of processing of each command message from the client.
2974 MessageContext = AllocSetContextCreate(TopMemoryContext,
2976 ALLOCSET_DEFAULT_MINSIZE,
2977 ALLOCSET_DEFAULT_INITSIZE,
2978 ALLOCSET_DEFAULT_MAXSIZE);
2981 * Remember stand-alone backend startup time
2983 if (!IsUnderPostmaster)
2984 PgStartTime = GetCurrentTimestamp();
2987 * POSTGRES main processing loop begins here
2989 * If an exception is encountered, processing resumes here so we abort the
2990 * current transaction and start a new one.
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.)
3001 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
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.
3011 /* Since not using PG_TRY, must reset error stack by hand */
3012 error_context_stack = NULL;
3014 /* Prevent interrupts while cleaning up */
3018 * Forget any pending QueryCancel request, since we're returning to
3019 * the idle loop anyway, and cancel the statement timer if running.
3021 QueryCancelPending = false;
3022 disable_sig_alarm(true);
3023 QueryCancelPending = false; /* again in case timeout occurred */
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.
3030 DoingCommandRead = false;
3031 DisableNotifyInterrupt();
3032 DisableCatchupInterrupt();
3034 /* Make sure libpq is in a good state */
3037 /* Report the error to the client and/or server log */
3041 * Make sure debug_query_string gets reset before we possibly clobber
3042 * the storage it points at.
3044 debug_query_string = NULL;
3047 * Abort the current transaction in order to recover.
3049 AbortCurrentTransaction();
3052 * Now return to normal top-level context and clear ErrorContext for
3055 MemoryContextSwitchTo(TopMemoryContext);
3057 QueryContext = NULL;
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).
3064 if (doing_extended_query_message)
3065 ignore_till_sync = true;
3067 /* We don't have a transaction command open anymore */
3068 xact_started = false;
3070 /* Now we can allow interrupts again */
3071 RESUME_INTERRUPTS();
3074 /* We can now handle ereport(ERROR) */
3075 PG_exception_stack = &local_sigjmp_buf;
3077 PG_SETMASK(&UnBlockSig);
3079 if (!ignore_till_sync)
3080 send_ready_for_query = true; /* initially, or after error */
3083 * Non-error queries loop here.
3089 * At top of loop, reset extended-query-message flag, so that any
3090 * errors encountered in "idle" state don't provoke skip.
3092 doing_extended_query_message = false;
3095 * Release storage left over from prior query cycle, and create a new
3096 * query input buffer in the cleared MessageContext.
3098 MemoryContextSwitchTo(MessageContext);
3099 MemoryContextResetAndDeleteChildren(MessageContext);
3101 initStringInfo(&input_message);
3104 * (1) If we've reached idle state, tell the frontend we're ready for
3107 * Note: this includes fflush()'ing the last of the prior output.
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).
3115 if (send_ready_for_query)
3117 if (IsTransactionOrTransactionBlock())
3119 set_ps_display("idle in transaction", false);
3120 pgstat_report_activity("<IDLE> in transaction");
3124 pgstat_report_tabstat();
3126 set_ps_display("idle", false);
3127 pgstat_report_activity("<IDLE>");
3130 ReadyForQuery(whereToSendOutput);
3131 send_ready_for_query = false;
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.)
3140 QueryCancelPending = false; /* forget any earlier CANCEL signal */
3141 DoingCommandRead = true;
3144 * (3) read a command (loop blocks here)
3146 firstchar = ReadCommand(&input_message);
3149 * (4) disable async signal conditions again.
3151 DoingCommandRead = false;
3154 * (5) check for any other interesting events that happened while we
3160 ProcessConfigFile(PGC_SIGHUP);
3164 * (6) process the command. But ignore it if we're skipping till
3167 if (ignore_till_sync && firstchar != EOF)
3172 case 'Q': /* simple query */
3174 const char *query_string;
3176 /* Set statement_timestamp() */
3177 SetCurrentStatementStartTimestamp();
3179 query_string = pq_getmsgstring(&input_message);
3180 pq_getmsgend(&input_message);
3182 exec_simple_query(query_string);
3184 send_ready_for_query = true;
3188 case 'P': /* parse */
3190 const char *stmt_name;
3191 const char *query_string;
3193 Oid *paramTypes = NULL;
3195 /* Set statement_timestamp() */
3196 SetCurrentStatementStartTimestamp();
3198 stmt_name = pq_getmsgstring(&input_message);
3199 query_string = pq_getmsgstring(&input_message);
3200 numParams = pq_getmsgint(&input_message, 2);
3205 paramTypes = (Oid *) palloc(numParams * sizeof(Oid));
3206 for (i = 0; i < numParams; i++)
3207 paramTypes[i] = pq_getmsgint(&input_message, 4);
3209 pq_getmsgend(&input_message);
3211 exec_parse_message(query_string, stmt_name,
3212 paramTypes, numParams);
3216 case 'B': /* bind */
3217 /* Set statement_timestamp() */
3218 SetCurrentStatementStartTimestamp();
3221 * this message is complex enough that it seems best to put
3222 * the field extraction out-of-line
3224 exec_bind_message(&input_message);
3227 case 'E': /* execute */
3229 const char *portal_name;
3232 /* Set statement_timestamp() */
3233 SetCurrentStatementStartTimestamp();
3235 portal_name = pq_getmsgstring(&input_message);
3236 max_rows = pq_getmsgint(&input_message, 4);
3237 pq_getmsgend(&input_message);
3239 exec_execute_message(portal_name, max_rows);
3243 case 'F': /* fastpath function call */
3244 /* Set statement_timestamp() */
3245 SetCurrentStatementStartTimestamp();
3247 /* Tell the collector what we're doing */
3248 pgstat_report_activity("<FASTPATH> function call");
3250 /* start an xact for this function invocation */
3251 start_xact_command();
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.
3262 /* switch back to message context */
3263 MemoryContextSwitchTo(MessageContext);
3265 if (HandleFunctionRequest(&input_message) == EOF)
3267 /* lost frontend connection during F message input */
3270 * Reset whereToSendOutput to prevent ereport from
3271 * attempting to send any more messages to client.
3273 if (whereToSendOutput == DestRemote)
3274 whereToSendOutput = DestNone;
3279 /* commit the function-invocation transaction */
3280 finish_xact_command();
3282 send_ready_for_query = true;
3285 case 'C': /* close */
3288 const char *close_target;
3290 close_type = pq_getmsgbyte(&input_message);
3291 close_target = pq_getmsgstring(&input_message);
3292 pq_getmsgend(&input_message);
3297 if (close_target[0] != '\0')
3298 DropPreparedStatement(close_target, false);
3301 /* special-case the unnamed statement */
3302 unnamed_stmt_pstmt = NULL;
3303 if (unnamed_stmt_context)
3305 DropDependentPortals(unnamed_stmt_context);
3306 MemoryContextDelete(unnamed_stmt_context);
3308 unnamed_stmt_context = NULL;
3315 portal = GetPortalByName(close_target);
3316 if (PortalIsValid(portal))
3317 PortalDrop(portal, false);
3322 (errcode(ERRCODE_PROTOCOL_VIOLATION),
3323 errmsg("invalid CLOSE message subtype %d",
3328 if (whereToSendOutput == DestRemote)
3329 pq_putemptymessage('3'); /* CloseComplete */
3333 case 'D': /* describe */
3336 const char *describe_target;
3338 /* Set statement_timestamp() (needed for xact) */
3339 SetCurrentStatementStartTimestamp();
3341 describe_type = pq_getmsgbyte(&input_message);
3342 describe_target = pq_getmsgstring(&input_message);
3343 pq_getmsgend(&input_message);
3345 switch (describe_type)
3348 exec_describe_statement_message(describe_target);
3351 exec_describe_portal_message(describe_target);
3355 (errcode(ERRCODE_PROTOCOL_VIOLATION),
3356 errmsg("invalid DESCRIBE message subtype %d",
3363 case 'H': /* flush */
3364 pq_getmsgend(&input_message);
3365 if (whereToSendOutput == DestRemote)
3369 case 'S': /* sync */
3370 pq_getmsgend(&input_message);
3371 finish_xact_command();
3372 send_ready_for_query = true;
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.
3384 * Reset whereToSendOutput to prevent ereport from attempting
3385 * to send any more messages to client.
3387 if (whereToSendOutput == DestRemote)
3388 whereToSendOutput = DestNone;
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
3399 case 'd': /* copy data */
3400 case 'c': /* copy done */
3401 case 'f': /* copy fail */
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.
3412 (errcode(ERRCODE_PROTOCOL_VIOLATION),
3413 errmsg("invalid frontend message type %d",
3416 } /* end of input-reading loop */
3418 /* can't get here because the above loop never exits */
3421 return 1; /* keep compiler quiet */
3424 #ifndef HAVE_GETRUSAGE
3425 #include "rusagestub.h"
3427 #include <sys/resource.h>
3428 #endif /* HAVE_GETRUSAGE */
3430 static struct rusage Save_r;
3431 static struct timeval Save_t;
3436 getrusage(RUSAGE_SELF, &Save_r);
3437 gettimeofday(&Save_t, NULL);
3439 /* ResetTupleCount(); */
3443 ShowUsage(const char *title)
3446 struct timeval user,
3448 struct timeval elapse_t;
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)
3459 elapse_t.tv_usec += 1000000;
3461 if (r.ru_utime.tv_usec < Save_r.ru_utime.tv_usec)
3463 r.ru_utime.tv_sec--;
3464 r.ru_utime.tv_usec += 1000000;
3466 if (r.ru_stime.tv_usec < Save_r.ru_stime.tv_usec)
3468 r.ru_stime.tv_sec--;
3469 r.ru_stime.tv_usec += 1000000;
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.
3479 initStringInfo(&str);
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",
3493 (long) user.tv_usec,
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,
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,
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 */
3524 bufusage = ShowBufferUsage();
3525 appendStringInfo(&str, "! buffer usage stats:\n%s", bufusage);
3528 /* remove trailing newline */
3529 if (str.data[str.len - 1] == '\n')
3530 str.data[--str.len] = '\0';
3533 (errmsg_internal("%s", title),
3534 errdetail("%s", str.data)));
3540 * on_proc_exit handler to log end of session
3543 log_disconnections(int code, Datum arg)
3545 Port *port = MyProcPort;
3553 TimestampDifference(port->SessionStartTime,
3554 GetCurrentTimestamp(),
3556 msecs = usecs / 1000;
3558 hours = secs / SECS_PER_HOUR;
3559 secs %= SECS_PER_HOUR;
3560 minutes = secs / SECS_PER_MINUTE;
3561 seconds = secs % SECS_PER_MINUTE;
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)));