@@ -1265,8 +1265,12 @@ pgss_store(const char *query, uint64 queryId,
12651265 key .queryid = queryId ;
12661266 key .toplevel = (exec_nested_level == 0 );
12671267
1268- /* Lookup the hash table entry with shared lock. */
1269- LWLockAcquire (pgss -> lock , LW_SHARED );
1268+ /*
1269+ * Lookup the hash table entry with shared lock.
1270+ * If exclusive lock is taken - just give up.
1271+ */
1272+ if (!LWLockConditionalAcquire (pgss -> lock , LW_SHARED ))
1273+ return ;
12701274
12711275 entry = (pgssEntry * ) hash_search (pgss_hash , & key , HASH_FIND , NULL );
12721276
@@ -1291,7 +1295,13 @@ pgss_store(const char *query, uint64 queryId,
12911295 norm_query = generate_normalized_query (jstate , query ,
12921296 query_location ,
12931297 & query_len );
1294- LWLockAcquire (pgss -> lock , LW_SHARED );
1298+ /* exclusive lock may be taken while we were doing this */
1299+ /* XXX: Andrey: I'm not sure we should drop here shared lock at all */
1300+ if (!LWLockConditionalAcquire (pgss -> lock , LW_SHARED ))
1301+ {
1302+ pfree (norm_query );
1303+ return ;
1304+ }
12951305 }
12961306
12971307 /* Append new query text to file with only shared lock held */
@@ -1307,7 +1317,9 @@ pgss_store(const char *query, uint64 queryId,
13071317
13081318 /* Need exclusive lock to make a new hashtable entry - promote */
13091319 LWLockRelease (pgss -> lock );
1310- LWLockAcquire (pgss -> lock , LW_EXCLUSIVE );
1320+ /* This renders impossible to enter another concurrent query */
1321+ if (!LWLockConditionalAcquire (pgss -> lock , LW_EXCLUSIVE ))
1322+ return ;
13111323
13121324 /*
13131325 * A garbage collection may have occurred while we weren't holding the
0 commit comments