syncdb_preflight.c (3649B)
1 /* 2 This file is part of TALER 3 (C) 2014--2022 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Lesser General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file syncdb/syncdb_preflight.c 18 * @brief preflight check for sync database 19 * @author Christian Grothoff 20 */ 21 #include "syncdb_pg.h" 22 23 24 static enum GNUNET_GenericReturnValue 25 internal_setup (void) 26 { 27 if (NULL == pg->conn) 28 { 29 #if AUTO_EXPLAIN 30 /* Enable verbose logging to see where queries do not 31 properly use indices */ 32 struct GNUNET_PQ_ExecuteStatement es[] = { 33 GNUNET_PQ_make_try_execute ("LOAD 'auto_explain';"), 34 GNUNET_PQ_make_try_execute ("SET auto_explain.log_min_duration=50;"), 35 GNUNET_PQ_make_try_execute ("SET auto_explain.log_timing=TRUE;"), 36 GNUNET_PQ_make_try_execute ("SET auto_explain.log_analyze=TRUE;"), 37 /* https://wiki.postgresql.org/wiki/Serializable suggests to really 38 force the default to 'serializable' if SSI is to be used. */ 39 GNUNET_PQ_make_try_execute ( 40 "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;"), 41 GNUNET_PQ_make_try_execute ("SET enable_sort=OFF;"), 42 GNUNET_PQ_make_try_execute ("SET enable_seqscan=OFF;"), 43 GNUNET_PQ_make_execute ("SET search_path TO sync;"), 44 GNUNET_PQ_EXECUTE_STATEMENT_END 45 }; 46 #else 47 struct GNUNET_PQ_ExecuteStatement es[] = { 48 GNUNET_PQ_make_execute ("SET search_path TO sync;"), 49 GNUNET_PQ_EXECUTE_STATEMENT_END 50 }; 51 #endif 52 struct GNUNET_PQ_Context *db_conn; 53 54 db_conn = GNUNET_PQ_connect_with_cfg2 (pg->cfg, 55 "syncdb-postgres", 56 "sync-", 57 es, 58 NULL, 59 GNUNET_PQ_FLAG_CHECK_CURRENT); 60 if (NULL == db_conn) 61 return GNUNET_SYSERR; 62 pg->conn = db_conn; 63 pg->prep_gen++; 64 } 65 if (NULL == pg->transaction_name) 66 GNUNET_PQ_reconnect_if_down (pg->conn); 67 return GNUNET_OK; 68 } 69 70 71 enum GNUNET_GenericReturnValue 72 SYNCDB_preflight (void) 73 { 74 struct GNUNET_PQ_ExecuteStatement es[] = { 75 GNUNET_PQ_make_execute ("ROLLBACK"), 76 GNUNET_PQ_EXECUTE_STATEMENT_END 77 }; 78 79 if (NULL == pg->conn) 80 { 81 if (GNUNET_OK != 82 internal_setup ()) 83 { 84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 85 "Failed to ensure DB is initialized\n"); 86 return GNUNET_SYSERR; 87 } 88 } 89 else 90 { 91 GNUNET_PQ_reconnect_if_down (pg->conn); 92 } 93 if (NULL == pg->transaction_name) 94 return GNUNET_OK; /* all good */ 95 if (GNUNET_OK == 96 GNUNET_PQ_exec_statements (pg->conn, 97 es)) 98 { 99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 100 "BUG: Preflight check rolled back transaction `%s'!\n", 101 pg->transaction_name); 102 } 103 else 104 { 105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 106 "BUG: Preflight check failed to rollback transaction `%s'!\n", 107 pg->transaction_name); 108 } 109 pg->transaction_name = NULL; 110 return GNUNET_NO; 111 } 112 113 114 /* end of syncdb_preflight.c */