


How to Obtain Pre-Update Column Values Using SQL Only
Problem:
PostgreSQL does not allow retrieving pre-update column values in the RETURNING clause of an UPDATE statement. This poses a challenge when seeking to obtain the old values of updated columns without employing additional mechanisms like triggers or procedures.
Solution Using a Self-Join:
One solution is to leverage a self-join in the FROM clause. This technique effectively creates a copy of the table and allows for referencing both the updated and pre-update values:
UPDATE tbl x SET tbl_id = 23 , name = 'New Guy' FROM tbl y -- Using the FROM clause WHERE x.tbl_id = y.tbl_id -- Must be UNIQUE NOT NULL AND x.tbl_id = 3 RETURNING y.tbl_id AS old_id, y.name AS old_name , x.tbl_id , x.name;
This method requires a UNIQUE NOT NULL column for self-joining, ensuring that each row is matched to a single counterpart in the joined instance.
Solutions for Concurrent Write Load:
In scenarios involving heavy write concurrency, two options exist for preventing race conditions:
Option 1: SERIALIZABLE Isolation Level:
Utilizing the SERIALIZABLE isolation level ensures exclusive access to rows during transaction execution, thereby preventing concurrent writes:
BEGIN ISOLATION LEVEL SERIALIZABLE; UPDATE ... ; COMMIT;
Option 2: Explicit Locking:
Alternately, explicit locking can be employed to prevent concurrent updates of rows:
UPDATE tbl x SET tbl_id = 24 , name = 'New Gal' FROM (SELECT tbl_id, name FROM tbl WHERE tbl_id = 4 FOR UPDATE) y WHERE x.tbl_id = y.tbl_id RETURNING y.tbl_id AS old_id, y.name AS old_name , x.tbl_id , x.name;
The above is the detailed content of How to Retrieve Old Column Values After an UPDATE in PostgreSQL Using Only SQL?. For more information, please follow other related articles on the PHP Chinese website!

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
