- PostgreSQL 11 Server Side Programming Quick Start Guide
- Luca Ferrari
- 316字
- 2021-06-10 19:23:03
Writable CTEs and the RETURNING clause: Pipelining statements
We can combine writable CTEs and the RETURNING clause to create a quick pipeline of SQL statements. As a practical example, let's suppose we need to archive all file entries to another table with the same structure that we will use in a historic archive. We will call this table archive. Since SQL does not allow move statements, a traditional approach would be to perform a two-step transaction, as shown in the following listing, where an INSERT statement is executed and the moved entries are deleted:
testdb=> BEGIN;
INSERT INTO archive_files
SELECT * FROM files;
DELETE FROM files; -- can use also a TRUNCATE
COMMIT;
With CTEs, we can also attack the problem from another angle; we can define a writable CTE to perform the entry deletion and return all of the deleted records. These records will materialize and can then be pushed into an INSERT statement. Since the CTE will execute as a single whole statement, transaction boundaries will apply to it (in auto-commit mode). This means that the operation with either succeed or fail as a whole. In the latter case, it won't delete any entries from the source table, as shown in the following listing:
testdb=> WITH deleting_files AS ( DELETE FROM files RETURNING * )
INSERT INTO archive_files SELECT * FROM deleting_files;
CTEs are also flexible enough to allow for the opposite to happen, as shown in the following listing, where an INSERT statement performs as an auxiliary table and a DELETE statement as the top-level statement:
testdb=> WITH inserting_files AS (
INSERT INTO files_archive
SELECT * FROM files RETURNING pk )
DELETE FROM files
WHERE pk IN ( SELECT pk FROM inserting_files );
- 我的J2EE成功之路
- 高性能混合信號ARM:ADuC7xxx原理與應用開發
- 控制與決策系統仿真
- 錯覺:AI 如何通過數據挖掘誤導我們
- Python Algorithmic Trading Cookbook
- Embedded Programming with Modern C++ Cookbook
- Machine Learning with Apache Spark Quick Start Guide
- Mastering ServiceNow Scripting
- Deep Reinforcement Learning Hands-On
- AVR單片機C語言程序設計實例精粹
- 案例解說虛擬儀器典型控制應用
- SQL語言與數據庫操作技術大全
- 局域網組建與使用完全自學手冊
- TensorFlow 2.0卷積神經網絡實戰
- 工業機器人實戰應用及調試