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 1vVzBp-00AJa5-1K for pgsql-general@arkaria.postgresql.org; Wed, 17 Dec 2025 21:36:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vVzBo-00GlbE-16 for pgsql-general@arkaria.postgresql.org; Wed, 17 Dec 2025 21:36:09 +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.96) (envelope-from ) id 1vVzBn-00Glb5-2U for pgsql-general@lists.postgresql.org; Wed, 17 Dec 2025 21:36:08 +0000 Received: from mail-yw1-x1129.google.com ([2607:f8b0:4864:20::1129]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vVzBl-001IfZ-12 for pgsql-general@postgresql.org; Wed, 17 Dec 2025 21:36:07 +0000 Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-78e7cfd782aso37030247b3.0 for ; Wed, 17 Dec 2025 13:36:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; t=1766007363; x=1766612163; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=xtAC0UT0cBPsmqnQuQwxsDMJPb0vllQNrpgA8Oss664=; b=Kp/HK5Qg56eT74bNfhhXXK06KCdGChEwEjZB0GXU5FTBRj6q77ST2hIqqfXlPg/C75 hT3Mt+GT75BESLFFeO0X/C/M+mxdInOV/Ls7s1hRrojd8XUy5I982vFiU/IYvfgWNnbW 4IUecvDcpJOY/loG699gI0lEwfXwxglYQYKWyi/IypdI4/v5xySUWdFs2gSlofETV8cG 1UCuU5DJgUMXuYq+liQa6AEGOGLUXjG+1l66gymSe82fmT/aVCTEjzGFTICZXmFnJDXy 8IOqrevXIYQRWM6YFrNjAgL7DxjeDzhaN0P3EsJOvKbe89/byT8oxLq2S0znmhd4oWSj 9brg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766007363; x=1766612163; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xtAC0UT0cBPsmqnQuQwxsDMJPb0vllQNrpgA8Oss664=; b=JiuIp2jNconF3IaK0AX8sdI1kuXVl926BKMBVWmZK6Vfhl3FPHCKROiH+I69ih2P3s 3/mf/GwEPT2OVcFct7Prqa4s2eWONluQ+viG9f4A//UzwwE+85HD88cv9rtPD8yG6EnH yPfBqgQZWeMIr6m6yL3vwFg3XHQgkdcJyrQtwgNCNCOhBdrShY9dcOy//79Vs4Ut5Ku5 AJ5bXk7XOJZnfwzEtP5UaiIq2mgelR/CqjqiM1Wu3imml/aOg/YtUiLOSnMvCmrWLd+6 l7FfszlmotmFI3CueL1ZH80qGqP/oehHjp9EjHXpHgH7KAnyka57YW0MzVLG5WGCWHiF aGlQ== X-Forwarded-Encrypted: i=1; AJvYcCW0v1LJkd/epDaadedhr21X3QsZz4RjUn4zJsrJhWpH3kDQSG3qVFA/23w0Q9D5QVE7TzTSEODlb3FYocfB@postgresql.org X-Gm-Message-State: AOJu0YzQZzTursmfcEl1M9w34c/haVL4KlaE5TUY3SLAJZzM/S4Q1dts sAeRMBHfz/5kcv/veQk3vq9DGEDd7uvj3CtC7qHkLGt1C1rS1Oc7pd7WiGKG5rJJx+c= X-Gm-Gg: AY/fxX7Uu3WKgylQ01bmKZvji7dxY+EzrrxdZAauymUP3YvB2hsQkrj3fSFPUYUyHav CB+p7eL8VR/Nz76pIskZ8Cj6qQ7ouJBlSALWbluTW8iW3NaBrKO+CNBqp9IoJ49vUTqdqD3wfPE LALlZ2hRtRVkRFpGo202qO6XuMkRolxPZQK9Zs15ygBGg9LYdSPLCKbPIPdiQio6LS5GI4vrqyH O9qTblHsadPa75bqI8+8MF+7QWI2nOPhwJDGJ1G9687myVMWMCeEI4tUhS6t8ruzC78xQ+Pj7t+ d8UzhhSiEBoz6rmbrtmF7INgYLMM0IZEKV7COfcUNYGaQOOJZcfHEQFEtPOI+IkOTOjtwZvHbQo TllQRsjW+HTV6fL/9mqykeYlhQ3U3R9nk6j6W5XQF5V67bqQZmgZAckAPfYUkhA93cidrEZIyCW 1y1zzTZsmJ77BAWY1AxkWTTJacK3d3cptPEznAv5q6RQ9A/wg/GA14tCCqVf05Byd2ag== X-Google-Smtp-Source: AGHT+IFqN6RmNi45JGutn3XttfPEUWC2QUaGnFzpgmMtTrW3zmzNBhBSvErglc5MgwNJVD9grF0Phg== X-Received: by 2002:a05:690c:6088:b0:787:e9bc:f9e2 with SMTP id 00721157ae682-78e68341414mr143257977b3.29.1766007363346; Wed, 17 Dec 2025 13:36:03 -0800 (PST) Received: from [192.168.4.42] (162-239-31-113.lightspeed.dybhfl.sbcglobal.net. [162.239.31.113]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78fa6f52c76sm1367527b3.13.2025.12.17.13.36.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Dec 2025 13:36:02 -0800 (PST) Message-ID: <4edde38e-8d0d-4b66-993d-e38dca3bf2cb@joeconway.com> Date: Wed, 17 Dec 2025 16:36:01 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Record last SELECT on a row? To: Tom Lane Cc: Matthias Leisi , pgsql-general References: <287E4DF6-35A2-4062-AEBA-32DB1DE35C5D@leisi.net> <91687275-3826-49fc-b705-70ab2b6e0bcf@joeconway.com> <2654835.1765996654@sss.pgh.pa.us> Content-Language: en-US From: Joe Conway In-Reply-To: <2654835.1765996654@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/17/25 13:37, Tom Lane wrote: > Joe Conway writes: >> Possibly try using/abusing RLS? > > Cute idea, but I think it doesn't reliably address the problem of > wanting to identify the specific rows that were read. In your toy > example it'd work, because the generated plan is > > regression=> explain verbose select * from t1 where c1=42; > QUERY PLAN > ------------------------------------------------------------ > Seq Scan on public.t1 (cost=0.00..343.38 rows=2 width=36) > Output: c1, c2 > Filter: ((t1.c1 = 42) AND audit(t1.c1)) > (3 rows) > > so the WHERE clause is applied before the RLS filter. But in any > slightly-more-complicated situation, like a non-leakproof WHERE > condition, the order would be reversed so the log would overstate > which rows were read. Sure, of course we have had requests for a leakproofness check bypass[1] for some use cases, and this could be one more such case. > If the application's behavior is simple and well-defined, this might > be good enough, of course. FWIW when I read the original email in the thread I got the impression that the application behavior was pretty simple WRT this table. But of course I could easily be wrong... > I thought of a way that could possibly do this reliably, but it's > vastly more work than the use-case seems worth: > > 1. Convert the SELECTs into SELECT FOR UPDATE (you could do this > without changing the application, by interposing a view). SELECT > FOR SHARE might be good enough, not sure. > > 2. Write a logical replication output plugin that parses the WAL log > well enough to identify the tuple locks taken by FOR UPDATE. Yeah this seems like a pretty heavy lift. > This should work to log only the rows actually read, because FOR > UPDATE is postponed to the top of the query plan, unlike RLS. [1] https://www.postgresql.org/message-id/flat/CAMxA3rsGQh9waorObOZyqrFqZ5uQ0b5D7SL6X6nh2kLhX%3D90vg%40mail.gmail.com#4a03eafc8c9660177874e11811c8f410 -- Joe Conway PostgreSQL Contributors Team Amazon Web Services: https://aws.amazon.com