1 /* -*-pgsql-c-*- */
2 /*
3  *
4  * $Header$
5  *
6  * pgpool: a language independent connection pool server for PostgreSQL
7  * written by Tatsuo Ishii
8  *
9  * Copyright (c) 2003-2021	PgPool Global Development Group
10  *
11  * Permission to use, copy, modify, and distribute this software and
12  * its documentation for any purpose and without fee is hereby
13  * granted, provided that the above copyright notice appear in all
14  * copies and that both that copyright notice and this permission
15  * notice appear in supporting documentation, and that the name of the
16  * author not be used in advertising or publicity pertaining to
17  * distribution of the software without specific, written prior
18  * permission. The author makes no representations about the
19  * suitability of this software for any purpose.  It is provided "as
20  * is" without express or implied warranty.
21  *
22  * pool_config.h.: pool_config.l related header file
23  *
24  */
25 
26 #ifndef POOL_CONFIG_H
27 #define POOL_CONFIG_H
28 
29 #include "pcp/libpcp_ext.h"
30 
31 /*
32  * watchdog
33  */
34 #define WD_MAX_HOST_NAMELEN MAX_FDQN_HOSTNAME_LEN
35 #define WD_MAX_PATH_LEN (128)
36 #define MAX_WATCHDOG_NUM (128)
37 #define WD_SEND_TIMEOUT (1)
38 #define WD_MAX_IF_NUM (256)
39 #define WD_MAX_IF_NAME_LEN (16)
40 
41 #include "utils/regex_array.h"
42 /*
43  *  Regex support in white and black list function
44  */
45 #include <regex.h>
46 #define BLACKLIST	0
47 #define WHITELIST	1
48 #define PATTERN_ARR_SIZE 16     /* Default length of regex array: 16 patterns */
49 typedef struct {
50   char *pattern;
51   int type;
52   int flag;
53   regex_t regexv;
54 } RegPattern;
55 
56 typedef enum MasterSlaveSubModes
57 {
58 	SLONY_MODE = 1,
59 	STREAM_MODE
60 }MasterSlaveSubModes;
61 
62 typedef enum LogStandbyDelayModes
63 {
64 	LSD_ALWAYS = 1,
65 	LSD_OVER_THRESHOLD,
66 	LSD_NONE
67 }LogStandbyDelayModes;
68 
69 
70 typedef enum MemCacheMethod
71 {
72 	SHMEM_CACHE = 1,
73 	MEMCACHED_CACHE
74 }MemCacheMethod;
75 
76 typedef enum WdLifeCheckMethod
77 {
78 	LIFECHECK_BY_QUERY = 1,
79 	LIFECHECK_BY_HB,
80 	LIFECHECK_BY_EXTERNAL
81 }WdLifeCheckMethod;
82 
83 /*
84  * Flags for backendN_flag
85  */
86 #define POOL_FAILOVER	0x0001	/* allow or disallow failover */
87 #define POOL_DISALLOW_TO_FAILOVER(x) ((unsigned short)(x) & POOL_FAILOVER)
88 #define POOL_ALLOW_TO_FAILOVER(x) (!(POOL_DISALLOW_TO_FAILOVER(x)))
89 
90 /*
91  * watchdog list
92  */
93 typedef struct WdRemoteNodeInfo {
94 	char hostname[WD_MAX_HOST_NAMELEN];		/* host name */
95 	int pgpool_port;						/* pgpool port */
96 	int wd_port;							/* watchdog port */
97 } WdRemoteNodeInfo;
98 
99 typedef struct WdRemoteNodesConfig{
100 	int				  num_wd;		/* number of watchdogs */
101 	WdRemoteNodeInfo  wd_remote_node_info[MAX_WATCHDOG_NUM];
102 } WdRemoteNodesConfig;
103 
104 
105 typedef struct {
106 	char addr[WD_MAX_HOST_NAMELEN];
107 	char if_name[WD_MAX_IF_NAME_LEN];
108 	int dest_port;
109 } WdHbIf;
110 
111 #define WD_INFO(wd_id) (pool_config->wd_remote_nodes.wd_remote_node_info[(wd_id)])
112 #define WD_HB_IF(if_id) (pool_config->hb_if[(if_id)])
113 
114 /*
115  * configuration parameters
116  */
117 typedef struct {
118 	char *listen_addresses;			/* hostnames/IP addresses to listen on */
119     int	port;						/* port # to bind */
120 	char *pcp_listen_addresses;		/* PCP listen address to listen on */
121 	int pcp_port;					/* PCP port # to bind */
122 	char *socket_dir;				/* pgpool socket directory */
123 	char *wd_ipc_socket_dir;		/* watchdog command IPC socket directory */
124 	char *pcp_socket_dir;			/* PCP socket directory */
125     int	num_init_children;			/* # of children initially pre-forked */
126     int	listen_backlog_multiplier;	/* determines the size of the connection queue */
127 	bool serialize_accept;			/* if non 0, serialize call to accept() to avoid thundering herd problem */
128     int	child_life_time;			/* if idle for this seconds, child exits */
129     int	connection_life_time;		/* if idle for this seconds, connection closes */
130     int	child_max_connections;		/* if max_connections received, child exits */
131 	int client_idle_limit;			/* If client_idle_limit is n (n > 0), the client is forced to be
132 									 *disconnected after n seconds idle */
133 	int authentication_timeout;		/* maximum time in seconds to complete client authentication */
134     int	max_pool;					/* max # of connection pool per child */
135     char *logdir;					/* logging directory */
136     char *log_destination_str;		/* log destination: stderr and/or syslog */
137     int log_destination;			/* log destination */
138     int syslog_facility;			/* syslog facility: LOCAL0, LOCAL1, ... */
139     char *syslog_ident;				/* syslog ident string: pgpool */
140     char *pid_file_name;			/* pid file name */
141 	bool replication_mode;			/* replication mode */
142 	bool log_connections;			/* logs incoming connections */
143 	bool log_hostname;				/* resolve hostname */
144 	bool enable_pool_hba;			/* enables pool_hba.conf file authentication */
145 	char *pool_passwd;				/* pool_passwd file name. "" disables pool_passwd */
146 	bool load_balance_mode;			/* load balance mode */
147 
148 	bool replication_stop_on_mismatch;	/* if there's a data mismatch between master and secondary
149 										 * start degeneration to stop replication mode
150 										 */
151 	bool failover_if_affected_tuples_mismatch;	/* If there's a disagreement with the number of affected tuples in
152 												 * UPDATE/DELETE, then degenerate the node which is most likely
153 												 * "minority".  # If false, just abort the transaction to keep the
154 												 * consistency.
155 												 */
156 	bool replicate_select;			/* replicate SELECT statement when load balancing is disabled. */
157 	char **reset_query_list;		/* comma separated list of queries to be issued at the end of session */
158 	char **white_function_list;		/* list of functions with no side effects */
159 	char **black_function_list;		/* list of functions with side effects */
160 	char *log_line_prefix;			/* printf-style string to output at beginning of each log line */
161     int log_error_verbosity;		/* controls how much detail about error should be emitted */
162     int client_min_messages;		/* controls which message should be sent to client */
163     int log_min_messages;			/* controls which message should be emitted to server log */
164 	bool master_slave_mode;			/* operate in master/slave mode */
165 	MasterSlaveSubModes master_slave_sub_mode;		/* either "slony" or "stream" */
166 	int64 delay_threshold;			/* If the standby server delays more than delay_threshold,
167 									 * any query goes to the primary only. The unit is in bytes.
168 									 * 0 disables the check. Default is 0.
169 									 * Note that health_check_period required to be greater than 0
170 									 * to enable the functionality.
171 									 */
172 	LogStandbyDelayModes log_standby_delay;	/* how to log standby lag */
173 	bool connection_cache;					/* cache connection pool? */
174 	int health_check_timeout;				/* health check timeout */
175 	int health_check_period;				/* health check period */
176 	char *health_check_user;				/* PostgreSQL user name for health check */
177 	char *health_check_password;			/* password for health check username */
178 	char *health_check_database;			/* database name for health check username */
179 	int health_check_max_retries;			/* health check max retries */
180 	int health_check_retry_delay;			/* amount of time to wait between retries */
181 	int connect_timeout;					/* timeout value before giving up connecting to backend */
182 	int sr_check_period;					/* streaming replication check period */
183 	char *sr_check_user;					/* PostgreSQL user name for streaming replication check */
184 	char *sr_check_password;				/* password for sr_check_user */
185 	char *sr_check_database;				/* PostgreSQL database name for streaming replication check */
186 	char *failover_command;					/* execute command when failover happens */
187 	char *follow_master_command;			/* execute command when failover is ended */
188 	char *failback_command;					/* execute command when failback happens */
189 
190 	bool	fail_over_on_backend_error;		/* If true, trigger fail over when writing to the backend
191 											 * communication socket fails. This is the same behavior of
192 											 * pgpool-II 2.2.x or earlier. If set to false, pgpool will report
193 											 * an error and disconnect the session.
194 											 */
195 
196 	char *recovery_user;					/* PostgreSQL user name for online recovery */
197 	char *recovery_password;				/* PostgreSQL user password for online recovery */
198 	char *recovery_1st_stage_command;		/* Online recovery command in 1st stage */
199 	char *recovery_2nd_stage_command;		/* Online recovery command in 2nd stage */
200 	int recovery_timeout;					/* maximum time in seconds to wait for remote start-up */
201 	int search_primary_node_timeout;		/* maximum time in seconds to search for new primary
202 											 * node after failover
203 											 */
204 	int client_idle_limit_in_recovery;		/* If > 0, the client is forced to be
205 											 *  disconnected after n seconds idle
206 											 *  This parameter is only valid while in recovery 2nd stage
207 											 */
208 	bool insert_lock;						/* automatically locking of table with INSERT to keep SERIAL data consistency?
209 											 */
210 	bool ignore_leading_white_space;		/* ignore leading white spaces of each query */
211  	bool log_statement;						/* logs all SQL statements */
212  	bool log_per_node_statement;			/* logs per node detailed SQL statements */
213 	char *lobj_lock_table;					/* table name to lock for rewriting lo_creat */
214 
215 	BackendDesc *backend_desc;				/* PostgreSQL Server description. Placed on shared memory */
216 
217 	LOAD_BALANCE_STATUS	load_balance_status[MAX_NUM_BACKENDS];	/* to remember which DB node is selected for load balancing */
218 
219 	/* followings till syslog, does not exist in the configuration file */
220 	int num_reset_queries;					/* number of queries in reset_query_list */
221 	int num_white_function_list;			/* number of functions in white_function_list */
222 	int num_black_function_list;			/* number of functions in black_function_list */
223 	int num_white_memqcache_table_list;		/* number of functions in white_memqcache_table_list */
224 	int num_black_memqcache_table_list;		/* number of functions in black_memqcache_table_list */
225 	int num_wd_monitoring_interfaces_list;  /* number of items in wd_monitoring_interfaces_list */
226 
227 	/* ssl configuration */
228 	bool ssl;								/* if non 0, activate ssl support (frontend+backend) */
229 	char *ssl_cert;							/* path to ssl certificate (frontend only) */
230 	char *ssl_key;							/* path to ssl key (frontend only) */
231 	char *ssl_ca_cert;						/* path to root (CA) certificate */
232 	char *ssl_ca_cert_dir;					/* path to directory containing CA certificates */
233 	char	   *ssl_ciphers;				/* allowed ssl ciphers */
234 	bool		ssl_prefer_server_ciphers; /*Use SSL cipher preferences, rather than the client's*/
235 
236 
237 	int64 relcache_expire;					/* relation cache life time in seconds */
238 	int relcache_size;						/* number of relation cache life entry */
239 	bool check_temp_table;					/* enable temporary table check */
240 	bool check_unlogged_table;				/* enable unlogged table check */
241 
242 	/* followings are for regex support and do not exist in the configuration file */
243 	RegPattern *lists_patterns;				/* Precompiled regex patterns for black/white lists */
244 	int pattc;								/* number of regexp pattern */
245 	int current_pattern_size;				/* size of the regex pattern array */
246 
247 	bool memory_cache_enabled;				/* if true, use the memory cache functionality, false by default */
248 	MemCacheMethod memqcache_method;		/* Cache store method. Either 'shmem'(shared memory) or 'memcached'. 'shmem' by default */
249 	char *memqcache_memcached_host;			/* Memcached host name. Mandatory if memqcache_method=memcached. */
250 	int memqcache_memcached_port;			/* Memcached port number. Mandatory if memqcache_method=memcached. */
251 	int64 memqcache_total_size;				/* Total memory size in bytes for storing memory cache. Mandatory if memqcache_method=shmem. */
252 	int memqcache_max_num_cache;			/* Total number of cache entries. Mandatory if memqcache_method=shmem. */
253 	int memqcache_expire;					/* Memory cache entry life time specified in seconds. 60 by default. */
254 	bool memqcache_auto_cache_invalidation; /* If true, invalidation of query cache is triggered by corresponding */
255 											/* DDL/DML/DCL(and memqcache_expire).  If false, it is only triggered */
256 											/* by memqcache_expire.  True by default. */
257 	int memqcache_maxcache;					/* Maximum SELECT result size in bytes. */
258 	int memqcache_cache_block_size;			/* Cache block size in bytes. 8192 by default */
259 	char *memqcache_oiddir;					/* Temporary work directory to record table oids */
260 	char **white_memqcache_table_list;		/* list of tables to memqcache */
261 	char **black_memqcache_table_list;		/* list of tables not to memqcache */
262 
263 	RegPattern *lists_memqcache_table_patterns; /* Precompiled regex patterns for black/white lists */
264 	int memqcache_table_pattc;					/* number of regexp pattern */
265 	int current_memqcache_table_pattern_size;	/* size of the regex pattern array */
266 
267 	/*
268 	 * database_redirect_preference_list = 'postgres:primary,mydb[0-4]:1,mydb[5-9]:2'
269 	 */
270 	char *database_redirect_preference_list;	/* raw string in pgpool.conf */
271 	RegArray *redirect_dbnames;					/* Precompiled regex patterns for db prefrence list */
272 	Left_right_tokens *db_redirect_tokens;		/* db redirect for dbname and node string */
273 
274 	/*
275 	 * app_name_redirect_preference_list = 'psql:primary,myapp[0-4]:1,myapp[5-9]:standby'
276 	 */
277 	char *app_name_redirect_preference_list;	/* raw string in pgpool.conf */
278 	RegArray *redirect_app_names;				/* Precompiled regex patterns for app name prefrence list */
279 	Left_right_tokens *app_name_redirect_tokens;/* app name redirect for app_name and node string */
280 
281 	bool allow_sql_comments;		/* if on, ignore SQL comments when judging if load balance or query cache
282 									 * is possible. If off, SQL comments effectively prevent the judgment
283 									 * (pre 3.4 behavior). For backward compatibilty sake, default is off.
284 									 */
285 	/*
286 	 * add for watchdog
287 	 */
288 	bool use_watchdog;						/* Enables watchdog */
289 	WdLifeCheckMethod wd_lifecheck_method;	/* method of lifecheck. 'heartbeat' or 'query' */
290 	bool clear_memqcache_on_escalation;		/* Clear query cache on shmem when escalating ?*/
291 	char *wd_escalation_command;			/* Executes this command at escalation on new active pgpool.*/
292 	char *wd_de_escalation_command;			/* Executes this command when master pgpool goes down.*/
293 	char *wd_hostname;						/* watchdog hostname */
294 	int wd_port;							/* watchdog port */
295 	int wd_priority;						/* watchdog node priority, during leader election*/
296 	WdRemoteNodesConfig wd_remote_nodes;	/* watchdog lists */
297 	char * trusted_servers;					/* icmp reachable server list (A,B,C) */
298 	char * delegate_IP;						/* delegate IP address */
299 	int  wd_interval;						/* lifecheck interval (sec) */
300 	char *wd_authkey;						/* Authentication key for watchdog communication */
301 	char * ping_path;						/* path to ping command */
302 	char * if_cmd_path;						/* path to interface up/down command */
303 	char * if_up_cmd;						/* ifup command */
304 	char * if_down_cmd;						/* ifdown command */
305 	char * arping_path;						/* path to arping command */
306 	char * arping_cmd;						/* arping command */
307 	int  wd_life_point;						/* life point (retry times at lifecheck) */
308 	char *wd_lifecheck_query;				/* lifecheck query */
309 	char *wd_lifecheck_dbname;				/* Database name connected for lifecheck */
310 	char *wd_lifecheck_user;				/* PostgreSQL user name for watchdog */
311 	char *wd_lifecheck_password;			/* password for watchdog user */
312 	int wd_heartbeat_port;					/* Port number for heartbeat lifecheck */
313 	int wd_heartbeat_keepalive;				/* Interval time of sending heartbeat signal (sec) */
314 	int wd_heartbeat_deadtime;				/* Deadtime interval for heartbeat signal (sec) */
315 	WdHbIf hb_if[WD_MAX_IF_NUM];			/* interface devices */
316 	int num_hb_if;							/* number of interface devices */
317 	char **wd_monitoring_interfaces_list;	/* network interface name list to be monitored by watchdog */
318 } POOL_CONFIG;
319 
320 extern POOL_CONFIG* pool_config;
321 
322 typedef enum
323 {
324 	CFGCXT_BOOT,
325 	CFGCXT_INIT,
326 	CFGCXT_RELOAD,
327 	CFGCXT_PCP,
328 	CFGCXT_SESSION
329 } ConfigContext;
330 
331 typedef struct ConfigVariable
332 {
333 	char	   *name;
334 	char	   *value;
335 	int			sourceline;
336 	struct ConfigVariable *next;
337 } ConfigVariable;
338 
339 
340 extern int pool_init_config(void);
341 extern bool pool_get_config(const char *config_file, ConfigContext context);
342 extern int eval_logical(const char *str);
343 extern char *pool_flag_to_str(unsigned short flag);
344 extern char* backend_status_to_str(BACKEND_STATUS status);
345 
346 /* methods used for regexp support */
347 extern int add_regex_pattern(const char *type, char *s);
348 extern int growFunctionPatternArray(RegPattern item);
349 extern int growMemqcacheTablePatternArray(RegPattern item);
350 
351 #endif /* POOL_CONFIG_H */
352