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 1s8zM0-00GZbA-6x for pgsql-general@arkaria.postgresql.org; Mon, 20 May 2024 09:30:49 +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 1s8zLz-0003ri-VP for pgsql-general@arkaria.postgresql.org; Mon, 20 May 2024 09:30:47 +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 1s8zLz-0003rX-Hi for pgsql-general@lists.postgresql.org; Mon, 20 May 2024 09:30:47 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s8zLt-001AYx-Js for pgsql-general@lists.postgresql.org; Mon, 20 May 2024 09:30:46 +0000 Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-574d1a1c36aso6981127a12.3 for ; Mon, 20 May 2024 02:30:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716197441; x=1716802241; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=bpl5menyTcgyfDpGAG56XHe6qAEULVJEorvBFJSVH6w=; b=CH+QCbs9mL2IADrWahjFFB/SFd9b/asdDz3MDLEAIlFoDee/OrHLdKd43lKdNgJKcu HmGe2VKpuZKDFt06RAus6DhQ9VxoG7DeVc3dnNzyMRt3IyqvqBaX8JDE4FrfLTw81f4U xEwyPrXeCvWYZOfSsPaeJ03bnHgcmaE3uAEknqEAcFNKhUMnSARU3RDPkoSRjyUF6bNT ahvh1JQJnD7sYc3DxGAN0RIEIsEkPT9+bx3qcvTm4guvNTDZAX4TBN5Gux5PiXa2WJ8j efngLgc7bcjaqbhTYioX0q+xjyz1yTDeQ+7ZKiBGMzcaj+fMXpLSQ1/kpikTvX8k8ItC NRvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716197441; x=1716802241; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bpl5menyTcgyfDpGAG56XHe6qAEULVJEorvBFJSVH6w=; b=fW3D00WZhXnxqWqkfNgL3qWV8ZDgzlAUjpN65NL2QGfujTk2101lwMBoubx16j3Wud PlZ+AInPKnYsX8VgE7RaXSOS7SeQ3t1I38lgjrDQ5R0zAkgdCMzgf1y7jAFTe2nJSVu0 9aVcZxeSQrP/hPt+aoZYgyB6RIbEC0O+vCr554lXhjAZVpYCGwQdNICxTjZ6GsG4iraP SyG5D1+sdjaGdf8eps0QqWCyhrDct60qfHFBpd1nuLdvx+nx4hglWOWI++7l0l0+sPvs upKhWCP3fyGLv62RZMOaPWyBY+xaoQ/yGT+w7zyGT9zYQfqCNMhuDnFEKAkhsmcmBhKF HHDQ== X-Gm-Message-State: AOJu0Yx75mycdoJr2ZO//tp5KTcfIZ4Kx9NOiSpxcC8jSMQWfFwUUQUU OQJpKNFlGO3SCn/ghaFyWyzn5eCliTUV3wZT2TI9D3DzWqF0XL3Y X-Google-Smtp-Source: AGHT+IEaigDvWv+UjzQ7b/P+gvu+HoUiABG3xKxbOjOxpPPKS4J3+bI5xtfAZqZp9Wv4B1m8ZY8YAw== X-Received: by 2002:a50:d653:0:b0:572:7d75:a715 with SMTP id 4fb4d7f45d1cf-5734d6b4120mr18836270a12.28.1716197440567; Mon, 20 May 2024 02:30:40 -0700 (PDT) Received: from smtpclient.apple ([188.212.112.125]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5734d4cf324sm14047228a12.24.2024.05.20.02.30.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 May 2024 02:30:40 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.500.171.1.1\)) Subject: Re: Updating 457 rows in a table From: Alban Hertroys In-Reply-To: <7e82178c-1249-b592-bfea-5152563ceb4c@appl-ecosys.com> Date: Mon, 20 May 2024 11:30:29 +0200 Cc: pgsql-general Content-Transfer-Encoding: quoted-printable Message-Id: <7F273F19-168F-4C66-85C5-B4BA8395E7A6@gmail.com> References: <164b3681-7a42-1e5-6a7b-80196ed33ded@appl-ecosys.com> <1E14886A-BBCB-40E4-8231-1159D12BD261@thebuild.com> <98639d1a-dddf-38e3-49b3-63632136a0f@appl-ecosys.com> <372F0968-BD78-45EB-AE70-015DBF923EBC@thebuild.com> <7e82178c-1249-b592-bfea-5152563ceb4c@appl-ecosys.com> To: Rich Shepard X-Mailer: Apple Mail (2.3774.500.171.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 19 May 2024, at 20:37, Rich Shepard = wrote: >=20 > On Sun, 19 May 2024, Christophe Pettus wrote: >=20 >> Of course, you can probably also shorten the query to: >>=20 >> UPDATE people SET active=3Dtrue WHERE ... >>=20 >> Where ... is the predicate you would have used in the SELECT id WHERE = ... >=20 > Ah, yes. Hadn't thought of that. The statement would be > UPDATE people SET active=3Dtrue WHERE email is not null; That aside, while you=E2=80=99re not absolutely 100% definitely sure = that an UPDATE or DELETE statement is going to do exactly what you = intended, and for good measure if you are, wrapping such statements in a = transaction allows you to ROLLBACK to get back to the state that you = started from. So: =3D> BEGIN; =3D> UPDATE people SET active=3Dtrue WHERE email is not null; (497 rows affected) If that does indeed read =E2=80=9C497 rows affected=E2=80=9D: =3D> COMMIT; But if that doesn=E2=80=99t read 497, instead of COMMITting the = transaction, you now have the opportunity to investigate what other rows = changed that shouldn=E2=80=99t have and how to change your predicates - = and then simply type: =3D> ROLLBACK; Don=E2=80=99t forget to start a new transaction again for the next = attempt. In PostgreSQL this also works for almost all DDL statements (CREATE = TABLE, DROP TABLE, TRUNCATE TABLE, etc.), which is one of the features = about this database that I really appreciate - some big names don=E2=80=99= t have that. Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.