LCOV - code coverage report
Current view: top level - src/fe_utils - query_utils.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 87.5 % 32 28
Test Date: 2026-03-11 19:16:03 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * Facilities for frontend code to query a databases.
       4              :  *
       5              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       6              :  * Portions Copyright (c) 1994, Regents of the University of California
       7              :  *
       8              :  * src/fe_utils/query_utils.c
       9              :  *
      10              :  *-------------------------------------------------------------------------
      11              :  */
      12              : #include "postgres_fe.h"
      13              : 
      14              : #include "common/logging.h"
      15              : #include "fe_utils/cancel.h"
      16              : #include "fe_utils/query_utils.h"
      17              : 
      18              : /*
      19              :  * Run a query, return the results, exit program on failure.
      20              :  */
      21              : PGresult *
      22          848 : executeQuery(PGconn *conn, const char *query, bool echo)
      23              : {
      24              :     PGresult   *res;
      25              : 
      26          848 :     if (echo)
      27           42 :         printf("%s\n", query);
      28              : 
      29          848 :     res = PQexec(conn, query);
      30         1696 :     if (!res ||
      31          848 :         PQresultStatus(res) != PGRES_TUPLES_OK)
      32              :     {
      33            2 :         pg_log_error("query failed: %s", PQerrorMessage(conn));
      34            2 :         pg_log_error_detail("Query was: %s", query);
      35            2 :         PQfinish(conn);
      36            2 :         exit(1);
      37              :     }
      38              : 
      39          846 :     return res;
      40              : }
      41              : 
      42              : 
      43              : /*
      44              :  * As above for a SQL command (which returns nothing).
      45              :  */
      46              : void
      47          163 : executeCommand(PGconn *conn, const char *query, bool echo)
      48              : {
      49              :     PGresult   *res;
      50              : 
      51          163 :     if (echo)
      52            7 :         printf("%s\n", query);
      53              : 
      54          163 :     res = PQexec(conn, query);
      55          326 :     if (!res ||
      56          163 :         PQresultStatus(res) != PGRES_COMMAND_OK)
      57              :     {
      58            0 :         pg_log_error("query failed: %s", PQerrorMessage(conn));
      59            0 :         pg_log_error_detail("Query was: %s", query);
      60            0 :         PQfinish(conn);
      61            0 :         exit(1);
      62              :     }
      63              : 
      64          163 :     PQclear(res);
      65          163 : }
      66              : 
      67              : 
      68              : /*
      69              :  * As above for a SQL maintenance command (returns command success).
      70              :  * Command is executed with a cancel handler set, so Ctrl-C can
      71              :  * interrupt it.
      72              :  */
      73              : bool
      74           15 : executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
      75              : {
      76              :     PGresult   *res;
      77              :     bool        r;
      78              : 
      79           15 :     if (echo)
      80            7 :         printf("%s\n", query);
      81              : 
      82           15 :     SetCancelConn(conn);
      83           15 :     res = PQexec(conn, query);
      84           15 :     ResetCancelConn();
      85              : 
      86           15 :     r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
      87              : 
      88           15 :     PQclear(res);
      89              : 
      90           15 :     return r;
      91              : }
        

Generated by: LCOV version 2.0-1