Advertisement
Guest User

Why does this code eat so much memory?

a guest
Mar 30th, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. // Modified from libpq example code.
  2. // This code is made available under the same license as the original.
  3.  
  4. // Build commmand:
  5. // gcc -o libpq-bench libpq-bench.c -I`pg_config --includedir` -L`pg_config --libdir` -lpq
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <libpq-fe.h>
  10.  
  11. int
  12. main(int argc, char **argv)
  13. {
  14.     const char *conninfo;
  15.     PGconn     *conn;
  16.     PGresult   *res;
  17.  
  18.     if (argc > 1)
  19.         conninfo = argv[1];
  20.     else
  21.         conninfo = "";
  22.  
  23.     conn = PQconnectdb(conninfo);
  24.  
  25.     if (PQstatus(conn) != CONNECTION_OK)
  26.     {
  27.         fprintf(stderr, "Connection to database failed: %s",
  28.                 PQerrorMessage(conn));
  29.         PQfinish(conn);
  30.         exit(1);
  31.     }
  32.  
  33.     int value;
  34.     if (!PQsendQuery(conn, "select generate_series(1, 10000000)"))
  35.     {
  36.         fprintf(stderr, "Send query failed: %s", PQerrorMessage(conn));
  37.         PQfinish(conn);
  38.         exit(1);
  39.     }
  40.    
  41.     int i, j;
  42.     for (;;) {
  43.         res = PQgetResult(conn);
  44.         if (res == NULL)
  45.             break;
  46.         for (i = 0; i < PQntuples(res); i++) {
  47.             value = atoi(PQgetvalue(res, i, 0));
  48.         }
  49.         PQclear(res);
  50.     }
  51.  
  52.     printf("%d\n", value);
  53.     PQfinish(conn);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement