isolationtester: append session name to application_name.
authorAndres Freund <andres@anarazel.de>
Mon, 13 Dec 2021 20:02:06 +0000 (12:02 -0800)
committerAndres Freund <andres@anarazel.de>
Mon, 13 Dec 2021 20:02:06 +0000 (12:02 -0800)
When writing / debugging an isolation test it sometimes is useful to see which
session holds what lock etc. To make it easier, both as part of spec files and
interactively, append the session name to application_name. Since b1907d688
application_name already contains the test name, this appends the session's
name to that.

insert-conflict-specconflict did something like this manually, which can now
be removed.

As we have done lately with other test infrastructure improvements, backpatch
this change, to make it easier to backpatch tests.

Author: Andres Freund <andres@anarazel.de>
Reviewed-By: Michael Paquier <michael@paquier.xyz>
Reviewed-By: Andrew Dunstan <andrew@dunslane.net>
Discussion: https://postgr.es/m/20211211012052.2blmzcmxnxqawd2z@alap3.anarazel.de
Backpatch: 10-, to make backpatching of tests easier.

src/test/isolation/expected/insert-conflict-specconflict.out
src/test/isolation/isolationtester.c
src/test/isolation/specs/insert-conflict-specconflict.spec

index bb8f950f2cfbb5613619a0050cf232e526b7e0ce..e34a821c403c72d60f64a4f9467b1be886d8f11b 100644 (file)
@@ -490,15 +490,15 @@ step controller_print_speculative_locks:
     WHERE
         locktype IN ('spectoken', 'transactionid')
         AND pa.datname = current_database()
-        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict-s%'
+        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict/s%'
     ORDER BY 1, 2, 3, 4;
 
 application_name                         |locktype     |mode         |granted
 -----------------------------------------+-------------+-------------+-------
-isolation/insert-conflict-specconflict-s1|spectoken    |ShareLock    |f      
-isolation/insert-conflict-specconflict-s1|transactionid|ExclusiveLock|t      
-isolation/insert-conflict-specconflict-s2|spectoken    |ExclusiveLock|t      
-isolation/insert-conflict-specconflict-s2|transactionid|ExclusiveLock|t      
+isolation/insert-conflict-specconflict/s1|spectoken    |ShareLock    |f      
+isolation/insert-conflict-specconflict/s1|transactionid|ExclusiveLock|t      
+isolation/insert-conflict-specconflict/s2|spectoken    |ExclusiveLock|t      
+isolation/insert-conflict-specconflict/s2|transactionid|ExclusiveLock|t      
 (4 rows)
 
 step controller_unlock_2_4: SELECT pg_advisory_unlock(2, 4);
@@ -517,14 +517,14 @@ step controller_print_speculative_locks:
     WHERE
         locktype IN ('spectoken', 'transactionid')
         AND pa.datname = current_database()
-        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict-s%'
+        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict/s%'
     ORDER BY 1, 2, 3, 4;
 
 application_name                         |locktype     |mode         |granted
 -----------------------------------------+-------------+-------------+-------
-isolation/insert-conflict-specconflict-s1|transactionid|ExclusiveLock|t      
-isolation/insert-conflict-specconflict-s1|transactionid|ShareLock    |f      
-isolation/insert-conflict-specconflict-s2|transactionid|ExclusiveLock|t      
+isolation/insert-conflict-specconflict/s1|transactionid|ExclusiveLock|t      
+isolation/insert-conflict-specconflict/s1|transactionid|ShareLock    |f      
+isolation/insert-conflict-specconflict/s2|transactionid|ExclusiveLock|t      
 (3 rows)
 
 step s2_commit: COMMIT;
@@ -544,7 +544,7 @@ step controller_print_speculative_locks:
     WHERE
         locktype IN ('spectoken', 'transactionid')
         AND pa.datname = current_database()
-        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict-s%'
+        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict/s%'
     ORDER BY 1, 2, 3, 4;
 
 application_name|locktype|mode|granted
index 88594a3cb5dda1111251a3d12a283a4af52c554a..12179f251469c8d8885106aa0c2bcb1a9979ad53 100644 (file)
@@ -154,10 +154,14 @@ main(int argc, char **argv)
 
    for (i = 0; i < nconns; i++)
    {
+       const char *sessionname;
+
        if (i == 0)
-           conns[i].sessionname = "control connection";
+           sessionname = "control connection";
        else
-           conns[i].sessionname = testspec->sessions[i - 1]->name;
+           sessionname = testspec->sessions[i - 1]->name;
+
+       conns[i].sessionname = sessionname;
 
        conns[i].conn = PQconnectdb(conninfo);
        if (PQstatus(conns[i].conn) != CONNECTION_OK)
@@ -182,6 +186,26 @@ main(int argc, char **argv)
                                 blackholeNoticeProcessor,
                                 NULL);
 
+       /*
+        * Similarly, append the session name to application_name to make it
+        * easier to map spec file sessions to log output and
+        * pg_stat_activity. The reason to append instead of just setting the
+        * name is that we don't know the name of the test currently running.
+        */
+       res = PQexecParams(conns[i].conn,
+                          "SELECT set_config('application_name',\n"
+                          "  current_setting('application_name') || '/' || $1,\n"
+                          "  false)",
+                          1, NULL,
+                          &sessionname,
+                          NULL, NULL, 0);
+       if (PQresultStatus(res) != PGRES_TUPLES_OK)
+       {
+           fprintf(stderr, "setting of application name failed: %s",
+                   PQerrorMessage(conns[i].conn));
+           exit(1);
+       }
+
        /* Save each connection's backend PID for subsequent use. */
        conns[i].backend_pid = PQbackendPID(conns[i].conn);
        conns[i].backend_pid_str = psprintf("%d", conns[i].backend_pid);
index 55b8bb100f4a52da5dde49321219b89a2de78476..0d55a015b6e5bd00ea33e4eaa119b6ace3c8629a 100644 (file)
@@ -47,7 +47,6 @@ session controller
 setup
 {
     SET default_transaction_isolation = 'read committed';
-    SET application_name = 'isolation/insert-conflict-specconflict-controller';
 }
 step controller_locks {SELECT pg_advisory_lock(sess, lock), sess, lock FROM generate_series(1, 2) a(sess), generate_series(1,3) b(lock);}
 step controller_unlock_1_1 { SELECT pg_advisory_unlock(1, 1); }
@@ -66,7 +65,7 @@ step controller_print_speculative_locks {
     WHERE
         locktype IN ('spectoken', 'transactionid')
         AND pa.datname = current_database()
-        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict-s%'
+        AND pa.application_name LIKE 'isolation/insert-conflict-specconflict/s%'
     ORDER BY 1, 2, 3, 4;
 }
 
@@ -75,7 +74,6 @@ setup
 {
     SET default_transaction_isolation = 'read committed';
     SET spec.session = 1;
-    SET application_name = 'isolation/insert-conflict-specconflict-s1';
 }
 step s1_begin  { BEGIN; }
 step s1_create_non_unique_index { CREATE INDEX upserttest_key_idx ON upserttest((blurt_and_lock_4(key))); }
@@ -90,7 +88,6 @@ setup
 {
     SET default_transaction_isolation = 'read committed';
     SET spec.session = 2;
-    SET application_name = 'isolation/insert-conflict-specconflict-s2';
 }
 step s2_begin  { BEGIN; }
 step s2_upsert { INSERT INTO upserttest(key, data) VALUES('k1', 'inserted s2') ON CONFLICT (blurt_and_lock_123(key)) DO UPDATE SET data = upserttest.data || ' with conflict update s2'; }