Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wZjJp-001F9R-1W for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Jun 2026 06:00:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wZjJn-004VIL-0G for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Jun 2026 06:00:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wZjJm-004VIB-1t for pgsql-hackers@lists.postgresql.org; Wed, 17 Jun 2026 06:00:06 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wZjJk-00000000l9q-3uET for pgsql-hackers@postgresql.org; Wed, 17 Jun 2026 06:00:05 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-490b3637b90so41658225e9.3 for ; Tue, 16 Jun 2026 23:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781676003; x=1782280803; darn=postgresql.org; h=to:subject:from:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=iit85sHjKvk+Wg/FYIjOhg7uoS8n2+TyzpO+8CK74wc=; b=INRT6xV+ZZK2/ymRAkyrIVR3RNxMl92/fokukiUWwUA32cpfaljN+8iRFfflxFe6WR kHiZ9BYDpG1QWLuC6BppmuDB8HL2iWdb8ZXDBko532JdXYxFj49fYz+6ETxDJ4Ct/8OL 17mxfVmfnrdjghMMiQ3F3t4oEPlYu3P3GSwSZBgdfhSLwYTX5fR2Zfe+vf+yBm+wF2fs 4yxaHxtE1nFbpD1wJiE6jjTiFk5k6hmbPhQuUdjh/O1Vz69UVh5jPzzQ/L3m5eZF4qgK ayEpovkKeiW8FPTNqRbzmQyD7O4I4yP2VvBTm5o91qKBPl/GBhssJ+jhazHTVkyA+c0f 3/dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781676003; x=1782280803; h=to:subject:from:content-language:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=iit85sHjKvk+Wg/FYIjOhg7uoS8n2+TyzpO+8CK74wc=; b=gPCGZF7XzKz8J0DPkLMw0fpKxm6mxHJsslBAWPIQsrQe+blRRq0EB7PQaP2Xda+Quu DXsN7hIjEiJCxz/gBOY5MVGqrlELaJGGNm9ZMNzADV8ahUkuupQMNQhcSLYm0V6ZbJHP /csMcFbGErRenbLpXJe9B67aqaFa62S1XaF9bNX9I3LJmPUnZfTw9G0/BdGy19QVkoIT a1q1J5F+YQuRcQ2dME96id/ma7ZGZFdYkP5WV3ZenryLjr0bR5twwU9cul4yWn3XHvdu rDgw8S6Nw5YfGU34t1dnLHGq7BVnkmAAwGqPp52/R7cojLBZ4TyYa9cDxcHGgUZYJS23 adrw== X-Gm-Message-State: AOJu0Yz49b4323tpaXh91xG0i0PTRqbAcypBEguYBjtvmkg9Kw8K35Wd 1o2hYxwmUWUB6bpBpEO5a/BoGygbvrWel7mXX8pwHzoBkFSvS50v0utfmE6OOw== X-Gm-Gg: Acq92OEIiBDnIEyVgf5txWYpPLCVnEMSd9wGLORgsJop7OeNjjxhfD0ZrCCL9H7MGDS DQxbpGZN9AlrMoVeVtALZ2yCG8qaci1dKxmLcgIjt9XdtNXmPUKtoWMVI7SbIS99NbXX+Fwl2dy CgqPwxjeSzoToYmFVSXJEVimn9YL6PY0EA8zwVFu4MVPAkxMVxranCiHOK0hFHpyh4EF2Nzq1Yd 8uz+Isyb8b2auurDt+x4f5zKFXIdtId/ENoECRlLr3o+s//GZrqfhBz1hLcGGbtftexZx0SW61Q /NqFOyWtwTocqZdKxbTcoxCYqL4XoODB12PKGDLwtes8dEjWSfF9XL8VbyoIuhT1+bBm/YhvMTr AUjasMkFFtJs+UnoqZ1AEZoEK+5US5K6fIJE4uMY3fElx+/YmFczqkuHNYpQSdo+fEGgp1rJTmP RJVnB8SlhIcqRW/12Zff84/U8Qxg== X-Received: by 2002:a05:600c:828c:b0:490:be14:bfda with SMTP id 5b1f17b1804b1-4923339fd76mr40716095e9.6.1781676002729; Tue, 16 Jun 2026 23:00:02 -0700 (PDT) Received: from [192.168.0.50] ([89.149.120.180]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49233be965dsm26466185e9.1.2026.06.16.23.00.01 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Jun 2026 23:00:01 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------U6EqqLoVE430w79SH75sgZIx" Message-ID: Date: Wed, 17 Jun 2026 09:00:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US From: Alexander Lakhin Subject: Unexpected behavior after OOM errors To: pgsql-hackers List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------U6EqqLoVE430w79SH75sgZIx Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hello hackers, I'd like to share my findings related to OOM error handling. I'm not sure how large the class of such anomalies is (and if all of these can be detected and fixed), but please look at a few issues I have discovered so far: 1) An issue in lookup_type_cache() The following modification: --- a/src/backend/utils/hash/dynahash.c +++ b/src/backend/utils/hash/dynahash.c @@ -104,6 +104,7 @@  #include "storage/spin.h"  #include "utils/memutils.h" +#include "common/pg_prng.h"  /*   * Constants @@ -528,7 +529,7 @@ hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)       * that this is the first allocation made with the alloc function.  That's       * a little ugly, but works for now.       */ -    hashp->hctl = (HASHHDR *) hashp->alloc(sizeof(HASHHDR), hashp->alloc_arg); +    hashp->hctl = (pg_prng_double(&pg_global_prng_state) < 0.001) ? NULL : (HASHHDR *) hashp->alloc(sizeof(HASHHDR), hashp->alloc_arg);      if (!hashp->hctl)          ereport(ERROR,                  (errcode(ERRCODE_OUT_OF_MEMORY), @@ -609,7 +610,7 @@ hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)          {              int         temp = (i == 0) ? nelem_alloc_first : nelem_alloc; -            if (!element_alloc(hashp, temp, i)) +            if ((pg_prng_double(&pg_global_prng_state) < 0.001) || !element_alloc(hashp, temp, i))                  ereport(ERROR,                          (errcode(ERRCODE_OUT_OF_MEMORY),                           errmsg("out of memory"))); makes this script: for i in {1..10000}; do cat << 'EOS' | psql >>psql.log SELECT 1 ORDER BY 1; SELECT 1 ORDER BY 1; EOS grep "terminated by signal" server.log && break; done trigger an assertion failure: 2026-06-17 07:26:07.837 EEST [87325:3] [unknown] LOG:  connection authorized: user=law database=regression application_name=psql 2026-06-17 07:26:07.837 EEST [87325:4] psql LOG:  statement: SELECT 1 ORDER BY 1; 2026-06-17 07:26:07.837 EEST [87325:5] psql ERROR:  out of memory at character 19 2026-06-17 07:26:07.837 EEST [87325:6] psql LOG:  statement: SELECT 1 ORDER BY 1; TRAP: failed Assert("TypeCacheHash != NULL && RelIdToTypeIdCacheHash != NULL"), File: "typcache.c", Line: 441, PID: 87325 ExceptionalCondition at assert.c:51:13 lookup_type_cache at typcache.c:444:27 get_sort_group_operators at parse_oper.c:207:13 addTargetToSortList at parse_clause.c:3647:4 transformSortClause at parse_clause.c:2959:14 transformSelectStmt at analyze.c:1806:18 transformStmt at analyze.c:396:15 transformOptionalSelectInto at analyze.c:327:1 transformTopLevelStmt at analyze.c:276:11 parse_analyze_fixedparams at analyze.c:144:10 pg_analyze_and_rewrite_fixedparams at postgres.c:699:10 exec_simple_query at postgres.c:1206:20 PostgresMain at postgres.c:4860:27 BackendInitialize at backend_startup.c:142:1 postmaster_child_launch at launch_backend.c:269:3 BackendStartup at postmaster.c:3627:8 ServerLoop at postmaster.c:1731:10 PostmasterMain at postmaster.c:1415:11 main at main.c:236:2 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x8b)[0x7b3d5c02a28b] postgres: law regression [local] SELECT(_start+0x25)[0x5944d79e8155] 2026-06-17 07:26:07.914 EEST [85875:6] LOG:  client backend (PID 87325) was terminated by signal 6: Aborted Without asserts enables, the server might crash. 2) An issue in GetSnapshotData() The following modification: --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -71,2 +71,3 @@  #include "utils/wait_event.h" +#include "common/pg_prng.h" @@ -2157,3 +2158,3 @@ GetSnapshotData(Snapshot snapshot)          Assert(snapshot->subxip == NULL); -        snapshot->subxip = (TransactionId *) +        snapshot->subxip = (pg_prng_double(&pg_global_prng_state) < 0.01) ? NULL : (TransactionId *)              malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId)); makes this script (max_prepared_transactions = 2 in postgresql.conf): for i in {1..1000}; do cat << 'EOS' | psql >>psql.log SELECT 1; BEGIN;   CREATE TABLE t1(a int);   SAVEPOINT sp1;     INSERT INTO t1 VALUES (1);   ROLLBACK TO sp1;   INSERT INTO t1 VALUES (2); PREPARE TRANSACTION 'pt1'; BEGIN; CREATE TABLE t2(a int); ROLLBACK; ROLLBACK PREPARED 'pt1'; EOS grep "terminated by signal" server.log && break; done trigger a segmentation fault: 2026-06-17 07:37:52.619 EEST [108789:3] [unknown] LOG:  connection authorized: user=law database=regression application_name=psql 2026-06-17 07:37:52.620 EEST [108789:4] psql LOG:  statement: SELECT 1; 2026-06-17 07:37:52.620 EEST [108789:5] psql ERROR:  out of memory 2026-06-17 07:37:52.620 EEST [108789:6] psql LOG:  statement: BEGIN; 2026-06-17 07:37:52.620 EEST [108789:7] psql LOG:  statement: CREATE TABLE t1(a int); 2026-06-17 07:37:52.621 EEST [108789:8] psql LOG:  statement: SAVEPOINT sp1; 2026-06-17 07:37:52.621 EEST [108789:9] psql LOG:  statement: INSERT INTO t1 VALUES (1); 2026-06-17 07:37:52.621 EEST [108789:10] psql LOG:  statement: ROLLBACK TO sp1; 2026-06-17 07:37:52.621 EEST [108789:11] psql LOG:  statement: INSERT INTO t1 VALUES (2); 2026-06-17 07:37:52.621 EEST [108789:12] psql LOG:  statement: PREPARE TRANSACTION 'pt1'; 2026-06-17 07:37:52.622 EEST [108789:13] psql LOG:  statement: BEGIN; 2026-06-17 07:37:52.622 EEST [108789:14] psql LOG:  statement: CREATE TABLE t2(a int); 2026-06-17 07:37:52.777 EEST [108710:6] LOG:  client backend (PID 108789) was terminated by signal 11: Segmentation fault 2026-06-17 07:37:52.777 EEST [108710:7] DETAIL:  Failed process was running: CREATE TABLE t2(a int); Program terminated with signal SIGSEGV, Segmentation fault. #0  __memcpy_avx512_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:289 (gdb) bt #0  __memcpy_avx512_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:289 #1  0x00005e857d7dc534 in GetSnapshotData (snapshot=0x5e857df07ae0 ) at procarray.c:2297 #2  0x00005e857da9e452 in GetTransactionSnapshot () at snapmgr.c:331 #3  0x00005e857d823117 in PortalRunUtility (portal=0x5e85b337f3b0, pstmt=0x5e85b32fcd58, isTopLevel=true,     setHoldSnapshot=false, dest=0x5e85b32fd118, qc=0x7ffe081d5aa0) at pquery.c:1127 #4  0x00005e857d82343b in PortalRunMulti (portal=0x5e85b337f3b0, isTopLevel=true, setHoldSnapshot=false, dest=0x5e85b32fd118,     altdest=0x5e85b32fd118, qc=0x7ffe081d5aa0) at pquery.c:1307 #5  0x00005e857d82289a in PortalRun (portal=0x5e85b337f3b0, count=9223372036854775807, isTopLevel=true, dest=0x5e85b32fd118,     altdest=0x5e85b32fd118, qc=0x7ffe081d5aa0) at pquery.c:784 (gdb) f 1 #1  0x00005e857d7dc534 in GetSnapshotData (snapshot=0x5e857df07ae0 ) at procarray.c:2297 2297 memcpy(snapshot->subxip + subcount, (gdb) p *snapshot $1 = {snapshot_type = SNAPSHOT_MVCC, xmin = 745, xmax = 747, xip = 0x5e85b3329030, xcnt = 0, subxip = 0x0, subxcnt = 0,   suboverflowed = false, takenDuringRecovery = false, copied = false, curcid = 3, speculativeToken = 0, vistest = 0x0,   active_count = 0, regd_count = 0, ph_node = {first_child = 0x0, next_sibling = 0x0, prev_or_parent = 0x0},   snapXactCompletionCount = 55} 3) An issue in StandbyAcquireAccessExclusiveLock() No modification needed. Please try the attached TAP test on REL_17_STABLE. It fails as below: t/099_out_of_shared_memory.pl .. Bailout called.  Further testing stopped:  pg_ctl start failed 099_out_of_shared_memory_standby.log contains: 2026-06-17 07:53:03.237 EEST [167771] LOG:  database system is ready to accept read-only connections 2026-06-17 07:53:03.240 EEST [167775] LOG:  started streaming WAL from primary at 0/3000000 on timeline 1 2026-06-17 07:53:03.269 EEST [167774] FATAL:  out of shared memory 2026-06-17 07:53:03.269 EEST [167774] HINT:  You might need to increase "max_locks_per_transaction". 2026-06-17 07:53:03.269 EEST [167774] CONTEXT:  WAL redo at 0/32218D8 for Standby/LOCK: xid 738 db 5 rel 17839 2026-06-17 07:53:03.269 EEST [167774] WARNING:  you don't own a lock of type AccessExclusiveLock 2026-06-17 07:53:03.269 EEST [167774] LOG:  RecoveryLockHash contains entry for lock no longer recorded by lock manager: xid 738 database 5 relation 17839 TRAP: failed Assert("false"), File: "standby.c", Line: 1053, PID: 167774 ExceptionalCondition at assert.c:52:13 StandbyReleaseXidEntryLocks at standby.c:1056:8 StandbyReleaseAllLocks at standby.c:1116:3 ShutdownRecoveryTransactionEnvironment at standby.c:178:2 StartupProcExit at startup.c:208:1 shmem_exit at ipc.c:282:9 proc_exit_prepare at ipc.c:201:2 proc_exit at ipc.c:155:2 errfinish at elog.c:593:5 LockAcquireExtended at lock.c:1020:4 LockAcquire at lock.c:763:1 StandbyAcquireAccessExclusiveLock at standby.c:1026:10 standby_redo at standby.c:1175:35 ApplyWalRecord at xlogrecovery.c:2008:13 PerformWalRecovery at xlogrecovery.c:1835:8 StartupXLOG at xlog.c:5803:24 StartupProcessMain at startup.c:264:2 postmaster_child_launch at launch_backend.c:281:9 StartChildProcess at postmaster.c:3918:8 PostmasterMain at postmaster.c:1369:13 startup_hacks at main.c:219:1 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x8b)[0x71abb4e2a28b] postgres: standby: startup recovering 000000010000000000000003(_start+0x25)[0x64d0286de095] 2026-06-17 07:53:03.279 EEST [167771] LOG:  startup process (PID 167774) was terminated by signal 6: Aborted Best regards, Alexander --------------U6EqqLoVE430w79SH75sgZIx Content-Type: application/x-perl; name="099_out_of_shared_memory.pl" Content-Disposition: attachment; filename="099_out_of_shared_memory.pl" Content-Transfer-Encoding: 7bit use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $primary = PostgreSQL::Test::Cluster->new('primary'); $primary->init(allows_streaming => 1); $primary->append_conf( 'postgresql.conf', q{ max_worker_processes = 0 max_locks_per_transaction = 10 autovacuum_max_workers = 1000 }); $primary->start; $primary->backup('backup'); my $standby = PostgreSQL::Test::Cluster->new('standby'); $standby->init_from_backup($primary, 'backup', has_streaming => 1); $standby->append_conf( 'postgresql.conf', q{ hot_standby = on max_worker_processes = 0 max_locks_per_transaction = 10 autovacuum_max_workers = 1 }); $primary->safe_psql('postgres', q{ BEGIN; SELECT format('CREATE TABLE t_%s (x int)', g) FROM generate_series(1, 500) g \gexec COMMIT; }); $standby->start(); sleep(3); $standby->safe_psql('postgres', 'SELECT 1'); --------------U6EqqLoVE430w79SH75sgZIx--