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.94.2) (envelope-from ) id 1vGLXu-00D0Ny-8T for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Nov 2025 18:14:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vGLXs-00FpP8-BL for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Nov 2025 18:14:15 +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.94.2) (envelope-from ) id 1vGLXs-00FpP0-2W for pgsql-bugs@lists.postgresql.org; Tue, 04 Nov 2025 18:14:15 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vGLXo-005yWu-2e for pgsql-bugs@postgresql.org; Tue, 04 Nov 2025 18:14:14 +0000 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-7aae5f2633dso2545316b3a.3 for ; Tue, 04 Nov 2025 10:14:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1762280050; x=1762884850; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=AoWys6DQGZ3nk9/I3f3WDaVN5gYNhiiBAvfIrHbVTfM=; b=apHT4Eg5dMYGmi05Mx77nsQqPfv2FhQ6StY438v3jfoLWekFXQsZj3JX9sTVUwsUSF f10UOD3t+5ZnlAZCAK6rPknEdSRPtPO/kkEFKvv7MNb0Weu7ABhJm1g7sWqRFgcRHI+r KS/+AcPlfTlvWi87PhTLB/wAdKcc8aGl7KQ9QTXdvzFA/95Xnh5N46TT8djCDQW3KOhg /8zYo06mKWkjzXtOXiZDwn8ay/JEz16Cc0JkzdrIoVIia/RIfKgJmnZPH6K1thbe8+Jp qEC9X/M/80X8oZiDzIYBUpdT8cp/OXEH2wxCChQqQ8NEOHQk4BUiMyuAQx13+rHX9/kq tbYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762280050; x=1762884850; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AoWys6DQGZ3nk9/I3f3WDaVN5gYNhiiBAvfIrHbVTfM=; b=MzYTN4OLqUQQpu2H2Xhwq7Hp+kpXSDaPY3Bynz1rc82ZvjG4MfEeUEXbQyGIi8fH5w 7O09rBOHbv53+2iL7aSJR4QDlpCoHfQeIpHDZFXvLFTf9EmAszhJlk6i0Re0bdQ9nJv/ y1oH2siobWMetkY70S2JXFQSqs7Ziw07nL0M4u5M7mBmuhsFgUv5jgcDbxXk3lNCR/Yy d2S7wpGlh4HJgz74xFbur0MzlVTssCjegrClwFY8Hg4ZEg5poz6FFtpqrOLZgQZETBEJ /D9rS8ESWtOcP3+92bEeWFKPJQNAGCP5YXqtnSqmbSEH+36fVc1YAg+pNL2lo9I7lWb6 CXYw== X-Gm-Message-State: AOJu0Yzi5OMvIlSgK92SzBerXlrLz+clm3ywzV7vMdmFnnp2aLArekJ1 lIWDlOi/+yS7JMuBtsRxFQhN7W9qi+1EzctM8gKGitV59BpA45SAR2WauSoJoLYjjGrU3IhTdtx 7BcM= X-Gm-Gg: ASbGncubFHvHjrVPOLtAESNy881T+CSK3x8peo5CChBJlSuu6zXh81L9LxN3yMA0ly/ To7xR6jFWvYWxld7vJvy8BkhUmsQYZbPIPpvF4eUZpGc+c/Sh5oAIK2znA69nWQGU05RsPO0UbK OWwB4AmV2f89l7oL0QgRfsZY09RrbAHdQElFyvbClksCA1cwZgkiKG4/fnUSFLoNwKygq0rFbbj AV7Jk2OZIjPgnBEjXeI564z2SiIGvQ6PTQC5XrtfaRKYZuyZ2cF94OZJno43u5yJva873V1+zun 6MB7L5q0LTt6MrEN6nJjWBTi8T0z+JJr2WAARe8X/EbRImFEy0GxIe+2Q4CYMpfkJpuXM8XzINW OflXUyqih67qRKsd22hkW2xfr90t+6jcoiNXyJbKND12nr13p+8X9F08OgPIwne69PXv5897Qjw IdR+W595ji2FHZ+B1w1zmSZYn9VCbW4dY9FtD1XbPXtdg= X-Google-Smtp-Source: AGHT+IHeT5YzNQoiY0XPLMxgIF/+IAh1KUnNZoo0+bb/vKpcDA9qAEtNhJbpkuSzbwDkswZ/+SCjug== X-Received: by 2002:a05:6a00:124c:b0:7ab:63fe:d7d5 with SMTP id d2e1a72fcca58-7ae1f09a060mr185693b3a.20.1762280049565; Tue, 04 Nov 2025 10:14:09 -0800 (PST) Received: from jeff-ws-bridge.lan (c-24-7-19-3.hsd1.ca.comcast.net. [24.7.19.3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7acd623f0aesm3588170b3a.55.2025.11.04.10.14.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Nov 2025 10:14:09 -0800 (PST) Message-ID: Subject: ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML From: Jeff Davis To: pgsql-bugs@postgresql.org Date: Tue, 04 Nov 2025 10:14:08 -0800 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This doesn't represent an actual use case, it's just a contrived test. The docs say: "Adding a column with a volatile DEFAULT (e.g., clock_timestamp()), a stored generated column, an identity column, or a column with a domain data type that has constraints will cause the entire table and its indexes to be rewritten. Adding a virtual generated column never requires a rewrite." https://www.postgresql.org/docs/current/sql-altertable.html (in Notes section). The following SQL seems to lose the updates during the ALTER: CREATE TABLE t(id INT); INSERT INTO t VALUES (1), (2); CREATE FUNCTION f() RETURNS INT VOLATILE AS $$ BEGIN UPDATE t SET id =3D id + 10; RETURN (SELECT MAX(id) FROM t); END $$ LANGUAGE plpgsql; ALTER TABLE t ADD COLUMN c INT DEFAULT f(); SELECT * FROM t; id | c =20 ----+---- 1 | 12 2 | 22 (2 rows) It happens because the updates happen on the old heap while the function is being evaluated, and the old heap is thrown away. But uncontrolled DML happening during an ALTER seems hard to even define, so I'm not sure how to fix it, or if we even need to fix it. Thoughts? Regards, Jeff Davis