4 * $Header: /cvsroot/pgpool/pgpool-II/pool.h,v 1.36.2.5 2009/09/06 03:52:12 t-ishii Exp $
6 * pgpool: a language independent connection pool server for PostgreSQL
7 * written by Tatsuo Ishii
9 * Copyright (c) 2003-2009 PgPool Global Development Group
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.
22 * pool.h.: master definition header file
30 #include "pool_type.h"
31 #include "pool_signal.h"
35 #include <sys/types.h>
38 /* undef this if you have problems with non blocking accept() */
41 #define POOLMAXPATHLEN 8192
43 /* configuration file name */
44 #define POOL_CONF_FILE_NAME "pgpool.conf"
46 /* PCP user/password file name */
47 #define PCP_PASSWD_FILE_NAME "pcp.conf"
49 /* HBA configuration file name */
50 #define HBA_CONF_FILE_NAME "pool_hba.conf"
52 /* pid file directory */
53 #define DEFAULT_LOGDIR "/tmp"
55 /* Unix domain socket directory */
56 #define DEFAULT_SOCKET_DIR "/tmp"
59 #define DEFAULT_PID_FILE_NAME "/var/run/pgpool/pgpool.pid"
72 INIT_CONFIG = 1, /* 0x01 */
73 RELOAD_CONFIG = 2 /* 0x02 */
74 } POOL_CONFIG_CONTEXT;
77 /* protocol major version numbers */
78 #define PROTO_MAJOR_V2 2
79 #define PROTO_MAJOR_V3 3
81 /* Cancel packet proto major */
82 #define PROTO_CANCEL 80877102
85 * In protocol 3.0 and later, the startup packet length is not fixed, but
86 * we set an arbitrary limit on it anyway. This is just to prevent simple
87 * denial-of-service attacks via sending enough data to run the server
90 #define MAX_STARTUP_PACKET_LENGTH 10000
93 typedef struct StartupPacket_v2
95 int protoVersion; /* Protocol version */
96 char database[SM_DATABASE]; /* Database name */
97 char user[SM_USER]; /* User name */
98 char options[SM_OPTIONS]; /* Optional additional args */
99 char unused[SM_UNUSED]; /* Unused */
100 char tty[SM_TTY]; /* Tty for debug output */
103 /* startup packet info */
106 char *startup_packet; /* raw startup packet without packet length (malloced area) */
107 int len; /* raw startup packet length */
108 int major; /* protocol major version */
109 int minor; /* protocol minor version */
110 char *database; /* database name in startup_packet (malloced area) */
111 char *user; /* user name in startup_packet (malloced area) */
114 typedef struct CancelPacket
116 int protoVersion; /* Protocol version */
117 int pid; /* bcckend process id */
118 int key; /* cancel key */
122 * configuration paramters
125 char *listen_addresses; /* hostnames/IP addresses to listen on */
126 int port; /* port # to bind */
127 int pcp_port; /* PCP port # to bind */
128 char *socket_dir; /* pgpool socket directory */
129 char *pcp_socket_dir; /* PCP socket directory */
130 int pcp_timeout; /* PCP timeout for an idle client */
131 int num_init_children; /* # of children initially pre-forked */
132 int child_life_time; /* if idle for this seconds, child exits */
133 int connection_life_time; /* if idle for this seconds, connection closes */
134 int child_max_connections; /* if max_connections received, child exits */
135 int client_idle_limit; /* If client_idle_limit is n (n > 0), the client is forced to be
136 disconnected after n seconds idle */
137 int authentication_timeout; /* maximum time in seconds to complete client authentication */
138 int max_pool; /* max # of connection pool per child */
139 char *logdir; /* logging directory */
140 char *pid_file_name; /* pid file name */
141 char *backend_socket_dir; /* Unix domain socket directory for the PostgreSQL server */
142 int replication_mode; /* replication mode */
144 int log_connections; /* 0:false, 1:true - logs incoming connections */
145 int log_hostname; /* 0:false, 1:true - resolve hostname */
146 int enable_pool_hba; /* 0:false, 1:true - enables pool_hba.conf file authentication */
148 int load_balance_mode; /* load balance mode */
150 int replication_stop_on_mismatch; /* if there's a data mismatch between master and secondary
151 * start degenration to stop replication mode
153 int replicate_select; /* if non 0, replicate SELECT statement when load balancing is disabled. */
154 char **reset_query_list; /* comma separated list of quries to be issued at the end of session */
156 int print_timestamp; /* if non 0, print time stamp to each log line */
157 int master_slave_mode; /* if non 0, operate in master/slave mode */
158 int connection_cache; /* if non 0, cache connection pool */
159 int health_check_timeout; /* health check timeout */
160 int health_check_period; /* health check period */
161 char *health_check_user; /* PostgreSQL user name for health check */
162 char *failover_command; /* execute command when failover happens */
163 char *failback_command; /* execute command when failback happens */
164 char *recovery_user; /* PostgreSQL user name for online recovery */
165 char *recovery_password; /* PostgreSQL user password for online recovery */
166 char *recovery_1st_stage_command; /* Online recovery command in 1st stage */
167 char *recovery_2nd_stage_command; /* Online recovery command in 2nd stage */
168 int recovery_timeout; /* maximum time in seconds to wait for remote start-up */
169 int client_idle_limit_in_recovery; /* If > 0, the client is forced to be
170 * disconnected after n seconds idle
171 * This parameter is only valid while in recovery 2nd statge */
172 int insert_lock; /* if non 0, automatically lock table with INSERT to keep SERIAL
174 int ignore_leading_white_space; /* ignore leading white spaces of each query */
175 int log_statement; /* 0:false, 1: true - logs all SQL statements */
177 int parallel_mode; /* if non 0, run in parallel query mode */
179 int enable_query_cache; /* if non 0, use query cache. 0 by default */
181 char *pgpool2_hostname; /* pgpool2 hostname */
182 char *system_db_hostname; /* system DB hostname */
183 int system_db_port; /* system DB port number */
184 char *system_db_dbname; /* system DB name */
185 char *system_db_schema; /* system DB schema name */
186 char *system_db_user; /* user name to access system DB */
187 char *system_db_password; /* password to access system DB */
189 BackendDesc *backend_desc; /* PostgreSQL Server description. Placed on shared memory */
191 LOAD_BALANCE_STATUS load_balance_status[MAX_NUM_BACKENDS]; /* to remember which DB node is selected for load balancing */
193 /* followings do not exist in the configuration file */
194 int current_slot; /* current backend slot # */
195 int replication_enabled; /* replication mode enabled */
196 int master_slave_enabled; /* master/slave mode enabled */
197 int num_reset_queries; /* number of queries in reset_query_list */
200 #define MAX_PASSWORD_SIZE 1024
203 int num; /* number of entries */
204 char **names; /* parameter names */
205 char **values; /* values */
209 * stream connection structure
212 int fd; /* fd for connection */
214 char *wbuf; /* write buffer for the connection */
215 int wbufsz; /* write buffer size */
216 int wbufpo; /* buffer offset */
218 char *hp; /* pending data buffer head address */
219 int po; /* pending data offset */
220 int bufsz; /* pending data buffer size */
221 int len; /* pending data length */
223 char *sbuf; /* buffer for pool_read_string */
224 int sbufsz; /* its size in bytes */
226 char *buf2; /* buffer for pool_read2 */
227 int bufsz2; /* its size in bytes */
229 int isbackend; /* this connection is for backend if non 0 */
230 int db_node_id; /* DB node id for this connection */
232 char tstate; /* transaction state (V3 only) */
235 * following are used to remember when re-use the authenticated connection
237 int auth_kind; /* 3: clear text password, 4: crypt password, 5: md5 password */
238 int pwd_size; /* password (sent back from frontend) size in host order */
239 char password[MAX_PASSWORD_SIZE]; /* password (sent back from frontend) */
240 char salt[4]; /* password salt */
243 * following are used to remember current session paramter status.
244 * re-used connection will need them (V3 only)
248 int no_forward; /* if non 0, do not write to frontend */
250 char kind; /* kind cache */
253 * frontend info needed for hba
257 UserAuth auth_method;
264 * connection pool structure
267 StartupPacket *sp; /* startup packet info */
268 int pid; /* backend pid */
269 int key; /* cancel key */
270 POOL_CONNECTION *con;
271 time_t closetime; /* absolute time in second when the connection closed
272 * if 0, that means the connection is under use.
274 } POOL_CONNECTION_POOL_SLOT;
277 ConnectionInfo *info; /* connection info on shmem */
278 POOL_CONNECTION_POOL_SLOT *slots[MAX_NUM_BACKENDS];
279 } POOL_CONNECTION_POOL;
284 /* persistent connection to the system DB */
285 POOL_CONNECTION_POOL_SLOT *connection;
286 BACKEND_STATUS *system_db_status;
287 } POOL_SYSTEMDB_CONNECTION_POOL;
290 * for pool_clear_cache() in pool_query_cache.c
292 * used to specify the time which cached data created before it to be deleted.
304 millennium, millenniums
313 #define NUM_BACKENDS (in_load_balance? (selected_slot+1) : \
314 (((!REPLICATION && !PARALLEL_MODE)||master_slave_dml)? Req_info->master_node_id+1: \
315 pool_config->backend_desc->num_backends))
317 /* NUM_BACKENDS now always returns actual number of backends if not in_load_balance */
318 #define NUM_BACKENDS (in_load_balance ? (selected_slot+1) : pool_config->backend_desc->num_backends)
319 #define BACKEND_INFO(backend_id) (pool_config->backend_desc->backend_info[(backend_id)])
320 #define LOAD_BALANCE_STATUS(backend_id) (pool_config->load_balance_status[(backend_id)])
321 /* if RAW_MODE, VALID_BACKEND returns the selected node only */
322 #define VALID_BACKEND(backend_id) \
323 (RAW_MODE ? (backend_id) == MASTER_NODE_ID : \
324 (in_load_balance ? LOAD_BALANCE_STATUS(backend_id) == LOAD_SELECTED : \
325 ((BACKEND_INFO(backend_id).backend_status == CON_UP) || \
326 (BACKEND_INFO(backend_id).backend_status == CON_CONNECT_WAIT))))
327 #define CONNECTION_SLOT(p, slot) ((p)->slots[(slot)])
328 #define CONNECTION(p, slot) (CONNECTION_SLOT(p, slot)->con)
329 #define MASTER_CONNECTION(p) ((p)->slots[MASTER_NODE_ID])
330 #define MASTER_NODE_ID (in_load_balance? selected_slot : Req_info->master_node_id)
331 #define IS_MASTER_NODE_ID(node_id) (MASTER_NODE_ID == (node_id))
332 //#define SECONDARY_CONNECTION(p) ((p)->slots[1])
333 #define REPLICATION (pool_config->replication_enabled)
334 #define MASTER_SLAVE (pool_config->master_slave_enabled)
335 #define DUAL_MODE (REPLICATION || MASTER_SLAVE)
336 #define PARALLEL_MODE (pool_config->parallel_mode)
337 #define RAW_MODE (!REPLICATION && !PARALLEL_MODE && !MASTER_SLAVE)
338 #define MASTER(p) MASTER_CONNECTION(p)->con
339 //#define SECONDARY(p) SECONDARY_CONNECTION(p)->con
340 #define MAJOR(p) MASTER_CONNECTION(p)->sp->major
341 #define TSTATE(p) MASTER(p)->tstate
342 #define SYSDB_INFO (system_db_info->info)
343 #define SYSDB_CONNECTION (system_db_info->connection)
344 #define SYSDB_STATUS (*system_db_info->system_db_status)
346 #define Max(x, y) ((x) > (y) ? (x) : (y))
347 #define Min(x, y) ((x) < (y) ? (x) : (y))
349 #define LOCK_COMMENT "/*INSERT LOCK*/"
350 #define LOCK_COMMENT_SZ (sizeof(LOCK_COMMENT)-1)
351 #define NO_LOCK_COMMENT "/*NO INSERT LOCK*/"
352 #define NO_LOCK_COMMENT_SZ (sizeof(NO_LOCK_COMMENT)-1)
354 #define MAX_NUM_SEMAPHORES 3
355 #define CONN_COUNTER_SEM 0
356 #define REQUEST_INFO_SEM 1
358 #define MY_PROCESS_INFO (pids[my_proc_id])
361 * number specified when semaphore is locked/unlocked
371 * up/down request info area in shared memory
376 NODE_RECOVERY_REQUEST,
381 POOL_REQUEST_KIND kind; /* request kind */
382 int node_id[MAX_NUM_BACKENDS]; /* request node id */
383 int master_node_id; /* the youngest node id which is not in down status */
387 /* description of row. corresponding to RowDescription message */
389 char *attrname; /* attribute name */
390 int oid; /* 0 or non 0 if it's a table oblect */
391 int attrnumber; /* attribute number starting with 1. 0 if it's not a table */
392 int typeoid; /* data type oid */
393 int size; /* data length minus means variable data type */
394 int mod; /* data type modifier */
398 int num_attrs; /* number of attributes */
403 RowDesc *rowdesc; /* attribute info */
404 int numrows; /* number of rows */
405 int *nullflags; /* if NULL, -1 or length of the string excluding termination null */
406 char **data; /* actual row character data terminated with null */
407 } POOL_SELECT_RESULT;
412 extern pid_t mypid; /* parent pid */
413 extern POOL_CONFIG *pool_config; /* configuration values */
414 extern POOL_CONNECTION_POOL *pool_connection_pool; /* connection pool */
415 extern volatile sig_atomic_t backend_timer_expired; /* flag for connection closed timer is expired */
416 extern long int weight_master; /* normalized weight of master (0-RAND_MAX range) */
417 extern int my_proc_id; /* process table id (!= UNIX's PID) */
418 extern POOL_SYSTEMDB_CONNECTION_POOL *system_db_info; /* systemdb */
419 extern ProcessInfo *pids; /* shmem process information table */
420 extern ConnectionInfo *con_info; /* shmem connection info table */
421 extern int in_load_balance; /* non 0 if in load balance mode */
422 extern int selected_slot; /* selected DB node for load balance */
423 extern int master_slave_dml; /* non 0 if master/slave mode is specified in config file */
424 extern POOL_REQUEST_INFO *Req_info;
425 extern volatile sig_atomic_t *InRecovery;
426 extern char remote_ps_data[]; /* used for set_ps_display */
427 extern volatile sig_atomic_t got_sighup;
429 #define QUERY_STRING_BUFFER_LEN 1024
430 extern char query_string_buffer[]; /* last query string sent to simpleQuery() */
435 extern char *get_config_file_name(void);
436 extern char *get_hba_file_name(void);
438 extern void pool_error(const char *fmt,...)
439 __attribute__((format (printf, 1, 2)));
440 extern void pool_debug(const char *fmt,...)
441 __attribute__((format (printf, 1, 2)));
442 extern void pool_log(const char *fmt,...)
443 __attribute__((format (printf, 1, 2)));
445 extern void pool_error(const char *fmt,...);
446 extern void pool_debug(const char *fmt,...);
447 extern void pool_log(const char *fmt,...);
449 extern int pool_init_config(void);
450 extern int pool_get_config(char *confpath, POOL_CONFIG_CONTEXT context);
451 extern void do_child(int unix_fd, int inet_fd);
452 extern void pcp_do_child(int unix_fd, int inet_fd, char *pcp_conf_file);
453 extern int select_load_balancing_node(void);
454 extern int pool_init_cp(void);
455 extern POOL_STATUS pool_process_query(POOL_CONNECTION *frontend,
456 POOL_CONNECTION_POOL *backend,
457 int connection_reuse,
458 int first_ready_for_query_received);
460 extern POOL_CONNECTION *pool_open(int fd);
461 extern void pool_close(POOL_CONNECTION *cp);
462 extern int pool_read(POOL_CONNECTION *cp, void *buf, int len);
463 extern char *pool_read2(POOL_CONNECTION *cp, int len);
464 extern int pool_write(POOL_CONNECTION *cp, void *buf, int len);
465 extern int pool_flush(POOL_CONNECTION *cp);
466 extern int pool_flush_it(POOL_CONNECTION *cp);
467 extern int pool_write_and_flush(POOL_CONNECTION *cp, void *buf, int len);
468 extern char *pool_read_string(POOL_CONNECTION *cp, int *len, int line);
469 extern int pool_unread(POOL_CONNECTION *cp, void *data, int len);
471 extern int pool_do_auth(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend);
472 extern int pool_do_reauth(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *cp);
474 extern int pool_init_cp(void);
475 extern POOL_CONNECTION_POOL *pool_create_cp(void);
476 extern POOL_CONNECTION_POOL *pool_get_cp(char *user, char *database, int protoMajor, int check_socket);
477 extern void pool_discard_cp(char *user, char *database, int protoMajor);
478 extern void pool_backend_timer(void);
480 extern POOL_STATUS ErrorResponse(POOL_CONNECTION *frontend,
481 POOL_CONNECTION_POOL *backend);
483 extern POOL_STATUS NoticeResponse(POOL_CONNECTION *frontend,
484 POOL_CONNECTION_POOL *backend);
486 extern void notice_backend_error(int node_id);
487 extern void degenerate_backend_set(int *node_id_set, int count);
488 extern void send_failback_request(int node_id);
490 extern void pool_connection_pool_timer(POOL_CONNECTION_POOL *backend);
491 extern RETSIGTYPE pool_backend_timer_handler(int sig);
493 extern int connect_inet_domain_socket(int secondary_backend);
494 extern int connect_unix_domain_socket(int secondary_backend);
495 extern int connect_inet_domain_socket_by_port(char *host, int port);
496 extern int connect_unix_domain_socket_by_port(int port, char *socket_dir);
498 extern void pool_set_timeout(int timeoutval);
499 extern int pool_check_fd(POOL_CONNECTION *cp);
501 extern void pool_send_frontend_exits(POOL_CONNECTION_POOL *backend);
503 extern int pool_read_message_length(POOL_CONNECTION_POOL *cp);
504 extern int *pool_read_message_length2(POOL_CONNECTION_POOL *cp);
505 extern signed char pool_read_kind(POOL_CONNECTION_POOL *cp);
506 extern int pool_read_int(POOL_CONNECTION_POOL *cp);
508 extern POOL_STATUS SimpleForwardToFrontend(char kind, POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend);
509 extern POOL_STATUS SimpleForwardToBackend(char kind, POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend);
510 extern POOL_STATUS ParameterStatus(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend);
512 extern int pool_init_params(ParamStatus *params);
513 extern void pool_discard_params(ParamStatus *params);
514 extern char *pool_find_name(ParamStatus *params, char *name, int *pos);
515 extern int pool_get_param(ParamStatus *params, int index, char **name, char **value);
516 extern int pool_add_param(ParamStatus *params, char *name, char *value);
517 extern void pool_param_debug_print(ParamStatus *params);
519 extern void pool_send_error_message(POOL_CONNECTION *frontend, int protoMajor,
526 extern void pool_send_fatal_message(POOL_CONNECTION *frontend, int protoMajor,
533 extern void pool_send_severity_message(POOL_CONNECTION *frontend, int protoMajor,
541 extern void pool_send_readyforquery(POOL_CONNECTION *frontend);
542 extern int send_startup_packet(POOL_CONNECTION_POOL_SLOT *cp);
543 extern void pool_free_startup_packet(StartupPacket *sp);
544 extern void child_exit(int code);
546 extern int health_check(void);
547 extern int system_db_health_check(void);
549 extern void init_prepared_list(void);
551 extern void *pool_shared_memory_create(size_t size);
552 extern void pool_shmem_exit(int code);
554 extern int pool_semaphore_create(int numSems);
555 extern void pool_semaphore_lock(int semNum);
556 extern void pool_semaphore_unlock(int semNum);
558 extern BackendInfo *pool_get_node_info(int node_number);
559 extern int pool_get_node_count(void);
560 extern int *pool_get_process_list(int *array_size);
561 extern ProcessInfo *pool_get_process_info(pid_t pid);
562 extern SystemDBInfo *pool_get_system_db_info(void);
563 extern POOL_STATUS OneNode_do_command(POOL_CONNECTION *frontend, POOL_CONNECTION *backend, char *query, char *database);
565 extern POOL_CONNECTION_POOL_SLOT *make_persistent_db_connection(
566 char *hostname, int port, char *dbname, char *user, char *password);
568 extern POOL_STATUS do_query(POOL_CONNECTION *backend, char *query, POOL_SELECT_RESULT **result);
570 /* define pool_system.c */
571 extern POOL_CONNECTION_POOL_SLOT *pool_system_db_connection(void);
572 extern DistDefInfo *pool_get_dist_def_info (char * dbname, char * schema_name, char * table_name);
573 extern RepliDefInfo *pool_get_repli_def_info (char * dbname, char * schema_name, char * table_name);
574 extern int pool_get_id (DistDefInfo *info, const char * value);
575 extern int system_db_connect (void);
576 extern int pool_memset_system_db_info (SystemDBInfo *info);
577 extern void pool_close_libpq_connection(void);
579 /* pool_query_cache.c */
580 extern POOL_STATUS pool_query_cache_lookup(POOL_CONNECTION *frontend, char *query, char *database, char tstate);
581 extern int pool_query_cache_register(char kind, POOL_CONNECTION *frontend, char *database, char *data, int data_len, char *query);
582 extern int pool_query_cache_table_exists(void);
583 extern int pool_clear_cache_by_time(Interval *interval, int size);
586 extern void load_hba(char *hbapath);
587 extern void ClientAuthentication(POOL_CONNECTION *frontend);
590 extern void pool_getnameinfo_all(SockAddr *saddr, char *remote_host, char *remote_port);
593 extern size_t strlcpy(char *dst, const char *src, size_t siz);
596 extern bool update_process_title;
597 extern char **save_ps_display_args(int argc, char **argv);
598 extern void init_ps_display(const char *username, const char *dbname,
599 const char *host_info, const char *initial_str);
600 extern void set_ps_display(const char *activity, bool force);
601 extern const char *get_ps_display(int *displen);
604 extern int start_recovery(int recovery_node);
605 extern void finish_recovery(void);
608 extern void pool_set_nonblock(int fd);
609 extern void pool_unset_nonblock(int fd);
610 extern void cancel_request(CancelPacket *sp);