1 /* -*-pgsql-c-*- */
2 /*
3  *
4  * pgpool: a language independent connection pool server for PostgreSQL
5  * written by Tatsuo Ishii
6  *
7  * Copyright (c) 2003-2019	PgPool Global Development Group
8  *
9  * Permission to use, copy, modify, and distribute this software and
10  * its documentation for any purpose and without fee is hereby
11  * granted, provided that the above copyright notice appear in all
12  * copies and that both that copyright notice and this permission
13  * notice appear in supporting documentation, and that the name of the
14  * author not be used in advertising or publicity pertaining to
15  * distribution of the software without specific, written prior
16  * permission. The author makes no representations about the
17  * suitability of this software for any purpose.  It is provided "as
18  * is" without express or implied warranty.
19  *
20  * pool.h.: master definition header file
21  *
22  */
23 
24 #ifndef POOL_H
25 #define POOL_H
26 
27 #include "config.h"
28 #include "pool_type.h"
29 #include "pcp/libpcp_ext.h"
30 #include "utils/pool_signal.h"
31 #include "auth/pool_passwd.h"
32 #include "parser/nodes.h"
33 #include <stdio.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <limits.h>
38 
39 #ifdef USE_SSL
40 #include <openssl/crypto.h>
41 #include <openssl/ssl.h>
42 #include <openssl/err.h>
43 #endif
44 
45 #include <syslog.h>
46 
47 /* undef this if you have problems with non blocking accept() */
48 #define NONE_BLOCK
49 
50 #define POOLMAXPATHLEN 8192
51 
52 #define POOLKEYFILE 	".pgpoolkey"
53 #define POOLKEYFILEENV "PGPOOLKEYFILE"
54 
55 /*
56  * Brought from PostgreSQL's pg_config_manual.h.
57  *
58  * Maximum length for identifiers (e.g. table names, column names,
59  * function names).  Names actually are limited to one less byte than this,
60  * because the length must include a trailing zero byte.
61  *
62  * Please note that in version 2 protocol, maximum user name length is
63  * SM_USER, which is 32.
64  */
65 #define NAMEDATALEN 64
66 
67 /* configuration file name */
68 #define POOL_CONF_FILE_NAME "pgpool.conf"
69 
70 /* PCP user/password file name */
71 #define PCP_PASSWD_FILE_NAME "pcp.conf"
72 
73 /* HBA configuration file name */
74 #define HBA_CONF_FILE_NAME "pool_hba.conf"
75 
76 /* pid file directory */
77 #define DEFAULT_LOGDIR "/tmp"
78 
79 /* Unix domain socket directory */
80 #define DEFAULT_SOCKET_DIR "/tmp"
81 
82 /* Unix domain socket directory for watchdog IPC */
83 #define DEFAULT_WD_IPC_SOCKET_DIR "/tmp"
84 
85 /* pid file name */
86 #define DEFAULT_PID_FILE_NAME "/var/run/pgpool/pgpool.pid"
87 
88 /* status file name */
89 #define STATUS_FILE_NAME "pgpool_status"
90 
91 /* default string used to identify pgpool on syslog output */
92 #define DEFAULT_SYSLOG_IDENT "pgpool"
93 
94 /* function return codes */
95 #define GENERAL_ERROR		(-1)
96 #define RETRY				(-2)
97 #define OPERATION_TIMEOUT	(-3)
98 
99 
100 typedef enum
101 {
102 	POOL_CONTINUE = 0,
103 	POOL_IDLE,
104 	POOL_END,
105 	POOL_ERROR,
106 	POOL_FATAL,
107 	POOL_DEADLOCK
108 }			POOL_STATUS;
109 
110 typedef enum
111 {
112 	POOL_SOCKET_CLOSED = 0,
113 	POOL_SOCKET_VALID,
114 	POOL_SOCKET_ERROR,
115 	POOL_SOCKET_EOF
116 }			POOL_SOCKET_STATE;
117 
118 /* protocol major version numbers */
119 #define PROTO_MAJOR_V2	2
120 #define PROTO_MAJOR_V3	3
121 
122 /* Cancel packet proto major */
123 #define PROTO_CANCEL	80877102
124 
125 /*
126  * In protocol 3.0 and later, the startup packet length is not fixed, but
127  * we set an arbitrary limit on it anyway.	This is just to prevent simple
128  * denial-of-service attacks via sending enough data to run the server
129  * out of memory.
130  */
131 #define MAX_STARTUP_PACKET_LENGTH 10000
132 
133 
134 typedef struct StartupPacket_v2
135 {
136 	int			protoVersion;	/* Protocol version */
137 	char		database[SM_DATABASE];	/* Database name */
138 	char		user[SM_USER];	/* User name */
139 	char		options[SM_OPTIONS];	/* Optional additional args */
140 	char		unused[SM_UNUSED];	/* Unused */
141 	char		tty[SM_TTY];	/* Tty for debug output */
142 }			StartupPacket_v2;
143 
144 /* startup packet info */
145 typedef struct
146 {
147 	char	   *startup_packet; /* raw startup packet without packet length
148 								 * (malloced area) */
149 	int			len;			/* raw startup packet length */
150 	int			major;			/* protocol major version */
151 	int			minor;			/* protocol minor version */
152 	char	   *database;		/* database name in startup_packet (malloced
153 								 * area) */
154 	char	   *user;			/* user name in startup_packet (malloced area) */
155 	char	   *application_name;	/* not malloced. pointing to in
156 									 * startup_packet */
157 } StartupPacket;
158 
159 typedef struct CancelPacket
160 {
161 	int			protoVersion;	/* Protocol version */
162 	int			pid;			/* backend process id */
163 	int			key;			/* cancel key */
164 }			CancelPacket;
165 
166 #define MAX_PASSWORD_SIZE		1024
167 
168 typedef struct
169 {
170 	int			num;			/* number of entries */
171 	char	  **names;			/* parameter names */
172 	char	  **values;			/* values */
173 }			ParamStatus;
174 
175 /*
176  * HbaLines is declared in pool_hba.h
177  * we use forward declaration here
178  */
179 typedef struct HbaLine HbaLine;
180 
181 
182 #ifdef USE_SSL
183 /*
184  *	Hardcoded DH parameters, used in ephemeral DH keying.
185  *
186  *	If you want to create your own hardcoded DH parameters
187  *	for fun and profit, review "Assigned Number for SKIP
188  *	Protocols" (http://www.skip-vpn.org/spec/numbers.html)
189  *	for suggestions.
190  */
191 #define FILE_DH2048 \
192 "-----BEGIN DH PARAMETERS-----\n\
193 MIIBCAKCAQEA9kJXtwh/CBdyorrWqULzBej5UxE5T7bxbrlLOCDaAadWoxTpj0BV\n\
194 89AHxstDqZSt90xkhkn4DIO9ZekX1KHTUPj1WV/cdlJPPT2N286Z4VeSWc39uK50\n\
195 T8X8dryDxUcwYc58yWb/Ffm7/ZFexwGq01uejaClcjrUGvC/RgBYK+X0iP1YTknb\n\
196 zSC0neSRBzZrM2w4DUUdD3yIsxx8Wy2O9vPJI8BD8KVbGI2Ou1WMuF040zT9fBdX\n\
197 Q6MdGGzeMyEstSr/POGxKUAYEY18hKcKctaGxAMZyAcpesqVDNmWn6vQClCbAkbT\n\
198 CD1mpF1Bn5x8vYlLIhkmuquiXsNV6TILOwIBAg==\n\
199 -----END DH PARAMETERS-----\n"
200 #endif
201 
202 /*
203  * Macro that allows to cast constness away from an expression, but doesn't
204  * allow changing the underlying type.  Enforcement of the latter
205  * currently only works for gcc like compilers.
206  *
207  * Please note IT IS NOT SAFE to cast constness away if the result will ever
208  * be modified (it would be undefined behaviour). Doing so anyway can cause
209  * compiler misoptimizations or runtime crashes (modifying readonly memory).
210  * It is only safe to use when the the result will not be modified, but API
211  * design or language restrictions prevent you from declaring that
212  * (e.g. because a function returns both const and non-const variables).
213  *
214  * Note that this only works in function scope, not for global variables (it'd
215  * be nice, but not trivial, to improve that).
216  */
217 #if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
218 #define unconstify(underlying_type, expr) \
219 	(StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
220 					  "wrong cast"), \
221 	  (underlying_type) (expr))
222 #else
223 #define unconstify(underlying_type, expr) \
224 	((underlying_type) (expr))
225 #endif
226 
227 /*
228  * stream connection structure
229  */
230 typedef struct
231 {
232 	int			fd;				/* fd for connection */
233 
234 	char	   *wbuf;			/* write buffer for the connection */
235 	int			wbufsz;			/* write buffer size */
236 	int			wbufpo;			/* buffer offset */
237 
238 #ifdef USE_SSL
239 	SSL_CTX    *ssl_ctx;		/* SSL connection context */
240 	SSL		   *ssl;			/* SSL connection */
241 	X509	   *peer;
242 	char	   *cert_cn;		/* common in the ssl certificate presented by
243 								 * frontend connection Used for cert
244 								 * authentication */
245 	bool		client_cert_loaded;
246 
247 #endif
248 	int			ssl_active;		/* SSL is failed if < 0, off if 0, on if > 0 */
249 
250 	char	   *hp;				/* pending data buffer head address */
251 	int			po;				/* pending data offset */
252 	int			bufsz;			/* pending data buffer size */
253 	int			len;			/* pending data length */
254 
255 	char	   *sbuf;			/* buffer for pool_read_string */
256 	int			sbufsz;			/* its size in bytes */
257 
258 	char	   *buf2;			/* buffer for pool_read2 */
259 	int			bufsz2;			/* its size in bytes */
260 
261 	char	   *buf3;			/* buffer for pool_push/pop */
262 	int			bufsz3;			/* its size in bytes */
263 
264 	int			isbackend;		/* this connection is for backend if non 0 */
265 	int			db_node_id;		/* DB node id for this connection */
266 
267 	char		tstate;			/* Transaction state (V3 only) 'I' if idle
268 								 * (not in a transaction block); 'T' if in a
269 								 * transaction block; or 'E' if in a failed
270 								 * transaction block */
271 
272 	/* True if an internal transaction has already started */
273 	bool		is_internal_transaction_started;
274 
275 	/*
276 	 * following are used to remember when re-use the authenticated connection
277 	 */
278 	int			auth_kind;		/* 3: clear text password, 4: crypt password,
279 								 * 5: md5 password */
280 	int			pwd_size;		/* password (sent back from frontend) size in
281 								 * host order */
282 	char		password[MAX_PASSWORD_SIZE + 1];	/* password (sent back
283 													 * from frontend) */
284 	char		salt[4];		/* password salt */
285 	PasswordType passwordType;
286 
287 	/*
288 	 * following are used to remember current session parameter status.
289 	 * re-used connection will need them (V3 only)
290 	 */
291 	ParamStatus params;
292 
293 	int			no_forward;		/* if non 0, do not write to frontend */
294 
295 	char		kind;			/* kind cache */
296 
297 	/* true if remote end closed the connection */
298 	POOL_SOCKET_STATE socket_state;
299 
300 	/*
301 	 * frontend info needed for hba
302 	 */
303 	int			protoVersion;
304 	SockAddr	raddr;
305 	HbaLine    *pool_hba;
306 	char	   *database;
307 	char	   *username;
308 	char	   *remote_hostname;
309 	int			remote_hostname_resolv;
310 	bool		frontend_authenticated;
311 	PasswordMapping *passwordMapping;
312 	ConnectionInfo *con_info;	/* shared memory coninfo used for handling the
313 								 * query containing pg_terminate_backend */
314 }			POOL_CONNECTION;
315 
316 /*
317  * connection pool structure
318  */
319 typedef struct
320 {
321 	StartupPacket *sp;			/* startup packet info */
322 	int			pid;			/* backend pid */
323 	int			key;			/* cancel key */
324 	POOL_CONNECTION *con;
325 	time_t		closetime;		/* absolute time in second when the connection
326 								 * closed if 0, that means the connection is
327 								 * under use. */
328 }			POOL_CONNECTION_POOL_SLOT;
329 
330 typedef struct
331 {
332 	ConnectionInfo *info;		/* connection info on shmem */
333 	POOL_CONNECTION_POOL_SLOT *slots[MAX_NUM_BACKENDS];
334 }			POOL_CONNECTION_POOL;
335 
336 /*
337  * for pool_clear_cache() in pool_query_cache.c
338  *
339  * used to specify the time which cached data created before it to be deleted.
340  */
341 typedef enum
342 {
343 	second, seconds,
344 	minute, minutes,
345 	hour, hours,
346 	day, days,
347 	week, weeks,
348 	month, months,
349 	year, years,
350 	decade, decades,
351 	century, centuries,
352 	millennium, millenniums
353 }			UNIT;
354 
355 typedef struct
356 {
357 	int			quantity;
358 	UNIT		unit;
359 } Interval;
360 
361 /* Defined in pool_session_context.h */
362 extern int	pool_get_major_version(void);
363 
364 /* NUM_BACKENDS now always returns actual number of backends */
365 #define NUM_BACKENDS (pool_config->backend_desc->num_backends)
366 #define BACKEND_INFO(backend_id) (pool_config->backend_desc->backend_info[(backend_id)])
367 #define LOAD_BALANCE_STATUS(backend_id) (pool_config->load_balance_status[(backend_id)])
368 
369 /*
370  * This macro returns true if:
371  *   current query is in progress and the DB node is healthy OR
372  *   no query is in progress and the DB node is healthy
373  */
374 extern bool pool_is_node_to_be_sent_in_current_query(int node_id);
375 extern int	pool_virtual_master_db_node_id(void);
376 extern BACKEND_STATUS * my_backend_status[];
377 extern int	my_master_node_id;
378 
379 #define VALID_BACKEND(backend_id) \
380 	((RAW_MODE && (backend_id) == REAL_MASTER_NODE_ID) ||		\
381 	(pool_is_node_to_be_sent_in_current_query((backend_id)) &&	\
382 	 ((*(my_backend_status[(backend_id)]) == CON_UP) ||			\
383 	  (*(my_backend_status[(backend_id)]) == CON_CONNECT_WAIT))))
384 
385 /*
386  * For raw mode failover control
387  */
388 #define VALID_BACKEND_RAW(backend_id) \
389 	((*(my_backend_status[(backend_id)]) == CON_UP) ||			\
390 	 (*(my_backend_status[(backend_id)]) == CON_CONNECT_WAIT))
391 
392 #define CONNECTION_SLOT(p, slot) ((p)->slots[(slot)])
393 #define CONNECTION(p, slot) (CONNECTION_SLOT(p, slot)->con)
394 
395 /*
396  * The first DB node id appears in pgpool.conf or the first "live" DB
397  * node otherwise.
398  */
399 #define REAL_MASTER_NODE_ID (Req_info->master_node_id)
400 
401 /*
402  * The primary node id in streaming replication mode. If not in the
403  * mode or there's no primary node, this macro returns
404  * REAL_MASTER_NODE_ID.
405  */
406 #define PRIMARY_NODE_ID (Req_info->primary_node_id >=0 && VALID_BACKEND_RAW(Req_info->primary_node_id) ? \
407 						 Req_info->primary_node_id:REAL_MASTER_NODE_ID)
408 #define IS_PRIMARY_NODE_ID(node_id)	(node_id == PRIMARY_NODE_ID)
409 
410 /*
411  * Real primary node id. If not in the mode or there's no primary
412  * node, this macro returns -1.
413  */
414 #define REAL_PRIMARY_NODE_ID (Req_info->primary_node_id)
415 
416 /*
417  * "Virtual" master node id. It's same as REAL_MASTER_NODE_ID if not
418  * in load balance mode. If in load balance, it's the first load
419  * balance node.
420  */
421 #define MASTER_NODE_ID (pool_virtual_master_db_node_id())
422 #define IS_MASTER_NODE_ID(node_id) (MASTER_NODE_ID == (node_id))
423 #define MASTER_CONNECTION(p) ((p)->slots[MASTER_NODE_ID])
424 #define MASTER(p) MASTER_CONNECTION(p)->con
425 
426 /*
427  * Backend node status in streaming replication mode.
428  */
429 typedef enum
430 {
431 	POOL_NODE_STATUS_UNUSED,	/* unused */
432 	POOL_NODE_STATUS_PRIMARY,	/* primary ndoe */
433 	POOL_NODE_STATUS_STANDBY,	/* standby node */
434 	POOL_NODE_STATUS_INVALID	/* invalid node (split branin, stand alone) */
435 }			POOL_NODE_STATUS;
436 
437 #define REPLICATION (pool_config->replication_mode)
438 #define MASTER_SLAVE (pool_config->master_slave_mode)
439 #define STREAM (MASTER_SLAVE && pool_config->master_slave_sub_mode == STREAM_MODE)
440 #define LOGICAL (MASTER_SLAVE && pool_config->master_slave_sub_mode == LOGICAL_MODE)
441 #define SLONY (MASTER_SLAVE && pool_config->master_slave_sub_mode == SLONY_MODE)
442 #define DUAL_MODE (REPLICATION || MASTER_SLAVE)
443 #define RAW_MODE (!REPLICATION && !MASTER_SLAVE)
444 #define SL_MODE (STREAM || LOGICAL) /* streaming or logical replication mode */
445 #define MAJOR(p) (pool_get_major_version())
446 #define TSTATE(p, i) (CONNECTION(p, i)->tstate)
447 #define INTERNAL_TRANSACTION_STARTED(p, i) (CONNECTION(p, i)->is_internal_transaction_started)
448 
449 #define Max(x, y)		((x) > (y) ? (x) : (y))
450 #define Min(x, y)		((x) < (y) ? (x) : (y))
451 
452 #define LOCK_COMMENT "/*INSERT LOCK*/"
453 #define LOCK_COMMENT_SZ (sizeof(LOCK_COMMENT)-1)
454 #define NO_LOCK_COMMENT "/*NO INSERT LOCK*/"
455 #define NO_LOCK_COMMENT_SZ (sizeof(NO_LOCK_COMMENT)-1)
456 #define NO_LOAD_BALANCE "/*NO LOAD BALANCE*/"
457 #define NO_LOAD_BALANCE_COMMENT_SZ (sizeof(NO_LOAD_BALANCE)-1)
458 
459 #define MAX_NUM_SEMAPHORES		7
460 #define CONN_COUNTER_SEM		0
461 #define REQUEST_INFO_SEM		1
462 #define SHM_CACHE_SEM			2
463 #define QUERY_CACHE_STATS_SEM	3
464 #define PCP_REQUEST_SEM			4
465 #define ACCEPT_FD_SEM			5
466 #define FOLLOW_PRIMARY_SEM		6
467 #define MAX_REQUEST_QUEUE_SIZE	10
468 
469 #define MAX_SEC_WAIT_FOR_CLUSTER_TRANSATION 10	/* time in seconds to keep
470 												 * retrying for a watchdog
471 												 * command if the cluster is
472 												 * not in stable state */
473 #define MAX_IDENTIFIER_LEN		128
474 
475 #define SERIALIZE_ACCEPT (pool_config->serialize_accept == true && \
476 						  pool_config->child_life_time == 0)
477 
478 /*
479  * number specified when semaphore is locked/unlocked
480  */
481 typedef enum SemNum
482 {
483 	SEMNUM_CONFIG,
484 	SEMNUM_NODES,
485 	SEMNUM_PROCESSES
486 }			SemNum;
487 
488 /*
489  * up/down request info area in shared memory
490  */
491 typedef enum
492 {
493 	NODE_UP_REQUEST = 0,
494 	NODE_DOWN_REQUEST,
495 	NODE_RECOVERY_REQUEST,
496 	CLOSE_IDLE_REQUEST,
497 	PROMOTE_NODE_REQUEST,
498 	NODE_QUARANTINE_REQUEST
499 }			POOL_REQUEST_KIND;
500 
501 #define REQ_DETAIL_SWITCHOVER	0x00000001	/* failover due to switch over */
502 #define REQ_DETAIL_WATCHDOG		0x00000002	/* failover req from watchdog */
503 #define REQ_DETAIL_CONFIRMED	0x00000004	/* failover req that does not
504 											 * require majority vote */
505 #define REQ_DETAIL_UPDATE		0x00000008	/* failover req is just and update
506 											 * node status request */
507 
508 typedef struct
509 {
510 	POOL_REQUEST_KIND kind;		/* request kind */
511 	unsigned char request_details;	/* option flags kind */
512 	int			node_id[MAX_NUM_BACKENDS];	/* request node id */
513 	int			count;			/* request node ids count */
514 }			POOL_REQUEST_NODE;
515 
516 typedef struct
517 {
518 	POOL_REQUEST_NODE request[MAX_REQUEST_QUEUE_SIZE];
519 	int			request_queue_head;
520 	int			request_queue_tail;
521 	int			master_node_id; /* the youngest node id which is not in down
522 								 * status */
523 	int			primary_node_id;	/* the primary node id in streaming
524 									 * replication mode */
525 	int			conn_counter;
526 	bool		switching;		/* it true, failover or failback is in
527 								 * progress */
528 	/* false if follow primary command or detach_false_primary in
529 	 * execution */
530 	bool		follow_primary_count;
531 	bool		follow_primary_ongoing;	/* true if follow primary command is ongoing */
532 }			POOL_REQUEST_INFO;
533 
534 /* description of row. corresponding to RowDescription message */
535 typedef struct
536 {
537 	char	   *attrname;		/* attribute name */
538 	int			oid;			/* 0 or non 0 if it's a table object */
539 	int			attrnumber;		/* attribute number starting with 1. 0 if it's
540 								 * not a table */
541 	int			typeoid;		/* data type oid */
542 	int			size;			/* data length minus means variable data type */
543 	int			mod;			/* data type modifier */
544 }			AttrInfo;
545 
546 typedef struct
547 {
548 	int			num_attrs;		/* number of attributes */
549 	AttrInfo   *attrinfo;
550 }			RowDesc;
551 
552 typedef struct
553 {
554 	RowDesc    *rowdesc;		/* attribute info */
555 	int			numrows;		/* number of rows */
556 	int		   *nullflags;		/* if NULL, -1 or length of the string
557 								 * excluding termination null */
558 	char	  **data;			/* actual row character data terminated with
559 								 * null */
560 }			POOL_SELECT_RESULT;
561 
562 /*
563  * recovery mode
564  */
565 typedef enum
566 {
567 	RECOVERY_INIT = 0,
568 	RECOVERY_ONLINE,
569 	RECOVERY_DETACH,
570 	RECOVERY_PROMOTE
571 }			POOL_RECOVERY_MODE;
572 
573 /*
574  * global variables
575  */
576 extern pid_t mypid;				/* parent pid */
577 
578 typedef enum
579 {
580 	PT_MAIN,
581 	PT_CHILD,
582 	PT_WORKER,
583 	PT_HB_SENDER,
584 	PT_HB_RECEIVER,
585 	PT_WATCHDOG,
586 	PT_LIFECHECK,
587 	PT_FOLLOWCHILD,
588 	PT_WATCHDOG_UTILITY,
589 	PT_PCP,
590 	PT_PCP_WORKER,
591 	PT_HEALTH_CHECK
592 }			ProcessType;
593 
594 extern ProcessType processType;
595 
596 typedef enum
597 {
598 	INITIALIZING,
599 	PERFORMING_HEALTH_CHECK,
600 	SLEEPING,
601 	WAITIG_FOR_CONNECTION,
602 	BACKEND_CONNECTING,
603 	PROCESSING,
604 	EXITING
605 }			ProcessState;
606 
607 #define MAX_PG_VERSION_STRING	512
608 
609 /*
610  * PostgreSQL version descriptor
611  */
612 typedef struct
613 {
614 	short	major;	/* major version number in up to 3 digits decimal.
615 					 * Examples: 120, 110, 100, 96.
616 					 */
617 	short	minor;	/* minor version number in up to 2 digits decimal.
618 					 * Examples: 0, 1, 2, 10, 23.
619 					 */
620 	char	version_string[MAX_PG_VERSION_STRING+1];	/* original version string */
621 }			PGVersion;
622 
623 extern ProcessState processState;
624 
625 extern POOL_CONNECTION_POOL * pool_connection_pool; /* connection pool */
626 extern volatile sig_atomic_t backend_timer_expired; /* flag for connection
627 													 * closed timer is expired */
628 extern volatile sig_atomic_t health_check_timer_expired;	/* non 0 if health check
629 															 * timer expired */
630 extern long int weight_master;	/* normalized weight of master (0-RAND_MAX
631 								 * range) */
632 extern int	my_proc_id;			/* process table id (!= UNIX's PID) */
633 extern ProcessInfo * process_info;	/* shmem process information table */
634 extern ConnectionInfo * con_info;	/* shmem connection info table */
635 extern POOL_REQUEST_INFO * Req_info;
636 extern volatile sig_atomic_t *InRecovery;
637 extern char remote_ps_data[];	/* used for set_ps_display */
638 extern volatile sig_atomic_t got_sighup;
639 extern volatile sig_atomic_t exit_request;
640 extern volatile sig_atomic_t ignore_sigusr1;
641 
642 #define QUERY_STRING_BUFFER_LEN 1024
643 extern char query_string_buffer[];	/* last query string sent to simpleQuery() */
644 
645 extern BACKEND_STATUS private_backend_status[MAX_NUM_BACKENDS];
646 
647 extern char remote_host[];		/* client host */
648 extern char remote_port[];		/* client port */
649 
650 /*
651  * public functions
652  */
653 extern void register_watchdog_quorum_change_interupt(void);
654 extern void register_watchdog_state_change_interupt(void);
655 extern void register_backend_state_sync_req_interupt(void);
656 extern void register_inform_quarantine_nodes_req(void);
657 
658 extern bool register_node_operation_request(POOL_REQUEST_KIND kind, int *node_id_set, int count, unsigned char flags);
659 extern char *get_config_file_name(void);
660 extern char *get_hba_file_name(void);
661 extern void do_child(int *fds);
662 extern void pcp_main(int unix_fd, int inet_fd);
663 extern int	select_load_balancing_node(void);
664 extern int	pool_init_cp(void);
665 extern POOL_STATUS pool_process_query(POOL_CONNECTION * frontend,
666 									  POOL_CONNECTION_POOL * backend,
667 									  int reset_request);
668 
669 extern void connection_do_auth(POOL_CONNECTION_POOL_SLOT * cp, char *password);
670 extern int	pool_do_auth(POOL_CONNECTION * frontend, POOL_CONNECTION_POOL * backend);
671 extern int	pool_do_reauth(POOL_CONNECTION * frontend, POOL_CONNECTION_POOL * cp);
672 extern void authenticate_frontend(POOL_CONNECTION * frontend);
673 
674 extern bool is_backend_cache_empty(POOL_CONNECTION_POOL * backend);
675 
676 /* SSL functionality */
677 extern void pool_ssl_negotiate_serverclient(POOL_CONNECTION * cp);
678 extern void pool_ssl_negotiate_clientserver(POOL_CONNECTION * cp);
679 extern void pool_ssl_close(POOL_CONNECTION * cp);
680 extern int	pool_ssl_read(POOL_CONNECTION * cp, void *buf, int size);
681 extern int	pool_ssl_write(POOL_CONNECTION * cp, const void *buf, int size);
682 extern bool pool_ssl_pending(POOL_CONNECTION * cp);
683 extern int	SSL_ServerSide_init(void);
684 
685 extern POOL_STATUS ErrorResponse(POOL_CONNECTION * frontend,
686 								 POOL_CONNECTION_POOL * backend);
687 
688 extern void NoticeResponse(POOL_CONNECTION * frontend,
689 			   POOL_CONNECTION_POOL * backend);
690 
691 extern void notice_backend_error(int node_id, unsigned char flags);
692 extern bool degenerate_backend_set(int *node_id_set, int count, unsigned char flags);
693 extern bool degenerate_backend_set_ex(int *node_id_set, int count, unsigned char flags, bool error, bool test_only);
694 extern bool promote_backend(int node_id, unsigned char flags);
695 extern bool send_failback_request(int node_id, bool throw_error, unsigned char flags);
696 
697 extern void pool_send_frontend_exits(POOL_CONNECTION_POOL * backend);
698 
699 extern int	pool_read_message_length(POOL_CONNECTION_POOL * cp);
700 extern int *pool_read_message_length2(POOL_CONNECTION_POOL * cp);
701 extern signed char pool_read_kind(POOL_CONNECTION_POOL * cp);
702 extern int	pool_read_int(POOL_CONNECTION_POOL * cp);
703 
704 extern POOL_STATUS SimpleForwardToFrontend(char kind, POOL_CONNECTION * frontend, POOL_CONNECTION_POOL * backend);
705 extern POOL_STATUS SimpleForwardToBackend(char kind, POOL_CONNECTION * frontend, POOL_CONNECTION_POOL * backend, int len, char *contents);
706 extern POOL_STATUS ParameterStatus(POOL_CONNECTION * frontend, POOL_CONNECTION_POOL * backend);
707 
708 extern int	pool_init_params(ParamStatus * params);
709 extern void pool_discard_params(ParamStatus * params);
710 extern char *pool_find_name(ParamStatus * params, char *name, int *pos);
711 extern int	pool_get_param(ParamStatus * params, int index, char **name, char **value);
712 extern int	pool_add_param(ParamStatus * params, char *name, char *value);
713 extern void pool_param_debug_print(ParamStatus * params);
714 
715 extern void pool_send_error_message(POOL_CONNECTION * frontend, int protoMajor,
716 						char *code,
717 						char *message,
718 						char *detail,
719 						char *hint,
720 						char *file,
721 						int line);
722 extern void pool_send_fatal_message(POOL_CONNECTION * frontend, int protoMajor,
723 						char *code,
724 						char *message,
725 						char *detail,
726 						char *hint,
727 						char *file,
728 						int line);
729 extern void pool_send_severity_message(POOL_CONNECTION * frontend, int protoMajor,
730 						   char *code,
731 						   char *message,
732 						   char *detail,
733 						   char *hint,
734 						   char *file,
735 						   char *severity,
736 						   int line);
737 extern void pool_send_readyforquery(POOL_CONNECTION * frontend);
738 extern void send_startup_packet(POOL_CONNECTION_POOL_SLOT * cp);
739 extern void pool_free_startup_packet(StartupPacket *sp);
740 extern void child_exit(int code);
741 
742 extern void init_prepared_list(void);
743 extern void proc_exit(int);
744 
745 extern void *pool_shared_memory_create(size_t size);
746 extern void pool_shmem_exit(int code);
747 
748 extern void pool_semaphore_create(int numSems);
749 extern void pool_semaphore_lock(int semNum);
750 extern int	pool_semaphore_lock_allow_interrupt(int semNum);
751 extern void pool_semaphore_unlock(int semNum);
752 
753 extern BackendInfo * pool_get_node_info(int node_number);
754 extern int	pool_get_node_count(void);
755 extern int *pool_get_process_list(int *array_size);
756 extern ProcessInfo * pool_get_process_info(pid_t pid);
757 extern POOL_STATUS OneNode_do_command(POOL_CONNECTION * frontend, POOL_CONNECTION * backend, char *query, char *database);
758 
759 /* child.c */
760 extern POOL_CONNECTION_POOL_SLOT * make_persistent_db_connection(
761 																 int db_node_id, char *hostname, int port, char *dbname, char *user, char *password, bool retry);
762 extern POOL_CONNECTION_POOL_SLOT * make_persistent_db_connection_noerror(
763 																		 int db_node_id, char *hostname, int port, char *dbname, char *user, char *password, bool retry);
764 extern void discard_persistent_db_connection(POOL_CONNECTION_POOL_SLOT * cp);
765 
766 /* define pool_system.c */
767 extern void pool_close_libpq_connection(void);
768 
769 /* pool_ip.c */
770 extern void pool_getnameinfo_all(SockAddr *saddr, char *remote_host, char *remote_port);
771 
772 /* strlcpy.c */
773 #ifndef HAVE_STRLCPY
774 extern size_t strlcpy(char *dst, const char *src, size_t siz);
775 #endif
776 
777 /* ps_status.c */
778 extern bool update_process_title;
779 extern char **save_ps_display_args(int argc, char **argv);
780 extern void init_ps_display(const char *username, const char *dbname,
781 				const char *host_info, const char *initial_str);
782 extern void set_ps_display(const char *activity, bool force);
783 extern const char *get_ps_display(int *displen);
784 extern void pool_ps_idle_display(POOL_CONNECTION_POOL * backend);
785 
786 /* recovery.c */
787 extern void start_recovery(int recovery_node);
788 extern void finish_recovery(void);
789 extern int wait_connection_closed(void);
790 extern int ensure_conn_counter_validity(void);
791 
792 /* child.c */
793 extern void cancel_request(CancelPacket * sp);
794 extern void check_stop_request(void);
795 extern void pool_initialize_private_backend_status(void);
796 extern bool is_session_connected(void);
797 extern int	send_to_pg_frontend(char *data, int len, bool flush);
798 extern int	pg_frontend_exists(void);
799 extern int	set_pg_frontend_blocking(bool blocking);
800 extern int	get_frontend_protocol_version(void);
801 extern PGVersion *Pgversion(POOL_CONNECTION_POOL * backend);
802 
803 /* pool_process_query.c */
804 extern void reset_variables(void);
805 extern void reset_connection(void);
806 extern void per_node_statement_log(POOL_CONNECTION_POOL * backend, int node_id, char *query);
807 extern void per_node_error_log(POOL_CONNECTION_POOL * backend, int node_id, char *query, char *prefix, bool unread);
808 extern int	pool_extract_error_message(bool read_kind, POOL_CONNECTION * backend, int major, bool unread, char **message);
809 extern POOL_STATUS do_command(POOL_CONNECTION * frontend, POOL_CONNECTION * backend,
810 							  char *query, int protoMajor, int pid, int key, int no_ready_for_query);
811 extern void do_query(POOL_CONNECTION * backend, char *query, POOL_SELECT_RESULT * *result, int major);
812 extern void free_select_result(POOL_SELECT_RESULT * result);
813 extern int	compare(const void *p1, const void *p2);
814 extern void do_error_execute_command(POOL_CONNECTION_POOL * backend, int node_id, int major);
815 extern POOL_STATUS pool_discard_packet_contents(POOL_CONNECTION_POOL * cp);
816 extern void pool_dump_valid_backend(int backend_id);
817 extern bool pool_push_pending_data(POOL_CONNECTION * backend);
818 
819 /* pool_auth.c */
820 extern void pool_random_salt(char *md5Salt);
821 extern void pool_random(void *buf, size_t len);
822 
823 /* main.c */
824 extern void pool_sleep(unsigned int second);
825 extern char *get_pool_key(void);
826 
827 /* pool_worker_child.c */
828 extern void do_worker_child(void);
829 extern int	get_query_result(POOL_CONNECTION_POOL_SLOT * *slots, int backend_id, char *query, POOL_SELECT_RESULT * *res);
830 
831 /* md5.c */
832 extern bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf);
833 
834 /* pool_connection_pool.c */
835 extern int	pool_init_cp(void);
836 extern POOL_CONNECTION_POOL * pool_create_cp(void);
837 extern POOL_CONNECTION_POOL * pool_get_cp(char *user, char *database, int protoMajor, int check_socket);
838 extern void pool_discard_cp(char *user, char *database, int protoMajor);
839 extern void pool_backend_timer(void);
840 extern void pool_connection_pool_timer(POOL_CONNECTION_POOL * backend);
841 extern RETSIGTYPE pool_backend_timer_handler(int sig);
842 extern int	connect_inet_domain_socket(int slot, bool retry);
843 extern int	connect_unix_domain_socket(int slot, bool retry);
844 extern int	connect_inet_domain_socket_by_port(char *host, int port, bool retry);
845 extern int	connect_unix_domain_socket_by_port(int port, char *socket_dir, bool retry);
846 extern int	pool_pool_index(void);
847 
848 /* utils/statistics.c */
849 size_t		stat_shared_memory_size(void);
850 void		stat_set_stat_area(void *address);
851 void		stat_init_stat_area(void);
852 void		stat_count_up(int backend_node_id, Node *parsetree);
853 uint64		stat_get_select_count(int backend_node_id);
854 
855 extern int	PgpoolMain(bool discard_status, bool clear_memcache_oidmaps);
856 
857 /* pcp_child.c */
858 extern int	send_to_pcp_frontend(char *data, int len, bool flush);
859 extern int	pcp_frontend_exists(void);
860 extern void pcp_worker_main(int port);
861 extern void pcp_mark_recovery_finished(void);
862 extern bool pcp_mark_recovery_in_progress(void);
863 
864 
865 /* pgpool_main.c */
866 extern int	pool_send_to_frontend(char *data, int len, bool flush);
867 extern int	pool_frontend_exists(void);
868 extern pid_t pool_waitpid(int *status);
869 extern int	write_status_file(void);
870 extern void do_health_check_child(int *node_id);
871 extern POOL_NODE_STATUS * verify_backend_node_status(POOL_CONNECTION_POOL_SLOT * *slots);
872 extern POOL_NODE_STATUS * pool_get_node_status(void);
873 extern void pool_set_backend_status_changed_time(int backend_id);
874 extern int	get_next_master_node(void);
875 extern bool pool_acquire_follow_primary_lock(bool block);
876 extern void pool_release_follow_primary_lock(void);
877 
878 #endif							/* POOL_H */
879