Skip to content

Commit

Permalink
isolationtester: append session name to application_name.
Browse files Browse the repository at this point in the history
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 b1907d6
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.
  • Loading branch information
anarazel committed Dec 13, 2021
1 parent 3f06c00 commit 65f860e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/test/isolation/expected/insert-conflict-specconflict.out
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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
Expand Down
28 changes: 26 additions & 2 deletions src/test/isolation/isolationtester.c
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/test/isolation/specs/insert-conflict-specconflict.spec
Expand Up @@ -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); }
Expand All @@ -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;
}

Expand All @@ -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))); }
Expand All @@ -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'; }
Expand Down

0 comments on commit 65f860e

Please sign in to comment.