Skip to content

Commit 111c9d9

Browse files
author
Commitfest Bot
committed
[CF 5825] v2 - Cancel problems of query to pg_stat_statements
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5825 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Roman Khapov
2 parents 9ea3b6f + 42e1649 commit 111c9d9

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static pgssEntry *entry_alloc(pgssHashKey *key, Size query_offset, int query_len
370370
static void entry_dealloc(void);
371371
static bool qtext_store(const char *query, int query_len,
372372
Size *query_offset, int *gc_count);
373-
static char *qtext_load_file(Size *buffer_size);
373+
static char *qtext_load_file(Size *buffer_size, bool fail_on_interrupts);
374374
static char *qtext_fetch(Size query_offset, int query_len,
375375
char *buffer, Size buffer_size);
376376
static bool need_gc_qtexts(void);
@@ -772,7 +772,7 @@ pgss_shmem_shutdown(int code, Datum arg)
772772
if (fwrite(&num_entries, sizeof(int32), 1, file) != 1)
773773
goto error;
774774

775-
qbuffer = qtext_load_file(&qbuffer_size);
775+
qbuffer = qtext_load_file(&qbuffer_size, false /* fail_on_interrupts */ );
776776
if (qbuffer == NULL)
777777
goto error;
778778

@@ -1790,7 +1790,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
17901790

17911791
/* No point in loading file now if there are active writers */
17921792
if (n_writers == 0)
1793-
qbuffer = qtext_load_file(&qbuffer_size);
1793+
qbuffer = qtext_load_file(&qbuffer_size, true /* fail_on_interrupts */ );
17941794
}
17951795

17961796
/*
@@ -1823,7 +1823,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18231823
pgss->gc_count != gc_count)
18241824
{
18251825
free(qbuffer);
1826-
qbuffer = qtext_load_file(&qbuffer_size);
1826+
qbuffer = qtext_load_file(&qbuffer_size, true /* fail_on_interrupts */ );
18271827
}
18281828
}
18291829

@@ -1842,6 +1842,12 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18421842
memset(values, 0, sizeof(values));
18431843
memset(nulls, 0, sizeof(nulls));
18441844

1845+
/* Can't process interrupts here - pgss-lock is acquired */
1846+
if (INTERRUPTS_PENDING_CONDITION())
1847+
{
1848+
break;
1849+
}
1850+
18451851
values[i++] = ObjectIdGetDatum(entry->key.userid);
18461852
values[i++] = ObjectIdGetDatum(entry->key.dbid);
18471853
if (api_version >= PGSS_V1_9)
@@ -2043,6 +2049,8 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
20432049

20442050
LWLockRelease(pgss->lock);
20452051

2052+
CHECK_FOR_INTERRUPTS();
2053+
20462054
free(qbuffer);
20472055
}
20482056

@@ -2341,7 +2349,7 @@ qtext_store(const char *query, int query_len,
23412349
* the caller is responsible for verifying that the result is sane.
23422350
*/
23432351
static char *
2344-
qtext_load_file(Size *buffer_size)
2352+
qtext_load_file(Size *buffer_size, bool fail_on_interrupts)
23452353
{
23462354
char *buf;
23472355
int fd;
@@ -2394,7 +2402,14 @@ qtext_load_file(Size *buffer_size)
23942402
nread = 0;
23952403
while (nread < stat.st_size)
23962404
{
2397-
int toread = Min(1024 * 1024 * 1024, stat.st_size - nread);
2405+
int toread = Min(32 * 1024 * 1024, stat.st_size - nread);
2406+
2407+
if (fail_on_interrupts && INTERRUPTS_PENDING_CONDITION())
2408+
{
2409+
free(buf);
2410+
CloseTransientFile(fd);
2411+
return NULL;
2412+
}
23982413

23992414
/*
24002415
* If we get a short read and errno doesn't get set, the reason is
@@ -2531,7 +2546,7 @@ gc_qtexts(void)
25312546
* file is only going to get bigger; hoping for a future non-OOM result is
25322547
* risky and can easily lead to complete denial of service.
25332548
*/
2534-
qbuffer = qtext_load_file(&qbuffer_size);
2549+
qbuffer = qtext_load_file(&qbuffer_size, false /* fail_on_interrupts */ );
25352550
if (qbuffer == NULL)
25362551
goto gc_fail;
25372552

0 commit comments

Comments
 (0)