|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * waitfuncs.c |
| 4 | + * Functions for SQL access to syntheses of multiple contention types. |
| 5 | + * |
| 6 | + * Copyright (c) 2002-2024, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + * IDENTIFICATION |
| 9 | + * src/backend/utils/adt/waitfuncs.c |
| 10 | + * |
| 11 | + *------------------------------------------------------------------------- |
| 12 | + */ |
| 13 | +#include "postgres.h" |
| 14 | + |
| 15 | +#include "catalog/pg_type.h" |
| 16 | +#include "storage/predicate_internals.h" |
| 17 | +#include "utils/array.h" |
| 18 | +#include "utils/builtins.h" |
| 19 | + |
| 20 | + |
| 21 | +/* |
| 22 | + * pg_isolation_test_session_is_blocked - support function for isolationtester |
| 23 | + * |
| 24 | + * Check if specified PID is blocked by any of the PIDs listed in the second |
| 25 | + * argument. Currently, this looks for blocking caused by waiting for |
| 26 | + * heavyweight locks or safe snapshots. We ignore blockage caused by PIDs |
| 27 | + * not directly under the isolationtester's control, eg autovacuum. |
| 28 | + * |
| 29 | + * This is an undocumented function intended for use by the isolation tester, |
| 30 | + * and may change in future releases as required for testing purposes. |
| 31 | + */ |
| 32 | +Datum |
| 33 | +pg_isolation_test_session_is_blocked(PG_FUNCTION_ARGS) |
| 34 | +{ |
| 35 | + int blocked_pid = PG_GETARG_INT32(0); |
| 36 | + ArrayType *interesting_pids_a = PG_GETARG_ARRAYTYPE_P(1); |
| 37 | + ArrayType *blocking_pids_a; |
| 38 | + int32 *interesting_pids; |
| 39 | + int32 *blocking_pids; |
| 40 | + int num_interesting_pids; |
| 41 | + int num_blocking_pids; |
| 42 | + int dummy; |
| 43 | + int i, |
| 44 | + j; |
| 45 | + |
| 46 | + /* Validate the passed-in array */ |
| 47 | + Assert(ARR_ELEMTYPE(interesting_pids_a) == INT4OID); |
| 48 | + if (array_contains_nulls(interesting_pids_a)) |
| 49 | + elog(ERROR, "array must not contain nulls"); |
| 50 | + interesting_pids = (int32 *) ARR_DATA_PTR(interesting_pids_a); |
| 51 | + num_interesting_pids = ArrayGetNItems(ARR_NDIM(interesting_pids_a), |
| 52 | + ARR_DIMS(interesting_pids_a)); |
| 53 | + |
| 54 | + /* |
| 55 | + * Get the PIDs of all sessions blocking the given session's attempt to |
| 56 | + * acquire heavyweight locks. |
| 57 | + */ |
| 58 | + blocking_pids_a = |
| 59 | + DatumGetArrayTypeP(DirectFunctionCall1(pg_blocking_pids, blocked_pid)); |
| 60 | + |
| 61 | + Assert(ARR_ELEMTYPE(blocking_pids_a) == INT4OID); |
| 62 | + Assert(!array_contains_nulls(blocking_pids_a)); |
| 63 | + blocking_pids = (int32 *) ARR_DATA_PTR(blocking_pids_a); |
| 64 | + num_blocking_pids = ArrayGetNItems(ARR_NDIM(blocking_pids_a), |
| 65 | + ARR_DIMS(blocking_pids_a)); |
| 66 | + |
| 67 | + /* |
| 68 | + * Check if any of these are in the list of interesting PIDs, that being |
| 69 | + * the sessions that the isolation tester is running. We don't use |
| 70 | + * "arrayoverlaps" here, because it would lead to cache lookups and one of |
| 71 | + * our goals is to run quickly with debug_discard_caches > 0. We expect |
| 72 | + * blocking_pids to be usually empty and otherwise a very small number in |
| 73 | + * isolation tester cases, so make that the outer loop of a naive search |
| 74 | + * for a match. |
| 75 | + */ |
| 76 | + for (i = 0; i < num_blocking_pids; i++) |
| 77 | + for (j = 0; j < num_interesting_pids; j++) |
| 78 | + { |
| 79 | + if (blocking_pids[i] == interesting_pids[j]) |
| 80 | + PG_RETURN_BOOL(true); |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + * Check if blocked_pid is waiting for a safe snapshot. We could in |
| 85 | + * theory check the resulting array of blocker PIDs against the |
| 86 | + * interesting PIDs list, but since there is no danger of autovacuum |
| 87 | + * blocking GetSafeSnapshot there seems to be no point in expending cycles |
| 88 | + * on allocating a buffer and searching for overlap; so it's presently |
| 89 | + * sufficient for the isolation tester's purposes to use a single element |
| 90 | + * buffer and check if the number of safe snapshot blockers is non-zero. |
| 91 | + */ |
| 92 | + if (GetSafeSnapshotBlockingPids(blocked_pid, &dummy, 1) > 0) |
| 93 | + PG_RETURN_BOOL(true); |
| 94 | + |
| 95 | + PG_RETURN_BOOL(false); |
| 96 | +} |
0 commit comments