Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nrwv1-0004sB-6Y for pgsql-general@arkaria.postgresql.org; Fri, 20 May 2022 07:19:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nrwuz-0004ZE-2D for pgsql-general@arkaria.postgresql.org; Fri, 20 May 2022 07:19:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nrwuy-0004Z4-NX for pgsql-general@lists.postgresql.org; Fri, 20 May 2022 07:19:24 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nrwur-0002Gm-ST for pgsql-general@postgresql.org; Fri, 20 May 2022 07:19:24 +0000 Received: by mail-ej1-x632.google.com with SMTP id gh17so922903ejc.6 for ; Fri, 20 May 2022 00:19:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=KxfWQttN47gmY09UNvZE9wcBWGKzhMLLYvITEwnQInk=; b=WeLX6sIF4dWyoUFOnTunEKeR5/vYHWLigxRvoTdWZybbRsO6fD93WjNiuMwVrdl426 eQ8SZb8EnBW/QJzRWtgiBvpM8YtQGNx4h33uBQNqcEV7pzSOHUU2C1zn+W53iNTVpwjP FZCUic9KsJj4/0A3RAvWM/IQv4A5dEW1fg+z7wR0RXbJzocDDnqp1mHhSE02N5iGV0Sa mPtJF8hTxoEKZ77FRz+k5tH6zMyG8bK3A2Pl6p8WjZUGrEiF9+sPSFavvLlafYqtP2lB VaVhZxzIH+MKfBwE8cs/VFGJucvpQXTkEg1X+7O0Eo+oTtap++bKXVHWQlRKcZdAWNoQ muog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=KxfWQttN47gmY09UNvZE9wcBWGKzhMLLYvITEwnQInk=; b=GqBME4VOv6z7dgKKGQ8J1AxWQerAaBR3s9et2M5QGC131OMpGVX9UcZXpaZZbmkimq GN/6ID9orZz6MpwsdxRWxuPIhuWHWRdc2yKRldwspHB9KNZ6HEH5L9PC41hGazaBOFyU 6N4tswNxQw2QDzPT2SCcqY9jg1PliBnn+ngPvC8MDxgZeIeJnYJwI7I+kWLUb5at164N Twdcvuw9Opu5MpdqstlqX7DJZzyOynDx0sn7YtHFfO5jg7n+HKFiGkKrc3euhvbQVbCk 0j3w5u7zJ+JkJoYLiOe7+B/WSqhdEGmGf4F5mfc06h+2oNoAJRDcq18a/NS7NfkUTuM/ b31A== X-Gm-Message-State: AOAM530CCf8ci5uBQHFAdiRu5n3lnyG+uFU0Zj0abXlIsCBr4C7uxai1 YvEVzg84+RbowzbUd1iq799tiRedKBe//A== X-Google-Smtp-Source: ABdhPJx7mglcIhYqy3qgLR9t83Hb+GybXHyFt/xYmZOjH3URQjcoGjfXBOuqQpQE8lIr7IjwMJq7Ag== X-Received: by 2002:a17:907:6d0a:b0:6f9:b861:82b8 with SMTP id sa10-20020a1709076d0a00b006f9b86182b8mr7507884ejc.478.1653031157060; Fri, 20 May 2022 00:19:17 -0700 (PDT) Received: from [10.0.0.15] (62-47-149-89.adsl.highway.telekom.at. [62.47.149.89]) by smtp.gmail.com with ESMTPSA id n17-20020aa7c791000000b0042ab02e3485sm3885969eds.44.2022.05.20.00.19.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 00:19:16 -0700 (PDT) Message-ID: <6b66751bfdf9954404348a60b93959188b620dc9.camel@cybertec.at> Subject: Re: Long living and expiring locks? From: Laurenz Albe To: Tim Uckun , pgsql-general Date: Fri, 20 May 2022 09:19:15 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2022-05-20 at 15:43 +1200, Tim Uckun wrote: > I have multiple processes connecting to the same database. In order to > prevent race conditions the I would like the process to  issue a > SELECT FOR UPDATE  to lock the records that need to be processed. > > The problem is that the locks are not inside of a single translation. > The process is multi step. Records are selected and presented to the > user, the user does some stuff and submits a form, the submitted form > updates those records and frees the lock. > > Ideally I would do a select for update when the form is shown to the > user but with an expiry time so that if the user decides to move on > and not submit any changes the locks will expire. > > I could do this with a locked_at field that I can update on select but > I would prefer to let postgres handle the locks if possible. The customary solution to this is to use optimistic locking: During the UPDATE, you check if a concurrent transaction modified the row since you read it, and if yes, you redo the whole operation. That could look like: SELECT id, col_to_change, othercol1, othercol2 FROM tab WHERE id = 42; [user interaction] UPDATE tab SET col_to_change = 'newvalue' WHERE id = 42 AND othercol1 = 'original value' AND othercol2 = 'original value'; If the UPDATE modifies no row, something must have changed, and you redo the whole operation. The advantage is that you don't have to keep a transaction open. (Regular) long transactions are a big problem in relational databases. In PostgreSQL you could use the system columns "ctid" and "xmin" to check if the row changed, so you don't have to fetch and check all columns. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com