dblink/isolationtester/fe_utils: Use new cancel API
authorAlvaro Herrera <[email protected]>
Mon, 18 Mar 2024 18:28:58 +0000 (19:28 +0100)
committerAlvaro Herrera <[email protected]>
Mon, 18 Mar 2024 18:28:58 +0000 (19:28 +0100)
Commit 61461a300c1c introduced new functions to libpq for cancelling
queries.  This replaces the usage of the old ones in parts of the
codebase with these newer ones.  This specifically leaves out changes to
psql and pgbench, as those would need a much larger refactor to be able
to call them due to the new functions not being signal-safe; and also
postgres_fdw, because the original code there is not clear to me
(Álvaro) and not fully tested.

Author: Jelte Fennema-Nio <[email protected]>
Discussion: https://postgr.es/m/CAGECzQT_VgOWWENUqvUV9xQmbaCyXjtRRAYO8W07oqashk_N+g@mail.gmail.com

contrib/dblink/dblink.c
src/fe_utils/connect_utils.c
src/test/isolation/isolationtester.c

index 19a362526d21dff5d8b1cdc68b15afebe7d40249..edbc9ab02ac1510d0eadad78af965735fa2a6b2f 100644 (file)
@@ -1346,22 +1346,28 @@ PG_FUNCTION_INFO_V1(dblink_cancel_query);
 Datum
 dblink_cancel_query(PG_FUNCTION_ARGS)
 {
-   int         res;
    PGconn     *conn;
-   PGcancel   *cancel;
-   char        errbuf[256];
+   PGcancelConn *cancelConn;
+   char       *msg;
 
    dblink_init();
    conn = dblink_get_named_conn(text_to_cstring(PG_GETARG_TEXT_PP(0)));
-   cancel = PQgetCancel(conn);
+   cancelConn = PQcancelCreate(conn);
 
-   res = PQcancel(cancel, errbuf, 256);
-   PQfreeCancel(cancel);
+   PG_TRY();
+   {
+       if (!PQcancelBlocking(cancelConn))
+           msg = pchomp(PQcancelErrorMessage(cancelConn));
+       else
+           msg = "OK";
+   }
+   PG_FINALLY();
+   {
+       PQcancelFinish(cancelConn);
+   }
+   PG_END_TRY();
 
-   if (res == 1)
-       PG_RETURN_TEXT_P(cstring_to_text("OK"));
-   else
-       PG_RETURN_TEXT_P(cstring_to_text(errbuf));
+   PG_RETURN_TEXT_P(cstring_to_text(msg));
 }
 
 
index 808d54461fdfc7bfbb25364ae88fe66b283fbc41..5ed9f3ba17b78b0fe2572def7d49145076e4f154 100644 (file)
@@ -157,19 +157,14 @@ connectMaintenanceDatabase(ConnParams *cparams,
 void
 disconnectDatabase(PGconn *conn)
 {
-   char        errbuf[256];
-
    Assert(conn != NULL);
 
    if (PQtransactionStatus(conn) == PQTRANS_ACTIVE)
    {
-       PGcancel   *cancel;
+       PGcancelConn *cancelConn = PQcancelCreate(conn);
 
-       if ((cancel = PQgetCancel(conn)))
-       {
-           (void) PQcancel(cancel, errbuf, sizeof(errbuf));
-           PQfreeCancel(cancel);
-       }
+       (void) PQcancelBlocking(cancelConn);
+       PQcancelFinish(cancelConn);
    }
 
    PQfinish(conn);
index ed110f740f1f5e7ba8f1b13aa2847f587da72ea0..0b342b5c2bbba131aeae163d81208809e74de451 100644 (file)
@@ -946,26 +946,21 @@ try_complete_step(TestSpec *testspec, PermutationStep *pstep, int flags)
             */
            if (td > max_step_wait && !canceled)
            {
-               PGcancel   *cancel = PQgetCancel(conn);
+               PGcancelConn *cancel_conn = PQcancelCreate(conn);
 
-               if (cancel != NULL)
+               if (PQcancelBlocking(cancel_conn))
                {
-                   char        buf[256];
-
-                   if (PQcancel(cancel, buf, sizeof(buf)))
-                   {
-                       /*
-                        * print to stdout not stderr, as this should appear
-                        * in the test case's results
-                        */
-                       printf("isolationtester: canceling step %s after %d seconds\n",
-                              step->name, (int) (td / USECS_PER_SEC));
-                       canceled = true;
-                   }
-                   else
-                       fprintf(stderr, "PQcancel failed: %s\n", buf);
-                   PQfreeCancel(cancel);
+                   /*
+                    * print to stdout not stderr, as this should appear in
+                    * the test case's results
+                    */
+                   printf("isolationtester: canceling step %s after %d seconds\n",
+                          step->name, (int) (td / USECS_PER_SEC));
+                   canceled = true;
                }
+               else
+                   fprintf(stderr, "PQcancel failed: %s\n", PQcancelErrorMessage(cancel_conn));
+               PQcancelFinish(cancel_conn);
            }
 
            /*