|
| 1 | +-- complain if script is sourced in psql, rather than via CREATE EXTENSION |
| 2 | +\echo Use"CREATE EXTENSION aqo" to load this file. \quit |
| 3 | + |
| 4 | +CREATETABLEpublic.aqo_queries ( |
| 5 | +query_hashintCONSTRAINT aqo_queries_query_hash_idxPRIMARY KEY, |
| 6 | +learn_aqobooleanNOT NULL, |
| 7 | +use_aqobooleanNOT NULL, |
| 8 | +fspace_hashintNOT NULL, |
| 9 | +auto_tuningbooleanNOT NULL |
| 10 | +); |
| 11 | + |
| 12 | +CREATETABLEpublic.aqo_query_texts ( |
| 13 | +query_hashintCONSTRAINT aqo_query_texts_query_hash_idxPRIMARY KEYREFERENCESpublic.aqo_queriesON DELETE CASCADE, |
| 14 | +query_texttextNOT NULL |
| 15 | +); |
| 16 | + |
| 17 | +CREATETABLEpublic.aqo_query_stat ( |
| 18 | +query_hashintCONSTRAINT aqo_query_stat_idxPRIMARY KEYREFERENCESpublic.aqo_queriesON DELETE CASCADE, |
| 19 | +execution_time_with_aqodouble precision[], |
| 20 | +execution_time_without_aqodouble precision[], |
| 21 | +planning_time_with_aqodouble precision[], |
| 22 | +planning_time_without_aqodouble precision[], |
| 23 | +cardinality_error_with_aqodouble precision[], |
| 24 | +cardinality_error_without_aqodouble precision[], |
| 25 | +executions_with_aqobigint, |
| 26 | +executions_without_aqobigint |
| 27 | +); |
| 28 | + |
| 29 | +CREATETABLEpublic.aqo_data ( |
| 30 | +fspace_hashintNOT NULLREFERENCESpublic.aqo_queriesON DELETE CASCADE, |
| 31 | +fsspace_hashintNOT NULL, |
| 32 | +nfeaturesintNOT NULL, |
| 33 | +featuresdouble precision[][], |
| 34 | +targetsdouble precision[] |
| 35 | +); |
| 36 | + |
| 37 | +CREATEUNIQUE INDEXaqo_fss_access_idxONpublic.aqo_data (fspace_hash, fsspace_hash); |
| 38 | + |
| 39 | +INSERT INTOpublic.aqo_queriesVALUES (0, false, false,0, false); |
| 40 | +INSERT INTOpublic.aqo_query_textsVALUES (0,'COMMON feature space (do not delete!)'); |
| 41 | +-- a virtual query for COMMON feature space |
| 42 | + |
| 43 | +CREATEFUNCTIONinvalidate_deactivated_queries_cache() RETURNS trigger |
| 44 | +AS'MODULE_PATHNAME' LANGUAGE C; |
| 45 | + |
| 46 | +CREATETRIGGERaqo_queries_invalidate AFTERUPDATEORDELETEOR TRUNCATE |
| 47 | +ONpublic.aqo_queries FOR EACH STATEMENT |
| 48 | +EXECUTE PROCEDURE invalidate_deactivated_queries_cache(); |
| 49 | + |
| 50 | +-- |
| 51 | +-- Service functions |
| 52 | +-- |
| 53 | + |
| 54 | +-- Show query state at the AQO knowledge base |
| 55 | +CREATEFUNCTIONpublic.aqo_status(hashint) |
| 56 | +RETURNS TABLE ( |
| 57 | +"learn"BOOL, |
| 58 | +"use aqo"BOOL, |
| 59 | +"auto tune"BOOL, |
| 60 | +"fspace hash"INT, |
| 61 | +"t_naqo"TEXT, |
| 62 | +"err_naqo"TEXT, |
| 63 | +"iters"BIGINT, |
| 64 | +"t_aqo"TEXT, |
| 65 | +"err_aqo"TEXT, |
| 66 | +"iters_aqo"BIGINT |
| 67 | +) |
| 68 | +AS $func$ |
| 69 | +SELECTlearn_aqo,use_aqo,auto_tuning,fspace_hash, |
| 70 | +to_char(execution_time_without_aqo[n4],'9.99EEEE'), |
| 71 | +to_char(cardinality_error_without_aqo[n2],'9.99EEEE'), |
| 72 | +executions_without_aqo, |
| 73 | +to_char(execution_time_with_aqo[n3],'9.99EEEE'), |
| 74 | +to_char(cardinality_error_with_aqo[n1],'9.99EEEE'), |
| 75 | +executions_with_aqo |
| 76 | +FROMpublic.aqo_queries aq,public.aqo_query_stat aqs, |
| 77 | +(SELECT array_length(n1,1)AS n1, array_length(n2,1)AS n2, |
| 78 | +array_length(n3,1)AS n3, array_length(n4,1)AS n4 |
| 79 | +FROM |
| 80 | +(SELECT cardinality_error_with_aqoAS n1, |
| 81 | +cardinality_error_without_aqoAS n2, |
| 82 | +execution_time_with_aqoAS n3, |
| 83 | +execution_time_without_aqoAS n4 |
| 84 | +FROMpublic.aqo_query_stat aqsWHERE |
| 85 | +aqs.query_hash= $1)AS al)AS q |
| 86 | +WHERE (aqs.query_hash=aq.query_hash)AND |
| 87 | +aqs.query_hash= $1; |
| 88 | +$func$ LANGUAGE SQL; |
| 89 | + |
| 90 | +CREATEFUNCTIONpublic.aqo_enable_query(hashint) |
| 91 | +RETURNS VOID |
| 92 | +AS $func$ |
| 93 | +UPDATEpublic.aqo_queriesSET |
| 94 | +learn_aqo='true', |
| 95 | +use_aqo='true' |
| 96 | +WHERE query_hash= $1; |
| 97 | +$func$ LANGUAGE SQL; |
| 98 | + |
| 99 | +CREATEFUNCTIONpublic.aqo_disable_query(hashint) |
| 100 | +RETURNS VOID |
| 101 | +AS $func$ |
| 102 | +UPDATEpublic.aqo_queriesSET |
| 103 | +learn_aqo='false', |
| 104 | +use_aqo='false', |
| 105 | +auto_tuning='false' |
| 106 | +WHERE query_hash= $1; |
| 107 | +$func$ LANGUAGE SQL; |
| 108 | + |
| 109 | +CREATEFUNCTIONpublic.aqo_clear_hist(hashint) |
| 110 | +RETURNS VOID |
| 111 | +AS $func$ |
| 112 | +DELETEFROMpublic.aqo_dataWHERE fspace_hash=$1; |
| 113 | +$func$ LANGUAGE SQL; |
| 114 | + |
| 115 | +-- Show queries that contains 'Never executed' nodes at the plan. |
| 116 | +CREATEFUNCTIONpublic.aqo_ne_queries() |
| 117 | +RETURNS SETOFint |
| 118 | +AS $func$ |
| 119 | +SELECT query_hashFROMpublic.aqo_query_stat aqs |
| 120 | +WHERE-1= ANY (cardinality_error_with_aqo::double precision[]); |
| 121 | +$func$ LANGUAGE SQL; |
| 122 | + |
| 123 | +CREATEFUNCTIONpublic.aqo_drop(hashint) |
| 124 | +RETURNS VOID |
| 125 | +AS $func$ |
| 126 | +DELETEFROMpublic.aqo_queries aqWHERE (aq.query_hash= $1); |
| 127 | +DELETEFROMpublic.aqo_data adWHERE (ad.fspace_hash= $1); |
| 128 | +DELETEFROMpublic.aqo_query_stat aqWHERE (aq.query_hash= $1); |
| 129 | +DELETEFROMpublic.aqo_query_texts aqWHERE (aq.query_hash= $1); |
| 130 | +$func$ LANGUAGE SQL; |