官术网_书友最值得收藏!

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;
Listing 14:  Moving entries with a transaction

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;
Listing 15:  Moving entries with a writable CTE

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 );
Listing 16:  Moving entries with a writable CTE
主站蜘蛛池模板: 红原县| 玉田县| 鹤山市| 喀喇沁旗| 普兰店市| 西城区| 南溪县| 阜城县| 白山市| 上饶市| 阿鲁科尔沁旗| 西畴县| 蓬莱市| 东乌珠穆沁旗| 乐山市| 绍兴县| 阿合奇县| 惠州市| 扶绥县| 乌兰浩特市| 县级市| 河间市| 汝阳县| 洱源县| 金秀| 龙南县| 眉山市| 梓潼县| 邹城市| 清徐县| 洮南市| 故城县| 宣恩县| 惠州市| 陵水| 泽库县| 美姑县| 峨眉山市| 德格县| 衢州市| 耿马|