Advertisement
Guest User

libpq bug?

a guest
Jun 18th, 2015
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. /*
  2.  *  gcc -o pgsql_check pgsql_check.c -lpq
  3.  *  ./pgsql_check
  4.  *
  5.  */
  6.  
  7.  
  8. /*
  9.  
  10.  
  11. valgrind:
  12.  
  13. ==2490== Memcheck, a memory error detector
  14. ==2490== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  15. ==2490== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
  16. ==2490== Command: ./pgsql_check
  17. ==2490==
  18. ==2490==
  19. ==2490== HEAP SUMMARY:
  20. ==2490==     in use at exit: 47,288 bytes in 2,864 blocks
  21. ==2490==   total heap usage: 5,670 allocs, 2,806 frees, 331,452 bytes allocated
  22. ==2490==
  23. ==2490== LEAK SUMMARY:
  24. ==2490==    definitely lost: 0 bytes in 0 blocks
  25. ==2490==    indirectly lost: 0 bytes in 0 blocks
  26. ==2490==      possibly lost: 0 bytes in 0 blocks
  27. ==2490==    still reachable: 47,288 bytes in 2,864 blocks
  28. ==2490==         suppressed: 0 bytes in 0 blocks
  29. ==2490== Rerun with --leak-check=full to see details of leaked memory
  30. ==2490==
  31. ==2490== For counts of detected and suppressed errors, rerun with: -v
  32. ==2490== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  33.  
  34.  
  35. */
  36.  
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40.  
  41. #include "/usr/include/postgresql/libpq-fe.h"
  42.  
  43.  
  44. static void
  45. exit_nicely(PGconn *conn)
  46. {
  47.     PQfinish(conn);
  48.     exit(1);
  49. }
  50.  
  51.  
  52. int
  53. main(int argc, char **argv)
  54. {
  55.     const char  *conninfo;
  56.     PGconn      *conn;
  57.     PGresult    *res;
  58.     int         nFields,
  59.                 nRows;
  60.     int         i,
  61.                 j;
  62.  
  63.     /*
  64.      * If the user supplies a parameter on the command line, use it as the
  65.      * conninfo string; otherwise default to setting dbname=postgres and using
  66.      * environment variables or defaults for all other connection parameters.
  67.      */
  68.     if (argc > 1)
  69.         conninfo = argv[1];
  70.     else
  71.         conninfo = "host=127.0.0.1 dbname=postgres user=postgres password=PASSWORD port=5432 connect_timeout=5";
  72.     /* Make a connection to the database */
  73.     conn = PQconnectdb(conninfo);
  74.  
  75.     /* Check to see that the backend connection was successfully made */
  76.     if (PQstatus(conn) != CONNECTION_OK)
  77.     {
  78.         fprintf(stderr, "Connection to database failed: %s",
  79.                 PQerrorMessage(conn));
  80.         exit_nicely(conn);
  81.     }
  82.  
  83.     /* Start a transaction block */
  84.     res = PQexec(conn, "BEGIN");
  85.     PQclear(res);
  86.     /* disable notice */
  87.     res = PQexec(conn, "SET client_min_messages TO WARNING;");
  88.     PQclear(res);
  89.     /* end the transaction */
  90.     res = PQexec(conn, "END");
  91.     PQclear(res);
  92.  
  93.  
  94.     /* close the connection to the database and clean-up */
  95.     PQfinish(conn);
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement