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.98.2) (envelope-from ) id 1x9NnN-00000001y0f-1m3V for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Sep 2026 14:18:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x9NnM-00000005vab-39Ae for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Sep 2026 14:18:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x9NmW-00000005s4V-0p3d for pgsql-hackers@lists.postgresql.org; Wed, 23 Sep 2026 14:17:08 +0000 Received: from mail-vs2-x2b.google.com ([2a00:1450:4864:3a::2b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x9NmT-00000000tJW-2lg3 for pgsql-hackers@lists.postgresql.org; Wed, 23 Sep 2026 14:17:07 +0000 Received: by mail-vs2-x2b.google.com with SMTP id 71dfb90a1353d-5c7d071ca02so48542e0c.1 for ; Wed, 23 Sep 2026 07:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790173023; x=1790777823; darn=lists.postgresql.org; h=content-type:mime-version:references:in-reply-to:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OCW+NBvUouXyTBgkcvAXZEdN6qRvJDiv2kQw7UfWNjE=; b=kjF0qc0lCLYZ+NFRuioVhUCQqc9T89djF/xtiUEukazLiW6vprH662pEJRSJUQJjac St52qNhQaAtHKpY8WP9qdKDtk9DerzlWeAARwQXqEFxtuj6RBj5TdnRFmP5AZhHjWk5p YAAULVxWGTGmRC6byP0RSrOUH5hncBmYFLjciCZoGNdy24po4nHn0JnrXa1FHksc7Ktx 8COHHFh7W28hXZzfYCk/e0afj2W91GOPrtyk5DBVtQUlu6y1DAjI/kC8ljFNY807EyaL qf48XvUR6eRv7+0+IGJHQz2SMk/AsGPjgZHBcBngkRH9mYbwykdcDDh7P+Eia6RMnjJf pC7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790173023; x=1790777823; h=content-type:mime-version:references:in-reply-to:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=OCW+NBvUouXyTBgkcvAXZEdN6qRvJDiv2kQw7UfWNjE=; b=TurWoJzTrmQ8Wa3y2DPPx8pxTPTVPXrPTl59dqAaa4XZN4nBH2SzJwXEtTtl7nZXL2 CE+daaAzQRUgn5XIwiiVkvSxvpI09Q5ElHT2wfz8dvSxgkVAZmR0jizqUe/v8SYK14kf c0s9cgPAW432dHAJijO4W1vSTAUn+ZcPtdmN02B1mHFDzI/IaWoJaJlpiV/j/xkeROQC 265ha2uKp5nQhzrOKezC2QJCtYXfQVzj6FxLjuhBW5kS3veWzShqxUGNoui+VWxDIpKp JoDEYtfk+dtk+JCD8e9QrlJf2jOi674hqFAZyaAV0e1DMYy/R3LrI3MM0NVmZc6+uzxy in/A== X-Gm-Message-State: AFuF++ncUcXTt0Fv+ewsrjuvASG6XuolcIwyCuPw79UafbNdrarEUklM clxOPPhX085URERCw5CRLt7ZZQfEaPyXwF0365Sg6Q30KrYzka3tt0dyTVlXi15vKpM= X-Gm-Gg: AYBFou35lhMmGSSNqQ6zJHgX8ZM/+pohEs1tohTs4tGQKS12CwxkLdzZNachZ3SrQYi ezO8MZKMpYZyEHULXMFTrv7IvhdN7ZxZX/lzB34wLMlmFxEGrFTP6iYZ1YPOZMEDSWp90uv02oO XAmbWZX/4yniLfcKyKF8QRUlIkZHS2uX+Sk8Z9MSGBxeFoN1Q9nOIE7miOg6AvgmvFDReh9EYYQ 7BfQSF7BrfYLKG2/gdjmzk00vLYAzWDCjc0duUgdxUMQZ3i0XjKPrcdWxMQ20OX4gZCx34Lm+m5 huQhkD1haN0POw/0tGPpLEJIAnFB21m9RzKZuhh7NVD9akKcm2O8NZvow513pqE+piBnVNiB/Hv omFYbSqDAzE8dCIhvoiiKTNWNo16ZS/Np8GpsMS2MNX42Da4Z18ulbMLFcnMSXFG1bI24IEq/dM SWLndBMaD/DE3MLQKnH+mw6VUDL6rkEiRr4IsLIdHFnR9Cb/mQYXPHVdMdDOAgN7UJaYcnxTvHr RE+XLPpluQLPP7Bc90WdtzTGgVeOcziBBebl9xrI/o92XUiKpqbKOpp1T0F5Xs1FpPVrw4oDL03 aWILopT80FHnoz2z5e77XG4sCg== X-Received: by 2002:ac5:c54b:0:b0:5ca:7637:ba77 with SMTP id 71dfb90a1353d-5ca7637c1ebmr550498e0c.5.1790173023495; Wed, 23 Sep 2026 07:17:03 -0700 (PDT) Received: from 192.168.1.109 ([2803:3c90:cee:ea00:5c3c:7e70:29e3:7a55]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5cac7115b29sm129901e0c.11.2026.09.23.07.17.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 07:17:02 -0700 (PDT) From: Manu To: Antonin Houska Cc: pgsql-hackers@lists.postgresql.org Subject: Re: REPACK enhancements Date: Wed, 23 Sep 2026 11:17:00 -0300 Message-ID: <179017302006.2626644.6788432141295311288@gmail.com> In-Reply-To: <28303.1790158015@localhost> References: <28303.1790158015@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4725419681133137714==" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --===============4725419681133137714== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Antonin Houska wrote: > > 3. 0008: assertion failure in compute_new_xmax_infomask() > > I don't know at the moment when the XID could get assigned. I need to > do some investigation. I measured it, so you don't have to. gdb attached to the backend running REPACK, breakpoint on AssignTransactionId(), backtrace on every hit (script attached). In a whole REPACK (CONCURRENTLY) run there is exactly one hit, and it is this one: #0 AssignTransactionId (xact.c:644) #1 GetCurrentTransactionId (xact.c:461) #2 LogAccessExclusiveLockPrepare (standby.c:1465) #3 LockAcquireExtended (lock.c:1008) lockmode=8 #4 LockRelationOid (lmgr.c:115) relid of the new heap #5 heap_create_with_catalog (heap.c:1294) "pg_temp_16384" #6 make_new_heap (repack.c:1709) #7 make_new_heap_for_repack (repack.c:1389) #8 rebuild_relation (repack.c:1230) So it is not the replay that assigns the XID, and it is not a race: it is REPACK itself, creating the transient heap, long before any change is applied. heap_create_with_catalog() locks the new relation with AccessExclusiveLock, and LockAcquireExtended() does if (lockmode >= AccessExclusiveLock && locktag->locktag_type == LOCKTAG_RELATION && !RecoveryInProgress() && XLogStandbyInfoActive()) { LogAccessExclusiveLockPrepare(); log_lock = true; } and LogAccessExclusiveLockPrepare() is a bare GetCurrentTransactionId(), so that the standby can release the lock at commit (its comment explains why). The two thresholds line up exactly: XLogStandbyInfoActive() is wal_level >= WAL_LEVEL_REPLICA, and repack.c:1012 already refuses to run with anything below WAL_LEVEL_REPLICA. So the branch is taken on every REPACK that is allowed to start at all, and the backend has a top XID from the moment it creates the transient heap. That makes the Assert unsatisfiable from that side: by the time the replay calls heap_update() for a change whose xmax belongs to another transaction, GetTopTransactionIdIfAny() is necessarily valid. It is not that the XID is assigned too early by accident - there is nowhere later to move it, short of creating the new heap in a separate transaction. Whether the right answer is to relax the Assert for this caller or to change where the XID comes from is your call; I did not want to guess at it, but the measurement seemed worth having before you spend time looking. > > 4. Progress reporting > ... > Do you mean that we should add variants of WRITE_NEW_HEAP and > REBUILD_INDEX specifically for the auxiliary table? Not necessarily - I think there are two separate things in there, and only the second one is a design question. The first is a plain reporting bug: build_new_index() sets the phase for indexes that are not the table's own - the identity index of the empty new heap and the indexes of the auxiliary table. That alone is what makes "rebuilding index" appear before "seq scanning heap", and twice with USING INDEX. Not setting the phase for those internal builds fixes the order without touching the catalog or the docs. The second is what the auxiliary table's work should be reported as, and there I would rather not add new values. Today, with v03 and USING INDEX, SORT_TUPLES and WRITE_NEW_HEAP are never reported at all, while the "REPACK Phases" table in monitoring.sgml still says "REPACK is currently sorting tuples" and "REPACK is currently writing the new heap". A user watching pg_stat_progress_repack on v19 and on v20 would see two documented phases disappear. > With the auxiliary table, sorting IMO hapens in two phases: 1) build > the clustering index and 2) scan the index and insert the output into > the new heap. As long as each phase is reported on its own, I don't > see room for SORT_TUPLES. Your two phases map onto the two existing values, I think: (1) is where the tuplesort actually runs, so that is SORT_TUPLES, and (2) is WRITE_NEW_HEAP. That is the room for SORT_TUPLES - the sort is the index build. It keeps the documented set of phases intact and needs no catversion bump. If you do prefer new values for the auxiliary table, that is fine too, but then monitoring.sgml and system_views.sql have to move with it. I have a test module for exactly that check (it cross-reads progress.h, system_views.sql and monitoring.sgml and compares the documented phases against the ones a real run reports) in [1]; I'm happy to run it against your next version either way, before it becomes someone else's bug report. One more thing to keep an eye on: Sami Imseih is changing the same phases in [2] (v2-0001 docs for v19, v2-0002 phases for v20). Worth syncing so the two don't collide. [1] https://commitfest.postgresql.org/patch/7331/ [2] https://commitfest.postgresql.org/patch/7330/ Regards, Manu --===============4725419681133137714== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="nocfbot-catch-xid-assignment.sh.txt" MIME-Version: 1.0 #!/usr/bin/env bash # #6957 v03-0008: WHO assigns the XID to the REPACK backend before the replay? # # The Assert in compute_new_xmax_infomask() (heapam.c) requires that, if the # xmax about to be written is not ours, we must not have a top XID: # # Assert(TransactionIdIsCurrentTransactionId(add_to_xmax) || # !TransactionIdIsValid(GetTopTransactionIdIfAny())) # # and it fails during the replay that runs after AccessExclusiveLock is taken. # Antonin said (2026-09-23) he does not know when that XID gets assigned. Th= is # measures it instead of reasoning about it: gdb attaches TO THE REPACK BACKE= ND # and dumps a backtrace on every AssignTransactionId(), plus the one at the # TRAP. The last backtrace before the TRAP is the one that assigned the XID # that breaks the Assert. # # catch_xid.sh [build] [repack_snapshot_after] set -u N=3D${1:-p6957a} AFTER=3D${2:-50} BIN=3D$HOME/pgprog/i-$N/bin HERE=3D$(cd "$(dirname "$0")" && pwd) PGD=3D$(mktemp -d /tmp/claude-1000/catchxid.XXXX) PORT=3D54967 OUT=3D$HERE/cazar_xid.out export PGPORT=3D$PORT PGHOST=3D/tmp PGDATABASE=3Dpostgres "$BIN/initdb" -D "$PGD" -A trust --no-sync > /dev/null cat >> "$PGD/postgresql.conf" < /dev/null P=3D"$BIN/psql -X -q" $P -c "CREATE TABLE t (id int PRIMARY KEY, v text)" \ -c "INSERT INTO t SELECT g, repeat('x', 200) FROM generate_series(1, 30000= ) g" # The session that runs REPACK: through a fifo, so we have its PID beforehand. FIFO=3D$(mktemp -u); mkfifo "$FIFO" # -At (unaligned, no headers) is required: with the table format, \g also # writes the "(1 row)" line and the PID comes out with a 1 glued to it. "$BIN/psql" -X -q -At < "$FIFO" > "$PGD/repack.out" 2>&1 & exec 3> "$FIFO" echo "SELECT pg_backend_pid() \\g $PGD/pid" >&3 for _ in $(seq 40); do [ -s "$PGD/pid" ] && break; sleep 0.2; done PID=3D$(tr -dc '0-9' < "$PGD/pid") kill -0 "$PID" 2>/dev/null || { echo "invalid PID: '$PID'"; exit 1; } echo "=3D=3D REPACK backend: $PID" cat > "$PGD/catch.gdb" <<'EOF' set pagination off set confirm off set print frame-arguments all # Postgres uses SIGUSR1 for IPC all the time: without this, gdb stops at the # first signal, the trailing `continue` is consumed there, and not a single # AssignTransactionId is captured (seen: 0 captures and one backtrace sitting # in epoll_wait). handle SIGUSR1 nostop noprint pass handle SIGUSR2 nostop noprint pass handle SIGALRM nostop noprint pass handle SIGPIPE nostop noprint pass break AssignTransactionId commands silent printf "\n=3D=3D=3D AssignTransactionId =3D=3D=3D\n" bt 22 continue end continue printf "\n=3D=3D=3D THE BACKEND STOPPED (TRAP/signal) =3D=3D=3D\n" bt 30 EOF gdb -p "$PID" -batch -x "$PGD/catch.gdb" > "$OUT" 2>&1 & GDB=3D$! sleep 3 # let gdb finish attaching before sending anything # Concurrent load: with no UPDATEs to replay there is no replay to break. ( for i in $(seq 1 300); do $P -c "UPDATE t SET v =3D 'upd' WHERE id =3D $i * 7" 2> /dev/null done ) & LOAD=3D$! [ "$AFTER" =3D 0 ] || echo "SET repack_snapshot_after =3D $AFTER;" >&3 echo "REPACK (CONCURRENTLY) t;" >&3 echo "SELECT 'done' \\g $PGD/done" >&3 for _ in $(seq 180); do [ -s "$PGD/done" ] && break; kill -0 "$PID" 2>/dev/nu= ll || break; sleep 1; done wait "$LOAD" 2>/dev/null exec 3>&- ; rm -f "$FIFO" wait "$GDB" 2>/dev/null echo "=3D=3D TRAPs in the log: $(grep -c '^TRAP' "$PGD/server.log")" grep -m1 '^TRAP' "$PGD/server.log" | cut -c1-150 echo "=3D=3D XID assignments captured: $(grep -c '=3D=3D=3D AssignTransaction= Id =3D=3D=3D' "$OUT")" cp "$PGD/server.log" "$HERE/cazar_xid.server.log" cp "$PGD/repack.out" "$HERE/cazar_xid.repack.out" 2>/dev/null "$BIN/pg_ctl" -D "$PGD" -m immediate -w stop > /dev/null 2>&1 rm -rf "$PGD" echo "=3D=3D backtraces in $OUT" --===============4725419681133137714==--