Change regex combination, allow hex
[oweals/gnunet.git] / src / psycstore / plugin_psycstore_postgres.c
index 8db3c1a61791c5d7cead360c75735063086c387a..273ab4e80fb74f641974ad770ec88e5ddbd2c474 100644 (file)
@@ -100,17 +100,16 @@ database_setup (struct Plugin *plugin)
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE TABLE IF NOT EXISTS channels (\n"
                               " id SERIAL,\n"
-                              " pub_key BYTEA,\n"
-                              " max_state_message_id INT,\n"
-                              " state_hash_message_id INT,\n"
+                              " pub_key BYTEA NOT NULL CHECK (LENGTH(pub_key)=32),\n"
+                              " max_state_message_id BIGINT,\n"
+                              " state_hash_message_id BIGINT,\n"
                               " PRIMARY KEY(id)\n"
                               ")" "WITH OIDS")) ||
 
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE UNIQUE INDEX IF NOT EXISTS channel_pub_key_idx \n"
-                              " ON channels (substring(pub_key from 1 for 5)\n"
-                              ")")) ||
+                              " ON channels (pub_key)")) ||
 
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
@@ -122,15 +121,14 @@ database_setup (struct Plugin *plugin)
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE TABLE IF NOT EXISTS slaves (\n"
                               " id SERIAL,\n"
-                              " pub_key BYTEA,\n"
+                              " pub_key BYTEA NOT NULL CHECK (LENGTH(pub_key)=32),\n"
                               " PRIMARY KEY(id)\n"
                               ")" "WITH OIDS")) ||
 
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE UNIQUE INDEX IF NOT EXISTS slaves_pub_key_idx \n"
-                              " ON slaves (substring(pub_key from 1 for 5)\n"
-                              ")")) ||
+                              " ON slaves (pub_key)")) ||
 
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
@@ -141,8 +139,8 @@ database_setup (struct Plugin *plugin)
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE TABLE IF NOT EXISTS membership (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
-                              "  slave_id INT NOT NULL REFERENCES slaves(id),\n"
+                              "  channel_id BIGINT NOT NULL REFERENCES channels(id),\n"
+                              "  slave_id BIGINT NOT NULL REFERENCES slaves(id),\n"
                               "  did_join INT NOT NULL,\n"
                               "  announced_at BIGINT NOT NULL,\n"
                               "  effective_since BIGINT NOT NULL,\n"
@@ -158,16 +156,16 @@ database_setup (struct Plugin *plugin)
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE TABLE IF NOT EXISTS messages (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
-                              "  hop_counter BIGINT NOT NULL,\n"
-                              "  signature BYTEA,\n"
-                              "  purpose BYTEA,\n"
+                              "  channel_id BIGINT NOT NULL REFERENCES channels(id),\n"
+                              "  hop_counter INT NOT NULL,\n"
+                              "  signature BYTEA CHECK (LENGTH(signature)=64),\n"
+                              "  purpose BYTEA CHECK (LENGTH(purpose)=8),\n"
                               "  fragment_id BIGINT NOT NULL,\n"
                               "  fragment_offset BIGINT NOT NULL,\n"
                               "  message_id BIGINT NOT NULL,\n"
                               "  group_generation BIGINT NOT NULL,\n"
-                              "  multicast_flags BIGINT NOT NULL,\n"
-                              "  psycstore_flags BIGINT NOT NULL,\n"
+                              "  multicast_flags INT NOT NULL,\n"
+                              "  psycstore_flags INT NOT NULL,\n"
                               "  data BYTEA,\n"
                               "  PRIMARY KEY (channel_id, fragment_id),\n"
                               "  UNIQUE (channel_id, message_id, fragment_offset)\n"
@@ -176,32 +174,20 @@ database_setup (struct Plugin *plugin)
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE TABLE IF NOT EXISTS state (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
+                              "  channel_id BIGINT NOT NULL REFERENCES channels(id),\n"
                               "  name TEXT NOT NULL,\n"
                               "  value_current BYTEA,\n"
-                              "  value_signed BYTEA\n"
+                              "  value_signed BYTEA,\n"
+                              "  PRIMARY KEY (channel_id, name)\n"
                               ")" "WITH OIDS")) ||
-
-      (GNUNET_OK !=
-         GNUNET_POSTGRES_exec(plugin->dbh,
-                              "CREATE UNIQUE INDEX IF NOT EXISTS state_uniq_idx \n"
-                              " ON state (channel_id, substring(name from 1 for 5)\n"
-                              ")")) ||
-
       (GNUNET_OK !=
          GNUNET_POSTGRES_exec(plugin->dbh,
                               "CREATE TABLE IF NOT EXISTS state_sync (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
+                              "  channel_id BIGINT NOT NULL REFERENCES channels(id),\n"
                               "  name TEXT NOT NULL,\n"
                               "  value BYTEA,\n"
-                              "  PRIMARY KEY (channel_id)\n"
-                              ")" "WITH OIDS")) ||
-
-      (GNUNET_OK !=
-         GNUNET_POSTGRES_exec(plugin->dbh,
-                              "CREATE UNIQUE INDEX IF NOT EXISTS state_sync_name_idx \n"
-                              " ON state_sync (substring(name from 1 for 5)\n"
-                              ")")))
+                              "  PRIMARY KEY (channel_id, name)\n"
+                              ")" "WITH OIDS")))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -277,159 +263,159 @@ database_setup (struct Plugin *plugin)
 
       /** @todo select_messages: add method_prefix filter */
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_messages",
-                "SELECT hop_counter, signature, purpose, fragment_id,\n"
-                "       fragment_offset, message_id, group_generation,\n"
-                "       multicast_flags, psycstore_flags, data\n"
-                "FROM messages\n"
-                "WHERE channel_id = get_chan_id($1) \n"
-                "      AND $2 <= message_id AND message_id <= $3"
-                "LIMIT $4;", 4)) ||
+                           "select_messages",
+                           "SELECT hop_counter, signature, purpose, fragment_id,\n"
+                           "       fragment_offset, message_id, group_generation,\n"
+                           "       multicast_flags, psycstore_flags, data\n"
+                           "FROM messages\n"
+                           "WHERE channel_id = get_chan_id($1) \n"
+                           "      AND $2 <= message_id AND message_id <= $3\n"
+                           "LIMIT $4;", 4)) ||
 
       /** @todo select_latest_messages: add method_prefix filter */
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_latest_fragments",
-                "SELECT  rev.hop_counter AS hop_counter,\n"
-                "        rev.signature AS signature,\n"
-                "        rev.purpose AS purpose,\n"
-                "        rev.fragment_id AS fragment_id,\n"
-                "        rev.fragment_offset AS fragment_offset,\n"
-                "        rev.message_id AS message_id,\n"
-                "        rev.group_generation AS group_generation,\n"
-                "        rev.multicast_flags AS multicast_flags,\n"
-                "        rev.psycstore_flags AS psycstore_flags,\n"
-                "        rev.data AS data\n"
-                " FROM\n"
-                " (SELECT hop_counter, signature, purpose, fragment_id,\n"
-                "        fragment_offset, message_id, group_generation,\n"
-                "        multicast_flags, psycstore_flags, data \n"
-                "  FROM messages\n"
-                "  WHERE channel_id = get_chan_id($1) \n"
-                "  ORDER BY fragment_id DESC\n"
-                "  LIMIT $2) AS rev\n"
-                " ORDER BY rev.fragment_id;", 2)) ||
+                           "select_latest_fragments",
+                           "SELECT  rev.hop_counter AS hop_counter,\n"
+                           "        rev.signature AS signature,\n"
+                           "        rev.purpose AS purpose,\n"
+                           "        rev.fragment_id AS fragment_id,\n"
+                           "        rev.fragment_offset AS fragment_offset,\n"
+                           "        rev.message_id AS message_id,\n"
+                           "        rev.group_generation AS group_generation,\n"
+                           "        rev.multicast_flags AS multicast_flags,\n"
+                           "        rev.psycstore_flags AS psycstore_flags,\n"
+                           "        rev.data AS data\n"
+                           " FROM\n"
+                           " (SELECT hop_counter, signature, purpose, fragment_id,\n"
+                           "        fragment_offset, message_id, group_generation,\n"
+                           "        multicast_flags, psycstore_flags, data \n"
+                           "  FROM messages\n"
+                           "  WHERE channel_id = get_chan_id($1) \n"
+                           "  ORDER BY fragment_id DESC\n"
+                           "  LIMIT $2) AS rev\n"
+                           " ORDER BY rev.fragment_id;", 2)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_latest_messages",
-                "SELECT hop_counter, signature, purpose, fragment_id,\n"
-                "       fragment_offset, message_id, group_generation,\n"
-                "        multicast_flags, psycstore_flags, data\n"
-                "FROM messages\n"
-                "WHERE channel_id = get_chan_id($1)\n"
-                "      AND message_id IN\n"
-                "      (SELECT message_id\n"
-                "       FROM messages\n"
-                "       WHERE channel_id = get_chan_id($2) \n"
-                "       GROUP BY message_id\n"
-                "       ORDER BY message_id\n"
-                "       DESC LIMIT $3)\n"
-                "ORDER BY fragment_id", 3)) ||
+                           "select_latest_messages",
+                           "SELECT hop_counter, signature, purpose, fragment_id,\n"
+                           "       fragment_offset, message_id, group_generation,\n"
+                           "        multicast_flags, psycstore_flags, data\n"
+                           "FROM messages\n"
+                           "WHERE channel_id = get_chan_id($1)\n"
+                           "      AND message_id IN\n"
+                           "      (SELECT message_id\n"
+                           "       FROM messages\n"
+                           "       WHERE channel_id = get_chan_id($2) \n"
+                           "       GROUP BY message_id\n"
+                           "       ORDER BY message_id\n"
+                           "       DESC LIMIT $3)\n"
+                           "ORDER BY fragment_id", 3)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_message_fragment",
-                "SELECT hop_counter, signature, purpose, fragment_id,\n"
-                "       fragment_offset, message_id, group_generation,\n"
-                "       multicast_flags, psycstore_flags, data\n"
-                "FROM messages\n"
-                "WHERE channel_id = get_chan_id($1) \n"
-                "      AND message_id = $2 AND fragment_offset = $3", 3)) ||
+                           "select_message_fragment",
+                           "SELECT hop_counter, signature, purpose, fragment_id,\n"
+                           "       fragment_offset, message_id, group_generation,\n"
+                           "       multicast_flags, psycstore_flags, data\n"
+                           "FROM messages\n"
+                           "WHERE channel_id = get_chan_id($1) \n"
+                           "      AND message_id = $2 AND fragment_offset = $3", 3)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_counters_message",
-                "SELECT fragment_id, message_id, group_generation\n"
-                "FROM messages\n"
-                "WHERE channel_id = get_chan_id($1)\n"
-                "ORDER BY fragment_id DESC LIMIT 1", 1)) ||
+                           "select_counters_message",
+                           "SELECT fragment_id, message_id, group_generation\n"
+                           "FROM messages\n"
+                           "WHERE channel_id = get_chan_id($1)\n"
+                           "ORDER BY fragment_id DESC LIMIT 1", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_counters_state",
-                "SELECT max_state_message_id\n"
-                "FROM channels\n"
-                "WHERE pub_key = $1 AND max_state_message_id IS NOT NULL", 1)) ||
+                           "select_counters_state",
+                           "SELECT max_state_message_id\n"
+                           "FROM channels\n"
+                           "WHERE pub_key = $1 AND max_state_message_id IS NOT NULL", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "update_max_state_message_id",
-                "UPDATE channels\n"
-                "SET max_state_message_id = $1\n"
-                "WHERE pub_key = $2", 2)) ||
+                           "update_max_state_message_id",
+                           "UPDATE channels\n"
+                           "SET max_state_message_id = $1\n"
+                           "WHERE pub_key = $2", 2)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "update_state_hash_message_id",
-                "UPDATE channels\n"
-                "SET state_hash_message_id = $1\n"
-                "WHERE pub_key = $2", 2)) ||
+                           "update_state_hash_message_id",
+                           "UPDATE channels\n"
+                           "SET state_hash_message_id = $1\n"
+                           "WHERE pub_key = $2", 2)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "insert_state_current",
-                "INSERT INTO state\n"
-                "  (channel_id, name, value_current, value_signed)\n"
-                "SELECT new.channel_id, new.name,\n"
-                "       new.value_current, old.value_signed\n"
-                "FROM (SELECT get_chan_id($1) AS channel_id,\n"
-                "             $2::TEXT AS name, $3::BYTEA AS value_current) AS new\n"
-                "LEFT JOIN (SELECT channel_id, name, value_signed\n"
-                "           FROM state) AS old\n"
-                "ON new.channel_id = old.channel_id AND new.name = old.name\n"
-                "ON CONFLICT ( channel_id, substring(name from 1 for 5) )\n"
-                "   DO UPDATE SET value_current = EXCLUDED.value_current,\n"
-                "                 value_signed = EXCLUDED.value_signed", 3)) ||
+                           "insert_state_current",
+                           "INSERT INTO state\n"
+                           "  (channel_id, name, value_current, value_signed)\n"
+                           "SELECT new.channel_id, new.name,\n"
+                           "       new.value_current, old.value_signed\n"
+                           "FROM (SELECT get_chan_id($1) AS channel_id,\n"
+                           "             $2::TEXT AS name, $3::BYTEA AS value_current) AS new\n"
+                           "LEFT JOIN (SELECT channel_id, name, value_signed\n"
+                           "           FROM state) AS old\n"
+                           "ON new.channel_id = old.channel_id AND new.name = old.name\n"
+                           "ON CONFLICT (channel_id, name)\n"
+                           "   DO UPDATE SET value_current = EXCLUDED.value_current,\n"
+                           "                 value_signed = EXCLUDED.value_signed", 3)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "delete_state_empty",
-                "DELETE FROM state\n"
-                "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = $1)\n"
-                "      AND (value_current IS NULL OR length(value_current) = 0)\n"
-                "      AND (value_signed IS NULL OR length(value_signed) = 0)", 1)) ||
+                           "delete_state_empty",
+                           "DELETE FROM state\n"
+                           "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = $1)\n"
+                           "      AND (value_current IS NULL OR length(value_current) = 0)\n"
+                           "      AND (value_signed IS NULL OR length(value_signed) = 0)", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "update_state_signed",
-                "UPDATE state\n"
-                "SET value_signed = value_current\n"
-                "WHERE channel_id = get_chan_id($1) ", 1)) ||
+                           "update_state_signed",
+                           "UPDATE state\n"
+                           "SET value_signed = value_current\n"
+                           "WHERE channel_id = get_chan_id($1) ", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "delete_state",
-                "DELETE FROM state\n"
-                "WHERE channel_id = get_chan_id($1) ", 1)) ||
+                           "delete_state",
+                           "DELETE FROM state\n"
+                           "WHERE channel_id = get_chan_id($1) ", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "insert_state_sync",
-                "INSERT INTO state_sync (channel_id, name, value)\n"
-                "VALUES (get_chan_id($1), $2, $3)", 3)) ||
+                           "insert_state_sync",
+                           "INSERT INTO state_sync (channel_id, name, value)\n"
+                           "VALUES (get_chan_id($1), $2, $3)", 3)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "insert_state_from_sync",
-                "INSERT INTO state\n"
-                " (channel_id, name, value_current, value_signed)\n"
-                "SELECT channel_id, name, value, value\n"
-                "FROM state_sync\n"
-                "WHERE channel_id = get_chan_id($1)", 1)) ||
+                           "insert_state_from_sync",
+                           "INSERT INTO state\n"
+                           " (channel_id, name, value_current, value_signed)\n"
+                           "SELECT channel_id, name, value, value\n"
+                           "FROM state_sync\n"
+                           "WHERE channel_id = get_chan_id($1)", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "delete_state_sync",
-                "DELETE FROM state_sync\n"
-                "WHERE channel_id = get_chan_id($1)", 1)) ||
+                           "delete_state_sync",
+                           "DELETE FROM state_sync\n"
+                           "WHERE channel_id = get_chan_id($1)", 1)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_state_one",
-                "SELECT value_current\n"
-                "FROM state\n"
-                "WHERE channel_id = get_chan_id($1)\n"
-                "      AND name = $2", 2)) ||
+                           "select_state_one",
+                           "SELECT value_current\n"
+                           "FROM state\n"
+                           "WHERE channel_id = get_chan_id($1)\n"
+                           "      AND name = $2", 2)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_state_prefix",
-                "SELECT name, value_current\n"
-                "FROM state\n"
-                "WHERE channel_id = get_chan_id($1)\n"
-                "      AND (name = $2 OR substr(name, 1, $3) = $4 || '_')", 4)) ||
+                           "select_state_prefix",
+                           "SELECT name, value_current\n"
+                           "FROM state\n"
+                           "WHERE channel_id = get_chan_id($1)\n"
+                           "      AND (name = $2 OR substr(name, 1, $3) = $4)", 4)) ||
 
       (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh,
-                "select_state_signed",
-                "SELECT name, value_signed\n"
-                "FROM state\n"
-                "WHERE channel_id = get_chan_id($1)\n"
-                "      AND value_signed IS NOT NULL", 1)))
+                           "select_state_signed",
+                           "SELECT name, value_signed\n"
+                           "FROM state\n"
+                           "WHERE channel_id = get_chan_id($1)\n"
+                           "      AND value_signed IS NOT NULL", 1)))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -667,12 +653,12 @@ postgres_membership_store (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  ret = GNUNET_PQ_exec_prepared (plugin->dbh, "insert_slave_key", params);
+  ret = GNUNET_PQ_exec_prepared (plugin->dbh, "insert_membership", params);
   if (GNUNET_OK !=
       GNUNET_POSTGRES_check_result (plugin->dbh,
                                     ret,
                                     PGRES_COMMAND_OK,
-                                    "PQexecPrepared", "insert_slave_key"))
+                                    "PQexecPrepared", "insert_membership"))
   {
     return GNUNET_SYSERR;
   }
@@ -713,7 +699,7 @@ membership_test (void *cls,
   if (GNUNET_OK !=
       GNUNET_POSTGRES_check_result (plugin->dbh,
                                     res,
-                                    PGRES_COMMAND_OK,
+                                    PGRES_TUPLES_OK,
                                     "PQexecPrepared", "select_membership"))
   {
     return GNUNET_SYSERR;
@@ -724,11 +710,12 @@ membership_test (void *cls,
     GNUNET_PQ_result_spec_end
   };
 
-  switch(GNUNET_PQ_extract_result (res, results_select, 0))
+  switch (GNUNET_PQ_extract_result (res, results_select, 0))
   {
     case GNUNET_OK:
       ret = GNUNET_YES;
       break;
+
     default:
       ret = GNUNET_NO;
       break;
@@ -763,8 +750,8 @@ fragment_store (void *cls,
   uint64_t message_id = GNUNET_ntohll (msg->message_id);
   uint64_t group_generation = GNUNET_ntohll (msg->group_generation);
 
-  uint64_t hop_counter = ntohl(msg->hop_counter);
-  uint64_t flags = ntohl(msg->flags);
+  uint32_t hop_counter = ntohl(msg->hop_counter);
+  uint32_t flags = ntohl(msg->flags);
 
   if (fragment_id > INT64_MAX || fragment_offset > INT64_MAX ||
       message_id > INT64_MAX || group_generation > INT64_MAX)
@@ -782,17 +769,16 @@ fragment_store (void *cls,
 
   struct GNUNET_PQ_QueryParam params_insert[] = {
     GNUNET_PQ_query_param_auto_from_type (channel_key),
-    GNUNET_PQ_query_param_uint64 (&hop_counter),
+    GNUNET_PQ_query_param_uint32 (&hop_counter),
     GNUNET_PQ_query_param_auto_from_type (&msg->signature),
     GNUNET_PQ_query_param_auto_from_type (&msg->purpose),
     GNUNET_PQ_query_param_uint64 (&fragment_id),
     GNUNET_PQ_query_param_uint64 (&fragment_offset),
     GNUNET_PQ_query_param_uint64 (&message_id),
     GNUNET_PQ_query_param_uint64 (&group_generation),
-    GNUNET_PQ_query_param_uint64 (&flags),
+    GNUNET_PQ_query_param_uint32 (&flags),
     GNUNET_PQ_query_param_uint32 (&psycstore_flags),
-    GNUNET_PQ_query_param_fixed_size (&msg[1], ntohs (msg->header.size)
-                                                  - sizeof (*msg)),
+    GNUNET_PQ_query_param_fixed_size (&msg[1], ntohs (msg->header.size) - sizeof (*msg)),
     GNUNET_PQ_query_param_end
   };
 
@@ -819,15 +805,13 @@ static int
 message_add_flags (void *cls,
                    const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
                    uint64_t message_id,
-                   uint64_t psycstore_flags)
+                   uint32_t psycstore_flags)
 {
   PGresult *res;
   struct Plugin *plugin = cls;
 
-  int ret = GNUNET_SYSERR;
-
   struct GNUNET_PQ_QueryParam params_update[] = {
-    GNUNET_PQ_query_param_uint64 (&psycstore_flags),
+    GNUNET_PQ_query_param_uint32 (&psycstore_flags),
     GNUNET_PQ_query_param_auto_from_type (channel_key),
     GNUNET_PQ_query_param_uint64 (&message_id),
     GNUNET_PQ_query_param_end
@@ -835,13 +819,13 @@ message_add_flags (void *cls,
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, "update_message_flags", params_update);
   if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", "update_message_flags"))
-    return ret;
+                                                 res,
+                                                 PGRES_COMMAND_OK,
+                                                 "PQexecPrepared","update_message_flags"))
+    return GNUNET_SYSERR;
 
   PQclear (res);
-  return ret;
+  return GNUNET_OK;
 }
 
 
@@ -850,7 +834,8 @@ fragment_row (struct Plugin *plugin,
               const char *stmt,
               PGresult *res,
               GNUNET_PSYCSTORE_FragmentCallback cb,
-              void *cb_cls)
+              void *cb_cls,
+              uint64_t *returned_fragments)
 {
   uint32_t hop_counter;
   void *signature = NULL;
@@ -862,14 +847,13 @@ fragment_row (struct Plugin *plugin,
   uint64_t fragment_offset;
   uint64_t message_id;
   uint64_t group_generation;
-  uint64_t flags;
+  uint32_t flags;
   void *buf;
   size_t buf_size;
   int ret = GNUNET_SYSERR;
   struct GNUNET_MULTICAST_MessageHeader *mp;
 
-  uint64_t msg_flags;
-  unsigned int cnt;
+  uint32_t msg_flags;
 
   struct GNUNET_PQ_ResultSpec results[] = {
     GNUNET_PQ_result_spec_uint32 ("hop_counter", &hop_counter),
@@ -879,10 +863,9 @@ fragment_row (struct Plugin *plugin,
     GNUNET_PQ_result_spec_uint64 ("fragment_offset", &fragment_offset),
     GNUNET_PQ_result_spec_uint64 ("message_id", &message_id),
     GNUNET_PQ_result_spec_uint64 ("group_generation", &group_generation),
-    GNUNET_PQ_result_spec_uint64 ("msg_flags", &msg_flags),
-    GNUNET_PQ_result_spec_uint64 ("flags", &flags),
-    GNUNET_PQ_result_spec_variable_size ("data", &buf,
-                                         &buf_size),
+    GNUNET_PQ_result_spec_uint32 ("multicast_flags", &msg_flags),
+    GNUNET_PQ_result_spec_uint32 ("psycstore_flags", &flags),
+    GNUNET_PQ_result_spec_variable_size ("data", &buf, &buf_size),
     GNUNET_PQ_result_spec_end
   };
 
@@ -896,16 +879,12 @@ fragment_row (struct Plugin *plugin,
     return GNUNET_SYSERR;
   }
 
-  cnt = PQntuples (res);
-  if (cnt == 0)
+  int nrows = PQntuples (res);
+  for (int row = 0; row < nrows; row++)
   {
-    ret = GNUNET_NO;
-  }
-  else
-  {
-    if (GNUNET_OK != GNUNET_PQ_extract_result(res, results, 0)) {
-      PQclear (res);
-      return GNUNET_SYSERR;
+    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, row))
+    {
+      break;
     }
 
     mp = GNUNET_malloc (sizeof (*mp) + buf_size);
@@ -930,42 +909,37 @@ fragment_row (struct Plugin *plugin,
                    buf_size);
     GNUNET_PQ_cleanup_result(results);
     ret = cb (cb_cls, mp, (enum GNUNET_PSYCSTORE_MessageFlags) flags);
+    if (NULL != returned_fragments)
+      (*returned_fragments)++;
   }
 
-  PQclear (res);
   return ret;
 }
 
 
 static int
-fragment_select (struct Plugin *plugin, const char *stmt,
+fragment_select (struct Plugin *plugin,
+                 const char *stmt,
                  struct GNUNET_PQ_QueryParam *params,
                  uint64_t *returned_fragments,
-                 GNUNET_PSYCSTORE_FragmentCallback cb, void *cb_cls)
+                 GNUNET_PSYCSTORE_FragmentCallback cb,
+                 void *cb_cls)
 {
   PGresult *res;
   int ret = GNUNET_SYSERR;
 
-  // FIXME
-  if (NULL == plugin->dbh || NULL == stmt || NULL == params)
-  {
-    fprintf(stderr, "%p %p %p\n", plugin->dbh, stmt, params);
-    return GNUNET_SYSERR;
-  }
-
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
   if (GNUNET_YES ==
       GNUNET_POSTGRES_check_result (plugin->dbh,
                                     res,
-                                    PGRES_COMMAND_OK,
+                                    PGRES_TUPLES_OK,
                                     "PQexecPrepared", stmt))
   {
     if (PQntuples (res) == 0)
       ret = GNUNET_NO;
     else
     {
-      ret = fragment_row (plugin, stmt, res, cb, cb_cls);
-      (*returned_fragments)++;
+      ret = fragment_row (plugin, stmt, res, cb, cb_cls, returned_fragments);
     }
     PQclear (res);
   }
@@ -1052,6 +1026,9 @@ message_get (void *cls,
   struct Plugin *plugin = cls;
   *returned_fragments = 0;
 
+  if (0 == fragment_limit)
+    fragment_limit = INT64_MAX;
+
   struct GNUNET_PQ_QueryParam params_select[] = {
     GNUNET_PQ_query_param_auto_from_type (channel_key),
     GNUNET_PQ_query_param_uint64 (&first_message_id),
@@ -1123,14 +1100,14 @@ message_get_fragment (void *cls,
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
   if (GNUNET_OK == GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
+                                                 res,
+                                                 PGRES_TUPLES_OK,
+                                                 "PQexecPrepared", stmt))
   {
     if (PQntuples (res) == 0)
       ret = GNUNET_NO;
     else
-      ret = fragment_row (plugin, stmt, res, cb, cb_cls);
+      ret = fragment_row (plugin, stmt, res, cb, cb_cls, NULL);
 
     PQclear (res);
   }
@@ -1164,22 +1141,21 @@ counters_message_get (void *cls,
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
   if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
+                                                 res,
+                                                 PGRES_TUPLES_OK,
+                                                 "PQexecPrepared", stmt))
   {
     return GNUNET_SYSERR;
   }
 
   struct GNUNET_PQ_ResultSpec results_select[] = {
-    GNUNET_PQ_result_spec_uint64 ("max_fragment_id", max_fragment_id),
-    GNUNET_PQ_result_spec_uint64 ("max_message_id", max_message_id),
-    GNUNET_PQ_result_spec_uint64 ("max_group_generation", max_group_generation),
+    GNUNET_PQ_result_spec_uint64 ("fragment_id", max_fragment_id),
+    GNUNET_PQ_result_spec_uint64 ("message_id", max_message_id),
+    GNUNET_PQ_result_spec_uint64 ("group_generation", max_group_generation),
     GNUNET_PQ_result_spec_end
   };
 
-  if (GNUNET_OK != GNUNET_PQ_extract_result (res,
-                                  results_select, 0))
+  if (GNUNET_OK != GNUNET_PQ_extract_result (res, results_select, 0))
   {
     PQclear (res);
     return GNUNET_SYSERR;
@@ -1217,9 +1193,9 @@ counters_state_get (void *cls,
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
   if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
+                                                 res,
+                                                 PGRES_TUPLES_OK,
+                                                 "PQexecPrepared", stmt))
   {
     return GNUNET_SYSERR;
   }
@@ -1229,8 +1205,7 @@ counters_state_get (void *cls,
     GNUNET_PQ_result_spec_end
   };
 
-  ret = GNUNET_PQ_extract_result (res,
-                                  results_select, 0);
+  ret = GNUNET_PQ_extract_result (res, results_select, 0);
 
   if (GNUNET_OK != ret)
   {
@@ -1260,15 +1235,15 @@ state_assign (struct Plugin *plugin, const char *stmt,
   struct GNUNET_PQ_QueryParam params[] = {
     GNUNET_PQ_query_param_auto_from_type (channel_key),
     GNUNET_PQ_query_param_string (name),
-    GNUNET_PQ_query_param_auto_from_type (value),
+    GNUNET_PQ_query_param_fixed_size (value, value_size),
     GNUNET_PQ_query_param_end
   };
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
   if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
+                                                 res,
+                                                 PGRES_COMMAND_OK,
+                                                 "PQexecPrepared", stmt))
   {
     return GNUNET_SYSERR;
   }
@@ -1294,9 +1269,9 @@ update_message_id (struct Plugin *plugin, const char *stmt,
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
   if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
+                                                 res,
+                                                 PGRES_COMMAND_OK,
+                                                 "PQexecPrepared", stmt))
   {
     return GNUNET_SYSERR;
   }
@@ -1333,6 +1308,7 @@ state_modify_begin (void *cls,
     case GNUNET_NO: // no state yet
       ret = GNUNET_OK;
       break;
+
     default:
       return ret;
     }
@@ -1371,8 +1347,8 @@ state_modify_op (void *cls,
   switch (op)
   {
   case GNUNET_PSYC_OP_ASSIGN:
-    return state_assign (plugin, "insert_state_current", channel_key,
-                         name, value, value_size);
+    return state_assign (plugin, "insert_state_current",
+                         channel_key, name, value, value_size);
 
   default: /** @todo implement more state operations */
     GNUNET_break (0);
@@ -1427,8 +1403,8 @@ state_sync_assign (void *cls,
                 const char *name, const void *value, size_t value_size)
 {
   struct Plugin *plugin = cls;
-  return state_assign (plugin, "insert_state_sync", channel_key,
-                       name, value, value_size);
+  return state_assign (plugin, "insert_state_sync",
+                       channel_key, name, value, value_size);
 }
 
 
@@ -1534,9 +1510,9 @@ state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
 
   res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
   if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
+                                                 res,
+                                                 PGRES_TUPLES_OK,
+                                                 "PQexecPrepared", stmt))
   {
     return GNUNET_SYSERR;
   }
@@ -1547,8 +1523,7 @@ state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
     ret = GNUNET_NO;
   }
 
-  ret = GNUNET_PQ_extract_result (res,
-                                  results, 0);
+  ret = GNUNET_PQ_extract_result (res, results, 0);
 
   if (GNUNET_OK != ret)
   {
@@ -1580,7 +1555,7 @@ state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_
 {
   PGresult *res;
   struct Plugin *plugin = cls;
-  int ret = GNUNET_SYSERR;
+  int ret = GNUNET_NO;
 
   const char *stmt = "select_state_prefix";
 
@@ -1604,27 +1579,20 @@ state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_
     GNUNET_PQ_result_spec_end
   };
 
-  do
+  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
+  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
+                                                 res,
+                                                 PGRES_TUPLES_OK,
+                                                 "PQexecPrepared", stmt))
   {
-    res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-    if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
-    {
-      break;
-    }
-
-    if (PQntuples (res) == 0)
-    {
-      PQclear (res);
-      ret = GNUNET_NO;
-      break;
-    }
+    return GNUNET_SYSERR;
+  }
 
-    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, 0))
+  int nrows = PQntuples (res);
+  for (int row = 0; row < nrows; row++)
+  {
+    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, row))
     {
-      PQclear (res);
       break;
     }
 
@@ -1633,7 +1601,6 @@ state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_
               value_size);
     GNUNET_PQ_cleanup_result(results);
   }
-  while (ret == GNUNET_YES);
 
   PQclear (res);
 
@@ -1655,7 +1622,7 @@ state_get_signed (void *cls,
 {
   PGresult *res;
   struct Plugin *plugin = cls;
-  int ret = GNUNET_SYSERR;
+  int ret = GNUNET_NO;
 
   const char *stmt = "select_state_signed";
 
@@ -1674,27 +1641,20 @@ state_get_signed (void *cls,
     GNUNET_PQ_result_spec_end
   };
 
-  do
+  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
+  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
+                                                 res,
+                                                 PGRES_TUPLES_OK,
+                                                 "PQexecPrepared", stmt))
   {
-    res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-    if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                      res,
-                                      PGRES_COMMAND_OK,
-                                      "PQexecPrepared", stmt))
-    {
-      break;
-    }
-
-    if (PQntuples (res) == 0)
-    {
-      PQclear (res);
-      ret = GNUNET_NO;
-      break;
-    }
+    return GNUNET_SYSERR;
+  }
 
-    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, 0))
+  int nrows = PQntuples (res);
+  for (int row = 0; row < nrows; row++)
+  {
+    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, row))
     {
-      PQclear (res);
       break;
     }
 
@@ -1702,9 +1662,8 @@ state_get_signed (void *cls,
               value_signed,
               value_size);
 
-    GNUNET_PQ_cleanup_result(results);
+    GNUNET_PQ_cleanup_result (results);
   }
-  while (ret == GNUNET_YES);
 
   PQclear (res);