Changeset 35 for trunk


Ignore:
Timestamp:
May 19, 2006, 8:35:34 PM (18 years ago)
Author:
rainer
Message:

Fix concurrent inserts with postgres

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sql_init/samhain.postgres.init

    r1 r35  
    11CREATE SEQUENCE log_log_index_seq START 1;
    22CREATE TABLE    log (
    3         log_index INTEGER NOT NULL DEFAULT NEXTVAL('log_log_index_seq'),
     3        log_index INTEGER NOT NULL,
    44        log_ref   BIGINT NULL,
    55        log_host  VARCHAR(64)   NOT NULL DEFAULT 'localhost',
  • trunk/src/sh_database.c

    r30 r35  
    870870  PGresult        * res;
    871871  unsigned int      i;
     872  const char      * params[1];
     873  char              id_param[32];
    872874  static SH_TIMEOUT sh_timer = { 0, 3600, S_TRUE };
    873875
     
    940942
    941943
    942   /* do the insert
    943    */
    944   res = PQexec(conn, query);
    945   if (PQresultStatus(res) != PGRES_COMMAND_OK)
     944  /* get the unique row index
     945   */
     946  res = PQexec(conn, _("SELECT NEXTVAL('log_log_index_seq')"));
     947  if (PQresultStatus(res) != PGRES_TUPLES_OK)
    946948    {
    947949      PQclear(res);
    948950      goto err_out;
    949951    }
     952
     953  *id = atoi (PQgetvalue(res, 0, 0));
    950954  PQclear(res);
    951955
    952   /* get the unique row index
    953    */
    954   res = PQexec(conn, _("SELECT last_value FROM log_log_index_seq"));
    955   if (PQresultStatus(res) != PGRES_TUPLES_OK)
     956  sl_snprintf(id_param, 32, "%ld", *id);
     957  params[0] = id_param;
     958
     959  /* do the insert
     960   */
     961  res = PQexecParams(conn, query, 1, NULL, params, NULL, NULL, 1);
     962  if (PQresultStatus(res) != PGRES_COMMAND_OK)
    956963    {
    957964      PQclear(res);
    958965      goto err_out;
    959966    }
    960 
    961   *id = atoi (PQgetvalue(res, 0, 0));
    962 
    963967  PQclear(res);
     968
    964969  if (S_FALSE == sh_persistent_dbconn)
    965970    {
     
    12441249  /*@+bufferoverflowhigh@*/
    12451250
    1246 #ifdef WITH_ORACLE
     1251#if defined(WITH_ORACLE)
    12471252  /* Oracle needs some help for the time format (fix by Michael Somers)
    12481253   */
     
    12571262               (db_entry->msg[0] == '\0' ? _("NULL") : db_entry->msg),
    12581263               '\'');
     1264  (void) sl_snprintf (columns, 1023,
     1265                      _("(log_ref,log_host,log_time,log_sev,log_msg"));
     1266#elif defined(WITH_POSTGRES)
     1267  /* Prepare query for PQexecParams
     1268   */
     1269  (void)
     1270  sl_snprintf (values, SH_QUERY_MAX,
     1271               _("($1,%s,%c%s%c,%c%s%c,%c%s%c,%c%s%c"),
     1272               id >= 0 ? num : _("NULL"),
     1273               '\'', db_entry->host,'\'',
     1274               '\'', db_entry->time,'\'',
     1275               '\'', db_entry->sev, '\'',
     1276               '\'',
     1277               (db_entry->msg[0] == '\0' ? _("NULL") : db_entry->msg),
     1278               '\'');
     1279  (void) sl_snprintf (columns, 1023,
     1280                      _("(log_index,log_ref,log_host,log_time,log_sev,log_msg"));
    12591281#else
    12601282  (void)
     
    12671289               (db_entry->msg[0] == '\0' ? _("NULL") : db_entry->msg),
    12681290               '\'');
    1269 #endif
    1270 
    12711291  (void) sl_snprintf (columns, 1023,
    12721292                      _("(log_ref,log_host,log_time,log_sev,log_msg"));
     1293#endif
     1294
    12731295
    12741296  /*@-type@*//* byte* versus char[..] */
     
    16661688/* recursively enter linked list of messages into database, last first
    16671689 */
    1668 int sh_database_insert_rec (dbins * curr, unsigned int depth)
    1669 {
    1670   long    id = 0;
     1690long sh_database_insert_rec (dbins * curr, int depth)
     1691{
     1692  unsigned long    id = 0;
    16711693  dbins * prev;
    16721694
     
    17001722  SH_FREE(curr);
    17011723
    1702   SL_RETURN(id, _("sh_database_insert"));
     1724  SL_RETURN(id, _("sh_database_insert_rec"));
    17031725}
    17041726
Note: See TracChangeset for help on using the changeset viewer.