File: pool_query_cache.c

package info (click to toggle)
pgpool2 3.3.4-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 9,196 kB
  • sloc: ansic: 59,850; sh: 12,893; yacc: 10,787; lex: 4,637; sql: 743; makefile: 481; java: 469; php: 125; ruby: 98; asm: 5
file content (1020 lines) | stat: -rw-r--r-- 27,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
/* -*-pgsql-c-*- */
/*
 * $Header$
 *
 * pgpool: a language independent connection pool server for PostgreSQL
 * written by Tatsuo Ishii
 *
 * Copyright (c) 2003-2010	PgPool Global Development Group
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of the
 * author not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. The author makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * pool_query_cache.c: query cache
 *
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include "pool.h"
#include "md5.h"
#include "pool_stream.h"
#include "pool_config.h"
#include "pool_proto_modules.h"
#include "parser/parsenodes.h"

#define QUERY_CACHE_TABLE_NAME "query_cache"
#define CACHE_REGISTER_PREPARED_STMT "register_prepared_stmt"
#define DEFAULT_CACHE_SIZE 8192

#define SYSDB_CON (SYSDB_CONNECTION->con)
#define SYSDB_MAJOR (SYSDB_CONNECTION->sp->major)
#define CACHE_TABLE_INFO (SYSDB_INFO->query_cache_table_info)

/* data structure to store RowDescription and DataRow cache */
typedef struct
{
	char *md5_query;			/* query in md5 hushed format*/
	char *query;				/* query string */
	char *cache;				/* cached data */
	int   cache_size;			/* cached data size */
	int   cache_offset;			/* points the end of cache */
	char *db_name;				/* database name */
	char *create_time;			/* cache create timestamp in ISO format */
} QueryCacheInfo;

typedef enum
{
	CACHE_FOUND, CACHE_NOT_FOUND, CACHE_ERROR
} CACHE_STATUS;

static QueryCacheInfo *query_cache_info;

static CACHE_STATUS search_system_db_for_cache(POOL_CONNECTION *frontend, char *sql, int sql_len, struct timeval *t, char tstate);
static int ForwardCacheToFrontend(POOL_CONNECTION *frontend, char *cache, char tstate);
static int init_query_cache_info(POOL_CONNECTION *pc, char *database, char *query);
static void free_query_cache_info(void);
static int malloc_failed(void *p);
static int write_cache(void *buf, int len);
static char *pq_time_to_str(time_t t);
static int system_db_connection_exists(void);
static void define_prepared_statements(void);

/* --------------------------------
 * pool_clear_cache - clears cache data from the SystemDB
 *
 * return 0 on success, -1 otherwise
 * --------------------------------
 */
int
pool_clear_cache_by_time(Interval *interval, int size)
{
	PGresult *pg_result = NULL;
	long interval_in_days = 0;
	long interval_in_seconds = 0;
	time_t query_delete_timepoint;
	char *query_delete_timepoint_in_str = NULL;
	int sql_len;
	char *sql = NULL;
	int i;

	if (! system_db_connection_exists())
		return -1;

	for (i = 0; i < size; i++)
	{
		int q = interval[i].quantity;

		switch (interval[i].unit)
		{
			case millennium:
			case millenniums:
				q *= 10;

			case century:
			case centuries:
				q *= 10;

			case decade:
			case decades:
				q *= 10;

			case year:
			case years:
				q *= 12;

			case month:
			case months:
				q *= 31;		/* should I change this according to the month? */
				interval_in_days += q;
				break;

			case week:
			case weeks:
				q *= 7;

			case day:
			case days:
				q *= 24;

			case hour:
			case hours:
				q *= 60;

			case minute:
			case minutes:
				q *= 60;

			case second:
			case seconds:
				interval_in_seconds += q;
				break;
		}
	}

	interval_in_seconds = (interval_in_days * 86400) + interval_in_seconds;
	query_delete_timepoint = time(NULL) - interval_in_seconds;

	query_delete_timepoint_in_str = pq_time_to_str(query_delete_timepoint);
	if (malloc_failed(query_delete_timepoint_in_str))
		return -1;

	sql_len =
		strlen(pool_config->system_db_schema) +
		strlen(QUERY_CACHE_TABLE_NAME) +
		strlen(query_delete_timepoint_in_str) +
		64;

	sql = (char *)malloc(sql_len);
	if (malloc_failed(sql))
	{
		free(query_delete_timepoint_in_str);
		return -1;
	}

	snprintf(sql, sql_len,
			 "DELETE FROM %s.%s WHERE create_time <= '%s'",
			 pool_config->system_db_schema,
			 QUERY_CACHE_TABLE_NAME,
			 query_delete_timepoint_in_str);

	pool_debug("pool_clear_cache: delete all query cache created before '%s'", query_delete_timepoint_in_str);

	pg_result = PQexec(system_db_info->pgconn, sql);
	if (!pg_result || PQresultStatus(pg_result) != PGRES_COMMAND_OK)
	{
		pool_error("pool_clear_cache: PQexec() failed. reason: %s",
				   PQerrorMessage(system_db_info->pgconn));

		PQclear(pg_result);
		free(query_delete_timepoint_in_str);
		free(sql);
		return -1;
	}

	PQclear(pg_result);
	free(query_delete_timepoint_in_str);
	free(sql);

	return 0;
}

/* --------------------------------
 * pool_query_cache_table_exists - checks if query_cache table exists in the SystemDB
 *
 * This function is called once and only once from the pgpool parent process.
 * return 1 if query_cache table exists, 0 otherwise.
 * --------------------------------
 */
int
pool_query_cache_table_exists(void)
{
	PGresult *pg_result = NULL;
	char *sql = NULL;
	int sql_len = strlen(pool_config->system_db_schema) + strlen(QUERY_CACHE_TABLE_NAME) + 64;

	if (! system_db_connection_exists())
		return 0;

	sql = (char *)malloc(sql_len);
	if (malloc_failed(sql))
		return 0;

	snprintf(sql, sql_len,
			 "SELECT hash, query, value, dbname, create_time FROM %s.%s LIMIT 1",
			 pool_config->system_db_schema,
			 QUERY_CACHE_TABLE_NAME);

	pg_result = PQexec(system_db_info->pgconn, sql);
	if (!pg_result || PQresultStatus(pg_result) != PGRES_TUPLES_OK)
	{
		pool_error("pool_query_cache_table_exists: PQexec() failed. reason: %s",
				   PQerrorMessage(system_db_info->pgconn));

		PQclear(pg_result);
		free(sql);
		return 0;
	}

	PQclear(pg_result);
	pool_close_libpq_connection();
	free(sql);

	return 1;
}

/* --------------------------------
 * pool_query_cache_lookup - retrieve query cache from the SystemDB
 *
 * creates a SQL query string for searching a cache from the SystemDB.
 *
 * returns POOL_CONTINUE if cache is found. returns POOL_END if cache was
 * not found. returns POOL_ERROR if an error has been encountered while
 * searching.
 *
 * Note that POOL_END and POOL_ERROR are treated the same by the caller
 * (pool_process_query.c).
 * POOL_END and POOL_ERROR both indicates to the caller that the search
 * query must be forwarded to the backends in order to retrieve data and
 * the result be cached.
 * Only difference is that POOL_ERROR indicates that some fatal error has
 * occurred; query cache function, however, should be seamless to the user
 * whether cache was not found or error has occurred during cache retrieve.
 * --------------------------------
 */
POOL_STATUS
pool_query_cache_lookup(POOL_CONNECTION *frontend, char *query, char *database, char tstate)
{
	char *sql = NULL;
	int sql_len;
	char md5_query[33];
	struct timeval timeout;
	int status;

	if (! system_db_connection_exists())
		return POOL_ERROR;		/* same as POOL_END ... at least for now */

	sql_len =
		strlen(pool_config->system_db_schema) +
		strlen(QUERY_CACHE_TABLE_NAME) +
		sizeof(md5_query) +
		strlen(database) +
		64;
	sql = (char *)malloc(sql_len);
	if (malloc_failed(sql))
		return POOL_ERROR;		/* should I exit here rather than returning an error? */

	/* cached data lookup */
	pool_md5_hash(query, strlen(query), md5_query);
	snprintf(sql, sql_len, "SELECT value FROM %s.%s WHERE hash = '%s' AND dbname = '%s'",
			 pool_config->system_db_schema,
			 QUERY_CACHE_TABLE_NAME,
			 md5_query,
			 database);

	/* set timeout value for select */
	timeout.tv_sec = pool_config->child_life_time;
	timeout.tv_usec = 0;

	pool_debug("pool_query_cache_lookup: searching cache for query: \"%s\"", query);
	status = search_system_db_for_cache(frontend, sql, strlen(sql)+1, &timeout, tstate);

	/* make sure that the remaining data is discarded */
	SYSDB_CON->po = 0;
	SYSDB_CON->len = 0;

	free(sql);

	/* cache found, and no backend communication needed */
	if (status == CACHE_FOUND)
	{
		return POOL_CONTINUE;
	}

	/* cache not found */

	if (status == CACHE_ERROR)
	{
		pool_error("pool_query_cache_lookup: query cache lookup failed");
		/* reset the SystemDB connection */
		if (system_db_info->pgconn)
			pool_close_libpq_connection();
		return POOL_ERROR;		/* same as POOL_END ... at least for now */
	}

	pool_debug("pool_query_cache_lookup: query cache not found");
	return POOL_END;
}

/* --------------------------------
 * search_system_db_for_cache - search for query cache in libpq protocol level
 *
 * sends a cache searching query string using libpq protocol to the SystemDB.
 * if the SystemDB returns cache, forward the data to the frontend, and return
 * CACHE_FOUND. if cache was not found, silently discards the remaining data
 * returned by the SystemDB, and return CACHE_NOT_FOUND. returns CACHE_ERROR
 * if an error was encountered.
 * --------------------------------
 */
static CACHE_STATUS
search_system_db_for_cache(POOL_CONNECTION *frontend, char *sql, int sql_len, struct timeval *t, char tstate)
{
	fd_set readmask;
	int fds;
	int num_fds;
	struct timeval *timeout = NULL;
	char kind;
	int readlen;
	char *data = NULL;
	CACHE_STATUS return_value = CACHE_ERROR;
	int cache_found = 0;

	pool_debug("pool_query_cache_lookup: executing query: \"%s\"", sql);

	pool_write(SYSDB_CON, "Q", 1);
	if (SYSDB_MAJOR == PROTO_MAJOR_V3)
	{
		int sendlen = htonl(sql_len + 4);
		pool_write(SYSDB_CON, &sendlen, sizeof(sendlen));
	}
	if (pool_write_and_flush(SYSDB_CON, sql, sql_len) < 0)
	{
		pool_error("pool_query_cache_lookup: error while sending data to the SystemDB");
		return CACHE_ERROR;
	}

	if ((t->tv_sec + t->tv_usec) == 0)
		timeout = NULL;
	else
		timeout = t;

	/* don't really need select() or for(;;) here, but we may need it someday... or not */
	for (;;)
	{
		FD_ZERO(&readmask);
		num_fds = 0;

		num_fds = SYSDB_CON->fd + 1;
		FD_SET(SYSDB_CON->fd, &readmask);
		fds = select(num_fds, &readmask, NULL, NULL, timeout);
		if (fds == -1)
		{
			if (errno == EINTR)
				continue;

			pool_error("pool_query_cache_lookup: select() failed. reason: %s", strerror(errno));
			return CACHE_ERROR;
		}

		/* select() timeout */
		if (fds == 0)
			return CACHE_ERROR;

		for (;;)
		{
			if (! FD_ISSET(SYSDB_CON->fd, &readmask))
			{
				pool_error("pool_query_cache_lookup: select() failed");
				return CACHE_ERROR;
			}

			/* read kind */
			if (pool_read(SYSDB_CON, &kind, sizeof(kind)) < 0)
			{
				pool_error("pool_query_cache_lookup: error while reading message kind");
				return CACHE_ERROR;
			}
			pool_debug("pool_query_cache_lookup: received %c from systemdb", kind);

			/* just do the routine work of reading data in. data won't be used */
			if (kind == 'T')
			{
				if (SYSDB_MAJOR == PROTO_MAJOR_V3)
				{
					if (pool_read(SYSDB_CON, &readlen, sizeof(int)) < 0)
					{
						pool_error("pool_query_cache_lookup: error while reading message length");
						return CACHE_ERROR;
					}
					readlen = ntohl(readlen) - sizeof(int);
					data = pool_read2(SYSDB_CON, readlen);
				}
				else
				{
					data = pool_read_string(SYSDB_CON, &readlen, 0);
				}
			}
			else if (kind == 'D') /* cache found! forward it to the frontend */
			{
				char *cache;
				int status;

				cache_found = 1;

				if (SYSDB_MAJOR == PROTO_MAJOR_V3)
				{
					if (pool_read(SYSDB_CON, &readlen, sizeof(readlen)) < 0)
					{
						pool_error("pool_query_cache_lookup: error while reading message length");
						return CACHE_ERROR;
					}
					readlen = ntohl(readlen) - sizeof(int);
					cache = pool_read2(SYSDB_CON, readlen);
				}
				else
				{
					cache = pool_read_string(SYSDB_CON, &readlen, 0);
				}

				if (cache == NULL)
				{
					pool_error("pool_query_cache_lookup: error while reading message body");
					return CACHE_ERROR;
				}

				cache[readlen] = '\0';

				cache += sizeof(short);	/* number of columns in 'D' (we know it's always going to be 1, so skip) */
				cache += sizeof(int); /* length of escaped bytea cache in string format. don't need the length */

				status = ForwardCacheToFrontend(frontend, cache, tstate);
				if (status < 0)
				{
					/* fatal error has occurred while forwarding cache */
					pool_error("pool_query_cache_lookup: query cache forwarding failed");
					return_value = CACHE_ERROR;
				}
			}
			else if (kind == 'C') /* see if 'D' was received */
			{
				if (cache_found)
					return_value = CACHE_FOUND;
				else
					return_value = CACHE_NOT_FOUND;

				/* must discard the remaining data */
				if (SYSDB_MAJOR == PROTO_MAJOR_V3)
				{
					if (pool_read(SYSDB_CON, &readlen, sizeof(int)) < 0)
					{
						pool_error("pool_query_cache_lookup: error while reading message length");
						return CACHE_ERROR;
					}
					readlen = ntohl(readlen) - sizeof(int);
					data = pool_read2(SYSDB_CON, readlen);
				}
				else
				{
					data = pool_read_string(SYSDB_CON, &readlen, 0);
				}
			}
			else if (kind == 'Z')
			{
				/* must discard the remaining data */
				if (SYSDB_MAJOR == PROTO_MAJOR_V3)
				{
					if (pool_read(SYSDB_CON, &readlen, sizeof(int)) < 0)
					{
						pool_error("pool_query_cache_lookup: error while reading message length");
						return CACHE_ERROR;
					}
					readlen = ntohl(readlen) - sizeof(int);
					data = pool_read2(SYSDB_CON, readlen);
				}
				else
				{
					data = pool_read_string(SYSDB_CON, &readlen, 0);
				}

				break;
			}
			else if (kind == 'E')
			{
				/* must discard the remaining data */
				if (SYSDB_MAJOR == PROTO_MAJOR_V3)
				{
					if (pool_read(SYSDB_CON, &readlen, sizeof(int)) < 0)
					{
						pool_error("pool_query_cache_lookup: error while reading message length");
						return CACHE_ERROR;
					}
					readlen = ntohl(readlen) - sizeof(int);
					data = pool_read2(SYSDB_CON, readlen);
				}
				else
				{
					data = pool_read_string(SYSDB_CON, &readlen, 0);
				}

				return_value = CACHE_ERROR;
			}
			else
			{
				/* shouldn't get here, but just in case */
				return CACHE_ERROR;
			}
		}

		break;
	}

	return return_value;
}

/* --------------------------------
 * ForwardCacheToFrontend - simply forwards cached data to the frontend
 *
 * since the cached data passed from the caller is in escaped binary string
 * format, unescape it and send it to the frontend appending 'Z' at the end.
 * returns 0 on success, -1 otherwise.
 * --------------------------------
 */
static int ForwardCacheToFrontend(POOL_CONNECTION *frontend, char *cache, char tstate)
{
	int sendlen;
	size_t sz;
	char *binary_cache = NULL;

	binary_cache = (char *)PQunescapeBytea((unsigned char *)cache, &sz);
	sendlen = (int) sz;
	if (malloc_failed(binary_cache))
		return -1;

	pool_debug("ForwardCacheToFrontend: query cache found (%d bytes)", sendlen);

	/* forward cache to the frontend */
	pool_write(frontend, binary_cache, sendlen);

	/* send ReadyForQuery to the frontend*/
	pool_write(frontend, "Z", 1);
	sendlen = htonl(5);
	pool_write(frontend, &sendlen, sizeof(int));
	if (pool_write_and_flush(frontend, &tstate, 1) < 0)
	{
		pool_error("pool_query_cache_lookup: error while writing data to the frontend");
		PQfreemem(binary_cache);
		return -1;
	}

	PQfreemem(binary_cache);
	return 0;
}

/* --------------------------------
 * pool_query_cache_register() - register query cache to the SystemDB
 *
 * returns 0 on success, -1 otherwise
 * --------------------------------
 */
int
pool_query_cache_register(char kind,
						  POOL_CONNECTION *frontend,
						  char *database,
						  char *data,
						  int data_len,
						  char *query)
{
	int ret;
	int send_len;

	if (! system_db_connection_exists())
		return -1;
	if (! CACHE_TABLE_INFO.has_prepared_statement)
		define_prepared_statements();

	switch (kind)
	{
		case 'T':				/* RowDescription */
		{
			/* for all SELECT result data from the backend, 'T' must come first */
			if (query_cache_info != NULL)
			{
				pool_error("pool_query_cache_register: received RowDescription in the wrong order");
				free_query_cache_info();
				return -1;
			}

			pool_debug("pool_query_cache_register: saving cache for query: \"%s\"", query);

			/* initialize query_cache_info and save the query */
			ret = init_query_cache_info(frontend, database, query);
			if (ret)
				return ret;

			/* store data into the cache */
			write_cache(&kind, 1);
			send_len = htonl(data_len + sizeof(int));
			write_cache(&send_len, sizeof(int));
			write_cache(data, data_len);

			break;
		}

		case 'D':				/* DataRow */
		{
			/* for all SELECT result data from the backend, 'T' must come first */
			if (query_cache_info == NULL)
			{
				pool_error("pool_query_cache_register: received DataRow in the wrong order");
				return -1;
			}

			write_cache(&kind, 1);
			send_len = htonl(data_len + sizeof(int));
			write_cache(&send_len, sizeof(int));
			write_cache(data, data_len);

			break;
		}

		case 'C':				/* CommandComplete */
		{
			PGresult *pg_result = NULL;
			char *escaped_query = NULL;
			size_t escaped_query_len;
			time_t now = time(NULL);
			char *values[5];
			int values_len[5];
			int values_format[5];
			int i;

			/* for all SELECT result data from the backend, 'T' must come first */
			if (query_cache_info == NULL)
			{
				pool_error("pool_query_cache_register: received CommandComplete in the wrong order");
				return -1;
			}

			/* pack CommandComplete data into the cache */
			write_cache(&kind, 1);
			send_len = htonl(data_len + sizeof(int));
			write_cache(&send_len, sizeof(int));
			write_cache(data, data_len);

			query_cache_info->create_time = pq_time_to_str(now);
			if (malloc_failed(query_cache_info->create_time))
			{
				free_query_cache_info();
				return -1;
			}

			escaped_query = (char *)malloc(strlen(query_cache_info->query) * 2 + 1);
			if (malloc_failed(escaped_query))
			{
				free_query_cache_info();
				return -1;
			}

/* 			escaped_query_len = PQescapeStringConn(system_db_info->pgconn, */
/* 												   escaped_query, */
/* 												   query_cache_info->query, */
/* 												   strlen(query_cache_info->query))); */
			escaped_query_len = PQescapeString(escaped_query, query_cache_info->query, strlen(query_cache_info->query));

			/* all the result data have been received. store into the SystemDB */
			values[0] = strdup(query_cache_info->md5_query);
			values[1] = strdup(escaped_query);
			values[2] = (char *)malloc(query_cache_info->cache_offset);
			memcpy(values[2], query_cache_info->cache, query_cache_info->cache_offset);
			values[3] = strdup(query_cache_info->db_name);
			values[4] = strdup(query_cache_info->create_time);
			for (i = 0; i < 5; i++)
			{
				if (malloc_failed(values[i]))
				{
					pool_error("pool_query_cache_register: malloc() failed");
					free_query_cache_info();
					{
						int j;
						for (j = 0; j < i; j++)
							free(values[j]);
					}
					return -1;
				}

				values_len[i] = strlen(values[i]);
			}
			values_format[0] = values_format[1] = values_format[3] = values_format[4] = 0;
			values_format[2] = 1;
			values_len[2] = query_cache_info->cache_offset;

			pg_result = PQexecPrepared(system_db_info->pgconn,
									   CACHE_TABLE_INFO.register_prepared_statement,
									   5,
									   (const char * const *)values,
									   values_len,
									   values_format,
									   0);
			if (!pg_result || PQresultStatus(pg_result) != PGRES_COMMAND_OK)
			{
				pool_error("pool_query_cache_register: PQexecPrepared() failed. reason: %s",
						   PQerrorMessage(system_db_info->pgconn));

				PQclear(pg_result);
				PQfreemem(escaped_query);
				free_query_cache_info();
				for (i = 0; i < 5; i++)
					free(values[i]);
				return -1;
			}

			PQclear(pg_result);
			PQfreemem(escaped_query);
			for (i = 0; i < 5; i++)
				free(values[i]);
			free_query_cache_info();

			break;
		}

		case 'E':
		{
			pool_debug("pool_query_cache_register: received 'E': free query cache buffer");

			pool_close_libpq_connection();
			free_query_cache_info();

			break;
		}
	}

	return 0;
}

/* --------------------------------
 * init_query_cache_info() - allocate memory for query_cache_info and stores query
 *
 * returns 0 on success, -1 otherwise
`* --------------------------------
 */
static int
init_query_cache_info(POOL_CONNECTION *pc, char *database, char *query)
{
	int query_len;				/* length of the SELECT query to be cached */

	query_cache_info = (QueryCacheInfo *)malloc(sizeof(QueryCacheInfo));
	if (malloc_failed(query_cache_info))
		return -1;

	/* query */
	query_len = strlen(query);
	query_cache_info->query = (char *)malloc(query_len + 1);
	if (malloc_failed(query_cache_info->query))
		return -1;
	memcpy(query_cache_info->query, query, query_len + 1);

	/* md5_query */
	query_cache_info->md5_query = (char *)malloc(33); /* md5sum is always 33 bytes (including the '\0') */
	if (malloc_failed(query_cache_info->md5_query))
		return -1;
	pool_md5_hash(query_cache_info->query, query_len, query_cache_info->md5_query);

	/* malloc DEFAULT_CACHE_SIZE for query_cache_info->cache */
	query_cache_info->cache = (char *)malloc(DEFAULT_CACHE_SIZE);
	if (malloc_failed(query_cache_info->cache))
		return -1;
	query_cache_info->cache_size = DEFAULT_CACHE_SIZE;
	query_cache_info->cache_offset = 0;

	/* save database name */
	query_cache_info->db_name = (char *)malloc(strlen(database)+1);
	if (malloc_failed(query_cache_info->db_name))
		return -1;
	strcpy(query_cache_info->db_name, database);

	/* initialize create_timestamp */
	query_cache_info->create_time = NULL;

	return 0;
}

/* --------------------------------
 * free_query_cache_info() - free query_cache_info and its members
 * --------------------------------
 */
static void
free_query_cache_info(void)
{
	if (query_cache_info == NULL)
		return;

	free(query_cache_info->md5_query);
	free(query_cache_info->query);
	free(query_cache_info->cache);
	free(query_cache_info->db_name);
	if (query_cache_info->create_time)
		free(query_cache_info->create_time);
	free(query_cache_info);
	query_cache_info = NULL;
}

/* --------------------------------
 * malloc_failed() - checks if the caller's most recent malloc() has succeeded
 *
 * returns 0 if malloc() was a success, -1 otherwise
`* --------------------------------
 */
static int
malloc_failed(void *p)
{
	if (p != NULL)
		return 0;

	pool_error("pool_query_cache: malloc() failed");
	free_query_cache_info();

	return -1;
}

/* --------------------------------
 * write_cache() - append result data to buffer
 *
 * returns 0 on success, -1 otherwise
 * --------------------------------
 */
static int
write_cache(void *buf, int len)
{
	int required_len;

	if (len < 0)
		return -1;

	required_len = query_cache_info->cache_offset + len;
	if (required_len > query_cache_info->cache_size)
	{
		char *ptr;

		required_len = query_cache_info->cache_size * 2;
		ptr = (char *)realloc(query_cache_info->cache, required_len);
		if (malloc_failed(ptr))
			return -1;

		query_cache_info->cache = ptr;
		query_cache_info->cache_size = required_len;

		pool_debug("pool_query_cache: extended cache buffer size to %d", query_cache_info->cache_size);
	}

	memcpy(query_cache_info->cache + query_cache_info->cache_offset, buf, len);
	query_cache_info->cache_offset += len;

	return 0;
}

/* --------------------------------
 * pq_time_to_str() - convert time_t to ISO standard time output string
 *
 * returns a pointer to newly allocated string, NULL if malloc fails
 * --------------------------------
 */
static char *
pq_time_to_str(time_t t)
{
	char *time_p;
	char iso_time[32];
	struct tm *tm;

	tm = localtime(&t);
	strftime(iso_time, sizeof(iso_time), "%Y-%m-%d %H:%M:%S%z", tm);

	time_p = strdup(iso_time);

	return time_p;
}


/* --------------------------------
 * system_db_connection_exists() - checks and if a connection to the SystemDB exists
 *
 * if not connected, it makes an attempt to connect to the SystemDB. If a connection
 * exists, returns 1, returns 0 otherwise.
 * --------------------------------
 */
static int
system_db_connection_exists(void)
{
	if (!system_db_info->pgconn ||
		(PQstatus(system_db_info->pgconn) != CONNECTION_OK))
	{
		if (system_db_connect())
			return 0;
	}

	return 1;
}


/* --------------------------------
 * define_prepared_statements() - defines prepared statements for the current session
 * --------------------------------
 */
static void
define_prepared_statements(void)
{
	PGresult *pg_result;
	char *sql = NULL;
	int sql_len;

	sql_len =
		strlen(pool_config->system_db_schema) +
		strlen(QUERY_CACHE_TABLE_NAME) +
		1024;

	sql = (char *)malloc(sql_len);
	if (malloc_failed(sql))
	{
		pool_error("pool_query_cache: malloc() failed");
		return;
	}

	free(CACHE_TABLE_INFO.register_prepared_statement);
	CACHE_TABLE_INFO.register_prepared_statement
		= strdup(CACHE_REGISTER_PREPARED_STMT);
	if (malloc_failed(CACHE_TABLE_INFO.register_prepared_statement))
	{
		pool_error("pool_query_cache: malloc() failed");
		free(sql);
		return;
	}

#ifdef HAVE_PQPREPARE
	snprintf(sql, sql_len,
			 "INSERT INTO %s.%s VALUES ( $1, $2, $3, $4, $5 )",
			 pool_config->system_db_schema,
			 QUERY_CACHE_TABLE_NAME);
	pg_result = PQprepare(system_db_info->pgconn,
						  CACHE_TABLE_INFO.register_prepared_statement,
						  sql,
						  5,
						  NULL);
#else
	snprintf(sql, sql_len,
			 "PREPARE %s (TEXT, TEXT, BYTEA, TEXT, TIMESTAMP WITH TIME ZONE) AS INSERT INTO %s.%s VALUES ( $1, $2, $3, $4, $5 )",
			 CACHE_TABLE_INFO.register_prepared_statement,
			 pool_config->system_db_schema,
			 QUERY_CACHE_TABLE_NAME);
	pg_result = PQexec(system_db_info->pgconn, sql);
#endif
	if (!pg_result || PQresultStatus(pg_result) != PGRES_COMMAND_OK)
	{
		pool_error("pool_query_cache: PQprepare() failed: %s", PQerrorMessage(system_db_info->pgconn));
		free(CACHE_TABLE_INFO.register_prepared_statement);
		free(sql);
		return;
	}

	pool_debug("pool_query_cache: prepared statements created");
	CACHE_TABLE_INFO.has_prepared_statement = 1;

	free(sql);
	PQclear(pg_result);
}

/* --------------------------------
 * Execute query cache look up
 * --------------------------------
 */
POOL_STATUS pool_execute_query_cache_lookup(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend, Node *node)
{
	SelectStmt *select = (SelectStmt *)node;
	POOL_STATUS status = POOL_END;	/* cache not found */

	if (! (select->intoClause || select->lockingClause))
	{
		parsed_query = strdup(nodeToString(node));
		if (parsed_query == NULL)
		{
			pool_error("pool_execute_query_cache_lookup: malloc failed");
			return POOL_ERROR;
		}

		status = pool_query_cache_lookup(frontend, parsed_query, backend->info->database, TSTATE(backend, MASTER_NODE_ID));
		if (status == POOL_CONTINUE)
		{
			free(parsed_query);
			parsed_query = NULL;
		}
	}

	return status;
}