
But TRUNCATE is as simple as updating a row in pg_class and unlinking a physical file, which is a much faster operation.
#Stimulsoft truncate rows full
The way TRUNCATE works is to assign a new file node number to the relation, and schedule the previous physical file for deletion on transaction commit!Īs you can see, using DELETE to delete all table data requires a full table scan and setting xmax of all of the rows. What happens when you TRUNCATE?Īs we saw, the physical file used for storing table data is determined by its file node number. See “ Database File Layout” for details). In addition to the Filters property the FilterOn property can also be used. Data filtering is set using the Filters property of the Data band. To select the necessary rows the data filtering is used. (There are some more details on the postgres file layout which are not very relevant to our discussion here. When rendering a report, sometimes it is necessary to print rows of the data source which correspond to the definite condition. So postgres stores the table data in $PGDATA/base/DATABASE_OID/RELFILENODE. You can also use pg_relation_filenode(relname) to get this value. pg_class has a column named relfilenode, which is the name of the physical file used to store the table data. Every postgres table has an entry in the catalog table pg_class. 16384 is the relation’s file node number. In the output above, 12368 is the database oid, which you can also get from the catalog table pg_database. Postgres =# SELECT pg_relation_filepath ( 'a' ) pg_relation_filepath - base / 12368 / 16384 ( 1 row ) You can get the location of the physical file for a relation using pg_relation_filepath(relname). To learn more about MVCC and vacuuming in postgres, checkout this blogpost.

Later, in a process called vacuuming, postgres physically deletes the rows from the disk. The row is still physically there, but it won’t be visible to future transactions. This helps postgres with concurrency control, but what is relevant to our topic is that when you do a DELETE, postgres iterates over all rows in the table and marks their xmax as the identifier of the current transaction.


Postgres for each row keeps the identifiers of earliest transaction (xmin) and the latest transaction (xmax) that can see the row. For example, using DELETE to delete all rows in a table with 1 million rows takes about 2.3 seconds, but truncating the same table would take about 10ms.īut how does postgres implement TRUNCATE that it is so fast? What happens when you DELETE?Įach transaction in postgres has a unique transaction id. The main advantage of it compared to using DELETE is performance. You can use TRUNCATE in postgres to delete all of the rows in a table.
