1 /*-------------------------------------------------------------------------
2  *
3  * postgres.c
4  *	  POSTGRES C Backend Interface
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/tcop/postgres.c
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 <fcntl.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #ifdef HAVE_SYS_SELECT_H
28 #include <sys/select.h>
29 #endif
30 #ifdef HAVE_SYS_RESOURCE_H
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 #endif
34 
35 #ifndef HAVE_GETRUSAGE
36 #include "rusagestub.h"
37 #endif
38 
39 #include "access/parallel.h"
40 #include "access/printtup.h"
41 #include "access/xact.h"
42 #include "catalog/pg_type.h"
43 #include "commands/async.h"
44 #include "commands/prepare.h"
45 #include "libpq/libpq.h"
46 #include "libpq/pqformat.h"
47 #include "libpq/pqsignal.h"
48 #include "miscadmin.h"
49 #include "nodes/print.h"
50 #include "optimizer/planner.h"
51 #include "pgstat.h"
52 #include "pg_trace.h"
53 #include "parser/analyze.h"
54 #include "parser/parser.h"
55 #include "pg_getopt.h"
56 #include "postmaster/autovacuum.h"
57 #include "postmaster/postmaster.h"
58 #include "replication/logicallauncher.h"
59 #include "replication/logicalworker.h"
60 #include "replication/slot.h"
61 #include "replication/walsender.h"
62 #include "rewrite/rewriteHandler.h"
63 #include "storage/bufmgr.h"
64 #include "storage/ipc.h"
65 #include "storage/proc.h"
66 #include "storage/procsignal.h"
67 #include "storage/sinval.h"
68 #include "tcop/fastpath.h"
69 #include "tcop/pquery.h"
70 #include "tcop/tcopprot.h"
71 #include "tcop/utility.h"
72 #include "utils/lsyscache.h"
73 #include "utils/memutils.h"
74 #include "utils/ps_status.h"
75 #include "utils/snapmgr.h"
76 #include "utils/timeout.h"
77 #include "utils/timestamp.h"
78 #include "mb/pg_wchar.h"
79 
80 
81 /* ----------------
82  *		global variables
83  * ----------------
84  */
85 const char *debug_query_string; /* client-supplied query string */
86 
87 /* Note: whereToSendOutput is initialized for the bootstrap/standalone case */
88 CommandDest whereToSendOutput = DestDebug;
89 
90 /* flag for logging end of session */
91 bool		Log_disconnections = false;
92 
93 int			log_statement = LOGSTMT_NONE;
94 
95 /* GUC variable for maximum stack depth (measured in kilobytes) */
96 int			max_stack_depth = 100;
97 
98 /* wait N seconds to allow attach from a debugger */
99 int			PostAuthDelay = 0;
100 
101 
102 
103 /* ----------------
104  *		private variables
105  * ----------------
106  */
107 
108 /* max_stack_depth converted to bytes for speed of checking */
109 static long max_stack_depth_bytes = 100 * 1024L;
110 
111 /*
112  * Stack base pointer -- initialized by PostmasterMain and inherited by
113  * subprocesses. This is not static because old versions of PL/Java modify
114  * it directly. Newer versions use set_stack_base(), but we want to stay
115  * binary-compatible for the time being.
116  */
117 char	   *stack_base_ptr = NULL;
118 
119 /*
120  * On IA64 we also have to remember the register stack base.
121  */
122 #if defined(__ia64__) || defined(__ia64)
123 char	   *register_stack_base_ptr = NULL;
124 #endif
125 
126 /*
127  * Flag to keep track of whether we have started a transaction.
128  * For extended query protocol this has to be remembered across messages.
129  */
130 static bool xact_started = false;
131 
132 /*
133  * Flag to indicate that we are doing the outer loop's read-from-client,
134  * as opposed to any random read from client that might happen within
135  * commands like COPY FROM STDIN.
136  */
137 static bool DoingCommandRead = false;
138 
139 /*
140  * Flags to implement skip-till-Sync-after-error behavior for messages of
141  * the extended query protocol.
142  */
143 static bool doing_extended_query_message = false;
144 static bool ignore_till_sync = false;
145 
146 /*
147  * If an unnamed prepared statement exists, it's stored here.
148  * We keep it separate from the hashtable kept by commands/prepare.c
149  * in order to reduce overhead for short-lived queries.
150  */
151 static CachedPlanSource *unnamed_stmt_psrc = NULL;
152 
153 /* assorted command-line switches */
154 static const char *userDoption = NULL;	/* -D switch */
155 static bool EchoQuery = false;	/* -E switch */
156 static bool UseSemiNewlineNewline = false;	/* -j switch */
157 
158 /* whether or not, and why, we were canceled by conflict with recovery */
159 static bool RecoveryConflictPending = false;
160 static bool RecoveryConflictRetryable = true;
161 static ProcSignalReason RecoveryConflictReason;
162 
163 /* ----------------------------------------------------------------
164  *		decls for routines only used in this file
165  * ----------------------------------------------------------------
166  */
167 static int	InteractiveBackend(StringInfo inBuf);
168 static int	interactive_getc(void);
169 static int	SocketBackend(StringInfo inBuf);
170 static int	ReadCommand(StringInfo inBuf);
171 static void forbidden_in_wal_sender(char firstchar);
172 static List *pg_rewrite_query(Query *query);
173 static bool check_log_statement(List *stmt_list);
174 static int	errdetail_execute(List *raw_parsetree_list);
175 static int	errdetail_params(ParamListInfo params);
176 static int	errdetail_abort(void);
177 static int	errdetail_recovery_conflict(void);
178 static void start_xact_command(void);
179 static void finish_xact_command(void);
180 static bool IsTransactionExitStmt(Node *parsetree);
181 static bool IsTransactionExitStmtList(List *pstmts);
182 static bool IsTransactionStmtList(List *pstmts);
183 static void drop_unnamed_stmt(void);
184 static void log_disconnections(int code, Datum arg);
185 
186 
187 /* ----------------------------------------------------------------
188  *		routines to obtain user input
189  * ----------------------------------------------------------------
190  */
191 
192 /* ----------------
193  *	InteractiveBackend() is called for user interactive connections
194  *
195  *	the string entered by the user is placed in its parameter inBuf,
196  *	and we act like a Q message was received.
197  *
198  *	EOF is returned if end-of-file input is seen; time to shut down.
199  * ----------------
200  */
201 
202 static int
InteractiveBackend(StringInfo inBuf)203 InteractiveBackend(StringInfo inBuf)
204 {
205 	int			c;				/* character read from getc() */
206 
207 	/*
208 	 * display a prompt and obtain input from the user
209 	 */
210 	printf("backend> ");
211 	fflush(stdout);
212 
213 	resetStringInfo(inBuf);
214 
215 	/*
216 	 * Read characters until EOF or the appropriate delimiter is seen.
217 	 */
218 	while ((c = interactive_getc()) != EOF)
219 	{
220 		if (c == '\n')
221 		{
222 			if (UseSemiNewlineNewline)
223 			{
224 				/*
225 				 * In -j mode, semicolon followed by two newlines ends the
226 				 * command; otherwise treat newline as regular character.
227 				 */
228 				if (inBuf->len > 1 &&
229 					inBuf->data[inBuf->len - 1] == '\n' &&
230 					inBuf->data[inBuf->len - 2] == ';')
231 				{
232 					/* might as well drop the second newline */
233 					break;
234 				}
235 			}
236 			else
237 			{
238 				/*
239 				 * In plain mode, newline ends the command unless preceded by
240 				 * backslash.
241 				 */
242 				if (inBuf->len > 0 &&
243 					inBuf->data[inBuf->len - 1] == '\\')
244 				{
245 					/* discard backslash from inBuf */
246 					inBuf->data[--inBuf->len] = '\0';
247 					/* discard newline too */
248 					continue;
249 				}
250 				else
251 				{
252 					/* keep the newline character, but end the command */
253 					appendStringInfoChar(inBuf, '\n');
254 					break;
255 				}
256 			}
257 		}
258 
259 		/* Not newline, or newline treated as regular character */
260 		appendStringInfoChar(inBuf, (char) c);
261 	}
262 
263 	/* No input before EOF signal means time to quit. */
264 	if (c == EOF && inBuf->len == 0)
265 		return EOF;
266 
267 	/*
268 	 * otherwise we have a user query so process it.
269 	 */
270 
271 	/* Add '\0' to make it look the same as message case. */
272 	appendStringInfoChar(inBuf, (char) '\0');
273 
274 	/*
275 	 * if the query echo flag was given, print the query..
276 	 */
277 	if (EchoQuery)
278 		printf("statement: %s\n", inBuf->data);
279 	fflush(stdout);
280 
281 	return 'Q';
282 }
283 
284 /*
285  * interactive_getc -- collect one character from stdin
286  *
287  * Even though we are not reading from a "client" process, we still want to
288  * respond to signals, particularly SIGTERM/SIGQUIT.
289  */
290 static int
interactive_getc(void)291 interactive_getc(void)
292 {
293 	int			c;
294 
295 	/*
296 	 * This will not process catchup interrupts or notifications while
297 	 * reading. But those can't really be relevant for a standalone backend
298 	 * anyway. To properly handle SIGTERM there's a hack in die() that
299 	 * directly processes interrupts at this stage...
300 	 */
301 	CHECK_FOR_INTERRUPTS();
302 
303 	c = getc(stdin);
304 
305 	ProcessClientReadInterrupt(false);
306 
307 	return c;
308 }
309 
310 /* ----------------
311  *	SocketBackend()		Is called for frontend-backend connections
312  *
313  *	Returns the message type code, and loads message body data into inBuf.
314  *
315  *	EOF is returned if the connection is lost.
316  * ----------------
317  */
318 static int
SocketBackend(StringInfo inBuf)319 SocketBackend(StringInfo inBuf)
320 {
321 	int			qtype;
322 
323 	/*
324 	 * Get message type code from the frontend.
325 	 */
326 	HOLD_CANCEL_INTERRUPTS();
327 	pq_startmsgread();
328 	qtype = pq_getbyte();
329 
330 	if (qtype == EOF)			/* frontend disconnected */
331 	{
332 		if (IsTransactionState())
333 			ereport(COMMERROR,
334 					(errcode(ERRCODE_CONNECTION_FAILURE),
335 					 errmsg("unexpected EOF on client connection with an open transaction")));
336 		else
337 		{
338 			/*
339 			 * Can't send DEBUG log messages to client at this point. Since
340 			 * we're disconnecting right away, we don't need to restore
341 			 * whereToSendOutput.
342 			 */
343 			whereToSendOutput = DestNone;
344 			ereport(DEBUG1,
345 					(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
346 					 errmsg("unexpected EOF on client connection")));
347 		}
348 		return qtype;
349 	}
350 
351 	/*
352 	 * Validate message type code before trying to read body; if we have lost
353 	 * sync, better to say "command unknown" than to run out of memory because
354 	 * we used garbage as a length word.
355 	 *
356 	 * This also gives us a place to set the doing_extended_query_message flag
357 	 * as soon as possible.
358 	 */
359 	switch (qtype)
360 	{
361 		case 'Q':				/* simple query */
362 			doing_extended_query_message = false;
363 			if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
364 			{
365 				/* old style without length word; convert */
366 				if (pq_getstring(inBuf))
367 				{
368 					if (IsTransactionState())
369 						ereport(COMMERROR,
370 								(errcode(ERRCODE_CONNECTION_FAILURE),
371 								 errmsg("unexpected EOF on client connection with an open transaction")));
372 					else
373 					{
374 						/*
375 						 * Can't send DEBUG log messages to client at this
376 						 * point. Since we're disconnecting right away, we
377 						 * don't need to restore whereToSendOutput.
378 						 */
379 						whereToSendOutput = DestNone;
380 						ereport(DEBUG1,
381 								(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
382 								 errmsg("unexpected EOF on client connection")));
383 					}
384 					return EOF;
385 				}
386 			}
387 			break;
388 
389 		case 'F':				/* fastpath function call */
390 			doing_extended_query_message = false;
391 			if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
392 			{
393 				if (GetOldFunctionMessage(inBuf))
394 				{
395 					if (IsTransactionState())
396 						ereport(COMMERROR,
397 								(errcode(ERRCODE_CONNECTION_FAILURE),
398 								 errmsg("unexpected EOF on client connection with an open transaction")));
399 					else
400 					{
401 						/*
402 						 * Can't send DEBUG log messages to client at this
403 						 * point. Since we're disconnecting right away, we
404 						 * don't need to restore whereToSendOutput.
405 						 */
406 						whereToSendOutput = DestNone;
407 						ereport(DEBUG1,
408 								(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
409 								 errmsg("unexpected EOF on client connection")));
410 					}
411 					return EOF;
412 				}
413 			}
414 			break;
415 
416 		case 'X':				/* terminate */
417 			doing_extended_query_message = false;
418 			ignore_till_sync = false;
419 			break;
420 
421 		case 'B':				/* bind */
422 		case 'C':				/* close */
423 		case 'D':				/* describe */
424 		case 'E':				/* execute */
425 		case 'H':				/* flush */
426 		case 'P':				/* parse */
427 			doing_extended_query_message = true;
428 			/* these are only legal in protocol 3 */
429 			if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
430 				ereport(FATAL,
431 						(errcode(ERRCODE_PROTOCOL_VIOLATION),
432 						 errmsg("invalid frontend message type %d", qtype)));
433 			break;
434 
435 		case 'S':				/* sync */
436 			/* stop any active skip-till-Sync */
437 			ignore_till_sync = false;
438 			/* mark not-extended, so that a new error doesn't begin skip */
439 			doing_extended_query_message = false;
440 			/* only legal in protocol 3 */
441 			if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
442 				ereport(FATAL,
443 						(errcode(ERRCODE_PROTOCOL_VIOLATION),
444 						 errmsg("invalid frontend message type %d", qtype)));
445 			break;
446 
447 		case 'd':				/* copy data */
448 		case 'c':				/* copy done */
449 		case 'f':				/* copy fail */
450 			doing_extended_query_message = false;
451 			/* these are only legal in protocol 3 */
452 			if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
453 				ereport(FATAL,
454 						(errcode(ERRCODE_PROTOCOL_VIOLATION),
455 						 errmsg("invalid frontend message type %d", qtype)));
456 			break;
457 
458 		default:
459 
460 			/*
461 			 * Otherwise we got garbage from the frontend.  We treat this as
462 			 * fatal because we have probably lost message boundary sync, and
463 			 * there's no good way to recover.
464 			 */
465 			ereport(FATAL,
466 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
467 					 errmsg("invalid frontend message type %d", qtype)));
468 			break;
469 	}
470 
471 	/*
472 	 * In protocol version 3, all frontend messages have a length word next
473 	 * after the type code; we can read the message contents independently of
474 	 * the type.
475 	 */
476 	if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
477 	{
478 		if (pq_getmessage(inBuf, 0))
479 			return EOF;			/* suitable message already logged */
480 	}
481 	else
482 		pq_endmsgread();
483 	RESUME_CANCEL_INTERRUPTS();
484 
485 	return qtype;
486 }
487 
488 /* ----------------
489  *		ReadCommand reads a command from either the frontend or
490  *		standard input, places it in inBuf, and returns the
491  *		message type code (first byte of the message).
492  *		EOF is returned if end of file.
493  * ----------------
494  */
495 static int
ReadCommand(StringInfo inBuf)496 ReadCommand(StringInfo inBuf)
497 {
498 	int			result;
499 
500 	if (whereToSendOutput == DestRemote)
501 		result = SocketBackend(inBuf);
502 	else
503 		result = InteractiveBackend(inBuf);
504 	return result;
505 }
506 
507 /*
508  * ProcessClientReadInterrupt() - Process interrupts specific to client reads
509  *
510  * This is called just before and after low-level reads.
511  * 'blocked' is true if no data was available to read and we plan to retry,
512  * false if about to read or done reading.
513  *
514  * Must preserve errno!
515  */
516 void
ProcessClientReadInterrupt(bool blocked)517 ProcessClientReadInterrupt(bool blocked)
518 {
519 	int			save_errno = errno;
520 
521 	if (DoingCommandRead)
522 	{
523 		/* Check for general interrupts that arrived before/while reading */
524 		CHECK_FOR_INTERRUPTS();
525 
526 		/* Process sinval catchup interrupts, if any */
527 		if (catchupInterruptPending)
528 			ProcessCatchupInterrupt();
529 
530 		/* Process notify interrupts, if any */
531 		if (notifyInterruptPending)
532 			ProcessNotifyInterrupt();
533 	}
534 	else if (ProcDiePending)
535 	{
536 		/*
537 		 * We're dying.  If there is no data available to read, then it's safe
538 		 * (and sane) to handle that now.  If we haven't tried to read yet,
539 		 * make sure the process latch is set, so that if there is no data
540 		 * then we'll come back here and die.  If we're done reading, also
541 		 * make sure the process latch is set, as we might've undesirably
542 		 * cleared it while reading.
543 		 */
544 		if (blocked)
545 			CHECK_FOR_INTERRUPTS();
546 		else
547 			SetLatch(MyLatch);
548 	}
549 
550 	errno = save_errno;
551 }
552 
553 /*
554  * ProcessClientWriteInterrupt() - Process interrupts specific to client writes
555  *
556  * This is called just before and after low-level writes.
557  * 'blocked' is true if no data could be written and we plan to retry,
558  * false if about to write or done writing.
559  *
560  * Must preserve errno!
561  */
562 void
ProcessClientWriteInterrupt(bool blocked)563 ProcessClientWriteInterrupt(bool blocked)
564 {
565 	int			save_errno = errno;
566 
567 	if (ProcDiePending)
568 	{
569 		/*
570 		 * We're dying.  If it's not possible to write, then we should handle
571 		 * that immediately, else a stuck client could indefinitely delay our
572 		 * response to the signal.  If we haven't tried to write yet, make
573 		 * sure the process latch is set, so that if the write would block
574 		 * then we'll come back here and die.  If we're done writing, also
575 		 * make sure the process latch is set, as we might've undesirably
576 		 * cleared it while writing.
577 		 */
578 		if (blocked)
579 		{
580 			/*
581 			 * Don't mess with whereToSendOutput if ProcessInterrupts wouldn't
582 			 * service ProcDiePending.
583 			 */
584 			if (InterruptHoldoffCount == 0 && CritSectionCount == 0)
585 			{
586 				/*
587 				 * We don't want to send the client the error message, as a)
588 				 * that would possibly block again, and b) it would likely
589 				 * lead to loss of protocol sync because we may have already
590 				 * sent a partial protocol message.
591 				 */
592 				if (whereToSendOutput == DestRemote)
593 					whereToSendOutput = DestNone;
594 
595 				CHECK_FOR_INTERRUPTS();
596 			}
597 		}
598 		else
599 			SetLatch(MyLatch);
600 	}
601 
602 	errno = save_errno;
603 }
604 
605 /*
606  * Do raw parsing (only).
607  *
608  * A list of parsetrees (RawStmt nodes) is returned, since there might be
609  * multiple commands in the given string.
610  *
611  * NOTE: for interactive queries, it is important to keep this routine
612  * separate from the analysis & rewrite stages.  Analysis and rewriting
613  * cannot be done in an aborted transaction, since they require access to
614  * database tables.  So, we rely on the raw parser to determine whether
615  * we've seen a COMMIT or ABORT command; when we are in abort state, other
616  * commands are not processed any further than the raw parse stage.
617  */
618 List *
pg_parse_query(const char * query_string)619 pg_parse_query(const char *query_string)
620 {
621 	List	   *raw_parsetree_list;
622 
623 	TRACE_POSTGRESQL_QUERY_PARSE_START(query_string);
624 
625 	if (log_parser_stats)
626 		ResetUsage();
627 
628 	raw_parsetree_list = raw_parser(query_string);
629 
630 	if (log_parser_stats)
631 		ShowUsage("PARSER STATISTICS");
632 
633 #ifdef COPY_PARSE_PLAN_TREES
634 	/* Optional debugging check: pass raw parsetrees through copyObject() */
635 	{
636 		List	   *new_list = copyObject(raw_parsetree_list);
637 
638 		/* This checks both copyObject() and the equal() routines... */
639 		if (!equal(new_list, raw_parsetree_list))
640 			elog(WARNING, "copyObject() failed to produce an equal raw parse tree");
641 		else
642 			raw_parsetree_list = new_list;
643 	}
644 #endif
645 
646 	TRACE_POSTGRESQL_QUERY_PARSE_DONE(query_string);
647 
648 	return raw_parsetree_list;
649 }
650 
651 /*
652  * Given a raw parsetree (gram.y output), and optionally information about
653  * types of parameter symbols ($n), perform parse analysis and rule rewriting.
654  *
655  * A list of Query nodes is returned, since either the analyzer or the
656  * rewriter might expand one query to several.
657  *
658  * NOTE: for reasons mentioned above, this must be separate from raw parsing.
659  */
660 List *
pg_analyze_and_rewrite(RawStmt * parsetree,const char * query_string,Oid * paramTypes,int numParams,QueryEnvironment * queryEnv)661 pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
662 					   Oid *paramTypes, int numParams,
663 					   QueryEnvironment *queryEnv)
664 {
665 	Query	   *query;
666 	List	   *querytree_list;
667 
668 	TRACE_POSTGRESQL_QUERY_REWRITE_START(query_string);
669 
670 	/*
671 	 * (1) Perform parse analysis.
672 	 */
673 	if (log_parser_stats)
674 		ResetUsage();
675 
676 	query = parse_analyze(parsetree, query_string, paramTypes, numParams,
677 						  queryEnv);
678 
679 	if (log_parser_stats)
680 		ShowUsage("PARSE ANALYSIS STATISTICS");
681 
682 	/*
683 	 * (2) Rewrite the queries, as necessary
684 	 */
685 	querytree_list = pg_rewrite_query(query);
686 
687 	TRACE_POSTGRESQL_QUERY_REWRITE_DONE(query_string);
688 
689 	return querytree_list;
690 }
691 
692 /*
693  * Do parse analysis and rewriting.  This is the same as pg_analyze_and_rewrite
694  * except that external-parameter resolution is determined by parser callback
695  * hooks instead of a fixed list of parameter datatypes.
696  */
697 List *
pg_analyze_and_rewrite_params(RawStmt * parsetree,const char * query_string,ParserSetupHook parserSetup,void * parserSetupArg,QueryEnvironment * queryEnv)698 pg_analyze_and_rewrite_params(RawStmt *parsetree,
699 							  const char *query_string,
700 							  ParserSetupHook parserSetup,
701 							  void *parserSetupArg,
702 							  QueryEnvironment *queryEnv)
703 {
704 	ParseState *pstate;
705 	Query	   *query;
706 	List	   *querytree_list;
707 
708 	Assert(query_string != NULL);	/* required as of 8.4 */
709 
710 	TRACE_POSTGRESQL_QUERY_REWRITE_START(query_string);
711 
712 	/*
713 	 * (1) Perform parse analysis.
714 	 */
715 	if (log_parser_stats)
716 		ResetUsage();
717 
718 	pstate = make_parsestate(NULL);
719 	pstate->p_sourcetext = query_string;
720 	pstate->p_queryEnv = queryEnv;
721 	(*parserSetup) (pstate, parserSetupArg);
722 
723 	query = transformTopLevelStmt(pstate, parsetree);
724 
725 	if (post_parse_analyze_hook)
726 		(*post_parse_analyze_hook) (pstate, query);
727 
728 	free_parsestate(pstate);
729 
730 	if (log_parser_stats)
731 		ShowUsage("PARSE ANALYSIS STATISTICS");
732 
733 	/*
734 	 * (2) Rewrite the queries, as necessary
735 	 */
736 	querytree_list = pg_rewrite_query(query);
737 
738 	TRACE_POSTGRESQL_QUERY_REWRITE_DONE(query_string);
739 
740 	return querytree_list;
741 }
742 
743 /*
744  * Perform rewriting of a query produced by parse analysis.
745  *
746  * Note: query must just have come from the parser, because we do not do
747  * AcquireRewriteLocks() on it.
748  */
749 static List *
pg_rewrite_query(Query * query)750 pg_rewrite_query(Query *query)
751 {
752 	List	   *querytree_list;
753 
754 	if (Debug_print_parse)
755 		elog_node_display(LOG, "parse tree", query,
756 						  Debug_pretty_print);
757 
758 	if (log_parser_stats)
759 		ResetUsage();
760 
761 	if (query->commandType == CMD_UTILITY)
762 	{
763 		/* don't rewrite utilities, just dump 'em into result list */
764 		querytree_list = list_make1(query);
765 	}
766 	else
767 	{
768 		/* rewrite regular queries */
769 		querytree_list = QueryRewrite(query);
770 	}
771 
772 	if (log_parser_stats)
773 		ShowUsage("REWRITER STATISTICS");
774 
775 #ifdef COPY_PARSE_PLAN_TREES
776 	/* Optional debugging check: pass querytree output through copyObject() */
777 	{
778 		List	   *new_list;
779 
780 		new_list = copyObject(querytree_list);
781 		/* This checks both copyObject() and the equal() routines... */
782 		if (!equal(new_list, querytree_list))
783 			elog(WARNING, "copyObject() failed to produce equal parse tree");
784 		else
785 			querytree_list = new_list;
786 	}
787 #endif
788 
789 	if (Debug_print_rewritten)
790 		elog_node_display(LOG, "rewritten parse tree", querytree_list,
791 						  Debug_pretty_print);
792 
793 	return querytree_list;
794 }
795 
796 
797 /*
798  * Generate a plan for a single already-rewritten query.
799  * This is a thin wrapper around planner() and takes the same parameters.
800  */
801 PlannedStmt *
pg_plan_query(Query * querytree,int cursorOptions,ParamListInfo boundParams)802 pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
803 {
804 	PlannedStmt *plan;
805 
806 	/* Utility commands have no plans. */
807 	if (querytree->commandType == CMD_UTILITY)
808 		return NULL;
809 
810 	/* Planner must have a snapshot in case it calls user-defined functions. */
811 	Assert(ActiveSnapshotSet());
812 
813 	TRACE_POSTGRESQL_QUERY_PLAN_START();
814 
815 	if (log_planner_stats)
816 		ResetUsage();
817 
818 	/* call the optimizer */
819 	plan = planner(querytree, cursorOptions, boundParams);
820 
821 	if (log_planner_stats)
822 		ShowUsage("PLANNER STATISTICS");
823 
824 #ifdef COPY_PARSE_PLAN_TREES
825 	/* Optional debugging check: pass plan output through copyObject() */
826 	{
827 		PlannedStmt *new_plan = copyObject(plan);
828 
829 		/*
830 		 * equal() currently does not have routines to compare Plan nodes, so
831 		 * don't try to test equality here.  Perhaps fix someday?
832 		 */
833 #ifdef NOT_USED
834 		/* This checks both copyObject() and the equal() routines... */
835 		if (!equal(new_plan, plan))
836 			elog(WARNING, "copyObject() failed to produce an equal plan tree");
837 		else
838 #endif
839 			plan = new_plan;
840 	}
841 #endif
842 
843 	/*
844 	 * Print plan if debugging.
845 	 */
846 	if (Debug_print_plan)
847 		elog_node_display(LOG, "plan", plan, Debug_pretty_print);
848 
849 	TRACE_POSTGRESQL_QUERY_PLAN_DONE();
850 
851 	return plan;
852 }
853 
854 /*
855  * Generate plans for a list of already-rewritten queries.
856  *
857  * For normal optimizable statements, invoke the planner.  For utility
858  * statements, just make a wrapper PlannedStmt node.
859  *
860  * The result is a list of PlannedStmt nodes.
861  */
862 List *
pg_plan_queries(List * querytrees,int cursorOptions,ParamListInfo boundParams)863 pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams)
864 {
865 	List	   *stmt_list = NIL;
866 	ListCell   *query_list;
867 
868 	foreach(query_list, querytrees)
869 	{
870 		Query	   *query = lfirst_node(Query, query_list);
871 		PlannedStmt *stmt;
872 
873 		if (query->commandType == CMD_UTILITY)
874 		{
875 			/* Utility commands require no planning. */
876 			stmt = makeNode(PlannedStmt);
877 			stmt->commandType = CMD_UTILITY;
878 			stmt->canSetTag = query->canSetTag;
879 			stmt->utilityStmt = query->utilityStmt;
880 			stmt->stmt_location = query->stmt_location;
881 			stmt->stmt_len = query->stmt_len;
882 		}
883 		else
884 		{
885 			stmt = pg_plan_query(query, cursorOptions, boundParams);
886 		}
887 
888 		stmt_list = lappend(stmt_list, stmt);
889 	}
890 
891 	return stmt_list;
892 }
893 
894 
895 /*
896  * exec_simple_query
897  *
898  * Execute a "simple Query" protocol message.
899  */
900 static void
exec_simple_query(const char * query_string)901 exec_simple_query(const char *query_string)
902 {
903 	CommandDest dest = whereToSendOutput;
904 	MemoryContext oldcontext;
905 	List	   *parsetree_list;
906 	ListCell   *parsetree_item;
907 	bool		save_log_statement_stats = log_statement_stats;
908 	bool		was_logged = false;
909 	bool		isTopLevel;
910 	char		msec_str[32];
911 
912 
913 	/*
914 	 * Report query to various monitoring facilities.
915 	 */
916 	debug_query_string = query_string;
917 
918 	pgstat_report_activity(STATE_RUNNING, query_string);
919 
920 	TRACE_POSTGRESQL_QUERY_START(query_string);
921 
922 	/*
923 	 * We use save_log_statement_stats so ShowUsage doesn't report incorrect
924 	 * results because ResetUsage wasn't called.
925 	 */
926 	if (save_log_statement_stats)
927 		ResetUsage();
928 
929 	/*
930 	 * Start up a transaction command.  All queries generated by the
931 	 * query_string will be in this same command block, *unless* we find a
932 	 * BEGIN/COMMIT/ABORT statement; we have to force a new xact command after
933 	 * one of those, else bad things will happen in xact.c. (Note that this
934 	 * will normally change current memory context.)
935 	 */
936 	start_xact_command();
937 
938 	/*
939 	 * Zap any pre-existing unnamed statement.  (While not strictly necessary,
940 	 * it seems best to define simple-Query mode as if it used the unnamed
941 	 * statement and portal; this ensures we recover any storage used by prior
942 	 * unnamed operations.)
943 	 */
944 	drop_unnamed_stmt();
945 
946 	/*
947 	 * Switch to appropriate context for constructing parsetrees.
948 	 */
949 	oldcontext = MemoryContextSwitchTo(MessageContext);
950 
951 	/*
952 	 * Do basic parsing of the query or queries (this should be safe even if
953 	 * we are in aborted transaction state!)
954 	 */
955 	parsetree_list = pg_parse_query(query_string);
956 
957 	/* Log immediately if dictated by log_statement */
958 	if (check_log_statement(parsetree_list))
959 	{
960 		ereport(LOG,
961 				(errmsg("statement: %s", query_string),
962 				 errhidestmt(true),
963 				 errdetail_execute(parsetree_list)));
964 		was_logged = true;
965 	}
966 
967 	/*
968 	 * Switch back to transaction context to enter the loop.
969 	 */
970 	MemoryContextSwitchTo(oldcontext);
971 
972 	/*
973 	 * We'll tell PortalRun it's a top-level command iff there's exactly one
974 	 * raw parsetree.  If more than one, it's effectively a transaction block
975 	 * and we want PreventTransactionChain to reject unsafe commands. (Note:
976 	 * we're assuming that query rewrite cannot add commands that are
977 	 * significant to PreventTransactionChain.)
978 	 */
979 	isTopLevel = (list_length(parsetree_list) == 1);
980 
981 	/*
982 	 * Run through the raw parsetree(s) and process each one.
983 	 */
984 	foreach(parsetree_item, parsetree_list)
985 	{
986 		RawStmt    *parsetree = lfirst_node(RawStmt, parsetree_item);
987 		bool		snapshot_set = false;
988 		const char *commandTag;
989 		char		completionTag[COMPLETION_TAG_BUFSIZE];
990 		List	   *querytree_list,
991 				   *plantree_list;
992 		Portal		portal;
993 		DestReceiver *receiver;
994 		int16		format;
995 
996 		/*
997 		 * Get the command name for use in status display (it also becomes the
998 		 * default completion tag, down inside PortalRun).  Set ps_status and
999 		 * do any special start-of-SQL-command processing needed by the
1000 		 * destination.
1001 		 */
1002 		commandTag = CreateCommandTag(parsetree->stmt);
1003 
1004 		set_ps_display(commandTag, false);
1005 
1006 		BeginCommand(commandTag, dest);
1007 
1008 		/*
1009 		 * If we are in an aborted transaction, reject all commands except
1010 		 * COMMIT/ABORT.  It is important that this test occur before we try
1011 		 * to do parse analysis, rewrite, or planning, since all those phases
1012 		 * try to do database accesses, which may fail in abort state. (It
1013 		 * might be safe to allow some additional utility commands in this
1014 		 * state, but not many...)
1015 		 */
1016 		if (IsAbortedTransactionBlockState() &&
1017 			!IsTransactionExitStmt(parsetree->stmt))
1018 			ereport(ERROR,
1019 					(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1020 					 errmsg("current transaction is aborted, "
1021 							"commands ignored until end of transaction block"),
1022 					 errdetail_abort()));
1023 
1024 		/* Make sure we are in a transaction command */
1025 		start_xact_command();
1026 
1027 		/* If we got a cancel signal in parsing or prior command, quit */
1028 		CHECK_FOR_INTERRUPTS();
1029 
1030 		/*
1031 		 * Set up a snapshot if parse analysis/planning will need one.
1032 		 */
1033 		if (analyze_requires_snapshot(parsetree))
1034 		{
1035 			PushActiveSnapshot(GetTransactionSnapshot());
1036 			snapshot_set = true;
1037 		}
1038 
1039 		/*
1040 		 * OK to analyze, rewrite, and plan this query.
1041 		 *
1042 		 * Switch to appropriate context for constructing querytrees (again,
1043 		 * these must outlive the execution context).
1044 		 */
1045 		oldcontext = MemoryContextSwitchTo(MessageContext);
1046 
1047 		querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
1048 												NULL, 0, NULL);
1049 
1050 		plantree_list = pg_plan_queries(querytree_list,
1051 										CURSOR_OPT_PARALLEL_OK, NULL);
1052 
1053 		/* Done with the snapshot used for parsing/planning */
1054 		if (snapshot_set)
1055 			PopActiveSnapshot();
1056 
1057 		/* If we got a cancel signal in analysis or planning, quit */
1058 		CHECK_FOR_INTERRUPTS();
1059 
1060 		/*
1061 		 * Create unnamed portal to run the query or queries in. If there
1062 		 * already is one, silently drop it.
1063 		 */
1064 		portal = CreatePortal("", true, true);
1065 		/* Don't display the portal in pg_cursors */
1066 		portal->visible = false;
1067 
1068 		/*
1069 		 * We don't have to copy anything into the portal, because everything
1070 		 * we are passing here is in MessageContext, which will outlive the
1071 		 * portal anyway.
1072 		 */
1073 		PortalDefineQuery(portal,
1074 						  NULL,
1075 						  query_string,
1076 						  commandTag,
1077 						  plantree_list,
1078 						  NULL);
1079 
1080 		/*
1081 		 * Start the portal.  No parameters here.
1082 		 */
1083 		PortalStart(portal, NULL, 0, InvalidSnapshot);
1084 
1085 		/*
1086 		 * Select the appropriate output format: text unless we are doing a
1087 		 * FETCH from a binary cursor.  (Pretty grotty to have to do this here
1088 		 * --- but it avoids grottiness in other places.  Ah, the joys of
1089 		 * backward compatibility...)
1090 		 */
1091 		format = 0;				/* TEXT is default */
1092 		if (IsA(parsetree->stmt, FetchStmt))
1093 		{
1094 			FetchStmt  *stmt = (FetchStmt *) parsetree->stmt;
1095 
1096 			if (!stmt->ismove)
1097 			{
1098 				Portal		fportal = GetPortalByName(stmt->portalname);
1099 
1100 				if (PortalIsValid(fportal) &&
1101 					(fportal->cursorOptions & CURSOR_OPT_BINARY))
1102 					format = 1; /* BINARY */
1103 			}
1104 		}
1105 		PortalSetResultFormat(portal, 1, &format);
1106 
1107 		/*
1108 		 * Now we can create the destination receiver object.
1109 		 */
1110 		receiver = CreateDestReceiver(dest);
1111 		if (dest == DestRemote)
1112 			SetRemoteDestReceiverParams(receiver, portal);
1113 
1114 		/*
1115 		 * Switch back to transaction context for execution.
1116 		 */
1117 		MemoryContextSwitchTo(oldcontext);
1118 
1119 		/*
1120 		 * Run the portal to completion, and then drop it (and the receiver).
1121 		 */
1122 		(void) PortalRun(portal,
1123 						 FETCH_ALL,
1124 						 isTopLevel,
1125 						 true,
1126 						 receiver,
1127 						 receiver,
1128 						 completionTag);
1129 
1130 		(*receiver->rDestroy) (receiver);
1131 
1132 		PortalDrop(portal, false);
1133 
1134 		if (IsA(parsetree->stmt, TransactionStmt))
1135 		{
1136 			/*
1137 			 * If this was a transaction control statement, commit it. We will
1138 			 * start a new xact command for the next command (if any).
1139 			 */
1140 			finish_xact_command();
1141 		}
1142 		else if (lnext(parsetree_item) == NULL)
1143 		{
1144 			/*
1145 			 * If this is the last parsetree of the query string, close down
1146 			 * transaction statement before reporting command-complete.  This
1147 			 * is so that any end-of-transaction errors are reported before
1148 			 * the command-complete message is issued, to avoid confusing
1149 			 * clients who will expect either a command-complete message or an
1150 			 * error, not one and then the other.  But for compatibility with
1151 			 * historical Postgres behavior, we do not force a transaction
1152 			 * boundary between queries appearing in a single query string.
1153 			 */
1154 			finish_xact_command();
1155 		}
1156 		else
1157 		{
1158 			/*
1159 			 * We need a CommandCounterIncrement after every query, except
1160 			 * those that start or end a transaction block.
1161 			 */
1162 			CommandCounterIncrement();
1163 		}
1164 
1165 		/*
1166 		 * Tell client that we're done with this query.  Note we emit exactly
1167 		 * one EndCommand report for each raw parsetree, thus one for each SQL
1168 		 * command the client sent, regardless of rewriting. (But a command
1169 		 * aborted by error will not send an EndCommand report at all.)
1170 		 */
1171 		EndCommand(completionTag, dest);
1172 	}							/* end loop over parsetrees */
1173 
1174 	/*
1175 	 * Close down transaction statement, if one is open.
1176 	 */
1177 	finish_xact_command();
1178 
1179 	/*
1180 	 * If there were no parsetrees, return EmptyQueryResponse message.
1181 	 */
1182 	if (!parsetree_list)
1183 		NullCommand(dest);
1184 
1185 	/*
1186 	 * Emit duration logging if appropriate.
1187 	 */
1188 	switch (check_log_duration(msec_str, was_logged))
1189 	{
1190 		case 1:
1191 			ereport(LOG,
1192 					(errmsg("duration: %s ms", msec_str),
1193 					 errhidestmt(true)));
1194 			break;
1195 		case 2:
1196 			ereport(LOG,
1197 					(errmsg("duration: %s ms  statement: %s",
1198 							msec_str, query_string),
1199 					 errhidestmt(true),
1200 					 errdetail_execute(parsetree_list)));
1201 			break;
1202 	}
1203 
1204 	if (save_log_statement_stats)
1205 		ShowUsage("QUERY STATISTICS");
1206 
1207 	TRACE_POSTGRESQL_QUERY_DONE(query_string);
1208 
1209 	debug_query_string = NULL;
1210 }
1211 
1212 /*
1213  * exec_parse_message
1214  *
1215  * Execute a "Parse" protocol message.
1216  */
1217 static void
exec_parse_message(const char * query_string,const char * stmt_name,Oid * paramTypes,int numParams)1218 exec_parse_message(const char *query_string,	/* string to execute */
1219 				   const char *stmt_name,	/* name for prepared stmt */
1220 				   Oid *paramTypes, /* parameter types */
1221 				   int numParams)	/* number of parameters */
1222 {
1223 	MemoryContext unnamed_stmt_context = NULL;
1224 	MemoryContext oldcontext;
1225 	List	   *parsetree_list;
1226 	RawStmt    *raw_parse_tree;
1227 	const char *commandTag;
1228 	List	   *querytree_list;
1229 	CachedPlanSource *psrc;
1230 	bool		is_named;
1231 	bool		save_log_statement_stats = log_statement_stats;
1232 	char		msec_str[32];
1233 
1234 	/*
1235 	 * Report query to various monitoring facilities.
1236 	 */
1237 	debug_query_string = query_string;
1238 
1239 	pgstat_report_activity(STATE_RUNNING, query_string);
1240 
1241 	set_ps_display("PARSE", false);
1242 
1243 	if (save_log_statement_stats)
1244 		ResetUsage();
1245 
1246 	ereport(DEBUG2,
1247 			(errmsg("parse %s: %s",
1248 					*stmt_name ? stmt_name : "<unnamed>",
1249 					query_string)));
1250 
1251 	/*
1252 	 * Start up a transaction command so we can run parse analysis etc. (Note
1253 	 * that this will normally change current memory context.) Nothing happens
1254 	 * if we are already in one.
1255 	 */
1256 	start_xact_command();
1257 
1258 	/*
1259 	 * Switch to appropriate context for constructing parsetrees.
1260 	 *
1261 	 * We have two strategies depending on whether the prepared statement is
1262 	 * named or not.  For a named prepared statement, we do parsing in
1263 	 * MessageContext and copy the finished trees into the prepared
1264 	 * statement's plancache entry; then the reset of MessageContext releases
1265 	 * temporary space used by parsing and rewriting. For an unnamed prepared
1266 	 * statement, we assume the statement isn't going to hang around long, so
1267 	 * getting rid of temp space quickly is probably not worth the costs of
1268 	 * copying parse trees.  So in this case, we create the plancache entry's
1269 	 * query_context here, and do all the parsing work therein.
1270 	 */
1271 	is_named = (stmt_name[0] != '\0');
1272 	if (is_named)
1273 	{
1274 		/* Named prepared statement --- parse in MessageContext */
1275 		oldcontext = MemoryContextSwitchTo(MessageContext);
1276 	}
1277 	else
1278 	{
1279 		/* Unnamed prepared statement --- release any prior unnamed stmt */
1280 		drop_unnamed_stmt();
1281 		/* Create context for parsing */
1282 		unnamed_stmt_context =
1283 			AllocSetContextCreate(MessageContext,
1284 								  "unnamed prepared statement",
1285 								  ALLOCSET_DEFAULT_SIZES);
1286 		oldcontext = MemoryContextSwitchTo(unnamed_stmt_context);
1287 	}
1288 
1289 	/*
1290 	 * Do basic parsing of the query or queries (this should be safe even if
1291 	 * we are in aborted transaction state!)
1292 	 */
1293 	parsetree_list = pg_parse_query(query_string);
1294 
1295 	/*
1296 	 * We only allow a single user statement in a prepared statement. This is
1297 	 * mainly to keep the protocol simple --- otherwise we'd need to worry
1298 	 * about multiple result tupdescs and things like that.
1299 	 */
1300 	if (list_length(parsetree_list) > 1)
1301 		ereport(ERROR,
1302 				(errcode(ERRCODE_SYNTAX_ERROR),
1303 				 errmsg("cannot insert multiple commands into a prepared statement")));
1304 
1305 	if (parsetree_list != NIL)
1306 	{
1307 		Query	   *query;
1308 		bool		snapshot_set = false;
1309 		int			i;
1310 
1311 		raw_parse_tree = linitial_node(RawStmt, parsetree_list);
1312 
1313 		/*
1314 		 * Get the command name for possible use in status display.
1315 		 */
1316 		commandTag = CreateCommandTag(raw_parse_tree->stmt);
1317 
1318 		/*
1319 		 * If we are in an aborted transaction, reject all commands except
1320 		 * COMMIT/ROLLBACK.  It is important that this test occur before we
1321 		 * try to do parse analysis, rewrite, or planning, since all those
1322 		 * phases try to do database accesses, which may fail in abort state.
1323 		 * (It might be safe to allow some additional utility commands in this
1324 		 * state, but not many...)
1325 		 */
1326 		if (IsAbortedTransactionBlockState() &&
1327 			!IsTransactionExitStmt(raw_parse_tree->stmt))
1328 			ereport(ERROR,
1329 					(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1330 					 errmsg("current transaction is aborted, "
1331 							"commands ignored until end of transaction block"),
1332 					 errdetail_abort()));
1333 
1334 		/*
1335 		 * Create the CachedPlanSource before we do parse analysis, since it
1336 		 * needs to see the unmodified raw parse tree.
1337 		 */
1338 		psrc = CreateCachedPlan(raw_parse_tree, query_string, commandTag);
1339 
1340 		/*
1341 		 * Set up a snapshot if parse analysis will need one.
1342 		 */
1343 		if (analyze_requires_snapshot(raw_parse_tree))
1344 		{
1345 			PushActiveSnapshot(GetTransactionSnapshot());
1346 			snapshot_set = true;
1347 		}
1348 
1349 		/*
1350 		 * Analyze and rewrite the query.  Note that the originally specified
1351 		 * parameter set is not required to be complete, so we have to use
1352 		 * parse_analyze_varparams().
1353 		 */
1354 		if (log_parser_stats)
1355 			ResetUsage();
1356 
1357 		query = parse_analyze_varparams(raw_parse_tree,
1358 										query_string,
1359 										&paramTypes,
1360 										&numParams);
1361 
1362 		/*
1363 		 * Check all parameter types got determined.
1364 		 */
1365 		for (i = 0; i < numParams; i++)
1366 		{
1367 			Oid			ptype = paramTypes[i];
1368 
1369 			if (ptype == InvalidOid || ptype == UNKNOWNOID)
1370 				ereport(ERROR,
1371 						(errcode(ERRCODE_INDETERMINATE_DATATYPE),
1372 						 errmsg("could not determine data type of parameter $%d",
1373 								i + 1)));
1374 		}
1375 
1376 		if (log_parser_stats)
1377 			ShowUsage("PARSE ANALYSIS STATISTICS");
1378 
1379 		querytree_list = pg_rewrite_query(query);
1380 
1381 		/* Done with the snapshot used for parsing */
1382 		if (snapshot_set)
1383 			PopActiveSnapshot();
1384 	}
1385 	else
1386 	{
1387 		/* Empty input string.  This is legal. */
1388 		raw_parse_tree = NULL;
1389 		commandTag = NULL;
1390 		psrc = CreateCachedPlan(raw_parse_tree, query_string, commandTag);
1391 		querytree_list = NIL;
1392 	}
1393 
1394 	/*
1395 	 * CachedPlanSource must be a direct child of MessageContext before we
1396 	 * reparent unnamed_stmt_context under it, else we have a disconnected
1397 	 * circular subgraph.  Klugy, but less so than flipping contexts even more
1398 	 * above.
1399 	 */
1400 	if (unnamed_stmt_context)
1401 		MemoryContextSetParent(psrc->context, MessageContext);
1402 
1403 	/* Finish filling in the CachedPlanSource */
1404 	CompleteCachedPlan(psrc,
1405 					   querytree_list,
1406 					   unnamed_stmt_context,
1407 					   paramTypes,
1408 					   numParams,
1409 					   NULL,
1410 					   NULL,
1411 					   CURSOR_OPT_PARALLEL_OK,	/* allow parallel mode */
1412 					   true);	/* fixed result */
1413 
1414 	/* If we got a cancel signal during analysis, quit */
1415 	CHECK_FOR_INTERRUPTS();
1416 
1417 	if (is_named)
1418 	{
1419 		/*
1420 		 * Store the query as a prepared statement.
1421 		 */
1422 		StorePreparedStatement(stmt_name, psrc, false);
1423 	}
1424 	else
1425 	{
1426 		/*
1427 		 * We just save the CachedPlanSource into unnamed_stmt_psrc.
1428 		 */
1429 		SaveCachedPlan(psrc);
1430 		unnamed_stmt_psrc = psrc;
1431 	}
1432 
1433 	MemoryContextSwitchTo(oldcontext);
1434 
1435 	/*
1436 	 * We do NOT close the open transaction command here; that only happens
1437 	 * when the client sends Sync.  Instead, do CommandCounterIncrement just
1438 	 * in case something happened during parse/plan.
1439 	 */
1440 	CommandCounterIncrement();
1441 
1442 	/*
1443 	 * Send ParseComplete.
1444 	 */
1445 	if (whereToSendOutput == DestRemote)
1446 		pq_putemptymessage('1');
1447 
1448 	/*
1449 	 * Emit duration logging if appropriate.
1450 	 */
1451 	switch (check_log_duration(msec_str, false))
1452 	{
1453 		case 1:
1454 			ereport(LOG,
1455 					(errmsg("duration: %s ms", msec_str),
1456 					 errhidestmt(true)));
1457 			break;
1458 		case 2:
1459 			ereport(LOG,
1460 					(errmsg("duration: %s ms  parse %s: %s",
1461 							msec_str,
1462 							*stmt_name ? stmt_name : "<unnamed>",
1463 							query_string),
1464 					 errhidestmt(true)));
1465 			break;
1466 	}
1467 
1468 	if (save_log_statement_stats)
1469 		ShowUsage("PARSE MESSAGE STATISTICS");
1470 
1471 	debug_query_string = NULL;
1472 }
1473 
1474 /*
1475  * exec_bind_message
1476  *
1477  * Process a "Bind" message to create a portal from a prepared statement
1478  */
1479 static void
exec_bind_message(StringInfo input_message)1480 exec_bind_message(StringInfo input_message)
1481 {
1482 	const char *portal_name;
1483 	const char *stmt_name;
1484 	int			numPFormats;
1485 	int16	   *pformats = NULL;
1486 	int			numParams;
1487 	int			numRFormats;
1488 	int16	   *rformats = NULL;
1489 	CachedPlanSource *psrc;
1490 	CachedPlan *cplan;
1491 	Portal		portal;
1492 	char	   *query_string;
1493 	char	   *saved_stmt_name;
1494 	ParamListInfo params;
1495 	MemoryContext oldContext;
1496 	bool		save_log_statement_stats = log_statement_stats;
1497 	bool		snapshot_set = false;
1498 	char		msec_str[32];
1499 
1500 	/* Get the fixed part of the message */
1501 	portal_name = pq_getmsgstring(input_message);
1502 	stmt_name = pq_getmsgstring(input_message);
1503 
1504 	ereport(DEBUG2,
1505 			(errmsg("bind %s to %s",
1506 					*portal_name ? portal_name : "<unnamed>",
1507 					*stmt_name ? stmt_name : "<unnamed>")));
1508 
1509 	/* Find prepared statement */
1510 	if (stmt_name[0] != '\0')
1511 	{
1512 		PreparedStatement *pstmt;
1513 
1514 		pstmt = FetchPreparedStatement(stmt_name, true);
1515 		psrc = pstmt->plansource;
1516 	}
1517 	else
1518 	{
1519 		/* special-case the unnamed statement */
1520 		psrc = unnamed_stmt_psrc;
1521 		if (!psrc)
1522 			ereport(ERROR,
1523 					(errcode(ERRCODE_UNDEFINED_PSTATEMENT),
1524 					 errmsg("unnamed prepared statement does not exist")));
1525 	}
1526 
1527 	/*
1528 	 * Report query to various monitoring facilities.
1529 	 */
1530 	debug_query_string = psrc->query_string;
1531 
1532 	pgstat_report_activity(STATE_RUNNING, psrc->query_string);
1533 
1534 	set_ps_display("BIND", false);
1535 
1536 	if (save_log_statement_stats)
1537 		ResetUsage();
1538 
1539 	/*
1540 	 * Start up a transaction command so we can call functions etc. (Note that
1541 	 * this will normally change current memory context.) Nothing happens if
1542 	 * we are already in one.
1543 	 */
1544 	start_xact_command();
1545 
1546 	/* Switch back to message context */
1547 	MemoryContextSwitchTo(MessageContext);
1548 
1549 	/* Get the parameter format codes */
1550 	numPFormats = pq_getmsgint(input_message, 2);
1551 	if (numPFormats > 0)
1552 	{
1553 		int			i;
1554 
1555 		pformats = (int16 *) palloc(numPFormats * sizeof(int16));
1556 		for (i = 0; i < numPFormats; i++)
1557 			pformats[i] = pq_getmsgint(input_message, 2);
1558 	}
1559 
1560 	/* Get the parameter value count */
1561 	numParams = pq_getmsgint(input_message, 2);
1562 
1563 	if (numPFormats > 1 && numPFormats != numParams)
1564 		ereport(ERROR,
1565 				(errcode(ERRCODE_PROTOCOL_VIOLATION),
1566 				 errmsg("bind message has %d parameter formats but %d parameters",
1567 						numPFormats, numParams)));
1568 
1569 	if (numParams != psrc->num_params)
1570 		ereport(ERROR,
1571 				(errcode(ERRCODE_PROTOCOL_VIOLATION),
1572 				 errmsg("bind message supplies %d parameters, but prepared statement \"%s\" requires %d",
1573 						numParams, stmt_name, psrc->num_params)));
1574 
1575 	/*
1576 	 * If we are in aborted transaction state, the only portals we can
1577 	 * actually run are those containing COMMIT or ROLLBACK commands. We
1578 	 * disallow binding anything else to avoid problems with infrastructure
1579 	 * that expects to run inside a valid transaction.  We also disallow
1580 	 * binding any parameters, since we can't risk calling user-defined I/O
1581 	 * functions.
1582 	 */
1583 	if (IsAbortedTransactionBlockState() &&
1584 		(!(psrc->raw_parse_tree &&
1585 		   IsTransactionExitStmt(psrc->raw_parse_tree->stmt)) ||
1586 		 numParams != 0))
1587 		ereport(ERROR,
1588 				(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1589 				 errmsg("current transaction is aborted, "
1590 						"commands ignored until end of transaction block"),
1591 				 errdetail_abort()));
1592 
1593 	/*
1594 	 * Create the portal.  Allow silent replacement of an existing portal only
1595 	 * if the unnamed portal is specified.
1596 	 */
1597 	if (portal_name[0] == '\0')
1598 		portal = CreatePortal(portal_name, true, true);
1599 	else
1600 		portal = CreatePortal(portal_name, false, false);
1601 
1602 	/*
1603 	 * Prepare to copy stuff into the portal's memory context.  We do all this
1604 	 * copying first, because it could possibly fail (out-of-memory) and we
1605 	 * don't want a failure to occur between GetCachedPlan and
1606 	 * PortalDefineQuery; that would result in leaking our plancache refcount.
1607 	 */
1608 	oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
1609 
1610 	/* Copy the plan's query string into the portal */
1611 	query_string = pstrdup(psrc->query_string);
1612 
1613 	/* Likewise make a copy of the statement name, unless it's unnamed */
1614 	if (stmt_name[0])
1615 		saved_stmt_name = pstrdup(stmt_name);
1616 	else
1617 		saved_stmt_name = NULL;
1618 
1619 	/*
1620 	 * Set a snapshot if we have parameters to fetch (since the input
1621 	 * functions might need it) or the query isn't a utility command (and
1622 	 * hence could require redoing parse analysis and planning).  We keep the
1623 	 * snapshot active till we're done, so that plancache.c doesn't have to
1624 	 * take new ones.
1625 	 */
1626 	if (numParams > 0 ||
1627 		(psrc->raw_parse_tree &&
1628 		 analyze_requires_snapshot(psrc->raw_parse_tree)))
1629 	{
1630 		PushActiveSnapshot(GetTransactionSnapshot());
1631 		snapshot_set = true;
1632 	}
1633 
1634 	/*
1635 	 * Fetch parameters, if any, and store in the portal's memory context.
1636 	 */
1637 	if (numParams > 0)
1638 	{
1639 		int			paramno;
1640 
1641 		params = (ParamListInfo) palloc(offsetof(ParamListInfoData, params) +
1642 										numParams * sizeof(ParamExternData));
1643 		/* we have static list of params, so no hooks needed */
1644 		params->paramFetch = NULL;
1645 		params->paramFetchArg = NULL;
1646 		params->parserSetup = NULL;
1647 		params->parserSetupArg = NULL;
1648 		params->numParams = numParams;
1649 		params->paramMask = NULL;
1650 
1651 		for (paramno = 0; paramno < numParams; paramno++)
1652 		{
1653 			Oid			ptype = psrc->param_types[paramno];
1654 			int32		plength;
1655 			Datum		pval;
1656 			bool		isNull;
1657 			StringInfoData pbuf;
1658 			char		csave;
1659 			int16		pformat;
1660 
1661 			plength = pq_getmsgint(input_message, 4);
1662 			isNull = (plength == -1);
1663 
1664 			if (!isNull)
1665 			{
1666 				const char *pvalue = pq_getmsgbytes(input_message, plength);
1667 
1668 				/*
1669 				 * Rather than copying data around, we just set up a phony
1670 				 * StringInfo pointing to the correct portion of the message
1671 				 * buffer.  We assume we can scribble on the message buffer so
1672 				 * as to maintain the convention that StringInfos have a
1673 				 * trailing null.  This is grotty but is a big win when
1674 				 * dealing with very large parameter strings.
1675 				 */
1676 				pbuf.data = (char *) pvalue;
1677 				pbuf.maxlen = plength + 1;
1678 				pbuf.len = plength;
1679 				pbuf.cursor = 0;
1680 
1681 				csave = pbuf.data[plength];
1682 				pbuf.data[plength] = '\0';
1683 			}
1684 			else
1685 			{
1686 				pbuf.data = NULL;	/* keep compiler quiet */
1687 				csave = 0;
1688 			}
1689 
1690 			if (numPFormats > 1)
1691 				pformat = pformats[paramno];
1692 			else if (numPFormats > 0)
1693 				pformat = pformats[0];
1694 			else
1695 				pformat = 0;	/* default = text */
1696 
1697 			if (pformat == 0)	/* text mode */
1698 			{
1699 				Oid			typinput;
1700 				Oid			typioparam;
1701 				char	   *pstring;
1702 
1703 				getTypeInputInfo(ptype, &typinput, &typioparam);
1704 
1705 				/*
1706 				 * We have to do encoding conversion before calling the
1707 				 * typinput routine.
1708 				 */
1709 				if (isNull)
1710 					pstring = NULL;
1711 				else
1712 					pstring = pg_client_to_server(pbuf.data, plength);
1713 
1714 				pval = OidInputFunctionCall(typinput, pstring, typioparam, -1);
1715 
1716 				/* Free result of encoding conversion, if any */
1717 				if (pstring && pstring != pbuf.data)
1718 					pfree(pstring);
1719 			}
1720 			else if (pformat == 1)	/* binary mode */
1721 			{
1722 				Oid			typreceive;
1723 				Oid			typioparam;
1724 				StringInfo	bufptr;
1725 
1726 				/*
1727 				 * Call the parameter type's binary input converter
1728 				 */
1729 				getTypeBinaryInputInfo(ptype, &typreceive, &typioparam);
1730 
1731 				if (isNull)
1732 					bufptr = NULL;
1733 				else
1734 					bufptr = &pbuf;
1735 
1736 				pval = OidReceiveFunctionCall(typreceive, bufptr, typioparam, -1);
1737 
1738 				/* Trouble if it didn't eat the whole buffer */
1739 				if (!isNull && pbuf.cursor != pbuf.len)
1740 					ereport(ERROR,
1741 							(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1742 							 errmsg("incorrect binary data format in bind parameter %d",
1743 									paramno + 1)));
1744 			}
1745 			else
1746 			{
1747 				ereport(ERROR,
1748 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1749 						 errmsg("unsupported format code: %d",
1750 								pformat)));
1751 				pval = 0;		/* keep compiler quiet */
1752 			}
1753 
1754 			/* Restore message buffer contents */
1755 			if (!isNull)
1756 				pbuf.data[plength] = csave;
1757 
1758 			params->params[paramno].value = pval;
1759 			params->params[paramno].isnull = isNull;
1760 
1761 			/*
1762 			 * We mark the params as CONST.  This ensures that any custom plan
1763 			 * makes full use of the parameter values.
1764 			 */
1765 			params->params[paramno].pflags = PARAM_FLAG_CONST;
1766 			params->params[paramno].ptype = ptype;
1767 		}
1768 	}
1769 	else
1770 		params = NULL;
1771 
1772 	/* Done storing stuff in portal's context */
1773 	MemoryContextSwitchTo(oldContext);
1774 
1775 	/* Get the result format codes */
1776 	numRFormats = pq_getmsgint(input_message, 2);
1777 	if (numRFormats > 0)
1778 	{
1779 		int			i;
1780 
1781 		rformats = (int16 *) palloc(numRFormats * sizeof(int16));
1782 		for (i = 0; i < numRFormats; i++)
1783 			rformats[i] = pq_getmsgint(input_message, 2);
1784 	}
1785 
1786 	pq_getmsgend(input_message);
1787 
1788 	/*
1789 	 * Obtain a plan from the CachedPlanSource.  Any cruft from (re)planning
1790 	 * will be generated in MessageContext.  The plan refcount will be
1791 	 * assigned to the Portal, so it will be released at portal destruction.
1792 	 */
1793 	cplan = GetCachedPlan(psrc, params, false, NULL);
1794 
1795 	/*
1796 	 * Now we can define the portal.
1797 	 *
1798 	 * DO NOT put any code that could possibly throw an error between the
1799 	 * above GetCachedPlan call and here.
1800 	 */
1801 	PortalDefineQuery(portal,
1802 					  saved_stmt_name,
1803 					  query_string,
1804 					  psrc->commandTag,
1805 					  cplan->stmt_list,
1806 					  cplan);
1807 
1808 	/* Done with the snapshot used for parameter I/O and parsing/planning */
1809 	if (snapshot_set)
1810 		PopActiveSnapshot();
1811 
1812 	/*
1813 	 * And we're ready to start portal execution.
1814 	 */
1815 	PortalStart(portal, params, 0, InvalidSnapshot);
1816 
1817 	/*
1818 	 * Apply the result format requests to the portal.
1819 	 */
1820 	PortalSetResultFormat(portal, numRFormats, rformats);
1821 
1822 	/*
1823 	 * Send BindComplete.
1824 	 */
1825 	if (whereToSendOutput == DestRemote)
1826 		pq_putemptymessage('2');
1827 
1828 	/*
1829 	 * Emit duration logging if appropriate.
1830 	 */
1831 	switch (check_log_duration(msec_str, false))
1832 	{
1833 		case 1:
1834 			ereport(LOG,
1835 					(errmsg("duration: %s ms", msec_str),
1836 					 errhidestmt(true)));
1837 			break;
1838 		case 2:
1839 			ereport(LOG,
1840 					(errmsg("duration: %s ms  bind %s%s%s: %s",
1841 							msec_str,
1842 							*stmt_name ? stmt_name : "<unnamed>",
1843 							*portal_name ? "/" : "",
1844 							*portal_name ? portal_name : "",
1845 							psrc->query_string),
1846 					 errhidestmt(true),
1847 					 errdetail_params(params)));
1848 			break;
1849 	}
1850 
1851 	if (save_log_statement_stats)
1852 		ShowUsage("BIND MESSAGE STATISTICS");
1853 
1854 	debug_query_string = NULL;
1855 }
1856 
1857 /*
1858  * exec_execute_message
1859  *
1860  * Process an "Execute" message for a portal
1861  */
1862 static void
exec_execute_message(const char * portal_name,long max_rows)1863 exec_execute_message(const char *portal_name, long max_rows)
1864 {
1865 	CommandDest dest;
1866 	DestReceiver *receiver;
1867 	Portal		portal;
1868 	bool		completed;
1869 	char		completionTag[COMPLETION_TAG_BUFSIZE];
1870 	const char *sourceText;
1871 	const char *prepStmtName;
1872 	ParamListInfo portalParams;
1873 	bool		save_log_statement_stats = log_statement_stats;
1874 	bool		is_xact_command;
1875 	bool		execute_is_fetch;
1876 	bool		was_logged = false;
1877 	char		msec_str[32];
1878 
1879 	/* Adjust destination to tell printtup.c what to do */
1880 	dest = whereToSendOutput;
1881 	if (dest == DestRemote)
1882 		dest = DestRemoteExecute;
1883 
1884 	portal = GetPortalByName(portal_name);
1885 	if (!PortalIsValid(portal))
1886 		ereport(ERROR,
1887 				(errcode(ERRCODE_UNDEFINED_CURSOR),
1888 				 errmsg("portal \"%s\" does not exist", portal_name)));
1889 
1890 	/*
1891 	 * If the original query was a null string, just return
1892 	 * EmptyQueryResponse.
1893 	 */
1894 	if (portal->commandTag == NULL)
1895 	{
1896 		Assert(portal->stmts == NIL);
1897 		NullCommand(dest);
1898 		return;
1899 	}
1900 
1901 	/* Does the portal contain a transaction command? */
1902 	is_xact_command = IsTransactionStmtList(portal->stmts);
1903 
1904 	/*
1905 	 * We must copy the sourceText and prepStmtName into MessageContext in
1906 	 * case the portal is destroyed during finish_xact_command. Can avoid the
1907 	 * copy if it's not an xact command, though.
1908 	 */
1909 	if (is_xact_command)
1910 	{
1911 		sourceText = pstrdup(portal->sourceText);
1912 		if (portal->prepStmtName)
1913 			prepStmtName = pstrdup(portal->prepStmtName);
1914 		else
1915 			prepStmtName = "<unnamed>";
1916 
1917 		/*
1918 		 * An xact command shouldn't have any parameters, which is a good
1919 		 * thing because they wouldn't be around after finish_xact_command.
1920 		 */
1921 		portalParams = NULL;
1922 	}
1923 	else
1924 	{
1925 		sourceText = portal->sourceText;
1926 		if (portal->prepStmtName)
1927 			prepStmtName = portal->prepStmtName;
1928 		else
1929 			prepStmtName = "<unnamed>";
1930 		portalParams = portal->portalParams;
1931 	}
1932 
1933 	/*
1934 	 * Report query to various monitoring facilities.
1935 	 */
1936 	debug_query_string = sourceText;
1937 
1938 	pgstat_report_activity(STATE_RUNNING, sourceText);
1939 
1940 	set_ps_display(portal->commandTag, false);
1941 
1942 	if (save_log_statement_stats)
1943 		ResetUsage();
1944 
1945 	BeginCommand(portal->commandTag, dest);
1946 
1947 	/*
1948 	 * Create dest receiver in MessageContext (we don't want it in transaction
1949 	 * context, because that may get deleted if portal contains VACUUM).
1950 	 */
1951 	receiver = CreateDestReceiver(dest);
1952 	if (dest == DestRemoteExecute)
1953 		SetRemoteDestReceiverParams(receiver, portal);
1954 
1955 	/*
1956 	 * Ensure we are in a transaction command (this should normally be the
1957 	 * case already due to prior BIND).
1958 	 */
1959 	start_xact_command();
1960 
1961 	/*
1962 	 * If we re-issue an Execute protocol request against an existing portal,
1963 	 * then we are only fetching more rows rather than completely re-executing
1964 	 * the query from the start. atStart is never reset for a v3 portal, so we
1965 	 * are safe to use this check.
1966 	 */
1967 	execute_is_fetch = !portal->atStart;
1968 
1969 	/* Log immediately if dictated by log_statement */
1970 	if (check_log_statement(portal->stmts))
1971 	{
1972 		ereport(LOG,
1973 				(errmsg("%s %s%s%s: %s",
1974 						execute_is_fetch ?
1975 						_("execute fetch from") :
1976 						_("execute"),
1977 						prepStmtName,
1978 						*portal_name ? "/" : "",
1979 						*portal_name ? portal_name : "",
1980 						sourceText),
1981 				 errhidestmt(true),
1982 				 errdetail_params(portalParams)));
1983 		was_logged = true;
1984 	}
1985 
1986 	/*
1987 	 * If we are in aborted transaction state, the only portals we can
1988 	 * actually run are those containing COMMIT or ROLLBACK commands.
1989 	 */
1990 	if (IsAbortedTransactionBlockState() &&
1991 		!IsTransactionExitStmtList(portal->stmts))
1992 		ereport(ERROR,
1993 				(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
1994 				 errmsg("current transaction is aborted, "
1995 						"commands ignored until end of transaction block"),
1996 				 errdetail_abort()));
1997 
1998 	/* Check for cancel signal before we start execution */
1999 	CHECK_FOR_INTERRUPTS();
2000 
2001 	/*
2002 	 * Okay to run the portal.
2003 	 */
2004 	if (max_rows <= 0)
2005 		max_rows = FETCH_ALL;
2006 
2007 	completed = PortalRun(portal,
2008 						  max_rows,
2009 						  true, /* always top level */
2010 						  !execute_is_fetch && max_rows == FETCH_ALL,
2011 						  receiver,
2012 						  receiver,
2013 						  completionTag);
2014 
2015 	(*receiver->rDestroy) (receiver);
2016 
2017 	if (completed)
2018 	{
2019 		if (is_xact_command)
2020 		{
2021 			/*
2022 			 * If this was a transaction control statement, commit it.  We
2023 			 * will start a new xact command for the next command (if any).
2024 			 */
2025 			finish_xact_command();
2026 		}
2027 		else
2028 		{
2029 			/*
2030 			 * We need a CommandCounterIncrement after every query, except
2031 			 * those that start or end a transaction block.
2032 			 */
2033 			CommandCounterIncrement();
2034 		}
2035 
2036 		/* Send appropriate CommandComplete to client */
2037 		EndCommand(completionTag, dest);
2038 	}
2039 	else
2040 	{
2041 		/* Portal run not complete, so send PortalSuspended */
2042 		if (whereToSendOutput == DestRemote)
2043 			pq_putemptymessage('s');
2044 	}
2045 
2046 	/*
2047 	 * Emit duration logging if appropriate.
2048 	 */
2049 	switch (check_log_duration(msec_str, was_logged))
2050 	{
2051 		case 1:
2052 			ereport(LOG,
2053 					(errmsg("duration: %s ms", msec_str),
2054 					 errhidestmt(true)));
2055 			break;
2056 		case 2:
2057 			ereport(LOG,
2058 					(errmsg("duration: %s ms  %s %s%s%s: %s",
2059 							msec_str,
2060 							execute_is_fetch ?
2061 							_("execute fetch from") :
2062 							_("execute"),
2063 							prepStmtName,
2064 							*portal_name ? "/" : "",
2065 							*portal_name ? portal_name : "",
2066 							sourceText),
2067 					 errhidestmt(true),
2068 					 errdetail_params(portalParams)));
2069 			break;
2070 	}
2071 
2072 	if (save_log_statement_stats)
2073 		ShowUsage("EXECUTE MESSAGE STATISTICS");
2074 
2075 	debug_query_string = NULL;
2076 }
2077 
2078 /*
2079  * check_log_statement
2080  *		Determine whether command should be logged because of log_statement
2081  *
2082  * stmt_list can be either raw grammar output or a list of planned
2083  * statements
2084  */
2085 static bool
check_log_statement(List * stmt_list)2086 check_log_statement(List *stmt_list)
2087 {
2088 	ListCell   *stmt_item;
2089 
2090 	if (log_statement == LOGSTMT_NONE)
2091 		return false;
2092 	if (log_statement == LOGSTMT_ALL)
2093 		return true;
2094 
2095 	/* Else we have to inspect the statement(s) to see whether to log */
2096 	foreach(stmt_item, stmt_list)
2097 	{
2098 		Node	   *stmt = (Node *) lfirst(stmt_item);
2099 
2100 		if (GetCommandLogLevel(stmt) <= log_statement)
2101 			return true;
2102 	}
2103 
2104 	return false;
2105 }
2106 
2107 /*
2108  * check_log_duration
2109  *		Determine whether current command's duration should be logged
2110  *
2111  * Returns:
2112  *		0 if no logging is needed
2113  *		1 if just the duration should be logged
2114  *		2 if duration and query details should be logged
2115  *
2116  * If logging is needed, the duration in msec is formatted into msec_str[],
2117  * which must be a 32-byte buffer.
2118  *
2119  * was_logged should be TRUE if caller already logged query details (this
2120  * essentially prevents 2 from being returned).
2121  */
2122 int
check_log_duration(char * msec_str,bool was_logged)2123 check_log_duration(char *msec_str, bool was_logged)
2124 {
2125 	if (log_duration || log_min_duration_statement >= 0)
2126 	{
2127 		long		secs;
2128 		int			usecs;
2129 		int			msecs;
2130 		bool		exceeded;
2131 
2132 		TimestampDifference(GetCurrentStatementStartTimestamp(),
2133 							GetCurrentTimestamp(),
2134 							&secs, &usecs);
2135 		msecs = usecs / 1000;
2136 
2137 		/*
2138 		 * This odd-looking test for log_min_duration_statement being exceeded
2139 		 * is designed to avoid integer overflow with very long durations:
2140 		 * don't compute secs * 1000 until we've verified it will fit in int.
2141 		 */
2142 		exceeded = (log_min_duration_statement == 0 ||
2143 					(log_min_duration_statement > 0 &&
2144 					 (secs > log_min_duration_statement / 1000 ||
2145 					  secs * 1000 + msecs >= log_min_duration_statement)));
2146 
2147 		if (exceeded || log_duration)
2148 		{
2149 			snprintf(msec_str, 32, "%ld.%03d",
2150 					 secs * 1000 + msecs, usecs % 1000);
2151 			if (exceeded && !was_logged)
2152 				return 2;
2153 			else
2154 				return 1;
2155 		}
2156 	}
2157 
2158 	return 0;
2159 }
2160 
2161 /*
2162  * errdetail_execute
2163  *
2164  * Add an errdetail() line showing the query referenced by an EXECUTE, if any.
2165  * The argument is the raw parsetree list.
2166  */
2167 static int
errdetail_execute(List * raw_parsetree_list)2168 errdetail_execute(List *raw_parsetree_list)
2169 {
2170 	ListCell   *parsetree_item;
2171 
2172 	foreach(parsetree_item, raw_parsetree_list)
2173 	{
2174 		RawStmt    *parsetree = lfirst_node(RawStmt, parsetree_item);
2175 
2176 		if (IsA(parsetree->stmt, ExecuteStmt))
2177 		{
2178 			ExecuteStmt *stmt = (ExecuteStmt *) parsetree->stmt;
2179 			PreparedStatement *pstmt;
2180 
2181 			pstmt = FetchPreparedStatement(stmt->name, false);
2182 			if (pstmt)
2183 			{
2184 				errdetail("prepare: %s", pstmt->plansource->query_string);
2185 				return 0;
2186 			}
2187 		}
2188 	}
2189 
2190 	return 0;
2191 }
2192 
2193 /*
2194  * errdetail_params
2195  *
2196  * Add an errdetail() line showing bind-parameter data, if available.
2197  */
2198 static int
errdetail_params(ParamListInfo params)2199 errdetail_params(ParamListInfo params)
2200 {
2201 	/* We mustn't call user-defined I/O functions when in an aborted xact */
2202 	if (params && params->numParams > 0 && !IsAbortedTransactionBlockState())
2203 	{
2204 		StringInfoData param_str;
2205 		MemoryContext oldcontext;
2206 		int			paramno;
2207 
2208 		/* Make sure any trash is generated in MessageContext */
2209 		oldcontext = MemoryContextSwitchTo(MessageContext);
2210 
2211 		initStringInfo(&param_str);
2212 
2213 		for (paramno = 0; paramno < params->numParams; paramno++)
2214 		{
2215 			ParamExternData *prm = &params->params[paramno];
2216 			Oid			typoutput;
2217 			bool		typisvarlena;
2218 			char	   *pstring;
2219 			char	   *p;
2220 
2221 			appendStringInfo(&param_str, "%s$%d = ",
2222 							 paramno > 0 ? ", " : "",
2223 							 paramno + 1);
2224 
2225 			if (prm->isnull || !OidIsValid(prm->ptype))
2226 			{
2227 				appendStringInfoString(&param_str, "NULL");
2228 				continue;
2229 			}
2230 
2231 			getTypeOutputInfo(prm->ptype, &typoutput, &typisvarlena);
2232 
2233 			pstring = OidOutputFunctionCall(typoutput, prm->value);
2234 
2235 			appendStringInfoCharMacro(&param_str, '\'');
2236 			for (p = pstring; *p; p++)
2237 			{
2238 				if (*p == '\'') /* double single quotes */
2239 					appendStringInfoCharMacro(&param_str, *p);
2240 				appendStringInfoCharMacro(&param_str, *p);
2241 			}
2242 			appendStringInfoCharMacro(&param_str, '\'');
2243 
2244 			pfree(pstring);
2245 		}
2246 
2247 		errdetail("parameters: %s", param_str.data);
2248 
2249 		pfree(param_str.data);
2250 
2251 		MemoryContextSwitchTo(oldcontext);
2252 	}
2253 
2254 	return 0;
2255 }
2256 
2257 /*
2258  * errdetail_abort
2259  *
2260  * Add an errdetail() line showing abort reason, if any.
2261  */
2262 static int
errdetail_abort(void)2263 errdetail_abort(void)
2264 {
2265 	if (MyProc->recoveryConflictPending)
2266 		errdetail("abort reason: recovery conflict");
2267 
2268 	return 0;
2269 }
2270 
2271 /*
2272  * errdetail_recovery_conflict
2273  *
2274  * Add an errdetail() line showing conflict source.
2275  */
2276 static int
errdetail_recovery_conflict(void)2277 errdetail_recovery_conflict(void)
2278 {
2279 	switch (RecoveryConflictReason)
2280 	{
2281 		case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
2282 			errdetail("User was holding shared buffer pin for too long.");
2283 			break;
2284 		case PROCSIG_RECOVERY_CONFLICT_LOCK:
2285 			errdetail("User was holding a relation lock for too long.");
2286 			break;
2287 		case PROCSIG_RECOVERY_CONFLICT_TABLESPACE:
2288 			errdetail("User was or might have been using tablespace that must be dropped.");
2289 			break;
2290 		case PROCSIG_RECOVERY_CONFLICT_SNAPSHOT:
2291 			errdetail("User query might have needed to see row versions that must be removed.");
2292 			break;
2293 		case PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK:
2294 			errdetail("User transaction caused buffer deadlock with recovery.");
2295 			break;
2296 		case PROCSIG_RECOVERY_CONFLICT_DATABASE:
2297 			errdetail("User was connected to a database that must be dropped.");
2298 			break;
2299 		default:
2300 			break;
2301 			/* no errdetail */
2302 	}
2303 
2304 	return 0;
2305 }
2306 
2307 /*
2308  * exec_describe_statement_message
2309  *
2310  * Process a "Describe" message for a prepared statement
2311  */
2312 static void
exec_describe_statement_message(const char * stmt_name)2313 exec_describe_statement_message(const char *stmt_name)
2314 {
2315 	CachedPlanSource *psrc;
2316 	StringInfoData buf;
2317 	int			i;
2318 
2319 	/*
2320 	 * Start up a transaction command. (Note that this will normally change
2321 	 * current memory context.) Nothing happens if we are already in one.
2322 	 */
2323 	start_xact_command();
2324 
2325 	/* Switch back to message context */
2326 	MemoryContextSwitchTo(MessageContext);
2327 
2328 	/* Find prepared statement */
2329 	if (stmt_name[0] != '\0')
2330 	{
2331 		PreparedStatement *pstmt;
2332 
2333 		pstmt = FetchPreparedStatement(stmt_name, true);
2334 		psrc = pstmt->plansource;
2335 	}
2336 	else
2337 	{
2338 		/* special-case the unnamed statement */
2339 		psrc = unnamed_stmt_psrc;
2340 		if (!psrc)
2341 			ereport(ERROR,
2342 					(errcode(ERRCODE_UNDEFINED_PSTATEMENT),
2343 					 errmsg("unnamed prepared statement does not exist")));
2344 	}
2345 
2346 	/* Prepared statements shouldn't have changeable result descs */
2347 	Assert(psrc->fixed_result);
2348 
2349 	/*
2350 	 * If we are in aborted transaction state, we can't run
2351 	 * SendRowDescriptionMessage(), because that needs catalog accesses.
2352 	 * Hence, refuse to Describe statements that return data.  (We shouldn't
2353 	 * just refuse all Describes, since that might break the ability of some
2354 	 * clients to issue COMMIT or ROLLBACK commands, if they use code that
2355 	 * blindly Describes whatever it does.)  We can Describe parameters
2356 	 * without doing anything dangerous, so we don't restrict that.
2357 	 */
2358 	if (IsAbortedTransactionBlockState() &&
2359 		psrc->resultDesc)
2360 		ereport(ERROR,
2361 				(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
2362 				 errmsg("current transaction is aborted, "
2363 						"commands ignored until end of transaction block"),
2364 				 errdetail_abort()));
2365 
2366 	if (whereToSendOutput != DestRemote)
2367 		return;					/* can't actually do anything... */
2368 
2369 	/*
2370 	 * First describe the parameters...
2371 	 */
2372 	pq_beginmessage(&buf, 't'); /* parameter description message type */
2373 	pq_sendint(&buf, psrc->num_params, 2);
2374 
2375 	for (i = 0; i < psrc->num_params; i++)
2376 	{
2377 		Oid			ptype = psrc->param_types[i];
2378 
2379 		pq_sendint(&buf, (int) ptype, 4);
2380 	}
2381 	pq_endmessage(&buf);
2382 
2383 	/*
2384 	 * Next send RowDescription or NoData to describe the result...
2385 	 */
2386 	if (psrc->resultDesc)
2387 	{
2388 		List	   *tlist;
2389 
2390 		/* Get the plan's primary targetlist */
2391 		tlist = CachedPlanGetTargetList(psrc, NULL);
2392 
2393 		SendRowDescriptionMessage(psrc->resultDesc, tlist, NULL);
2394 	}
2395 	else
2396 		pq_putemptymessage('n');	/* NoData */
2397 
2398 }
2399 
2400 /*
2401  * exec_describe_portal_message
2402  *
2403  * Process a "Describe" message for a portal
2404  */
2405 static void
exec_describe_portal_message(const char * portal_name)2406 exec_describe_portal_message(const char *portal_name)
2407 {
2408 	Portal		portal;
2409 
2410 	/*
2411 	 * Start up a transaction command. (Note that this will normally change
2412 	 * current memory context.) Nothing happens if we are already in one.
2413 	 */
2414 	start_xact_command();
2415 
2416 	/* Switch back to message context */
2417 	MemoryContextSwitchTo(MessageContext);
2418 
2419 	portal = GetPortalByName(portal_name);
2420 	if (!PortalIsValid(portal))
2421 		ereport(ERROR,
2422 				(errcode(ERRCODE_UNDEFINED_CURSOR),
2423 				 errmsg("portal \"%s\" does not exist", portal_name)));
2424 
2425 	/*
2426 	 * If we are in aborted transaction state, we can't run
2427 	 * SendRowDescriptionMessage(), because that needs catalog accesses.
2428 	 * Hence, refuse to Describe portals that return data.  (We shouldn't just
2429 	 * refuse all Describes, since that might break the ability of some
2430 	 * clients to issue COMMIT or ROLLBACK commands, if they use code that
2431 	 * blindly Describes whatever it does.)
2432 	 */
2433 	if (IsAbortedTransactionBlockState() &&
2434 		portal->tupDesc)
2435 		ereport(ERROR,
2436 				(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
2437 				 errmsg("current transaction is aborted, "
2438 						"commands ignored until end of transaction block"),
2439 				 errdetail_abort()));
2440 
2441 	if (whereToSendOutput != DestRemote)
2442 		return;					/* can't actually do anything... */
2443 
2444 	if (portal->tupDesc)
2445 		SendRowDescriptionMessage(portal->tupDesc,
2446 								  FetchPortalTargetList(portal),
2447 								  portal->formats);
2448 	else
2449 		pq_putemptymessage('n');	/* NoData */
2450 }
2451 
2452 
2453 /*
2454  * Convenience routines for starting/committing a single command.
2455  */
2456 static void
start_xact_command(void)2457 start_xact_command(void)
2458 {
2459 	if (!xact_started)
2460 	{
2461 		StartTransactionCommand();
2462 
2463 		/* Set statement timeout running, if any */
2464 		/* NB: this mustn't be enabled until we are within an xact */
2465 		if (StatementTimeout > 0)
2466 			enable_timeout_after(STATEMENT_TIMEOUT, StatementTimeout);
2467 		else
2468 			disable_timeout(STATEMENT_TIMEOUT, false);
2469 
2470 		xact_started = true;
2471 	}
2472 }
2473 
2474 static void
finish_xact_command(void)2475 finish_xact_command(void)
2476 {
2477 	if (xact_started)
2478 	{
2479 		/* Cancel any active statement timeout before committing */
2480 		disable_timeout(STATEMENT_TIMEOUT, false);
2481 
2482 		CommitTransactionCommand();
2483 
2484 #ifdef MEMORY_CONTEXT_CHECKING
2485 		/* Check all memory contexts that weren't freed during commit */
2486 		/* (those that were, were checked before being deleted) */
2487 		MemoryContextCheck(TopMemoryContext);
2488 #endif
2489 
2490 #ifdef SHOW_MEMORY_STATS
2491 		/* Print mem stats after each commit for leak tracking */
2492 		MemoryContextStats(TopMemoryContext);
2493 #endif
2494 
2495 		xact_started = false;
2496 	}
2497 }
2498 
2499 
2500 /*
2501  * Convenience routines for checking whether a statement is one of the
2502  * ones that we allow in transaction-aborted state.
2503  */
2504 
2505 /* Test a bare parsetree */
2506 static bool
IsTransactionExitStmt(Node * parsetree)2507 IsTransactionExitStmt(Node *parsetree)
2508 {
2509 	if (parsetree && IsA(parsetree, TransactionStmt))
2510 	{
2511 		TransactionStmt *stmt = (TransactionStmt *) parsetree;
2512 
2513 		if (stmt->kind == TRANS_STMT_COMMIT ||
2514 			stmt->kind == TRANS_STMT_PREPARE ||
2515 			stmt->kind == TRANS_STMT_ROLLBACK ||
2516 			stmt->kind == TRANS_STMT_ROLLBACK_TO)
2517 			return true;
2518 	}
2519 	return false;
2520 }
2521 
2522 /* Test a list that contains PlannedStmt nodes */
2523 static bool
IsTransactionExitStmtList(List * pstmts)2524 IsTransactionExitStmtList(List *pstmts)
2525 {
2526 	if (list_length(pstmts) == 1)
2527 	{
2528 		PlannedStmt *pstmt = linitial_node(PlannedStmt, pstmts);
2529 
2530 		if (pstmt->commandType == CMD_UTILITY &&
2531 			IsTransactionExitStmt(pstmt->utilityStmt))
2532 			return true;
2533 	}
2534 	return false;
2535 }
2536 
2537 /* Test a list that contains PlannedStmt nodes */
2538 static bool
IsTransactionStmtList(List * pstmts)2539 IsTransactionStmtList(List *pstmts)
2540 {
2541 	if (list_length(pstmts) == 1)
2542 	{
2543 		PlannedStmt *pstmt = linitial_node(PlannedStmt, pstmts);
2544 
2545 		if (pstmt->commandType == CMD_UTILITY &&
2546 			IsA(pstmt->utilityStmt, TransactionStmt))
2547 			return true;
2548 	}
2549 	return false;
2550 }
2551 
2552 /* Release any existing unnamed prepared statement */
2553 static void
drop_unnamed_stmt(void)2554 drop_unnamed_stmt(void)
2555 {
2556 	/* paranoia to avoid a dangling pointer in case of error */
2557 	if (unnamed_stmt_psrc)
2558 	{
2559 		CachedPlanSource *psrc = unnamed_stmt_psrc;
2560 
2561 		unnamed_stmt_psrc = NULL;
2562 		DropCachedPlan(psrc);
2563 	}
2564 }
2565 
2566 
2567 /* --------------------------------
2568  *		signal handler routines used in PostgresMain()
2569  * --------------------------------
2570  */
2571 
2572 /*
2573  * quickdie() occurs when signalled SIGQUIT by the postmaster.
2574  *
2575  * Some backend has bought the farm,
2576  * so we need to stop what we're doing and exit.
2577  */
2578 void
quickdie(SIGNAL_ARGS)2579 quickdie(SIGNAL_ARGS)
2580 {
2581 	sigaddset(&BlockSig, SIGQUIT);	/* prevent nested calls */
2582 	PG_SETMASK(&BlockSig);
2583 
2584 	/*
2585 	 * Prevent interrupts while exiting; though we just blocked signals that
2586 	 * would queue new interrupts, one may have been pending.  We don't want a
2587 	 * quickdie() downgraded to a mere query cancel.
2588 	 */
2589 	HOLD_INTERRUPTS();
2590 
2591 	/*
2592 	 * If we're aborting out of client auth, don't risk trying to send
2593 	 * anything to the client; we will likely violate the protocol, not to
2594 	 * mention that we may have interrupted the guts of OpenSSL or some
2595 	 * authentication library.
2596 	 */
2597 	if (ClientAuthInProgress && whereToSendOutput == DestRemote)
2598 		whereToSendOutput = DestNone;
2599 
2600 	/*
2601 	 * Notify the client before exiting, to give a clue on what happened.
2602 	 *
2603 	 * It's dubious to call ereport() from a signal handler.  It is certainly
2604 	 * not async-signal safe.  But it seems better to try, than to disconnect
2605 	 * abruptly and leave the client wondering what happened.  It's remotely
2606 	 * possible that we crash or hang while trying to send the message, but
2607 	 * receiving a SIGQUIT is a sign that something has already gone badly
2608 	 * wrong, so there's not much to lose.  Assuming the postmaster is still
2609 	 * running, it will SIGKILL us soon if we get stuck for some reason.
2610 	 *
2611 	 * Ideally this should be ereport(FATAL), but then we'd not get control
2612 	 * back...
2613 	 */
2614 	ereport(WARNING,
2615 			(errcode(ERRCODE_CRASH_SHUTDOWN),
2616 			 errmsg("terminating connection because of crash of another server process"),
2617 			 errdetail("The postmaster has commanded this server process to roll back"
2618 					   " the current transaction and exit, because another"
2619 					   " server process exited abnormally and possibly corrupted"
2620 					   " shared memory."),
2621 			 errhint("In a moment you should be able to reconnect to the"
2622 					 " database and repeat your command.")));
2623 
2624 	/*
2625 	 * We DO NOT want to run proc_exit() or atexit() callbacks -- we're here
2626 	 * because shared memory may be corrupted, so we don't want to try to
2627 	 * clean up our transaction.  Just nail the windows shut and get out of
2628 	 * town.  The callbacks wouldn't be safe to run from a signal handler,
2629 	 * anyway.
2630 	 *
2631 	 * Note we do _exit(2) not _exit(0).  This is to force the postmaster into
2632 	 * a system reset cycle if someone sends a manual SIGQUIT to a random
2633 	 * backend.  This is necessary precisely because we don't clean up our
2634 	 * shared memory state.  (The "dead man switch" mechanism in pmsignal.c
2635 	 * should ensure the postmaster sees this as a crash, too, but no harm in
2636 	 * being doubly sure.)
2637 	 */
2638 	_exit(2);
2639 }
2640 
2641 /*
2642  * Shutdown signal from postmaster: abort transaction and exit
2643  * at soonest convenient time
2644  */
2645 void
die(SIGNAL_ARGS)2646 die(SIGNAL_ARGS)
2647 {
2648 	int			save_errno = errno;
2649 
2650 	/* Don't joggle the elbow of proc_exit */
2651 	if (!proc_exit_inprogress)
2652 	{
2653 		InterruptPending = true;
2654 		ProcDiePending = true;
2655 	}
2656 
2657 	/* If we're still here, waken anything waiting on the process latch */
2658 	SetLatch(MyLatch);
2659 
2660 	/*
2661 	 * If we're in single user mode, we want to quit immediately - we can't
2662 	 * rely on latches as they wouldn't work when stdin/stdout is a file.
2663 	 * Rather ugly, but it's unlikely to be worthwhile to invest much more
2664 	 * effort just for the benefit of single user mode.
2665 	 */
2666 	if (DoingCommandRead && whereToSendOutput != DestRemote)
2667 		ProcessInterrupts();
2668 
2669 	errno = save_errno;
2670 }
2671 
2672 /*
2673  * Query-cancel signal from postmaster: abort current transaction
2674  * at soonest convenient time
2675  */
2676 void
StatementCancelHandler(SIGNAL_ARGS)2677 StatementCancelHandler(SIGNAL_ARGS)
2678 {
2679 	int			save_errno = errno;
2680 
2681 	/*
2682 	 * Don't joggle the elbow of proc_exit
2683 	 */
2684 	if (!proc_exit_inprogress)
2685 	{
2686 		InterruptPending = true;
2687 		QueryCancelPending = true;
2688 	}
2689 
2690 	/* If we're still here, waken anything waiting on the process latch */
2691 	SetLatch(MyLatch);
2692 
2693 	errno = save_errno;
2694 }
2695 
2696 /* signal handler for floating point exception */
2697 void
FloatExceptionHandler(SIGNAL_ARGS)2698 FloatExceptionHandler(SIGNAL_ARGS)
2699 {
2700 	/* We're not returning, so no need to save errno */
2701 	ereport(ERROR,
2702 			(errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
2703 			 errmsg("floating-point exception"),
2704 			 errdetail("An invalid floating-point operation was signaled. "
2705 					   "This probably means an out-of-range result or an "
2706 					   "invalid operation, such as division by zero.")));
2707 }
2708 
2709 /*
2710  * SIGHUP: set flag to re-read config file at next convenient time.
2711  *
2712  * Sets the ConfigReloadPending flag, which should be checked at convenient
2713  * places inside main loops. (Better than doing the reading in the signal
2714  * handler, ey?)
2715  */
2716 void
PostgresSigHupHandler(SIGNAL_ARGS)2717 PostgresSigHupHandler(SIGNAL_ARGS)
2718 {
2719 	int			save_errno = errno;
2720 
2721 	ConfigReloadPending = true;
2722 	SetLatch(MyLatch);
2723 
2724 	errno = save_errno;
2725 }
2726 
2727 /*
2728  * RecoveryConflictInterrupt: out-of-line portion of recovery conflict
2729  * handling following receipt of SIGUSR1. Designed to be similar to die()
2730  * and StatementCancelHandler(). Called only by a normal user backend
2731  * that begins a transaction during recovery.
2732  */
2733 void
RecoveryConflictInterrupt(ProcSignalReason reason)2734 RecoveryConflictInterrupt(ProcSignalReason reason)
2735 {
2736 	int			save_errno = errno;
2737 
2738 	/*
2739 	 * Don't joggle the elbow of proc_exit
2740 	 */
2741 	if (!proc_exit_inprogress)
2742 	{
2743 		RecoveryConflictReason = reason;
2744 		switch (reason)
2745 		{
2746 			case PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK:
2747 
2748 				/*
2749 				 * If we aren't waiting for a lock we can never deadlock.
2750 				 */
2751 				if (!IsWaitingForLock())
2752 					return;
2753 
2754 				/* Intentional drop through to check wait for pin */
2755 
2756 			case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
2757 
2758 				/*
2759 				 * If PROCSIG_RECOVERY_CONFLICT_BUFFERPIN is requested but we
2760 				 * aren't blocking the Startup process there is nothing more
2761 				 * to do.
2762 				 *
2763 				 * When PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK is
2764 				 * requested, if we're waiting for locks and the startup
2765 				 * process is not waiting for buffer pin (i.e., also waiting
2766 				 * for locks), we set the flag so that ProcSleep() will check
2767 				 * for deadlocks.
2768 				 */
2769 				if (!HoldingBufferPinThatDelaysRecovery())
2770 				{
2771 					if (reason == PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK &&
2772 						GetStartupBufferPinWaitBufId() < 0)
2773 						CheckDeadLockAlert();
2774 					return;
2775 				}
2776 
2777 				MyProc->recoveryConflictPending = true;
2778 
2779 				/* Intentional drop through to error handling */
2780 
2781 			case PROCSIG_RECOVERY_CONFLICT_LOCK:
2782 			case PROCSIG_RECOVERY_CONFLICT_TABLESPACE:
2783 			case PROCSIG_RECOVERY_CONFLICT_SNAPSHOT:
2784 
2785 				/*
2786 				 * If we aren't in a transaction any longer then ignore.
2787 				 */
2788 				if (!IsTransactionOrTransactionBlock())
2789 					return;
2790 
2791 				/*
2792 				 * If we can abort just the current subtransaction then we are
2793 				 * OK to throw an ERROR to resolve the conflict. Otherwise
2794 				 * drop through to the FATAL case.
2795 				 *
2796 				 * XXX other times that we can throw just an ERROR *may* be
2797 				 * PROCSIG_RECOVERY_CONFLICT_LOCK if no locks are held in
2798 				 * parent transactions
2799 				 *
2800 				 * PROCSIG_RECOVERY_CONFLICT_SNAPSHOT if no snapshots are held
2801 				 * by parent transactions and the transaction is not
2802 				 * transaction-snapshot mode
2803 				 *
2804 				 * PROCSIG_RECOVERY_CONFLICT_TABLESPACE if no temp files or
2805 				 * cursors open in parent transactions
2806 				 */
2807 				if (!IsSubTransaction())
2808 				{
2809 					/*
2810 					 * If we already aborted then we no longer need to cancel.
2811 					 * We do this here since we do not wish to ignore aborted
2812 					 * subtransactions, which must cause FATAL, currently.
2813 					 */
2814 					if (IsAbortedTransactionBlockState())
2815 						return;
2816 
2817 					RecoveryConflictPending = true;
2818 					QueryCancelPending = true;
2819 					InterruptPending = true;
2820 					break;
2821 				}
2822 
2823 				/* Intentional drop through to session cancel */
2824 
2825 			case PROCSIG_RECOVERY_CONFLICT_DATABASE:
2826 				RecoveryConflictPending = true;
2827 				ProcDiePending = true;
2828 				InterruptPending = true;
2829 				break;
2830 
2831 			default:
2832 				elog(FATAL, "unrecognized conflict mode: %d",
2833 					 (int) reason);
2834 		}
2835 
2836 		Assert(RecoveryConflictPending && (QueryCancelPending || ProcDiePending));
2837 
2838 		/*
2839 		 * All conflicts apart from database cause dynamic errors where the
2840 		 * command or transaction can be retried at a later point with some
2841 		 * potential for success. No need to reset this, since non-retryable
2842 		 * conflict errors are currently FATAL.
2843 		 */
2844 		if (reason == PROCSIG_RECOVERY_CONFLICT_DATABASE)
2845 			RecoveryConflictRetryable = false;
2846 	}
2847 
2848 	/*
2849 	 * Set the process latch. This function essentially emulates signal
2850 	 * handlers like die() and StatementCancelHandler() and it seems prudent
2851 	 * to behave similarly as they do.
2852 	 */
2853 	SetLatch(MyLatch);
2854 
2855 	errno = save_errno;
2856 }
2857 
2858 /*
2859  * ProcessInterrupts: out-of-line portion of CHECK_FOR_INTERRUPTS() macro
2860  *
2861  * If an interrupt condition is pending, and it's safe to service it,
2862  * then clear the flag and accept the interrupt.  Called only when
2863  * InterruptPending is true.
2864  *
2865  * Note: if INTERRUPTS_CAN_BE_PROCESSED() is true, then ProcessInterrupts
2866  * is guaranteed to clear the InterruptPending flag before returning.
2867  * (This is not the same as guaranteeing that it's still clear when we
2868  * return; another interrupt could have arrived.  But we promise that
2869  * any pre-existing one will have been serviced.)
2870  */
2871 void
ProcessInterrupts(void)2872 ProcessInterrupts(void)
2873 {
2874 	/* OK to accept any interrupts now? */
2875 	if (InterruptHoldoffCount != 0 || CritSectionCount != 0)
2876 		return;
2877 	InterruptPending = false;
2878 
2879 	if (ProcDiePending)
2880 	{
2881 		ProcDiePending = false;
2882 		QueryCancelPending = false; /* ProcDie trumps QueryCancel */
2883 		LockErrorCleanup();
2884 		/* As in quickdie, don't risk sending to client during auth */
2885 		if (ClientAuthInProgress && whereToSendOutput == DestRemote)
2886 			whereToSendOutput = DestNone;
2887 		if (ClientAuthInProgress)
2888 			ereport(FATAL,
2889 					(errcode(ERRCODE_QUERY_CANCELED),
2890 					 errmsg("canceling authentication due to timeout")));
2891 		else if (IsAutoVacuumWorkerProcess())
2892 			ereport(FATAL,
2893 					(errcode(ERRCODE_ADMIN_SHUTDOWN),
2894 					 errmsg("terminating autovacuum process due to administrator command")));
2895 		else if (IsLogicalWorker())
2896 			ereport(FATAL,
2897 					(errcode(ERRCODE_ADMIN_SHUTDOWN),
2898 					 errmsg("terminating logical replication worker due to administrator command")));
2899 		else if (IsLogicalLauncher())
2900 		{
2901 			ereport(DEBUG1,
2902 					(errmsg("logical replication launcher shutting down")));
2903 
2904 			/*
2905 			 * The logical replication launcher can be stopped at any time.
2906 			 * Use exit status 1 so the background worker is restarted.
2907 			 */
2908 			proc_exit(1);
2909 		}
2910 		else if (RecoveryConflictPending && RecoveryConflictRetryable)
2911 		{
2912 			pgstat_report_recovery_conflict(RecoveryConflictReason);
2913 			ereport(FATAL,
2914 					(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2915 					 errmsg("terminating connection due to conflict with recovery"),
2916 					 errdetail_recovery_conflict()));
2917 		}
2918 		else if (RecoveryConflictPending)
2919 		{
2920 			/* Currently there is only one non-retryable recovery conflict */
2921 			Assert(RecoveryConflictReason == PROCSIG_RECOVERY_CONFLICT_DATABASE);
2922 			pgstat_report_recovery_conflict(RecoveryConflictReason);
2923 			ereport(FATAL,
2924 					(errcode(ERRCODE_DATABASE_DROPPED),
2925 					 errmsg("terminating connection due to conflict with recovery"),
2926 					 errdetail_recovery_conflict()));
2927 		}
2928 		else
2929 			ereport(FATAL,
2930 					(errcode(ERRCODE_ADMIN_SHUTDOWN),
2931 					 errmsg("terminating connection due to administrator command")));
2932 	}
2933 	if (ClientConnectionLost)
2934 	{
2935 		QueryCancelPending = false; /* lost connection trumps QueryCancel */
2936 		LockErrorCleanup();
2937 		/* don't send to client, we already know the connection to be dead. */
2938 		whereToSendOutput = DestNone;
2939 		ereport(FATAL,
2940 				(errcode(ERRCODE_CONNECTION_FAILURE),
2941 				 errmsg("connection to client lost")));
2942 	}
2943 
2944 	/*
2945 	 * If a recovery conflict happens while we are waiting for input from the
2946 	 * client, the client is presumably just sitting idle in a transaction,
2947 	 * preventing recovery from making progress.  Terminate the connection to
2948 	 * dislodge it.
2949 	 */
2950 	if (RecoveryConflictPending && DoingCommandRead)
2951 	{
2952 		QueryCancelPending = false; /* this trumps QueryCancel */
2953 		RecoveryConflictPending = false;
2954 		LockErrorCleanup();
2955 		pgstat_report_recovery_conflict(RecoveryConflictReason);
2956 		ereport(FATAL,
2957 				(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2958 				 errmsg("terminating connection due to conflict with recovery"),
2959 				 errdetail_recovery_conflict(),
2960 				 errhint("In a moment you should be able to reconnect to the"
2961 						 " database and repeat your command.")));
2962 	}
2963 
2964 	/*
2965 	 * Don't allow query cancel interrupts while reading input from the
2966 	 * client, because we might lose sync in the FE/BE protocol.  (Die
2967 	 * interrupts are OK, because we won't read any further messages from
2968 	 * the client in that case.)
2969 	 */
2970 	if (QueryCancelPending && QueryCancelHoldoffCount != 0)
2971 	{
2972 		/*
2973 		 * Re-arm InterruptPending so that we process the cancel request as
2974 		 * soon as we're done reading the message.  (XXX this is seriously
2975 		 * ugly: it complicates INTERRUPTS_CAN_BE_PROCESSED(), and it means we
2976 		 * can't use that macro directly as the initial test in this function,
2977 		 * meaning that this code also creates opportunities for other bugs to
2978 		 * appear.)
2979 		 */
2980 		InterruptPending = true;
2981 	}
2982 	else if (QueryCancelPending)
2983 	{
2984 		bool		lock_timeout_occurred;
2985 		bool		stmt_timeout_occurred;
2986 
2987 		QueryCancelPending = false;
2988 
2989 		/*
2990 		 * If LOCK_TIMEOUT and STATEMENT_TIMEOUT indicators are both set, we
2991 		 * need to clear both, so always fetch both.
2992 		 */
2993 		lock_timeout_occurred = get_timeout_indicator(LOCK_TIMEOUT, true);
2994 		stmt_timeout_occurred = get_timeout_indicator(STATEMENT_TIMEOUT, true);
2995 
2996 		/*
2997 		 * If both were set, we want to report whichever timeout completed
2998 		 * earlier; this ensures consistent behavior if the machine is slow
2999 		 * enough that the second timeout triggers before we get here.  A tie
3000 		 * is arbitrarily broken in favor of reporting a lock timeout.
3001 		 */
3002 		if (lock_timeout_occurred && stmt_timeout_occurred &&
3003 			get_timeout_finish_time(STATEMENT_TIMEOUT) < get_timeout_finish_time(LOCK_TIMEOUT))
3004 			lock_timeout_occurred = false;	/* report stmt timeout */
3005 
3006 		if (lock_timeout_occurred)
3007 		{
3008 			LockErrorCleanup();
3009 			ereport(ERROR,
3010 					(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
3011 					 errmsg("canceling statement due to lock timeout")));
3012 		}
3013 		if (stmt_timeout_occurred)
3014 		{
3015 			LockErrorCleanup();
3016 			ereport(ERROR,
3017 					(errcode(ERRCODE_QUERY_CANCELED),
3018 					 errmsg("canceling statement due to statement timeout")));
3019 		}
3020 		if (IsAutoVacuumWorkerProcess())
3021 		{
3022 			LockErrorCleanup();
3023 			ereport(ERROR,
3024 					(errcode(ERRCODE_QUERY_CANCELED),
3025 					 errmsg("canceling autovacuum task")));
3026 		}
3027 		if (RecoveryConflictPending)
3028 		{
3029 			RecoveryConflictPending = false;
3030 			LockErrorCleanup();
3031 			pgstat_report_recovery_conflict(RecoveryConflictReason);
3032 			ereport(ERROR,
3033 					(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3034 					 errmsg("canceling statement due to conflict with recovery"),
3035 					 errdetail_recovery_conflict()));
3036 		}
3037 
3038 		/*
3039 		 * If we are reading a command from the client, just ignore the cancel
3040 		 * request --- sending an extra error message won't accomplish
3041 		 * anything.  Otherwise, go ahead and throw the error.
3042 		 */
3043 		if (!DoingCommandRead)
3044 		{
3045 			LockErrorCleanup();
3046 			ereport(ERROR,
3047 					(errcode(ERRCODE_QUERY_CANCELED),
3048 					 errmsg("canceling statement due to user request")));
3049 		}
3050 	}
3051 
3052 	if (IdleInTransactionSessionTimeoutPending)
3053 	{
3054 		/* Has the timeout setting changed since last we looked? */
3055 		if (IdleInTransactionSessionTimeout > 0)
3056 			ereport(FATAL,
3057 					(errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT),
3058 					 errmsg("terminating connection due to idle-in-transaction timeout")));
3059 		else
3060 			IdleInTransactionSessionTimeoutPending = false;
3061 
3062 	}
3063 
3064 	if (ParallelMessagePending)
3065 		HandleParallelMessages();
3066 }
3067 
3068 
3069 /*
3070  * IA64-specific code to fetch the AR.BSP register for stack depth checks.
3071  *
3072  * We currently support gcc, icc, and HP-UX's native compiler here.
3073  *
3074  * Note: while icc accepts gcc asm blocks on x86[_64], this is not true on
3075  * ia64 (at least not in icc versions before 12.x).  So we have to carry a
3076  * separate implementation for it.
3077  */
3078 #if defined(__ia64__) || defined(__ia64)
3079 
3080 #if defined(__hpux) && !defined(__GNUC__) && !defined(__INTEL_COMPILER)
3081 /* Assume it's HP-UX native compiler */
3082 #include <ia64/sys/inline.h>
3083 #define ia64_get_bsp() ((char *) (_Asm_mov_from_ar(_AREG_BSP, _NO_FENCE)))
3084 #elif defined(__INTEL_COMPILER)
3085 /* icc */
3086 #include <asm/ia64regs.h>
3087 #define ia64_get_bsp() ((char *) __getReg(_IA64_REG_AR_BSP))
3088 #else
3089 /* gcc */
3090 static __inline__ char *
ia64_get_bsp(void)3091 ia64_get_bsp(void)
3092 {
3093 	char	   *ret;
3094 
3095 	/* the ;; is a "stop", seems to be required before fetching BSP */
3096 	__asm__ __volatile__(
3097 						 ";;\n"
3098 						 "	mov	%0=ar.bsp	\n"
3099 :						 "=r"(ret));
3100 
3101 	return ret;
3102 }
3103 #endif
3104 #endif							/* IA64 */
3105 
3106 
3107 /*
3108  * set_stack_base: set up reference point for stack depth checking
3109  *
3110  * Returns the old reference point, if any.
3111  */
3112 pg_stack_base_t
set_stack_base(void)3113 set_stack_base(void)
3114 {
3115 	char		stack_base;
3116 	pg_stack_base_t old;
3117 
3118 #if defined(__ia64__) || defined(__ia64)
3119 	old.stack_base_ptr = stack_base_ptr;
3120 	old.register_stack_base_ptr = register_stack_base_ptr;
3121 #else
3122 	old = stack_base_ptr;
3123 #endif
3124 
3125 	/* Set up reference point for stack depth checking */
3126 	stack_base_ptr = &stack_base;
3127 #if defined(__ia64__) || defined(__ia64)
3128 	register_stack_base_ptr = ia64_get_bsp();
3129 #endif
3130 
3131 	return old;
3132 }
3133 
3134 /*
3135  * restore_stack_base: restore reference point for stack depth checking
3136  *
3137  * This can be used after set_stack_base() to restore the old value. This
3138  * is currently only used in PL/Java. When PL/Java calls a backend function
3139  * from different thread, the thread's stack is at a different location than
3140  * the main thread's stack, so it sets the base pointer before the call, and
3141  * restores it afterwards.
3142  */
3143 void
restore_stack_base(pg_stack_base_t base)3144 restore_stack_base(pg_stack_base_t base)
3145 {
3146 #if defined(__ia64__) || defined(__ia64)
3147 	stack_base_ptr = base.stack_base_ptr;
3148 	register_stack_base_ptr = base.register_stack_base_ptr;
3149 #else
3150 	stack_base_ptr = base;
3151 #endif
3152 }
3153 
3154 /*
3155  * check_stack_depth/stack_is_too_deep: check for excessively deep recursion
3156  *
3157  * This should be called someplace in any recursive routine that might possibly
3158  * recurse deep enough to overflow the stack.  Most Unixen treat stack
3159  * overflow as an unrecoverable SIGSEGV, so we want to error out ourselves
3160  * before hitting the hardware limit.
3161  *
3162  * check_stack_depth() just throws an error summarily.  stack_is_too_deep()
3163  * can be used by code that wants to handle the error condition itself.
3164  */
3165 void
check_stack_depth(void)3166 check_stack_depth(void)
3167 {
3168 	if (stack_is_too_deep())
3169 	{
3170 		ereport(ERROR,
3171 				(errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
3172 				 errmsg("stack depth limit exceeded"),
3173 				 errhint("Increase the configuration parameter \"max_stack_depth\" (currently %dkB), "
3174 						 "after ensuring the platform's stack depth limit is adequate.",
3175 						 max_stack_depth)));
3176 	}
3177 }
3178 
3179 bool
stack_is_too_deep(void)3180 stack_is_too_deep(void)
3181 {
3182 	char		stack_top_loc;
3183 	long		stack_depth;
3184 
3185 	/*
3186 	 * Compute distance from reference point to my local variables
3187 	 */
3188 	stack_depth = (long) (stack_base_ptr - &stack_top_loc);
3189 
3190 	/*
3191 	 * Take abs value, since stacks grow up on some machines, down on others
3192 	 */
3193 	if (stack_depth < 0)
3194 		stack_depth = -stack_depth;
3195 
3196 	/*
3197 	 * Trouble?
3198 	 *
3199 	 * The test on stack_base_ptr prevents us from erroring out if called
3200 	 * during process setup or in a non-backend process.  Logically it should
3201 	 * be done first, but putting it here avoids wasting cycles during normal
3202 	 * cases.
3203 	 */
3204 	if (stack_depth > max_stack_depth_bytes &&
3205 		stack_base_ptr != NULL)
3206 		return true;
3207 
3208 	/*
3209 	 * On IA64 there is a separate "register" stack that requires its own
3210 	 * independent check.  For this, we have to measure the change in the
3211 	 * "BSP" pointer from PostgresMain to here.  Logic is just as above,
3212 	 * except that we know IA64's register stack grows up.
3213 	 *
3214 	 * Note we assume that the same max_stack_depth applies to both stacks.
3215 	 */
3216 #if defined(__ia64__) || defined(__ia64)
3217 	stack_depth = (long) (ia64_get_bsp() - register_stack_base_ptr);
3218 
3219 	if (stack_depth > max_stack_depth_bytes &&
3220 		register_stack_base_ptr != NULL)
3221 		return true;
3222 #endif							/* IA64 */
3223 
3224 	return false;
3225 }
3226 
3227 /* GUC check hook for max_stack_depth */
3228 bool
check_max_stack_depth(int * newval,void ** extra,GucSource source)3229 check_max_stack_depth(int *newval, void **extra, GucSource source)
3230 {
3231 	long		newval_bytes = *newval * 1024L;
3232 	long		stack_rlimit = get_stack_depth_rlimit();
3233 
3234 	if (stack_rlimit > 0 && newval_bytes > stack_rlimit - STACK_DEPTH_SLOP)
3235 	{
3236 		GUC_check_errdetail("\"max_stack_depth\" must not exceed %ldkB.",
3237 							(stack_rlimit - STACK_DEPTH_SLOP) / 1024L);
3238 		GUC_check_errhint("Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent.");
3239 		return false;
3240 	}
3241 	return true;
3242 }
3243 
3244 /* GUC assign hook for max_stack_depth */
3245 void
assign_max_stack_depth(int newval,void * extra)3246 assign_max_stack_depth(int newval, void *extra)
3247 {
3248 	long		newval_bytes = newval * 1024L;
3249 
3250 	max_stack_depth_bytes = newval_bytes;
3251 }
3252 
3253 
3254 /*
3255  * set_debug_options --- apply "-d N" command line option
3256  *
3257  * -d is not quite the same as setting log_min_messages because it enables
3258  * other output options.
3259  */
3260 void
set_debug_options(int debug_flag,GucContext context,GucSource source)3261 set_debug_options(int debug_flag, GucContext context, GucSource source)
3262 {
3263 	if (debug_flag > 0)
3264 	{
3265 		char		debugstr[64];
3266 
3267 		sprintf(debugstr, "debug%d", debug_flag);
3268 		SetConfigOption("log_min_messages", debugstr, context, source);
3269 	}
3270 	else
3271 		SetConfigOption("log_min_messages", "notice", context, source);
3272 
3273 	if (debug_flag >= 1 && context == PGC_POSTMASTER)
3274 	{
3275 		SetConfigOption("log_connections", "true", context, source);
3276 		SetConfigOption("log_disconnections", "true", context, source);
3277 	}
3278 	if (debug_flag >= 2)
3279 		SetConfigOption("log_statement", "all", context, source);
3280 	if (debug_flag >= 3)
3281 		SetConfigOption("debug_print_parse", "true", context, source);
3282 	if (debug_flag >= 4)
3283 		SetConfigOption("debug_print_plan", "true", context, source);
3284 	if (debug_flag >= 5)
3285 		SetConfigOption("debug_print_rewritten", "true", context, source);
3286 }
3287 
3288 
3289 bool
set_plan_disabling_options(const char * arg,GucContext context,GucSource source)3290 set_plan_disabling_options(const char *arg, GucContext context, GucSource source)
3291 {
3292 	const char *tmp = NULL;
3293 
3294 	switch (arg[0])
3295 	{
3296 		case 's':				/* seqscan */
3297 			tmp = "enable_seqscan";
3298 			break;
3299 		case 'i':				/* indexscan */
3300 			tmp = "enable_indexscan";
3301 			break;
3302 		case 'o':				/* indexonlyscan */
3303 			tmp = "enable_indexonlyscan";
3304 			break;
3305 		case 'b':				/* bitmapscan */
3306 			tmp = "enable_bitmapscan";
3307 			break;
3308 		case 't':				/* tidscan */
3309 			tmp = "enable_tidscan";
3310 			break;
3311 		case 'n':				/* nestloop */
3312 			tmp = "enable_nestloop";
3313 			break;
3314 		case 'm':				/* mergejoin */
3315 			tmp = "enable_mergejoin";
3316 			break;
3317 		case 'h':				/* hashjoin */
3318 			tmp = "enable_hashjoin";
3319 			break;
3320 	}
3321 	if (tmp)
3322 	{
3323 		SetConfigOption(tmp, "false", context, source);
3324 		return true;
3325 	}
3326 	else
3327 		return false;
3328 }
3329 
3330 
3331 const char *
get_stats_option_name(const char * arg)3332 get_stats_option_name(const char *arg)
3333 {
3334 	switch (arg[0])
3335 	{
3336 		case 'p':
3337 			if (optarg[1] == 'a')	/* "parser" */
3338 				return "log_parser_stats";
3339 			else if (optarg[1] == 'l')	/* "planner" */
3340 				return "log_planner_stats";
3341 			break;
3342 
3343 		case 'e':				/* "executor" */
3344 			return "log_executor_stats";
3345 			break;
3346 	}
3347 
3348 	return NULL;
3349 }
3350 
3351 
3352 /* ----------------------------------------------------------------
3353  * process_postgres_switches
3354  *	   Parse command line arguments for PostgresMain
3355  *
3356  * This is called twice, once for the "secure" options coming from the
3357  * postmaster or command line, and once for the "insecure" options coming
3358  * from the client's startup packet.  The latter have the same syntax but
3359  * may be restricted in what they can do.
3360  *
3361  * argv[0] is ignored in either case (it's assumed to be the program name).
3362  *
3363  * ctx is PGC_POSTMASTER for secure options, PGC_BACKEND for insecure options
3364  * coming from the client, or PGC_SU_BACKEND for insecure options coming from
3365  * a superuser client.
3366  *
3367  * If a database name is present in the command line arguments, it's
3368  * returned into *dbname (this is allowed only if *dbname is initially NULL).
3369  * ----------------------------------------------------------------
3370  */
3371 void
process_postgres_switches(int argc,char * argv[],GucContext ctx,const char ** dbname)3372 process_postgres_switches(int argc, char *argv[], GucContext ctx,
3373 						  const char **dbname)
3374 {
3375 	bool		secure = (ctx == PGC_POSTMASTER);
3376 	int			errs = 0;
3377 	GucSource	gucsource;
3378 	int			flag;
3379 
3380 	if (secure)
3381 	{
3382 		gucsource = PGC_S_ARGV; /* switches came from command line */
3383 
3384 		/* Ignore the initial --single argument, if present */
3385 		if (argc > 1 && strcmp(argv[1], "--single") == 0)
3386 		{
3387 			argv++;
3388 			argc--;
3389 		}
3390 	}
3391 	else
3392 	{
3393 		gucsource = PGC_S_CLIENT;	/* switches came from client */
3394 	}
3395 
3396 #ifdef HAVE_INT_OPTERR
3397 
3398 	/*
3399 	 * Turn this off because it's either printed to stderr and not the log
3400 	 * where we'd want it, or argv[0] is now "--single", which would make for
3401 	 * a weird error message.  We print our own error message below.
3402 	 */
3403 	opterr = 0;
3404 #endif
3405 
3406 	/*
3407 	 * Parse command-line options.  CAUTION: keep this in sync with
3408 	 * postmaster/postmaster.c (the option sets should not conflict) and with
3409 	 * the common help() function in main/main.c.
3410 	 */
3411 	while ((flag = getopt(argc, argv, "B:bc:C:D:d:EeFf:h:ijk:lN:nOo:Pp:r:S:sTt:v:W:-:")) != -1)
3412 	{
3413 		switch (flag)
3414 		{
3415 			case 'B':
3416 				SetConfigOption("shared_buffers", optarg, ctx, gucsource);
3417 				break;
3418 
3419 			case 'b':
3420 				/* Undocumented flag used for binary upgrades */
3421 				if (secure)
3422 					IsBinaryUpgrade = true;
3423 				break;
3424 
3425 			case 'C':
3426 				/* ignored for consistency with the postmaster */
3427 				break;
3428 
3429 			case 'D':
3430 				if (secure)
3431 					userDoption = strdup(optarg);
3432 				break;
3433 
3434 			case 'd':
3435 				set_debug_options(atoi(optarg), ctx, gucsource);
3436 				break;
3437 
3438 			case 'E':
3439 				if (secure)
3440 					EchoQuery = true;
3441 				break;
3442 
3443 			case 'e':
3444 				SetConfigOption("datestyle", "euro", ctx, gucsource);
3445 				break;
3446 
3447 			case 'F':
3448 				SetConfigOption("fsync", "false", ctx, gucsource);
3449 				break;
3450 
3451 			case 'f':
3452 				if (!set_plan_disabling_options(optarg, ctx, gucsource))
3453 					errs++;
3454 				break;
3455 
3456 			case 'h':
3457 				SetConfigOption("listen_addresses", optarg, ctx, gucsource);
3458 				break;
3459 
3460 			case 'i':
3461 				SetConfigOption("listen_addresses", "*", ctx, gucsource);
3462 				break;
3463 
3464 			case 'j':
3465 				if (secure)
3466 					UseSemiNewlineNewline = true;
3467 				break;
3468 
3469 			case 'k':
3470 				SetConfigOption("unix_socket_directories", optarg, ctx, gucsource);
3471 				break;
3472 
3473 			case 'l':
3474 				SetConfigOption("ssl", "true", ctx, gucsource);
3475 				break;
3476 
3477 			case 'N':
3478 				SetConfigOption("max_connections", optarg, ctx, gucsource);
3479 				break;
3480 
3481 			case 'n':
3482 				/* ignored for consistency with postmaster */
3483 				break;
3484 
3485 			case 'O':
3486 				SetConfigOption("allow_system_table_mods", "true", ctx, gucsource);
3487 				break;
3488 
3489 			case 'o':
3490 				errs++;
3491 				break;
3492 
3493 			case 'P':
3494 				SetConfigOption("ignore_system_indexes", "true", ctx, gucsource);
3495 				break;
3496 
3497 			case 'p':
3498 				SetConfigOption("port", optarg, ctx, gucsource);
3499 				break;
3500 
3501 			case 'r':
3502 				/* send output (stdout and stderr) to the given file */
3503 				if (secure)
3504 					strlcpy(OutputFileName, optarg, MAXPGPATH);
3505 				break;
3506 
3507 			case 'S':
3508 				SetConfigOption("work_mem", optarg, ctx, gucsource);
3509 				break;
3510 
3511 			case 's':
3512 				SetConfigOption("log_statement_stats", "true", ctx, gucsource);
3513 				break;
3514 
3515 			case 'T':
3516 				/* ignored for consistency with the postmaster */
3517 				break;
3518 
3519 			case 't':
3520 				{
3521 					const char *tmp = get_stats_option_name(optarg);
3522 
3523 					if (tmp)
3524 						SetConfigOption(tmp, "true", ctx, gucsource);
3525 					else
3526 						errs++;
3527 					break;
3528 				}
3529 
3530 			case 'v':
3531 
3532 				/*
3533 				 * -v is no longer used in normal operation, since
3534 				 * FrontendProtocol is already set before we get here. We keep
3535 				 * the switch only for possible use in standalone operation,
3536 				 * in case we ever support using normal FE/BE protocol with a
3537 				 * standalone backend.
3538 				 */
3539 				if (secure)
3540 					FrontendProtocol = (ProtocolVersion) atoi(optarg);
3541 				break;
3542 
3543 			case 'W':
3544 				SetConfigOption("post_auth_delay", optarg, ctx, gucsource);
3545 				break;
3546 
3547 			case 'c':
3548 			case '-':
3549 				{
3550 					char	   *name,
3551 							   *value;
3552 
3553 					ParseLongOption(optarg, &name, &value);
3554 					if (!value)
3555 					{
3556 						if (flag == '-')
3557 							ereport(ERROR,
3558 									(errcode(ERRCODE_SYNTAX_ERROR),
3559 									 errmsg("--%s requires a value",
3560 											optarg)));
3561 						else
3562 							ereport(ERROR,
3563 									(errcode(ERRCODE_SYNTAX_ERROR),
3564 									 errmsg("-c %s requires a value",
3565 											optarg)));
3566 					}
3567 					SetConfigOption(name, value, ctx, gucsource);
3568 					free(name);
3569 					if (value)
3570 						free(value);
3571 					break;
3572 				}
3573 
3574 			default:
3575 				errs++;
3576 				break;
3577 		}
3578 
3579 		if (errs)
3580 			break;
3581 	}
3582 
3583 	/*
3584 	 * Optional database name should be there only if *dbname is NULL.
3585 	 */
3586 	if (!errs && dbname && *dbname == NULL && argc - optind >= 1)
3587 		*dbname = strdup(argv[optind++]);
3588 
3589 	if (errs || argc != optind)
3590 	{
3591 		if (errs)
3592 			optind--;			/* complain about the previous argument */
3593 
3594 		/* spell the error message a bit differently depending on context */
3595 		if (IsUnderPostmaster)
3596 			ereport(FATAL,
3597 					(errcode(ERRCODE_SYNTAX_ERROR),
3598 					 errmsg("invalid command-line argument for server process: %s", argv[optind]),
3599 					 errhint("Try \"%s --help\" for more information.", progname)));
3600 		else
3601 			ereport(FATAL,
3602 					(errcode(ERRCODE_SYNTAX_ERROR),
3603 					 errmsg("%s: invalid command-line argument: %s",
3604 							progname, argv[optind]),
3605 					 errhint("Try \"%s --help\" for more information.", progname)));
3606 	}
3607 
3608 	/*
3609 	 * Reset getopt(3) library so that it will work correctly in subprocesses
3610 	 * or when this function is called a second time with another array.
3611 	 */
3612 	optind = 1;
3613 #ifdef HAVE_INT_OPTRESET
3614 	optreset = 1;				/* some systems need this too */
3615 #endif
3616 }
3617 
3618 
3619 /* ----------------------------------------------------------------
3620  * PostgresMain
3621  *	   postgres main loop -- all backends, interactive or otherwise start here
3622  *
3623  * argc/argv are the command line arguments to be used.  (When being forked
3624  * by the postmaster, these are not the original argv array of the process.)
3625  * dbname is the name of the database to connect to, or NULL if the database
3626  * name should be extracted from the command line arguments or defaulted.
3627  * username is the PostgreSQL user name to be used for the session.
3628  * ----------------------------------------------------------------
3629  */
3630 void
PostgresMain(int argc,char * argv[],const char * dbname,const char * username)3631 PostgresMain(int argc, char *argv[],
3632 			 const char *dbname,
3633 			 const char *username)
3634 {
3635 	int			firstchar;
3636 	StringInfoData input_message;
3637 	sigjmp_buf	local_sigjmp_buf;
3638 	volatile bool send_ready_for_query = true;
3639 	bool		disable_idle_in_transaction_timeout = false;
3640 
3641 	/* Initialize startup process environment if necessary. */
3642 	if (!IsUnderPostmaster)
3643 		InitStandaloneProcess(argv[0]);
3644 
3645 	SetProcessingMode(InitProcessing);
3646 
3647 	/*
3648 	 * Set default values for command-line options.
3649 	 */
3650 	if (!IsUnderPostmaster)
3651 		InitializeGUCOptions();
3652 
3653 	/*
3654 	 * Parse command-line options.
3655 	 */
3656 	process_postgres_switches(argc, argv, PGC_POSTMASTER, &dbname);
3657 
3658 	/* Must have gotten a database name, or have a default (the username) */
3659 	if (dbname == NULL)
3660 	{
3661 		dbname = username;
3662 		if (dbname == NULL)
3663 			ereport(FATAL,
3664 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3665 					 errmsg("%s: no database nor user name specified",
3666 							progname)));
3667 	}
3668 
3669 	/* Acquire configuration parameters, unless inherited from postmaster */
3670 	if (!IsUnderPostmaster)
3671 	{
3672 		if (!SelectConfigFiles(userDoption, progname))
3673 			proc_exit(1);
3674 	}
3675 
3676 	/*
3677 	 * Set up signal handlers and masks.
3678 	 *
3679 	 * Note that postmaster blocked all signals before forking child process,
3680 	 * so there is no race condition whereby we might receive a signal before
3681 	 * we have set up the handler.
3682 	 *
3683 	 * Also note: it's best not to use any signals that are SIG_IGNored in the
3684 	 * postmaster.  If such a signal arrives before we are able to change the
3685 	 * handler to non-SIG_IGN, it'll get dropped.  Instead, make a dummy
3686 	 * handler in the postmaster to reserve the signal. (Of course, this isn't
3687 	 * an issue for signals that are locally generated, such as SIGALRM and
3688 	 * SIGPIPE.)
3689 	 */
3690 	if (am_walsender)
3691 		WalSndSignals();
3692 	else
3693 	{
3694 		pqsignal(SIGHUP, PostgresSigHupHandler);	/* set flag to read config
3695 													 * file */
3696 		pqsignal(SIGINT, StatementCancelHandler);	/* cancel current query */
3697 		pqsignal(SIGTERM, die); /* cancel current query and exit */
3698 
3699 		/*
3700 		 * In a standalone backend, SIGQUIT can be generated from the keyboard
3701 		 * easily, while SIGTERM cannot, so we make both signals do die()
3702 		 * rather than quickdie().
3703 		 */
3704 		if (IsUnderPostmaster)
3705 			pqsignal(SIGQUIT, quickdie);	/* hard crash time */
3706 		else
3707 			pqsignal(SIGQUIT, die); /* cancel current query and exit */
3708 		InitializeTimeouts();	/* establishes SIGALRM handler */
3709 
3710 		/*
3711 		 * Ignore failure to write to frontend. Note: if frontend closes
3712 		 * connection, we will notice it and exit cleanly when control next
3713 		 * returns to outer loop.  This seems safer than forcing exit in the
3714 		 * midst of output during who-knows-what operation...
3715 		 */
3716 		pqsignal(SIGPIPE, SIG_IGN);
3717 		pqsignal(SIGUSR1, procsignal_sigusr1_handler);
3718 		pqsignal(SIGUSR2, SIG_IGN);
3719 		pqsignal(SIGFPE, FloatExceptionHandler);
3720 
3721 		/*
3722 		 * Reset some signals that are accepted by postmaster but not by
3723 		 * backend
3724 		 */
3725 		pqsignal(SIGCHLD, SIG_DFL); /* system() requires this on some
3726 									 * platforms */
3727 	}
3728 
3729 	pqinitmask();
3730 
3731 	if (IsUnderPostmaster)
3732 	{
3733 		/* We allow SIGQUIT (quickdie) at all times */
3734 		sigdelset(&BlockSig, SIGQUIT);
3735 	}
3736 
3737 	PG_SETMASK(&BlockSig);		/* block everything except SIGQUIT */
3738 
3739 	if (!IsUnderPostmaster)
3740 	{
3741 		/*
3742 		 * Validate we have been given a reasonable-looking DataDir (if under
3743 		 * postmaster, assume postmaster did this already).
3744 		 */
3745 		Assert(DataDir);
3746 		ValidatePgVersion(DataDir);
3747 
3748 		/* Change into DataDir (if under postmaster, was done already) */
3749 		ChangeToDataDir();
3750 
3751 		/*
3752 		 * Create lockfile for data directory.
3753 		 */
3754 		CreateDataDirLockFile(false);
3755 
3756 		/* Initialize MaxBackends (if under postmaster, was done already) */
3757 		InitializeMaxBackends();
3758 	}
3759 
3760 	/* Early initialization */
3761 	BaseInit();
3762 
3763 	/*
3764 	 * Create a per-backend PGPROC struct in shared memory, except in the
3765 	 * EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
3766 	 * this before we can use LWLocks (and in the EXEC_BACKEND case we already
3767 	 * had to do some stuff with LWLocks).
3768 	 */
3769 #ifdef EXEC_BACKEND
3770 	if (!IsUnderPostmaster)
3771 		InitProcess();
3772 #else
3773 	InitProcess();
3774 #endif
3775 
3776 	/* We need to allow SIGINT, etc during the initial transaction */
3777 	PG_SETMASK(&UnBlockSig);
3778 
3779 	/*
3780 	 * General initialization.
3781 	 *
3782 	 * NOTE: if you are tempted to add code in this vicinity, consider putting
3783 	 * it inside InitPostgres() instead.  In particular, anything that
3784 	 * involves database access should be there, not here.
3785 	 */
3786 	InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
3787 
3788 	/*
3789 	 * If the PostmasterContext is still around, recycle the space; we don't
3790 	 * need it anymore after InitPostgres completes.  Note this does not trash
3791 	 * *MyProcPort, because ConnCreate() allocated that space with malloc()
3792 	 * ... else we'd need to copy the Port data first.  Also, subsidiary data
3793 	 * such as the username isn't lost either; see ProcessStartupPacket().
3794 	 */
3795 	if (PostmasterContext)
3796 	{
3797 		MemoryContextDelete(PostmasterContext);
3798 		PostmasterContext = NULL;
3799 	}
3800 
3801 	SetProcessingMode(NormalProcessing);
3802 
3803 	/*
3804 	 * Now all GUC states are fully set up.  Report them to client if
3805 	 * appropriate.
3806 	 */
3807 	BeginReportingGUCOptions();
3808 
3809 	/*
3810 	 * Also set up handler to log session end; we have to wait till now to be
3811 	 * sure Log_disconnections has its final value.
3812 	 */
3813 	if (IsUnderPostmaster && Log_disconnections)
3814 		on_proc_exit(log_disconnections, 0);
3815 
3816 	/* Perform initialization specific to a WAL sender process. */
3817 	if (am_walsender)
3818 		InitWalSender();
3819 
3820 	/*
3821 	 * process any libraries that should be preloaded at backend start (this
3822 	 * likewise can't be done until GUC settings are complete)
3823 	 */
3824 	process_session_preload_libraries();
3825 
3826 	/*
3827 	 * Send this backend's cancellation info to the frontend.
3828 	 */
3829 	if (whereToSendOutput == DestRemote)
3830 	{
3831 		StringInfoData buf;
3832 
3833 		pq_beginmessage(&buf, 'K');
3834 		pq_sendint(&buf, (int32) MyProcPid, sizeof(int32));
3835 		pq_sendint(&buf, (int32) MyCancelKey, sizeof(int32));
3836 		pq_endmessage(&buf);
3837 		/* Need not flush since ReadyForQuery will do it. */
3838 	}
3839 
3840 	/* Welcome banner for standalone case */
3841 	if (whereToSendOutput == DestDebug)
3842 		printf("\nPostgreSQL stand-alone backend %s\n", PG_VERSION);
3843 
3844 	/*
3845 	 * Create the memory context we will use in the main loop.
3846 	 *
3847 	 * MessageContext is reset once per iteration of the main loop, ie, upon
3848 	 * completion of processing of each command message from the client.
3849 	 */
3850 	MessageContext = AllocSetContextCreate(TopMemoryContext,
3851 										   "MessageContext",
3852 										   ALLOCSET_DEFAULT_SIZES);
3853 
3854 	/*
3855 	 * Remember stand-alone backend startup time
3856 	 */
3857 	if (!IsUnderPostmaster)
3858 		PgStartTime = GetCurrentTimestamp();
3859 
3860 	/*
3861 	 * POSTGRES main processing loop begins here
3862 	 *
3863 	 * If an exception is encountered, processing resumes here so we abort the
3864 	 * current transaction and start a new one.
3865 	 *
3866 	 * You might wonder why this isn't coded as an infinite loop around a
3867 	 * PG_TRY construct.  The reason is that this is the bottom of the
3868 	 * exception stack, and so with PG_TRY there would be no exception handler
3869 	 * in force at all during the CATCH part.  By leaving the outermost setjmp
3870 	 * always active, we have at least some chance of recovering from an error
3871 	 * during error recovery.  (If we get into an infinite loop thereby, it
3872 	 * will soon be stopped by overflow of elog.c's internal state stack.)
3873 	 *
3874 	 * Note that we use sigsetjmp(..., 1), so that this function's signal mask
3875 	 * (to wit, UnBlockSig) will be restored when longjmp'ing to here.  This
3876 	 * is essential in case we longjmp'd out of a signal handler on a platform
3877 	 * where that leaves the signal blocked.  It's not redundant with the
3878 	 * unblock in AbortTransaction() because the latter is only called if we
3879 	 * were inside a transaction.
3880 	 */
3881 
3882 	if (sigsetjmp(local_sigjmp_buf, 1) != 0)
3883 	{
3884 		/*
3885 		 * NOTE: if you are tempted to add more code in this if-block,
3886 		 * consider the high probability that it should be in
3887 		 * AbortTransaction() instead.  The only stuff done directly here
3888 		 * should be stuff that is guaranteed to apply *only* for outer-level
3889 		 * error recovery, such as adjusting the FE/BE protocol status.
3890 		 */
3891 
3892 		/* Since not using PG_TRY, must reset error stack by hand */
3893 		error_context_stack = NULL;
3894 
3895 		/* Prevent interrupts while cleaning up */
3896 		HOLD_INTERRUPTS();
3897 
3898 		/*
3899 		 * Forget any pending QueryCancel request, since we're returning to
3900 		 * the idle loop anyway, and cancel any active timeout requests.  (In
3901 		 * future we might want to allow some timeout requests to survive, but
3902 		 * at minimum it'd be necessary to do reschedule_timeouts(), in case
3903 		 * we got here because of a query cancel interrupting the SIGALRM
3904 		 * interrupt handler.)	Note in particular that we must clear the
3905 		 * statement and lock timeout indicators, to prevent any future plain
3906 		 * query cancels from being misreported as timeouts in case we're
3907 		 * forgetting a timeout cancel.
3908 		 */
3909 		disable_all_timeouts(false);
3910 		QueryCancelPending = false; /* second to avoid race condition */
3911 
3912 		/* Not reading from the client anymore. */
3913 		DoingCommandRead = false;
3914 
3915 		/* Make sure libpq is in a good state */
3916 		pq_comm_reset();
3917 
3918 		/* Report the error to the client and/or server log */
3919 		EmitErrorReport();
3920 
3921 		/*
3922 		 * Make sure debug_query_string gets reset before we possibly clobber
3923 		 * the storage it points at.
3924 		 */
3925 		debug_query_string = NULL;
3926 
3927 		/*
3928 		 * Abort the current transaction in order to recover.
3929 		 */
3930 		AbortCurrentTransaction();
3931 
3932 		if (am_walsender)
3933 			WalSndErrorCleanup();
3934 
3935 		/*
3936 		 * We can't release replication slots inside AbortTransaction() as we
3937 		 * need to be able to start and abort transactions while having a slot
3938 		 * acquired. But we never need to hold them across top level errors,
3939 		 * so releasing here is fine. There's another cleanup in ProcKill()
3940 		 * ensuring we'll correctly cleanup on FATAL errors as well.
3941 		 */
3942 		if (MyReplicationSlot != NULL)
3943 			ReplicationSlotRelease();
3944 
3945 		/* We also want to cleanup temporary slots on error. */
3946 		ReplicationSlotCleanup();
3947 
3948 		/*
3949 		 * Now return to normal top-level context and clear ErrorContext for
3950 		 * next time.
3951 		 */
3952 		MemoryContextSwitchTo(TopMemoryContext);
3953 		FlushErrorState();
3954 
3955 		/*
3956 		 * If we were handling an extended-query-protocol message, initiate
3957 		 * skip till next Sync.  This also causes us not to issue
3958 		 * ReadyForQuery (until we get Sync).
3959 		 */
3960 		if (doing_extended_query_message)
3961 			ignore_till_sync = true;
3962 
3963 		/* We don't have a transaction command open anymore */
3964 		xact_started = false;
3965 
3966 		/*
3967 		 * If an error occurred while we were reading a message from the
3968 		 * client, we have potentially lost track of where the previous
3969 		 * message ends and the next one begins.  Even though we have
3970 		 * otherwise recovered from the error, we cannot safely read any more
3971 		 * messages from the client, so there isn't much we can do with the
3972 		 * connection anymore.
3973 		 */
3974 		if (pq_is_reading_msg())
3975 			ereport(FATAL,
3976 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
3977 					 errmsg("terminating connection because protocol synchronization was lost")));
3978 
3979 		/* Now we can allow interrupts again */
3980 		RESUME_INTERRUPTS();
3981 	}
3982 
3983 	/* We can now handle ereport(ERROR) */
3984 	PG_exception_stack = &local_sigjmp_buf;
3985 
3986 	if (!ignore_till_sync)
3987 		send_ready_for_query = true;	/* initially, or after error */
3988 
3989 	/*
3990 	 * Non-error queries loop here.
3991 	 */
3992 
3993 	for (;;)
3994 	{
3995 		/*
3996 		 * At top of loop, reset extended-query-message flag, so that any
3997 		 * errors encountered in "idle" state don't provoke skip.
3998 		 */
3999 		doing_extended_query_message = false;
4000 
4001 		/*
4002 		 * Release storage left over from prior query cycle, and create a new
4003 		 * query input buffer in the cleared MessageContext.
4004 		 */
4005 		MemoryContextSwitchTo(MessageContext);
4006 		MemoryContextResetAndDeleteChildren(MessageContext);
4007 
4008 		initStringInfo(&input_message);
4009 
4010 		/*
4011 		 * Also consider releasing our catalog snapshot if any, so that it's
4012 		 * not preventing advance of global xmin while we wait for the client.
4013 		 */
4014 		InvalidateCatalogSnapshotConditionally();
4015 
4016 		/*
4017 		 * (1) If we've reached idle state, tell the frontend we're ready for
4018 		 * a new query.
4019 		 *
4020 		 * Note: this includes fflush()'ing the last of the prior output.
4021 		 *
4022 		 * This is also a good time to send collected statistics to the
4023 		 * collector, and to update the PS stats display.  We avoid doing
4024 		 * those every time through the message loop because it'd slow down
4025 		 * processing of batched messages, and because we don't want to report
4026 		 * uncommitted updates (that confuses autovacuum).  The notification
4027 		 * processor wants a call too, if we are not in a transaction block.
4028 		 */
4029 		if (send_ready_for_query)
4030 		{
4031 			if (IsAbortedTransactionBlockState())
4032 			{
4033 				set_ps_display("idle in transaction (aborted)", false);
4034 				pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL);
4035 
4036 				/* Start the idle-in-transaction timer */
4037 				if (IdleInTransactionSessionTimeout > 0)
4038 				{
4039 					disable_idle_in_transaction_timeout = true;
4040 					enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
4041 										 IdleInTransactionSessionTimeout);
4042 				}
4043 			}
4044 			else if (IsTransactionOrTransactionBlock())
4045 			{
4046 				set_ps_display("idle in transaction", false);
4047 				pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL);
4048 
4049 				/* Start the idle-in-transaction timer */
4050 				if (IdleInTransactionSessionTimeout > 0)
4051 				{
4052 					disable_idle_in_transaction_timeout = true;
4053 					enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
4054 										 IdleInTransactionSessionTimeout);
4055 				}
4056 			}
4057 			else
4058 			{
4059 				/* Send out notify signals and transmit self-notifies */
4060 				ProcessCompletedNotifies();
4061 
4062 				/*
4063 				 * Also process incoming notifies, if any.  This is mostly to
4064 				 * ensure stable behavior in tests: if any notifies were
4065 				 * received during the just-finished transaction, they'll be
4066 				 * seen by the client before ReadyForQuery is.
4067 				 */
4068 				if (notifyInterruptPending)
4069 					ProcessNotifyInterrupt();
4070 
4071 				pgstat_report_stat(false);
4072 
4073 				set_ps_display("idle", false);
4074 				pgstat_report_activity(STATE_IDLE, NULL);
4075 			}
4076 
4077 			ReadyForQuery(whereToSendOutput);
4078 			send_ready_for_query = false;
4079 		}
4080 
4081 		/*
4082 		 * (2) Allow asynchronous signals to be executed immediately if they
4083 		 * come in while we are waiting for client input. (This must be
4084 		 * conditional since we don't want, say, reads on behalf of COPY FROM
4085 		 * STDIN doing the same thing.)
4086 		 */
4087 		DoingCommandRead = true;
4088 
4089 		/*
4090 		 * (3) read a command (loop blocks here)
4091 		 */
4092 		firstchar = ReadCommand(&input_message);
4093 
4094 		/*
4095 		 * (4) turn off the idle-in-transaction timeout, if active.  We do
4096 		 * this before step (5) so that any last-moment timeout is certain to
4097 		 * be detected in step (5).
4098 		 */
4099 		if (disable_idle_in_transaction_timeout)
4100 		{
4101 			disable_timeout(IDLE_IN_TRANSACTION_SESSION_TIMEOUT, false);
4102 			disable_idle_in_transaction_timeout = false;
4103 		}
4104 
4105 		/*
4106 		 * (5) disable async signal conditions again.
4107 		 *
4108 		 * Query cancel is supposed to be a no-op when there is no query in
4109 		 * progress, so if a query cancel arrived while we were idle, just
4110 		 * reset QueryCancelPending. ProcessInterrupts() has that effect when
4111 		 * it's called when DoingCommandRead is set, so check for interrupts
4112 		 * before resetting DoingCommandRead.
4113 		 */
4114 		CHECK_FOR_INTERRUPTS();
4115 		DoingCommandRead = false;
4116 
4117 		/*
4118 		 * (6) check for any other interesting events that happened while we
4119 		 * slept.
4120 		 */
4121 		if (ConfigReloadPending)
4122 		{
4123 			ConfigReloadPending = false;
4124 			ProcessConfigFile(PGC_SIGHUP);
4125 		}
4126 
4127 		/*
4128 		 * (7) process the command.  But ignore it if we're skipping till
4129 		 * Sync.
4130 		 */
4131 		if (ignore_till_sync && firstchar != EOF)
4132 			continue;
4133 
4134 		switch (firstchar)
4135 		{
4136 			case 'Q':			/* simple query */
4137 				{
4138 					const char *query_string;
4139 
4140 					/* Set statement_timestamp() */
4141 					SetCurrentStatementStartTimestamp();
4142 
4143 					query_string = pq_getmsgstring(&input_message);
4144 					pq_getmsgend(&input_message);
4145 
4146 					if (am_walsender)
4147 					{
4148 						if (!exec_replication_command(query_string))
4149 							exec_simple_query(query_string);
4150 					}
4151 					else
4152 						exec_simple_query(query_string);
4153 
4154 					send_ready_for_query = true;
4155 				}
4156 				break;
4157 
4158 			case 'P':			/* parse */
4159 				{
4160 					const char *stmt_name;
4161 					const char *query_string;
4162 					int			numParams;
4163 					Oid		   *paramTypes = NULL;
4164 
4165 					forbidden_in_wal_sender(firstchar);
4166 
4167 					/* Set statement_timestamp() */
4168 					SetCurrentStatementStartTimestamp();
4169 
4170 					stmt_name = pq_getmsgstring(&input_message);
4171 					query_string = pq_getmsgstring(&input_message);
4172 					numParams = pq_getmsgint(&input_message, 2);
4173 					if (numParams > 0)
4174 					{
4175 						int			i;
4176 
4177 						paramTypes = (Oid *) palloc(numParams * sizeof(Oid));
4178 						for (i = 0; i < numParams; i++)
4179 							paramTypes[i] = pq_getmsgint(&input_message, 4);
4180 					}
4181 					pq_getmsgend(&input_message);
4182 
4183 					exec_parse_message(query_string, stmt_name,
4184 									   paramTypes, numParams);
4185 				}
4186 				break;
4187 
4188 			case 'B':			/* bind */
4189 				forbidden_in_wal_sender(firstchar);
4190 
4191 				/* Set statement_timestamp() */
4192 				SetCurrentStatementStartTimestamp();
4193 
4194 				/*
4195 				 * this message is complex enough that it seems best to put
4196 				 * the field extraction out-of-line
4197 				 */
4198 				exec_bind_message(&input_message);
4199 				break;
4200 
4201 			case 'E':			/* execute */
4202 				{
4203 					const char *portal_name;
4204 					int			max_rows;
4205 
4206 					forbidden_in_wal_sender(firstchar);
4207 
4208 					/* Set statement_timestamp() */
4209 					SetCurrentStatementStartTimestamp();
4210 
4211 					portal_name = pq_getmsgstring(&input_message);
4212 					max_rows = pq_getmsgint(&input_message, 4);
4213 					pq_getmsgend(&input_message);
4214 
4215 					exec_execute_message(portal_name, max_rows);
4216 				}
4217 				break;
4218 
4219 			case 'F':			/* fastpath function call */
4220 				forbidden_in_wal_sender(firstchar);
4221 
4222 				/* Set statement_timestamp() */
4223 				SetCurrentStatementStartTimestamp();
4224 
4225 				/* Report query to various monitoring facilities. */
4226 				pgstat_report_activity(STATE_FASTPATH, NULL);
4227 				set_ps_display("<FASTPATH>", false);
4228 
4229 				/* start an xact for this function invocation */
4230 				start_xact_command();
4231 
4232 				/*
4233 				 * Note: we may at this point be inside an aborted
4234 				 * transaction.  We can't throw error for that until we've
4235 				 * finished reading the function-call message, so
4236 				 * HandleFunctionRequest() must check for it after doing so.
4237 				 * Be careful not to do anything that assumes we're inside a
4238 				 * valid transaction here.
4239 				 */
4240 
4241 				/* switch back to message context */
4242 				MemoryContextSwitchTo(MessageContext);
4243 
4244 				HandleFunctionRequest(&input_message);
4245 
4246 				/* commit the function-invocation transaction */
4247 				finish_xact_command();
4248 
4249 				send_ready_for_query = true;
4250 				break;
4251 
4252 			case 'C':			/* close */
4253 				{
4254 					int			close_type;
4255 					const char *close_target;
4256 
4257 					forbidden_in_wal_sender(firstchar);
4258 
4259 					close_type = pq_getmsgbyte(&input_message);
4260 					close_target = pq_getmsgstring(&input_message);
4261 					pq_getmsgend(&input_message);
4262 
4263 					switch (close_type)
4264 					{
4265 						case 'S':
4266 							if (close_target[0] != '\0')
4267 								DropPreparedStatement(close_target, false);
4268 							else
4269 							{
4270 								/* special-case the unnamed statement */
4271 								drop_unnamed_stmt();
4272 							}
4273 							break;
4274 						case 'P':
4275 							{
4276 								Portal		portal;
4277 
4278 								portal = GetPortalByName(close_target);
4279 								if (PortalIsValid(portal))
4280 									PortalDrop(portal, false);
4281 							}
4282 							break;
4283 						default:
4284 							ereport(ERROR,
4285 									(errcode(ERRCODE_PROTOCOL_VIOLATION),
4286 									 errmsg("invalid CLOSE message subtype %d",
4287 											close_type)));
4288 							break;
4289 					}
4290 
4291 					if (whereToSendOutput == DestRemote)
4292 						pq_putemptymessage('3');	/* CloseComplete */
4293 				}
4294 				break;
4295 
4296 			case 'D':			/* describe */
4297 				{
4298 					int			describe_type;
4299 					const char *describe_target;
4300 
4301 					forbidden_in_wal_sender(firstchar);
4302 
4303 					/* Set statement_timestamp() (needed for xact) */
4304 					SetCurrentStatementStartTimestamp();
4305 
4306 					describe_type = pq_getmsgbyte(&input_message);
4307 					describe_target = pq_getmsgstring(&input_message);
4308 					pq_getmsgend(&input_message);
4309 
4310 					switch (describe_type)
4311 					{
4312 						case 'S':
4313 							exec_describe_statement_message(describe_target);
4314 							break;
4315 						case 'P':
4316 							exec_describe_portal_message(describe_target);
4317 							break;
4318 						default:
4319 							ereport(ERROR,
4320 									(errcode(ERRCODE_PROTOCOL_VIOLATION),
4321 									 errmsg("invalid DESCRIBE message subtype %d",
4322 											describe_type)));
4323 							break;
4324 					}
4325 				}
4326 				break;
4327 
4328 			case 'H':			/* flush */
4329 				pq_getmsgend(&input_message);
4330 				if (whereToSendOutput == DestRemote)
4331 					pq_flush();
4332 				break;
4333 
4334 			case 'S':			/* sync */
4335 				pq_getmsgend(&input_message);
4336 				finish_xact_command();
4337 				send_ready_for_query = true;
4338 				break;
4339 
4340 				/*
4341 				 * 'X' means that the frontend is closing down the socket. EOF
4342 				 * means unexpected loss of frontend connection. Either way,
4343 				 * perform normal shutdown.
4344 				 */
4345 			case 'X':
4346 			case EOF:
4347 
4348 				/*
4349 				 * Reset whereToSendOutput to prevent ereport from attempting
4350 				 * to send any more messages to client.
4351 				 */
4352 				if (whereToSendOutput == DestRemote)
4353 					whereToSendOutput = DestNone;
4354 
4355 				/*
4356 				 * NOTE: if you are tempted to add more code here, DON'T!
4357 				 * Whatever you had in mind to do should be set up as an
4358 				 * on_proc_exit or on_shmem_exit callback, instead. Otherwise
4359 				 * it will fail to be called during other backend-shutdown
4360 				 * scenarios.
4361 				 */
4362 				proc_exit(0);
4363 
4364 			case 'd':			/* copy data */
4365 			case 'c':			/* copy done */
4366 			case 'f':			/* copy fail */
4367 
4368 				/*
4369 				 * Accept but ignore these messages, per protocol spec; we
4370 				 * probably got here because a COPY failed, and the frontend
4371 				 * is still sending data.
4372 				 */
4373 				break;
4374 
4375 			default:
4376 				ereport(FATAL,
4377 						(errcode(ERRCODE_PROTOCOL_VIOLATION),
4378 						 errmsg("invalid frontend message type %d",
4379 								firstchar)));
4380 		}
4381 	}							/* end of input-reading loop */
4382 }
4383 
4384 /*
4385  * Throw an error if we're a WAL sender process.
4386  *
4387  * This is used to forbid anything else than simple query protocol messages
4388  * in a WAL sender process.  'firstchar' specifies what kind of a forbidden
4389  * message was received, and is used to construct the error message.
4390  */
4391 static void
forbidden_in_wal_sender(char firstchar)4392 forbidden_in_wal_sender(char firstchar)
4393 {
4394 	if (am_walsender)
4395 	{
4396 		if (firstchar == 'F')
4397 			ereport(ERROR,
4398 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
4399 					 errmsg("fastpath function calls not supported in a replication connection")));
4400 		else
4401 			ereport(ERROR,
4402 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
4403 					 errmsg("extended query protocol not supported in a replication connection")));
4404 	}
4405 }
4406 
4407 
4408 /*
4409  * Obtain platform stack depth limit (in bytes)
4410  *
4411  * Return -1 if unknown
4412  */
4413 long
get_stack_depth_rlimit(void)4414 get_stack_depth_rlimit(void)
4415 {
4416 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_STACK)
4417 	static long val = 0;
4418 
4419 	/* This won't change after process launch, so check just once */
4420 	if (val == 0)
4421 	{
4422 		struct rlimit rlim;
4423 
4424 		if (getrlimit(RLIMIT_STACK, &rlim) < 0)
4425 			val = -1;
4426 		else if (rlim.rlim_cur == RLIM_INFINITY)
4427 			val = LONG_MAX;
4428 		/* rlim_cur is probably of an unsigned type, so check for overflow */
4429 		else if (rlim.rlim_cur >= LONG_MAX)
4430 			val = LONG_MAX;
4431 		else
4432 			val = rlim.rlim_cur;
4433 	}
4434 	return val;
4435 #else							/* no getrlimit */
4436 #if defined(WIN32) || defined(__CYGWIN__)
4437 	/* On Windows we set the backend stack size in src/backend/Makefile */
4438 	return WIN32_STACK_RLIMIT;
4439 #else							/* not windows ... give up */
4440 	return -1;
4441 #endif
4442 #endif
4443 }
4444 
4445 
4446 static struct rusage Save_r;
4447 static struct timeval Save_t;
4448 
4449 void
ResetUsage(void)4450 ResetUsage(void)
4451 {
4452 	getrusage(RUSAGE_SELF, &Save_r);
4453 	gettimeofday(&Save_t, NULL);
4454 }
4455 
4456 void
ShowUsage(const char * title)4457 ShowUsage(const char *title)
4458 {
4459 	StringInfoData str;
4460 	struct timeval user,
4461 				sys;
4462 	struct timeval elapse_t;
4463 	struct rusage r;
4464 
4465 	getrusage(RUSAGE_SELF, &r);
4466 	gettimeofday(&elapse_t, NULL);
4467 	memcpy((char *) &user, (char *) &r.ru_utime, sizeof(user));
4468 	memcpy((char *) &sys, (char *) &r.ru_stime, sizeof(sys));
4469 	if (elapse_t.tv_usec < Save_t.tv_usec)
4470 	{
4471 		elapse_t.tv_sec--;
4472 		elapse_t.tv_usec += 1000000;
4473 	}
4474 	if (r.ru_utime.tv_usec < Save_r.ru_utime.tv_usec)
4475 	{
4476 		r.ru_utime.tv_sec--;
4477 		r.ru_utime.tv_usec += 1000000;
4478 	}
4479 	if (r.ru_stime.tv_usec < Save_r.ru_stime.tv_usec)
4480 	{
4481 		r.ru_stime.tv_sec--;
4482 		r.ru_stime.tv_usec += 1000000;
4483 	}
4484 
4485 	/*
4486 	 * the only stats we don't show here are for memory usage -- i can't
4487 	 * figure out how to interpret the relevant fields in the rusage struct,
4488 	 * and they change names across o/s platforms, anyway. if you can figure
4489 	 * out what the entries mean, you can somehow extract resident set size,
4490 	 * shared text size, and unshared data and stack sizes.
4491 	 */
4492 	initStringInfo(&str);
4493 
4494 	appendStringInfoString(&str, "! system usage stats:\n");
4495 	appendStringInfo(&str,
4496 					 "!\t%ld.%06ld s user, %ld.%06ld s system, %ld.%06ld s elapsed\n",
4497 					 (long) (r.ru_utime.tv_sec - Save_r.ru_utime.tv_sec),
4498 					 (long) (r.ru_utime.tv_usec - Save_r.ru_utime.tv_usec),
4499 					 (long) (r.ru_stime.tv_sec - Save_r.ru_stime.tv_sec),
4500 					 (long) (r.ru_stime.tv_usec - Save_r.ru_stime.tv_usec),
4501 					 (long) (elapse_t.tv_sec - Save_t.tv_sec),
4502 					 (long) (elapse_t.tv_usec - Save_t.tv_usec));
4503 	appendStringInfo(&str,
4504 					 "!\t[%ld.%06ld s user, %ld.%06ld s system total]\n",
4505 					 (long) user.tv_sec,
4506 					 (long) user.tv_usec,
4507 					 (long) sys.tv_sec,
4508 					 (long) sys.tv_usec);
4509 #if defined(HAVE_GETRUSAGE)
4510 	appendStringInfo(&str,
4511 					 "!\t%ld/%ld [%ld/%ld] filesystem blocks in/out\n",
4512 					 r.ru_inblock - Save_r.ru_inblock,
4513 	/* they only drink coffee at dec */
4514 					 r.ru_oublock - Save_r.ru_oublock,
4515 					 r.ru_inblock, r.ru_oublock);
4516 	appendStringInfo(&str,
4517 					 "!\t%ld/%ld [%ld/%ld] page faults/reclaims, %ld [%ld] swaps\n",
4518 					 r.ru_majflt - Save_r.ru_majflt,
4519 					 r.ru_minflt - Save_r.ru_minflt,
4520 					 r.ru_majflt, r.ru_minflt,
4521 					 r.ru_nswap - Save_r.ru_nswap,
4522 					 r.ru_nswap);
4523 	appendStringInfo(&str,
4524 					 "!\t%ld [%ld] signals rcvd, %ld/%ld [%ld/%ld] messages rcvd/sent\n",
4525 					 r.ru_nsignals - Save_r.ru_nsignals,
4526 					 r.ru_nsignals,
4527 					 r.ru_msgrcv - Save_r.ru_msgrcv,
4528 					 r.ru_msgsnd - Save_r.ru_msgsnd,
4529 					 r.ru_msgrcv, r.ru_msgsnd);
4530 	appendStringInfo(&str,
4531 					 "!\t%ld/%ld [%ld/%ld] voluntary/involuntary context switches\n",
4532 					 r.ru_nvcsw - Save_r.ru_nvcsw,
4533 					 r.ru_nivcsw - Save_r.ru_nivcsw,
4534 					 r.ru_nvcsw, r.ru_nivcsw);
4535 #endif							/* HAVE_GETRUSAGE */
4536 
4537 	/* remove trailing newline */
4538 	if (str.data[str.len - 1] == '\n')
4539 		str.data[--str.len] = '\0';
4540 
4541 	ereport(LOG,
4542 			(errmsg_internal("%s", title),
4543 			 errdetail_internal("%s", str.data)));
4544 
4545 	pfree(str.data);
4546 }
4547 
4548 /*
4549  * on_proc_exit handler to log end of session
4550  */
4551 static void
log_disconnections(int code,Datum arg)4552 log_disconnections(int code, Datum arg)
4553 {
4554 	Port	   *port = MyProcPort;
4555 	long		secs;
4556 	int			usecs;
4557 	int			msecs;
4558 	int			hours,
4559 				minutes,
4560 				seconds;
4561 
4562 	TimestampDifference(port->SessionStartTime,
4563 						GetCurrentTimestamp(),
4564 						&secs, &usecs);
4565 	msecs = usecs / 1000;
4566 
4567 	hours = secs / SECS_PER_HOUR;
4568 	secs %= SECS_PER_HOUR;
4569 	minutes = secs / SECS_PER_MINUTE;
4570 	seconds = secs % SECS_PER_MINUTE;
4571 
4572 	ereport(LOG,
4573 			(errmsg("disconnection: session time: %d:%02d:%02d.%03d "
4574 					"user=%s database=%s host=%s%s%s",
4575 					hours, minutes, seconds, msecs,
4576 					port->user_name, port->database_name, port->remote_host,
4577 					port->remote_port[0] ? " port=" : "", port->remote_port)));
4578 }
4579