-fix double free, linker issue
[oweals/gnunet.git] / src / datastore / plugin_datastore_postgres.c
index 3c37325daba4d06697a758da03800d2bf3838aa3..83fc423da667f73a8519f2a7970a7fbe6c92cfeb 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -26,9 +26,9 @@
 
 #include "platform.h"
 #include "gnunet_datastore_plugin.h"
+#include "gnunet_postgres_lib.h"
 #include <postgresql/libpq-fe.h>
 
-#define DEBUG_POSTGRES GNUNET_NO
 
 /**
  * After how many ms "busy" should a DB operation fail for good?
@@ -61,87 +61,6 @@ struct Plugin
 };
 
 
-/**
- * Check if the result obtained from Postgres has
- * the desired status code.  If not, log an error, clear the
- * result and return GNUNET_SYSERR.
- *
- * @param plugin global context
- * @param ret result to check
- * @param expected_status expected return value
- * @param command name of SQL command that was run
- * @param args arguments to SQL command
- * @param line line number for error reporting
- * @return GNUNET_OK if the result is acceptable
- */
-static int
-check_result (struct Plugin *plugin, PGresult * ret, int expected_status,
-              const char *command, const char *args, int line)
-{
-  if (ret == NULL)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                     "datastore-postgres",
-                     "Postgres failed to allocate result for `%s:%s' at %d\n",
-                     command, args, line);
-    return GNUNET_SYSERR;
-  }
-  if (PQresultStatus (ret) != expected_status)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                     "datastore-postgres",
-                     _("`%s:%s' failed at %s:%d with error: %s"), command, args,
-                     __FILE__, line, PQerrorMessage (plugin->dbh));
-    PQclear (ret);
-    return GNUNET_SYSERR;
-  }
-  return GNUNET_OK;
-}
-
-/**
- * Run simple SQL statement (without results).
- *
- * @param plugin global context
- * @param sql statement to run
- * @param line code line for error reporting
- */
-static int
-pq_exec (struct Plugin *plugin, const char *sql, int line)
-{
-  PGresult *ret;
-
-  ret = PQexec (plugin->dbh, sql);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
-    return GNUNET_SYSERR;
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-/**
- * Prepare SQL statement.
- *
- * @param plugin global context
- * @param name name for the prepared SQL statement
- * @param sql SQL code to prepare
- * @param nparams number of parameters in sql
- * @param line code line for error reporting
- * @return GNUNET_OK on success
- */
-static int
-pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
-            int nparams, int line)
-{
-  PGresult *ret;
-
-  ret = PQprepare (plugin->dbh, name, sql, nparams, NULL);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line))
-    return GNUNET_SYSERR;
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
 /**
  * @brief Get a database handle
  *
@@ -151,33 +70,11 @@ pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
 static int
 init_connection (struct Plugin *plugin)
 {
-  char *conninfo;
   PGresult *ret;
 
-  /* Open database and precompile statements */
-  conninfo = NULL;
-  (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
-                                                "datastore-postgres", "CONFIG",
-                                                &conninfo);
-  plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
+  plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg, "datastore-postgres");
   if (NULL == plugin->dbh)
-  {
-    /* FIXME: warn about out-of-memory? */
-    GNUNET_free_non_null (conninfo);
-    return GNUNET_SYSERR;
-  }
-  if (PQstatus (plugin->dbh) != CONNECTION_OK)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datastore-postgres",
-                     _
-                     ("Unable to initialize Postgres with configuration `%s': %s"),
-                     conninfo, PQerrorMessage (plugin->dbh));
-    PQfinish (plugin->dbh);
-    plugin->dbh = NULL;
-    GNUNET_free_non_null (conninfo);
     return GNUNET_SYSERR;
-  }
-  GNUNET_free_non_null (conninfo);
   ret =
       PQexec (plugin->dbh,
               "CREATE TABLE gn090 (" "  repl INTEGER NOT NULL DEFAULT 0,"
@@ -194,8 +91,7 @@ init_connection (struct Plugin *plugin)
                                                                                     (ret,
                                                                                      PG_DIAG_SQLSTATE)))))
   {
-    (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090",
-                         __LINE__);
+    (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090");
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
     return GNUNET_SYSERR;
@@ -203,29 +99,23 @@ init_connection (struct Plugin *plugin)
   if (PQresultStatus (ret) == PGRES_COMMAND_OK)
   {
     if ((GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash ON gn090 (hash)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_prio ON gn090 (prio)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire ON gn090 (expire)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin,
-                  "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh,
+                  "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin,
-                  "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh,
+                  "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)",
-                  __LINE__)))
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)")))
     {
       PQclear (ret);
       PQfinish (plugin->dbh);
@@ -238,8 +128,7 @@ init_connection (struct Plugin *plugin)
       PQexec (plugin->dbh,
               "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -248,8 +137,7 @@ init_connection (struct Plugin *plugin)
   PQclear (ret);
   ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -258,8 +146,7 @@ init_connection (struct Plugin *plugin)
   PQclear (ret);
   ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -267,57 +154,56 @@ init_connection (struct Plugin *plugin)
   }
   PQclear (ret);
   if ((GNUNET_OK !=
-       pq_prepare (plugin, "getvt",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getvt",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE hash=$1 AND vhash=$2 AND type=$3 "
-                   "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4, __LINE__)) ||
+                   "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "gett",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "gett",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE hash=$1 AND type=$2 "
-                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
+                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "getv",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getv",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE hash=$1 AND vhash=$2 "
-                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
+                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "get",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "get",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
-                   "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2,
-                   __LINE__)) ||
+                   "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "put",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "put",
                    "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
-                   "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9,
-                   __LINE__)) ||
+                   "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "update",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "update",
                    "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
-                   "WHERE oid = $3", 3, __LINE__)) ||
+                   "WHERE oid = $3", 3)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "decrepl",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "decrepl",
                    "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
-                   "WHERE oid = $1", 1, __LINE__)) ||
+                   "WHERE oid = $1", 1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "select_non_anonymous",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "select_non_anonymous",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
-                   1, __LINE__)) ||
+                   1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "select_expiration_order",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "select_expiration_order",
                    "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
                    "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
-                   1, __LINE__)) ||
+                   1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "select_replication_order",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "select_replication_order",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
-                   "ORDER BY repl DESC,RANDOM() LIMIT 1", 0, __LINE__)) ||
+                   "ORDER BY repl DESC,RANDOM() LIMIT 1", 0)) ||
+      (GNUNET_OK !=
+       GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1,
-                   __LINE__)))
+       GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0)))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -327,38 +213,6 @@ init_connection (struct Plugin *plugin)
 }
 
 
-/**
- * Delete the row identified by the given rowid (qid
- * in postgres).
- *
- * @param plugin global context
- * @param rowid which row to delete
- * @return GNUNET_OK on success
- */
-static int
-delete_by_rowid (struct Plugin *plugin, unsigned int rowid)
-{
-  uint32_t browid;
-  const char *paramValues[] = { (const char *) &browid };
-  int paramLengths[] = { sizeof (browid) };
-  const int paramFormats[] = { 1 };
-  PGresult *ret;
-
-  browid = htonl (rowid);
-  ret =
-      PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
-                      paramFormats, 1);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
-                    __LINE__))
-  {
-    return GNUNET_SYSERR;
-  }
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-
 /**
  * Get an estimate of how much space the database is
  * currently using.
@@ -378,18 +232,22 @@ postgres_plugin_estimate_size (void *cls)
                     "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
                     NULL, NULL, NULL, NULL, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size"))
   {
     return 0;
   }
-  if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
-      (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
+  if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) )
   {
     GNUNET_break (0);
     PQclear (ret);
     return 0;
   }
+  if (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))
+  {
+    GNUNET_break (0 == PQgetlength (ret, 0, 0));
+    PQclear (ret);
+    return 0;
+  }
   total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
   PQclear (ret);
   return total;
@@ -399,7 +257,7 @@ postgres_plugin_estimate_size (void *cls)
 /**
  * Store an item in the datastore.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin' 
  * @param key key for the item
  * @param size number of bytes in data
  * @param data content stored
@@ -412,14 +270,14 @@ postgres_plugin_estimate_size (void *cls)
  * @return GNUNET_OK on success
  */
 static int
-postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
+postgres_plugin_put (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
                      const void *data, enum GNUNET_BLOCK_Type type,
                      uint32_t priority, uint32_t anonymity,
                      uint32_t replication,
                      struct GNUNET_TIME_Absolute expiration, char **msg)
 {
   struct Plugin *plugin = cls;
-  GNUNET_HashCode vhash;
+  struct GNUNET_HashCode vhash;
   PGresult *ret;
   uint32_t btype = htonl (type);
   uint32_t bprio = htonl (priority);
@@ -443,8 +301,8 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
     sizeof (bprio),
     sizeof (banon),
     sizeof (bexpi),
-    sizeof (GNUNET_HashCode),
-    sizeof (GNUNET_HashCode),
+    sizeof (struct GNUNET_HashCode),
+    sizeof (struct GNUNET_HashCode),
     size
   };
   const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
@@ -454,15 +312,12 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
       PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths,
                       paramFormats, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put"))
     return GNUNET_SYSERR;
   PQclear (ret);
   plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
-#if DEBUG_POSTGRES
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                    "Stored %u bytes in database\n", (unsigned int) size);
-#endif
   return GNUNET_OK;
 }
 
@@ -475,11 +330,13 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
  * @param proc function to call the value (once only).
  * @param proc_cls closure for proc
  * @param res result from exec
+ * @param filename filename for error messages
  * @param line line number for error messages
  */
 static void
 process_result (struct Plugin *plugin, PluginDatumProcessor proc,
-                void *proc_cls, PGresult * res, int line)
+                void *proc_cls, PGresult * res, 
+               const char *filename, int line)
 {
   int iret;
   enum GNUNET_BLOCK_Type type;
@@ -488,16 +345,14 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   uint32_t size;
   unsigned int rowid;
   struct GNUNET_TIME_Absolute expiration_time;
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
 
   if (GNUNET_OK !=
-      check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
-                    line))
+      GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
+                                    filename, line))
   {
-#if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                      "Ending iteration (postgres error)\n");
-#endif
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     return;
   }
@@ -505,10 +360,8 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   if (0 == PQntuples (res))
   {
     /* no result */
-#if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                      "Ending iteration (no more results)\n");
-#endif
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     PQclear (res);
     return;
@@ -527,11 +380,11 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
       (sizeof (uint32_t) != PQfsize (res, 1)) ||
       (sizeof (uint32_t) != PQfsize (res, 2)) ||
       (sizeof (uint64_t) != PQfsize (res, 3)) ||
-      (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)))
+      (sizeof (struct GNUNET_HashCode) != PQgetlength (res, 0, 4)))
   {
     GNUNET_break (0);
     PQclear (res);
-    delete_by_rowid (plugin, rowid);
+    GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid);
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     return;
   }
@@ -541,13 +394,11 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   anonymity = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2));
   expiration_time.abs_value =
       GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
-  memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode));
+  memcpy (&key, PQgetvalue (res, 0, 4), sizeof (struct GNUNET_HashCode));
   size = PQgetlength (res, 0, 5);
-#if DEBUG_POSTGRES
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                    "Found result of size %u bytes and type %u in database\n",
                    (unsigned int) size, (unsigned int) type);
-#endif
   iret =
       proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
             (enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
@@ -555,23 +406,17 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   PQclear (res);
   if (iret == GNUNET_NO)
   {
-#if DEBUG_POSTGRES
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Processor asked for item %u to be removed.\n", rowid);
-#endif
-    if (GNUNET_OK == delete_by_rowid (plugin, rowid))
+    if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid))
     {
-#if DEBUG_POSTGRES
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                        "Deleting %u bytes from database\n",
                        (unsigned int) size);
-#endif
       plugin->env->duc (plugin->env->cls,
                         -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
-#if DEBUG_POSTGRES
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                        "Deleted %u bytes from database\n", (unsigned int) size);
-#endif
     }
   }
 }
@@ -581,7 +426,7 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
  * Iterate over the results for a particular key
  * in the datastore.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param offset offset of the result (modulo num-results);
  *        specific ordering does not matter for the offset
  * @param key maybe NULL (to match all entries)
@@ -598,8 +443,8 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
  */
 static void
 postgres_plugin_get_key (void *cls, uint64_t offset,
-                         const GNUNET_HashCode * key,
-                         const GNUNET_HashCode * vhash,
+                         const struct GNUNET_HashCode * key,
+                         const struct GNUNET_HashCode * vhash,
                          enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
                          void *proc_cls)
 {
@@ -616,14 +461,14 @@ postgres_plugin_get_key (void *cls, uint64_t offset,
 
   GNUNET_assert (key != NULL);
   paramValues[0] = (const char *) key;
-  paramLengths[0] = sizeof (GNUNET_HashCode);
+  paramLengths[0] = sizeof (struct GNUNET_HashCode);
   btype = htonl (type);
   if (type != 0)
   {
     if (vhash != NULL)
     {
       paramValues[1] = (const char *) vhash;
-      paramLengths[1] = sizeof (GNUNET_HashCode);
+      paramLengths[1] = sizeof (struct GNUNET_HashCode);
       paramValues[2] = (const char *) &btype;
       paramLengths[2] = sizeof (btype);
       paramValues[3] = (const char *) &blimit_off;
@@ -654,7 +499,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset,
     if (vhash != NULL)
     {
       paramValues[1] = (const char *) vhash;
-      paramLengths[1] = sizeof (GNUNET_HashCode);
+      paramLengths[1] = sizeof (struct GNUNET_HashCode);
       paramValues[2] = (const char *) &blimit_off;
       paramLengths[2] = sizeof (blimit_off);
       nparams = 3;
@@ -676,8 +521,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset,
     }
   }
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", pname,
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", pname))
   {
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     return;
@@ -701,7 +545,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset,
   ret =
       PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths,
                       paramFormats, 1);
-  process_result (plugin, proc, proc_cls, ret, __LINE__);
+  process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
 }
 
 
@@ -736,7 +580,7 @@ postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset,
   ret =
       PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues,
                       paramLengths, paramFormats, 1);
-  process_result (plugin, proc, proc_cls, ret, __LINE__);
+  process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
 }
 
 
@@ -768,7 +612,7 @@ struct ReplCtx
  * Decrements the replication counter and calls the original
  * iterator.
  *
- * @param cls closure
+ * @param cls closure with the 'struct ReplCtx*'
  * @param key key for the content
  * @param size number of bytes in data
  * @param data content stored
@@ -784,7 +628,7 @@ struct ReplCtx
  *         GNUNET_NO to delete the item and continue (if supported)
  */
 static int
-repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
+repl_proc (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
            const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
            uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
            uint64_t uid)
@@ -812,8 +656,8 @@ repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
         PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths,
                         paramFormats, 1);
     if (GNUNET_OK !=
-        check_result (plugin, qret, PGRES_COMMAND_OK, "PQexecPrepared",
-                      "decrepl", __LINE__))
+        GNUNET_POSTGRES_check_result (plugin->dbh, qret, PGRES_COMMAND_OK, "PQexecPrepared",
+                      "decrepl"))
       return GNUNET_SYSERR;
     PQclear (qret);
   }
@@ -827,7 +671,7 @@ repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
  * replication counter is decremented by one IF it was positive before.
  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param proc function to call the value (once only).
  * @param proc_cls closure for proc
  */
@@ -845,7 +689,7 @@ postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
   ret =
       PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL,
                       NULL, 1);
-  process_result (plugin, &repl_proc, &rc, ret, __LINE__);
+  process_result (plugin, &repl_proc, &rc, ret, __FILE__, __LINE__);
 }
 
 
@@ -853,7 +697,7 @@ postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
  * Get a random item for expiration.
  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param proc function to call the value (once only).
  * @param proc_cls closure for proc
  */
@@ -872,7 +716,7 @@ postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
   ret =
       PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
                       paramLengths, paramFormats, 1);
-  process_result (plugin, proc, proc_cls, ret, __LINE__);
+  process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
 }
 
 
@@ -925,23 +769,59 @@ postgres_plugin_update (void *cls, uint64_t uid, int delta,
       PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths,
                       paramFormats, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update"))
     return GNUNET_SYSERR;
   PQclear (ret);
   return GNUNET_OK;
 }
 
 
+
+/**
+ * Get all of the keys in the datastore.
+ *
+ * @param cls closure with the 'struct Plugin'
+ * @param proc function to call on each key
+ * @param proc_cls closure for proc
+ */
+static void
+postgres_plugin_get_keys (void *cls,
+                         PluginKeyProcessor proc,
+                         void *proc_cls)
+{
+  struct Plugin *plugin = cls;
+  int ret;
+  int i;
+  struct GNUNET_HashCode key;
+  PGresult * res;
+
+  res = PQexecPrepared (plugin->dbh, "get_keys", 0, NULL, NULL, NULL, 1);
+  ret = PQntuples (res);
+  for (i=0;i<ret;i++)
+  {
+    if (sizeof (struct GNUNET_HashCode) != PQgetlength (res, i, 0))
+    {
+      memcpy (&key, PQgetvalue (res, i, 0), sizeof (struct GNUNET_HashCode));
+      proc (proc_cls, &key, 1);    
+    }
+  }
+  PQclear (res);
+}
+
+
+
 /**
  * Drop database.
+ *
+ * @param cls closure with the 'struct Plugin'
  */
 static void
 postgres_plugin_drop (void *cls)
 {
   struct Plugin *plugin = cls;
-
-  pq_exec (plugin, "DROP TABLE gn090", __LINE__);
+  
+  if (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090"))
+    GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "postgres", _("Failed to drop table from database.\n"));
 }
 
 
@@ -974,6 +854,7 @@ libgnunet_plugin_datastore_postgres_init (void *cls)
   api->get_replication = &postgres_plugin_get_replication;
   api->get_expiration = &postgres_plugin_get_expiration;
   api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
+  api->get_keys = &postgres_plugin_get_keys;
   api->drop = &postgres_plugin_drop;
   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datastore-postgres",
                    _("Postgres database running\n"));