Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * gcc -o pgsql_check pgsql_check.c -lpq
- * ./pgsql_check
- *
- */
- /*
- valgrind:
- ==2490== Memcheck, a memory error detector
- ==2490== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
- ==2490== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
- ==2490== Command: ./pgsql_check
- ==2490==
- ==2490==
- ==2490== HEAP SUMMARY:
- ==2490== in use at exit: 47,288 bytes in 2,864 blocks
- ==2490== total heap usage: 5,670 allocs, 2,806 frees, 331,452 bytes allocated
- ==2490==
- ==2490== LEAK SUMMARY:
- ==2490== definitely lost: 0 bytes in 0 blocks
- ==2490== indirectly lost: 0 bytes in 0 blocks
- ==2490== possibly lost: 0 bytes in 0 blocks
- ==2490== still reachable: 47,288 bytes in 2,864 blocks
- ==2490== suppressed: 0 bytes in 0 blocks
- ==2490== Rerun with --leak-check=full to see details of leaked memory
- ==2490==
- ==2490== For counts of detected and suppressed errors, rerun with: -v
- ==2490== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "/usr/include/postgresql/libpq-fe.h"
- static void
- exit_nicely(PGconn *conn)
- {
- PQfinish(conn);
- exit(1);
- }
- int
- main(int argc, char **argv)
- {
- const char *conninfo;
- PGconn *conn;
- PGresult *res;
- int nFields,
- nRows;
- int i,
- j;
- /*
- * If the user supplies a parameter on the command line, use it as the
- * conninfo string; otherwise default to setting dbname=postgres and using
- * environment variables or defaults for all other connection parameters.
- */
- if (argc > 1)
- conninfo = argv[1];
- else
- conninfo = "host=127.0.0.1 dbname=postgres user=postgres password=PASSWORD port=5432 connect_timeout=5";
- /* Make a connection to the database */
- conn = PQconnectdb(conninfo);
- /* Check to see that the backend connection was successfully made */
- if (PQstatus(conn) != CONNECTION_OK)
- {
- fprintf(stderr, "Connection to database failed: %s",
- PQerrorMessage(conn));
- exit_nicely(conn);
- }
- /* Start a transaction block */
- res = PQexec(conn, "BEGIN");
- PQclear(res);
- /* disable notice */
- res = PQexec(conn, "SET client_min_messages TO WARNING;");
- PQclear(res);
- /* end the transaction */
- res = PQexec(conn, "END");
- PQclear(res);
- /* close the connection to the database and clean-up */
- PQfinish(conn);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement