Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b7ae5a

Browse files
committed
Stabilize the results of pg_notification_queue_usage().
This function wasn't touched in commit 51004c7, but that turns out to be a bad idea, because its results now include any dead space that exists in the NOTIFY queue on account of our being lazy about advancing the queue tail. Notably, the isolation tests now fail if run twice without a server restart between, because async-notify's first test of the function will already show a positive value. It seems likely that end users would be equally unhappy about the result's instability. To fix, just make the function call asyncQueueAdvanceTail before computing its result. That should end in producing the same value as before, and it's hard to believe that there's any practical use-case where pg_notification_queue_usage() is called so often as to create a performance degradation, especially compared to what we did before. Out of paranoia, also mark this function parallel-restricted (it was volatile, but parallel-safe by default, before). Although the code seems to work fine when run in a parallel worker, that's outside the design scope of async.c, and it's a bit scary to have intentional side-effects happening in a parallel worker. There seems no plausible use-case where it'd be important to try to parallelize this, so let's not take any risk of introducing new bugs. In passing, re-pgindent async.c and run reformat-dat-files on pg_proc.dat, just because I'm a neatnik. Discussion: https://postgr.es/m/13881.1574557302@sss.pgh.pa.us
1 parent 91da65f commit 8b7ae5a

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/backend/commands/async.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ typedef struct
347347
typedef struct ActionList
348348
{
349349
int nestingLevel; /* current transaction nesting depth */
350-
List *actions; /* list of ListenAction structs */
350+
List *actions; /* list of ListenAction structs */
351351
struct ActionList *upper; /* details for upper transaction levels */
352352
} ActionList;
353353

@@ -393,7 +393,7 @@ typedef struct NotificationList
393393
int nestingLevel; /* current transaction nesting depth */
394394
List *events; /* list of Notification structs */
395395
HTAB *hashtab; /* hash of NotificationHash structs, or NULL */
396-
struct NotificationList *upper; /* details for upper transaction levels */
396+
struct NotificationList *upper; /* details for upper transaction levels */
397397
} NotificationList;
398398

399399
#define MIN_HASHABLE_NOTIFIES 16 /* threshold to build hashtab */
@@ -1554,6 +1554,9 @@ pg_notification_queue_usage(PG_FUNCTION_ARGS)
15541554
{
15551555
double usage;
15561556

1557+
/* Advance the queue tail so we don't report a too-large result */
1558+
asyncQueueAdvanceTail();
1559+
15571560
LWLockAcquire(AsyncQueueLock, LW_SHARED);
15581561
usage = asyncQueueUsage();
15591562
LWLockRelease(AsyncQueueLock);
@@ -1834,9 +1837,8 @@ AtSubAbort_Notify(void)
18341837
* those are allocated in TopTransactionContext.
18351838
*
18361839
* Note that there might be no entries at all, or no entries for the
1837-
* current subtransaction level, either because none were ever created,
1838-
* or because we reentered this routine due to trouble during subxact
1839-
* abort.
1840+
* current subtransaction level, either because none were ever created, or
1841+
* because we reentered this routine due to trouble during subxact abort.
18401842
*/
18411843
while (pendingActions != NULL &&
18421844
pendingActions->nestingLevel >= my_level)
@@ -2400,9 +2402,9 @@ ClearPendingActionsAndNotifies(void)
24002402
{
24012403
/*
24022404
* Everything's allocated in either TopTransactionContext or the context
2403-
* for the subtransaction to which it corresponds. So, there's nothing
2404-
* to do here except rest the pointers; the space will be reclaimed when
2405-
* the contexts are deleted.
2405+
* for the subtransaction to which it corresponds. So, there's nothing to
2406+
* do here except reset the pointers; the space will be reclaimed when the
2407+
* contexts are deleted.
24062408
*/
24072409
pendingActions = NULL;
24082410
pendingNotifies = NULL;

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201911212
56+
#define CATALOG_VERSION_NO 201911241
5757

5858
#endif

src/include/catalog/pg_proc.dat

+8-10
Original file line numberDiff line numberDiff line change
@@ -7653,7 +7653,7 @@
76537653
{ oid => '3296',
76547654
descr => 'get the fraction of the asynchronous notification queue currently in use',
76557655
proname => 'pg_notification_queue_usage', provolatile => 'v',
7656-
prorettype => 'float8', proargtypes => '',
7656+
proparallel => 'r', prorettype => 'float8', proargtypes => '',
76577657
prosrc => 'pg_notification_queue_usage' },
76587658

76597659
# non-persistent series generator
@@ -9333,14 +9333,13 @@
93339333
proargtypes => 'jsonb jsonpath jsonb bool', prosrc => 'jsonb_path_match' },
93349334

93359335
{ oid => '1177', descr => 'jsonpath exists test with timezone',
9336-
proname => 'jsonb_path_exists_tz', provolatile => 's',
9337-
prorettype => 'bool', proargtypes => 'jsonb jsonpath jsonb bool',
9336+
proname => 'jsonb_path_exists_tz', provolatile => 's', prorettype => 'bool',
9337+
proargtypes => 'jsonb jsonpath jsonb bool',
93389338
prosrc => 'jsonb_path_exists_tz' },
93399339
{ oid => '1179', descr => 'jsonpath query with timezone',
9340-
proname => 'jsonb_path_query_tz', provolatile => 's',
9341-
prorows => '1000', proretset => 't',
9342-
prorettype => 'jsonb', proargtypes => 'jsonb jsonpath jsonb bool',
9343-
prosrc => 'jsonb_path_query_tz' },
9340+
proname => 'jsonb_path_query_tz', prorows => '1000', proretset => 't',
9341+
provolatile => 's', prorettype => 'jsonb',
9342+
proargtypes => 'jsonb jsonpath jsonb bool', prosrc => 'jsonb_path_query_tz' },
93449343
{ oid => '1180', descr => 'jsonpath query wrapped into array with timezone',
93459344
proname => 'jsonb_path_query_array_tz', provolatile => 's',
93469345
prorettype => 'jsonb', proargtypes => 'jsonb jsonpath jsonb bool',
@@ -9350,9 +9349,8 @@
93509349
prorettype => 'jsonb', proargtypes => 'jsonb jsonpath jsonb bool',
93519350
prosrc => 'jsonb_path_query_first_tz' },
93529351
{ oid => '2030', descr => 'jsonpath match with timezone',
9353-
proname => 'jsonb_path_match_tz', provolatile => 's',
9354-
prorettype => 'bool', proargtypes => 'jsonb jsonpath jsonb bool',
9355-
prosrc => 'jsonb_path_match_tz' },
9352+
proname => 'jsonb_path_match_tz', provolatile => 's', prorettype => 'bool',
9353+
proargtypes => 'jsonb jsonpath jsonb bool', prosrc => 'jsonb_path_match_tz' },
93569354

93579355
{ oid => '4010', descr => 'implementation of @? operator',
93589356
proname => 'jsonb_path_exists_opr', prorettype => 'bool',

0 commit comments

Comments
 (0)