diff -crN postgresql-8.1.0.orig/configure postgresql-8.1.0/configure
*** postgresql-8.1.0.orig/configure	2005-11-05 13:01:38.000000000 +0900
--- postgresql-8.1.0/configure	2005-12-14 02:57:09.000000000 +0900
***************
*** 2714,2720 ****
  fi
  
  if test "$GCC" = yes; then
!   CFLAGS="$CFLAGS -Wall -Wmissing-prototypes -Wpointer-arith -Winline"
  
    # Some versions of GCC support some additional useful warning flags.
    # Check whether they are supported, and add them to CFLAGS if so.
--- 2714,2720 ----
  fi
  
  if test "$GCC" = yes; then
!   CFLAGS="$CFLAGS -Wall -Wmissing-prototypes -Wpointer-arith -Winline -D_QUERY_CACHE"
  
    # Some versions of GCC support some additional useful warning flags.
    # Check whether they are supported, and add them to CFLAGS if so.
diff -crN postgresql-8.1.0.orig/configure.in postgresql-8.1.0/configure.in
*** postgresql-8.1.0.orig/configure.in	2005-11-05 13:01:41.000000000 +0900
--- postgresql-8.1.0/configure.in	2005-12-14 02:56:53.000000000 +0900
***************
*** 251,257 ****
  fi
  
  if test "$GCC" = yes; then
!   CFLAGS="$CFLAGS -Wall -Wmissing-prototypes -Wpointer-arith -Winline"
  
    # Some versions of GCC support some additional useful warning flags.
    # Check whether they are supported, and add them to CFLAGS if so.
--- 251,257 ----
  fi
  
  if test "$GCC" = yes; then
!   CFLAGS="$CFLAGS -Wall -Wmissing-prototypes -Wpointer-arith -Winline -D_QUERY_CACHE"
  
    # Some versions of GCC support some additional useful warning flags.
    # Check whether they are supported, and add them to CFLAGS if so.
diff -crN postgresql-8.1.0.orig/src/backend/libpq/pqcomm.c postgresql-8.1.0/src/backend/libpq/pqcomm.c
*** postgresql-8.1.0.orig/src/backend/libpq/pqcomm.c	2005-10-18 01:24:19.000000000 +0900
--- postgresql-8.1.0/src/backend/libpq/pqcomm.c	2005-12-14 02:56:54.000000000 +0900
***************
*** 65,70 ****
--- 65,74 ----
   */
  #include "postgres.h"
  
+ #ifdef _QUERY_CACHE
+ #include "querycache.h"
+ #endif
+ 
  #include <signal.h>
  #include <errno.h>
  #include <fcntl.h>
***************
*** 103,110 ****
--- 107,121 ----
  /*
   * Buffers for low-level I/O
   */
+ #ifdef _QUERY_CACHE
  
+ #ifndef PQ_BUFFER_SIZE
  #define PQ_BUFFER_SIZE 8192
+ #endif
+ 
+ #else
+ #define PQ_BUFFER_SIZE 8192
+ #endif
  
  static char PqSendBuffer[PQ_BUFFER_SIZE];
  static int	PqSendPointer;		/* Next index to store a byte in PqSendBuffer */
***************
*** 1007,1012 ****
--- 1018,1042 ----
   *		returns 0 if OK, EOF if trouble
   * --------------------------------
   */
+ #ifdef _QUERY_CACHE
+ 
+ int qc_pq_flush (const unsigned char *data, int len);
+ 
+ 
+ int
+ qc_pq_flush (const unsigned char *data, int len)
+ {
+   if (query_cache && query_cache_local)
+     {
+       PqSendPointer = len;
+       memcpy ((void *)PqSendBuffer, (const void *)data, len);
+       return pq_flush ();
+     }
+   return 0;  /* dummy */
+ }
+ 
+ #endif
+ 
  int
  pq_flush(void)
  {
***************
*** 1029,1034 ****
--- 1059,1073 ----
  	char	   *bufptr = PqSendBuffer;
  	char	   *bufend = PqSendBuffer + PqSendPointer;
  
+ #ifdef _QUERY_CACHE
+ 	/* ************************************************
+ 	 * ************** NOT SAFE ! REWRITE ! ************
+ 	 * ************************************************/
+ 	if (query_cache && query_cache_local)
+ 	  ///	  qc_get_data_from_buffer_to_current_query (PqSendBuffer, PqSendPointer);
+ 	  qc_get_data_from_buffer_to_current_query (bufptr, PqSendPointer);
+ #endif
+ 
  	while (bufptr < bufend)
  	{
  		int			r;
diff -crN postgresql-8.1.0.orig/src/backend/parser/parse_clause.c postgresql-8.1.0/src/backend/parser/parse_clause.c
*** postgresql-8.1.0.orig/src/backend/parser/parse_clause.c	2005-10-15 11:49:22.000000000 +0900
--- postgresql-8.1.0/src/backend/parser/parse_clause.c	2005-12-14 02:56:54.000000000 +0900
***************
*** 14,19 ****
--- 14,22 ----
   */
  
  #include "postgres.h"
+ #ifdef _QUERY_CACHE
+ #include "querycache.h"
+ #endif
  
  #include "access/heapam.h"
  #include "catalog/heap.h"
***************
*** 163,168 ****
--- 166,177 ----
  										NULL, inh, false);
  	pstate->p_target_rangetblentry = rte;
  
+ 
+ #ifdef _QUERY_CACHE
+ 	if (query_cache && query_cache_local)
+ 	  qc_set_oid (rte->relid);
+ #endif
+ 
  	/* assume new rte is at end */
  	rtindex = list_length(pstate->p_rtable);
  	Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
***************
*** 395,400 ****
--- 404,420 ----
  {
  	RangeTblEntry *rte;
  
+ #ifdef _QUERY_CACHE
+ 	/**
+ 	 * Processing is possible even with 
+ 	 * transformFromClause(ParseState *pstate, List *frmList) 
+ 	 */
+ 	if (query_cache && query_cache_local)
+ 	  {
+ 	    Oid oid = RangeVarGetRelid (r, false);
+ 	    qc_set_oid (oid);
+ 	  }
+ #endif
  	/*
  	 * mark this entry to indicate it comes from the FROM clause. In SQL, the
  	 * target list can only refer to range variables specified in the from
diff -crN postgresql-8.1.0.orig/src/backend/storage/ipc/ipci.c postgresql-8.1.0/src/backend/storage/ipc/ipci.c
*** postgresql-8.1.0.orig/src/backend/storage/ipc/ipci.c	2005-10-15 11:49:25.000000000 +0900
--- postgresql-8.1.0/src/backend/storage/ipc/ipci.c	2005-12-14 02:56:54.000000000 +0900
***************
*** 92,98 ****
  #ifdef EXEC_BACKEND
  		size = add_size(size, ShmemBackendArraySize());
  #endif
! 
  		/* might as well round it off to a multiple of a typical page size */
  		size = add_size(size, 8192 - (size % 8192));
  
--- 92,100 ----
  #ifdef EXEC_BACKEND
  		size = add_size(size, ShmemBackendArraySize());
  #endif
! #ifdef _QUERY_CACHE
! 		size += QueryCacheShmemSize();
! #endif
  		/* might as well round it off to a multiple of a typical page size */
  		size = add_size(size, 8192 - (size % 8192));
  
***************
*** 177,182 ****
--- 179,191 ----
  	 */
  	InitFreeSpaceMap();
  
+ #ifdef _QUERY_CACHE
+ 	/*
+ 	 * Set up query-cache space
+ 	 */
+ 	InitQueryCacheShmem();
+ #endif
+ 
  	/*
  	 * Set up interprocess signaling mechanisms
  	 */
diff -crN postgresql-8.1.0.orig/src/backend/tcop/Makefile postgresql-8.1.0/src/backend/tcop/Makefile
*** postgresql-8.1.0.orig/src/backend/tcop/Makefile	2003-11-30 04:51:57.000000000 +0900
--- postgresql-8.1.0/src/backend/tcop/Makefile	2005-12-14 03:12:45.000000000 +0900
***************
*** 12,18 ****
  top_builddir = ../../..
  include $(top_builddir)/src/Makefile.global
  
! OBJS= dest.o fastpath.o postgres.o pquery.o utility.o
  
  all: SUBSYS.o
  
--- 12,18 ----
  top_builddir = ../../..
  include $(top_builddir)/src/Makefile.global
  
! OBJS= dest.o fastpath.o postgres.o pquery.o utility.o querycache.o
  
  all: SUBSYS.o
  
diff -crN postgresql-8.1.0.orig/src/backend/tcop/postgres.c postgresql-8.1.0/src/backend/tcop/postgres.c
*** postgresql-8.1.0.orig/src/backend/tcop/postgres.c	2005-11-04 02:11:38.000000000 +0900
--- postgresql-8.1.0/src/backend/tcop/postgres.c	2005-12-14 02:56:54.000000000 +0900
***************
*** 19,24 ****
--- 19,28 ----
  
  #include "postgres.h"
  
+ #ifdef _QUERY_CACHE
+ #include "querycache.h"
+ #endif
+ 
  #include <unistd.h>
  #include <signal.h>
  #include <fcntl.h>
***************
*** 902,907 ****
--- 906,916 ----
  		 * destination.
  		 */
  		commandTag = CreateCommandTag(parsetree);
+ #ifdef _QUERY_CACHE
+ 		if (query_cache && query_cache_local)
+ 		  if (qc_check_query (commandTag, query_string) == true)
+ 		    goto QUERYCACHE; /* I don't hesitate using "GOTO". */
+ #endif
  
  		set_ps_display(commandTag);
  
***************
*** 1058,1069 ****
--- 1067,1091 ----
  		 * command the client sent, regardless of rewriting. (But a command
  		 * aborted by error will not send an EndCommand report at all.)
  		 */
+ #ifdef _QUERY_CACHE
+ 	QUERYCACHE:
+ #endif
  		EndCommand(completionTag, dest);
  	}							/* end loop over parsetrees */
  
  	/*
  	 * Close down transaction statement, if one is open.
  	 */
+ #ifdef _QUERY_CACHE
+ 	if (query_cache && query_cache_local)
+ 	  {
+ 	    pq_flush ();
+ 	    qc_operate_cache ();
+ 
+ 	    _check_query_cache ("");
+ 	  }
+ #endif
+ 
  	finish_xact_command();
  
  	/*
***************
*** 3071,3076 ****
--- 3093,3101 ----
  	/*
  	 * Non-error queries loop here.
  	 */
+ #ifdef _QUERY_CACHE
+ 	qc_init ();
+ #endif
  
  	for (;;)
  	{
diff -crN postgresql-8.1.0.orig/src/backend/utils/misc/guc.c postgresql-8.1.0/src/backend/utils/misc/guc.c
*** postgresql-8.1.0.orig/src/backend/utils/misc/guc.c	2005-11-05 08:50:30.000000000 +0900
--- postgresql-8.1.0/src/backend/utils/misc/guc.c	2005-12-14 02:56:54.000000000 +0900
***************
*** 177,182 ****
--- 177,191 ----
  
  int			num_temp_buffers = 1000;
  
+ #ifdef _QUERY_CACHE
+ bool		query_cache = false;
+ bool		query_cache_local = true;
+ int		query_cache_check_level = 0;
+ int             query_cache_oblock_num = 4092;
+ int             query_cache_qblock_num = 1024;
+ int             query_cache_max_store_block_num = 20;
+ #endif
+ 
  char	   *ConfigFileName;
  char	   *HbaFileName;
  char	   *IdentFileName;
***************
*** 966,972 ****
  		&standard_conforming_strings,
  		false, NULL, NULL
  	},
! 
  	/* End-of-list marker */
  	{
  		{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL
--- 975,998 ----
  		&standard_conforming_strings,
  		false, NULL, NULL
  	},
! #ifdef _QUERY_CACHE
! 	{
! 		{"query_cache", PGC_POSTMASTER, DEVELOPER_OPTIONS,
! 			gettext_noop("Query Cache (experimental)."),
! 			NULL
! 		},
! 		&query_cache,
! 		false, NULL, NULL
! 	},
! 	{
! 		{"query_cache_local", PGC_USERSET, DEVELOPER_OPTIONS,
! 			gettext_noop("Query Cache Local (experimental)."),
! 			NULL
! 		},
! 		&query_cache_local,
! 		true, NULL, NULL
! 	},
! #endif
  	/* End-of-list marker */
  	{
  		{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL
***************
*** 1525,1530 ****
--- 1551,1590 ----
  		0, 0, INT_MAX, assign_tcp_keepalives_count, show_tcp_keepalives_count
  	},
  
+ #ifdef _QUERY_CACHE
+ 	{
+ 		{"query_cache_check_level", PGC_USERSET, RESOURCES_MEM,
+ 			gettext_noop("Sets the maximum number of data block."),
+ 			NULL
+ 		},
+ 		&query_cache_check_level,
+ 		0, 0, 4, NULL, NULL
+ 	},
+ 	{
+ 		{"query_cache_oblock_num", PGC_POSTMASTER, RESOURCES_MEM,
+ 			gettext_noop("Sets the number of table_blocks of query cache table chunk."),
+ 			NULL
+ 		},
+ 		&query_cache_oblock_num,
+ 		4092, 512, INT_MAX / 8, NULL, NULL
+ 	},
+ 	{
+ 		{"query_cache_qblock_num", PGC_POSTMASTER, RESOURCES_MEM,
+ 			gettext_noop("Sets the number of query_blocks of query cache table chunk."),
+ 			NULL
+ 		},
+ 		&query_cache_qblock_num,
+ 		1024, 128, INT_MAX / 32, NULL, NULL
+ 	},
+ 	{
+ 		{"query_cache_max_store_block_num", PGC_USERSET, RESOURCES_MEM,
+ 			gettext_noop("Sets the maximum number of data block."),
+ 			NULL
+ 		},
+ 		&query_cache_max_store_block_num,
+ 		20, 5, 32, NULL, NULL
+ 	},
+ #endif
  	/* End-of-list marker */
  	{
  		{NULL, 0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL
diff -crN postgresql-8.1.0.orig/src/include/storage/lwlock.h postgresql-8.1.0/src/include/storage/lwlock.h
*** postgresql-8.1.0.orig/src/include/storage/lwlock.h	2005-10-15 11:49:46.000000000 +0900
--- postgresql-8.1.0/src/include/storage/lwlock.h	2005-12-14 02:56:54.000000000 +0900
***************
*** 48,54 ****
  	TwoPhaseStateLock,
  
  	NumFixedLWLocks,			/* must be last except for MaxDynamicLWLock */
! 
  	MaxDynamicLWLock = 1000000000
  } LWLockId;
  
--- 48,56 ----
  	TwoPhaseStateLock,
  
  	NumFixedLWLocks,			/* must be last except for MaxDynamicLWLock */
! #ifdef _QUERY_CACHE
! 	QueryCacheLock,
! #endif
  	MaxDynamicLWLock = 1000000000
  } LWLockId;
  
diff -crN postgresql-8.1.0.orig/src/include/utils/guc.h postgresql-8.1.0/src/include/utils/guc.h
*** postgresql-8.1.0.orig/src/include/utils/guc.h	2005-10-15 11:49:46.000000000 +0900
--- postgresql-8.1.0/src/include/utils/guc.h	2005-12-14 02:56:54.000000000 +0900
***************
*** 129,134 ****
--- 129,144 ----
  
  extern int	num_temp_buffers;
  
+ #ifdef _QUERY_CACHE
+ extern bool	query_cache;
+ extern bool	query_cache_local;
+ extern int	query_cache_check_level;
+ extern int      query_cache_oblock_num;
+ extern int      query_cache_qblock_num;
+ extern int      query_cache_max_store_block_num;
+ #endif
+ 
+ 
  extern char *ConfigFileName;
  extern char *HbaFileName;
  extern char *IdentFileName;
