首页 > 其他分享 >why do we need 'select…for share' instead of a simple 'select'

why do we need 'select…for share' instead of a simple 'select'

时间:2024-10-09 13:49:30浏览次数:1  
标签:do Transaction simple SHARE read transaction select data SELECT

(From chatgpt)
A simple SELECT query in PostgreSQL operates under the MVCC (Multi-Version Concurrency Control) model, which allows it to read data without locking the rows. This means it can see a snapshot of the data at the start of the transaction, regardless of whether other transactions are modifying that data at the same time. However, this also means that the data can change after you read it, and you wouldn't necessarily know about those changes unless you explicitly check again or use locking.

SELECT ... FOR SHARE introduces a row-level lock to prevent other transactions from making changes to the rows you are reading while you're still working with them. This is important in scenarios where you want consistency during a multi-step process that involves multiple queries. Let's dive deeper into why you'd want to use FOR SHARE over a simple SELECT.

Differences between a simple SELECT and SELECT ... FOR SHARE:

  1. Simple SELECT:

    • Reads data without acquiring any locks that would block other transactions.
    • Uses MVCC to give you a consistent snapshot of the data as it was when the transaction started.
    • Other transactions can modify the rows you're reading (update, delete, etc.), and you won't be notified of those changes.
    • It works fine for purely read-only operations where you don't care if the data changes after your read.
  2. SELECT ... FOR SHARE:

    • Acquires a shared lock on the rows you read.
    • Other transactions can still read these rows (with their own FOR SHARE), but they cannot modify or delete them until your transaction completes.
    • Prevents situations where another transaction modifies or deletes the rows you are reading before you've finished your own operation.
    • Useful when you need consistency across multiple steps in your transaction and want to ensure the rows you read remain unchanged while your transaction is ongoing.

Why not just use a simple SELECT?

A simple SELECT may not be sufficient in scenarios where the data you're reading must remain unchanged for the duration of your transaction or process. Without FOR SHARE, there’s no guarantee that another transaction won’t modify the data immediately after you read it, which could lead to:

  1. Inconsistent Data: The data you just read might be modified by another transaction while you're still working, causing issues if your next operations depend on it being the same.

  2. Lost Updates or Deletes: If you're planning to perform an action based on the data you just read, such as an update, without FOR SHARE, another transaction could modify or delete those rows before you execute your update, resulting in an error or unintended behavior.

Use Case Example:

Let’s say you have an e-commerce system where two transactions are happening:

  • Transaction A wants to check the availability of a product and, if available, place an order.
  • Transaction B wants to update the product's stock (e.g., after a restock or sale).

Without FOR SHARE, these transactions could interfere with each other. Here’s how things could go wrong with a simple SELECT:

Scenario without FOR SHARE:

  1. Transaction A:

    BEGIN;
    SELECT stock FROM products WHERE product_id = 1;
    -- Stock is 5 units.
    -- Planning to place an order.
    
  2. Transaction B (in parallel):

    BEGIN;
    UPDATE products SET stock = stock - 5 WHERE product_id = 1;
    -- Transaction B finishes and commits.
    COMMIT;
    
  3. Transaction A continues:

    -- Transaction A continues based on the earlier read (which was 5 units), not knowing stock has changed.
    INSERT INTO orders (product_id, quantity) VALUES (1, 2);
    COMMIT;
    

    In this scenario, Transaction A was working with stale data. The stock was modified by Transaction B in the middle, but Transaction A wasn’t aware, resulting in potentially incorrect behavior.

Scenario with FOR SHARE:

If Transaction A used SELECT ... FOR SHARE:

  1. Transaction A:

    BEGIN;
    SELECT stock FROM products WHERE product_id = 1 FOR SHARE;
    -- Stock is 5 units. Row is locked for modifications by other transactions.
    
  2. Transaction B (in parallel) tries to modify the stock but is blocked:

    UPDATE products SET stock = stock - 5 WHERE product_id = 1;
    -- Blocked until Transaction A finishes.
    
  3. Transaction A:

    -- Transaction A proceeds safely, knowing the data is still consistent.
    INSERT INTO orders (product_id, quantity) VALUES (1, 2);
    COMMIT;
    

In this case, Transaction B is blocked from modifying the stock until Transaction A completes, ensuring Transaction A is working with up-to-date and consistent data.

Conclusion:

  • Use a simple SELECT when you just need to read data without caring if it changes afterward.
  • Use SELECT ... FOR SHARE when you need to ensure that no other transaction modifies the data while your transaction is ongoing, but you're not making updates yourself. This is particularly important when your subsequent operations depend on the data staying consistent during a multi-step process.

标签:do,Transaction,simple,SHARE,read,transaction,select,data,SELECT
From: https://www.cnblogs.com/lddcool/p/18454066

相关文章

  • TFS命令 TF:undo(强制撤销签出文件) 用法
    1.在命令行中输入"CD C:\ProgramFiles(x86)\MicrosoftVisualStudio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\TeamExplorer\"(TF.EXE程序在文件夹),进入TF程序所在目录。或者配置环境变量PATH,如下图:2.查找指定用户用到的工作区,操作如下:TF......
  • Docker 环境下 GPU 监控实战:使用 Prometheus 实现 DCGM Exporter 部署与 GPU 性能监控
    Docker环境下GPU监控实战:使用Prometheus实现DCGMExporter部署与GPU性能监控文章目录Docker环境下GPU监控实战:使用Prometheus实现DCGMExporter部署与GPU性能监控一查看当前GPU信息二dcgm-exporter部署1)Dockerrun运行2)Dockercompose运行三......
  • 数据恢复篇:适用于 Windows 操作系统的 5 大数据恢复软件
    如今,数字空间正在快速发展。每个人都使用某种数字设备,如计算机、笔记本电脑、移动设备等来存储重要文档、照片、视频和其他重要文件。但事情并不总是一帆风顺。在很多情况下,技术会失败,您可能会遇到一些严重的问题,例如数据丢失。但是,有许多技术可以帮助您解决技术问题,例如数据恢......
  • 选择 PDF 编辑器时要考虑什么?如何选择适用于 Windows 10 的 PDF 编辑器
    选择PDF编辑器时要考虑什么?随着技术的出现,您在网上浏览时肯定会遇到一些PDF软件。但是,选择PDF编辑器时需要考虑什么?如果您是重度用户并将在您的工作场所使用它,建议您找到专业、使用方便且能够帮助您完成任务的PDF软件。以下是您在寻找优秀编辑器时可能考虑的几件事:适......
  • Docker 部署 Kafka 集群详解教程
    Kafka是一个分布式流处理平台,广泛用于构建实时数据管道和流应用。它能够处理高吞吐量的数据,并支持实时数据的发布和订阅。在本文中,我们将详细介绍如何使用Docker来部署Kafka集群,包括Kafka的选举原理。前提条件安装Docker和DockerCompose。理解Kafka和Zookee......
  • 在 X86_64(amd64) 平台上的docker支持打包跨平台的镜像(如arm64)
    在信创,ARM开始崛起的现在,Docker也从一开始的只支持x86_64架构变为支持各种架构了,虽然Docker的目的是保证只要Docker安装好,在任意机器上运行都能达到一样的效果,但是这个的前提是Docker镜像的架构和当前服务器的架构一致,以前都是x84_64架构自然可以,但现在也有别的架构,因此......
  • 界面控件Kendo UI for jQuery 2024 Q3亮点 - 支持切换编辑模式
    随着最新的2024Q3版本,Progress使用户能够使用现成的页面模板和构建块更快地构建令人惊叹的应用程序,使您的Telerik和KendoUI开发体验更好。Telerik和KendoUI 2024Q3版本将焦点放在新推出的页面模板和构建块上,每个页面模板和构建块都预先配置了TelerikUIforBlazor、KendoU......
  • javascript学习——DOM 概述
    DOM概述DOMDOM是JavaScript操作网页的接口,全称为“文档对象模型”(DocumentObjectModel)。它的作用是将网页转为一个JavaScript对象,从而可以用脚本进行各种操作(比如增删内容)。浏览器会根据DOM模型,将结构化文档(比如HTML和XML)解析成一系列的节点,再由这些节点组......
  • CentOS 8 停止维护后通过 rpm 包手动安装 docker
    根据Docker官方文档的指引,进入Dockerrpm包下载的地址,根据自己系统的架构和具体版本选择对应的路径这里我使用https://download.docker.com/linux/centos/7/x86_64/stable版本,根据docker官方的给出的安装命令选择性的下载对应的rpm包最终使用yum命令安装下载好的......
  • 窗体MainWindow的属性设置2
    文章目录1.控件名称2.菜单栏A.一级菜单B.子菜单①子菜单添加快捷键②子菜单添加图标3.工具栏A.将子菜单移除工具栏B.移除工具栏4.状态栏A.移除状态栏B.状态栏显示信息状态栏一直显示信息  本章将主要介绍窗体MainWindow控件的属性设置,例如控件名称、菜单栏、......