@@ -507,7 +507,7 @@ _form_qtext_record_cb(void *ctx, size_t *size)
507507{
508508 HASH_SEQ_STATUS * hash_seq = (HASH_SEQ_STATUS * ) ctx ;
509509 QueryTextEntry * entry ;
510- void * data ;
510+ void * data ;
511511 char * query_string ;
512512 char * ptr ;
513513
@@ -784,7 +784,7 @@ _deform_qtexts_record_cb(void *data, size_t size)
784784 HASH_ENTER , & found );
785785 Assert (!found );
786786
787- entry -> qtext_dp = dsa_allocate (qtext_dsa , len );
787+ entry -> qtext_dp = dsa_allocate_extended (qtext_dsa , len , DSA_ALLOC_NO_OOM | DSA_ALLOC_ZERO );
788788 if (!_check_dsa_validity (entry -> qtext_dp ))
789789 {
790790 /*
@@ -829,7 +829,7 @@ aqo_qtexts_load(void)
829829
830830 if (!found )
831831 {
832- if (!aqo_qtext_store (0 , "COMMON feature space (do not delete!)" ))
832+ if (!aqo_qtext_store (0 , "COMMON feature space (do not delete!)" , NULL ))
833833 elog (PANIC , "[AQO] DSA Initialization was unsuccessful" );
834834 }
835835}
@@ -944,6 +944,49 @@ aqo_queries_load(void)
944944 }
945945}
946946
947+ static long
948+ aqo_get_file_size (const char * filename )
949+ {
950+ FILE * file ;
951+ long size = 0 ;
952+
953+ file = AllocateFile (filename , PG_BINARY_R );
954+ if (file == NULL )
955+ {
956+ if (errno != ENOENT )
957+ goto read_error ;
958+ return size ;
959+ }
960+
961+ fseek (file , 0L , SEEK_END );
962+ size = ftell (file );
963+
964+ FreeFile (file );
965+ return size ;
966+
967+ read_error :
968+ ereport (LOG ,
969+ (errcode_for_file_access (),
970+ errmsg ("could not read file \"%s\": %m" , filename )));
971+ if (file )
972+ FreeFile (file );
973+ unlink (filename );
974+ return -1 ;
975+ }
976+
977+ void
978+ check_dsa_file_size (void )
979+ {
980+ long qtext_size = aqo_get_file_size (PGAQO_TEXT_FILE );
981+ long data_size = aqo_get_file_size (PGAQO_DATA_FILE );
982+
983+ if (qtext_size == -1 || data_size == -1 ||
984+ qtext_size + data_size >= dsm_size_max * 1024 * 1024 )
985+ {
986+ elog (ERROR , "aqo.dsm_size_max is too small" );
987+ }
988+ }
989+
947990static void
948991data_load (const char * filename , deform_record_t callback , void * ctx )
949992{
@@ -1090,13 +1133,16 @@ dsa_init()
10901133 * XXX: Maybe merge with aqo_queries ?
10911134 */
10921135bool
1093- aqo_qtext_store (uint64 queryid , const char * query_string )
1136+ aqo_qtext_store (uint64 queryid , const char * query_string , bool * dsa_valid )
10941137{
10951138 QueryTextEntry * entry ;
10961139 bool found ;
10971140 bool tblOverflow ;
10981141 HASHACTION action ;
10991142
1143+ if (dsa_valid )
1144+ * dsa_valid = true;
1145+
11001146 Assert (!LWLockHeldByMe (& aqo_state -> qtexts_lock ));
11011147
11021148 if (query_string == NULL || querytext_max_size == 0 )
@@ -1135,7 +1181,7 @@ aqo_qtext_store(uint64 queryid, const char *query_string)
11351181
11361182 entry -> queryid = queryid ;
11371183 size = size > querytext_max_size ? querytext_max_size : size ;
1138- entry -> qtext_dp = dsa_allocate0 (qtext_dsa , size );
1184+ entry -> qtext_dp = dsa_allocate_extended (qtext_dsa , size , DSA_ALLOC_NO_OOM | DSA_ALLOC_ZERO );
11391185
11401186 if (!_check_dsa_validity (entry -> qtext_dp ))
11411187 {
@@ -1144,7 +1190,10 @@ aqo_qtext_store(uint64 queryid, const char *query_string)
11441190 * that caller recognize it and don't try to call us more.
11451191 */
11461192 (void ) hash_search (qtexts_htab , & queryid , HASH_REMOVE , NULL );
1193+ _aqo_queries_remove (queryid );
11471194 LWLockRelease (& aqo_state -> qtexts_lock );
1195+ if (dsa_valid )
1196+ * dsa_valid = false;
11481197 return false;
11491198 }
11501199
@@ -1423,7 +1472,7 @@ aqo_data_store(uint64 fs, int fss, AqoDataArgs *data, List *reloids)
14231472 entry -> nrels = nrels ;
14241473
14251474 size = _compute_data_dsa (entry );
1426- entry -> data_dp = dsa_allocate0 (data_dsa , size );
1475+ entry -> data_dp = dsa_allocate_extended (data_dsa , size , DSA_ALLOC_NO_OOM | DSA_ALLOC_ZERO );
14271476
14281477 if (!_check_dsa_validity (entry -> data_dp ))
14291478 {
@@ -1455,7 +1504,7 @@ aqo_data_store(uint64 fs, int fss, AqoDataArgs *data, List *reloids)
14551504
14561505 /* Need to re-allocate DSA chunk */
14571506 dsa_free (data_dsa , entry -> data_dp );
1458- entry -> data_dp = dsa_allocate0 (data_dsa , size );
1507+ entry -> data_dp = dsa_allocate_extended (data_dsa , size , DSA_ALLOC_NO_OOM | DSA_ALLOC_ZERO );
14591508
14601509 if (!_check_dsa_validity (entry -> data_dp ))
14611510 {
@@ -2713,7 +2762,7 @@ aqo_query_texts_update(PG_FUNCTION_ARGS)
27132762
27142763 str_buff = (char * ) palloc (str_len );
27152764 text_to_cstring_buffer (str , str_buff , str_len );
2716- res = aqo_qtext_store (queryid , str_buff );
2765+ res = aqo_qtext_store (queryid , str_buff , NULL );
27172766 pfree (str_buff );
27182767
27192768 PG_RETURN_BOOL (res );
0 commit comments