Logging the feature of SQL-level read/write commits

From: Ronny Ko <gogo9th(at)hanmail(dot)net>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Logging the feature of SQL-level read/write commits
Date: 2019-05-04 05:32:06
Message-ID: 20190504143206.JGeiTaTTQACjYAQGbtz9nQ@gogo9th.hanmail.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

<html>
<head>
<style>
p{margin-top:0;margin-bottom:0}
</style>
</head>
<body>
<table class="txc-wrapper" border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td class="txc-wrapper-td"><div class="txc-content-wrapper" style="color:#111;font-family:Apple SD Gothic Neo,Malgun Gothic,'맑은 고딕',sans-serif;font-size:10pt;line-height:1.5;"><p>Hi,<br><br>I am trying to efficiently rollback a manually selectedd subset of committed SQL transactions by scanning an SQL transaction log. This feature is useful when a database administrator wants to rollback not the entire database system, but only particular SQL statements that affect a certain set of SQL tables. Unfortunately, this is impossible in the current PostgreSQL setup, because PostgreSQL's WAL(Write-Ahead Log) file doesn't provide any SQL statement-level redo records, but only physical block-level redo records.<br><br>To this end, my goal is to improve PostgreSQL to produce <b>augmented transaction logs</b>. In particular, the augmented transaction log's every committed transaction ID will contain an additional section called "rollback SQL statements", which is a minimal series of DELETE &amp; INSERT SQL statements that effectively rolls back one transaction to its immediately previous transaction. For example, suppose that we have the following SQL table:</p>
<p></p>
<p>=================</p>
<p><b>Table1</b></p>
<p><u>column1 | column2</u> <br></p>
<p>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 20</p>
<p>2&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 30</p>
<p>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 40</p>
<p>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 40<br></p>
<p>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 50</p>
<p>=================</p><div><br></div><p></p>
<p></p>
<p>And suppose that the following 100th transaction was committed:</p>
<p></p>
<p><span style="color:black"><span style="color:mediumblue">UPDATE</span> Table1<br><span style="color:">
</span> <span style="color:mediumblue">SET</span> <em>column1 </em>=<em> 10</em>,<em> column2 </em>=<em> 20</em><br><span style="color:">
</span> <span style="color:mediumblue">WHERE</span> <em>colum2 &gt; 20 </em>;</span></p>
<p></p>
<p></p>
<p><span style="color:black"><br></span></p>
<p><span style="color:black">Then, the <b>augmented transaction log</b> file will generate the following log entry for the above committed transaction: <br></span></p>
<p></p>
<p><span style="color:black"><br></span></p>
<p><span style="color:black"><u>Committed Transaction ID</u> = 100</span></p>
<p><span style="color:black"><u>Rollback SQL Statements</u> = <br></span></p>
<p><span style="color:black">-&nbsp;&nbsp; DELETE FROM Table1 WHERE&nbsp;column1 = 2 AND column2 = 30<br></span></p>
<p><span style="color:black"><span style="color:black">-&nbsp;&nbsp; INSERT INTO TABLE Table1 VALUES(column1, column2) (<span style="color:black">10, 20</span>)</span></span></p>
<p><span style="color:black">-&nbsp;&nbsp; DELETE FROM Table1 WHERE column1 = 3 AND column2 = 40<br></span></p>
<p><span style="color:black"><span style="color:black">-&nbsp;&nbsp; INSERT INTO TABLE Table1 VALUES(column1, column2) (<span style="color:black">10, 20</span>)</span></span></p>
<p><span style="color:black">-&nbsp;&nbsp; DELETE FROM Table1 WHERE column1 = 4&nbsp;AND column2 = 50<br></span></p>
<p><span style="color:black"><span style="color:black">-&nbsp;&nbsp; INSERT INTO TABLE Table1 VALUES(column1, column2) (<span style="color:black">10, 20</span>)</span></span></p>
<p><span style="color:black"><span style="color:black"><span style="color:black"><br></span></span></span></p>
<p><span style="color:black"><span style="color:black"><span style="color:black">Note that the above Rollback SQL statements are in the simplest forms without involving any complex SQL operations such as JOIN or sub-queries. Also note that we cannot create the above Rollback SQL statements purely based on original consecutive SQL transactions, because we don't know which rows of Table1 will need to be DELETED without actually scanning the entire Table1 and evaluating Transction #100's WHERE clause&nbsp;<span style="color:black"><span style="color:black"><span style="color:black">(i.e., <span style="color:black"><em>colum2 &gt; 20</em></span>)</span></span></span> on every single row of Table1. Therefore, to generate a list of simple Rollback SQL statements like the above, we have no choice but to embed this logging feature in the PostgreSQL's source code where the WAL(Write-Ahead Log) file is being updated. <br></span></span></span></p>
<p><span style="color:black"><span style="color:black"><span style="color:black"></span></span></span></p>
<p><span style="color:black"><span style="color:black"><span style="color:black">Since the current PostgreSQL doesn't support this feature, I plan to implement the above feature in the source code. But I have never worked on PostgreSQL source code in my&nbsp; life, and <b><u>I wonder if anybody could give me a hint on which source code files (and functions) are about recording redo records in the WAL file</u>.</b> In particular, when the SQL server records the information of updated block location &amp; values into the WAL file for each SQL statement that modifies any relations, we can additionally make the SQL server also write the list of the simplest INSERT &amp; DELETE SQL statements that effectively enforces such SQL table write operations. If such an SQL-level inforcement information is available in the WAL file, one can easily conjecture what will be the corresponding Rollback (i.e., inverse) SQL statements from there.<br></span></span></span></p>
<p><br>Thanks for anybody's comments. <br><br>Ronny</p>
<p><br></p>
<p><br></p></div></td></tr></table>
</body>
</html>
<img src="https://confirm.mail.daum.net/confirmapi/v1/users/gogo9th%40hanmail%2Enet/cmails/20190504143206%2EJGeiTaTTQACjYAQGbtz9nQ%40gogo9th%2Ehanmail%2Enet/recipients/pgsql-hackers%40lists%2Epostgresql%2Eorg">

Attachment Content-Type Size
unknown_filename text/html 6.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Noah Misch 2019-05-04 07:15:19 Re: compiler warning in pgcrypto imath.c
Previous Message Thomas Munro 2019-05-04 03:56:24 Re: First-draft release notes for back branches are up