postgresqlHow do I use PostgreSQL's XMIN and XMAX features?
PostgreSQL's XMIN and XMAX features are used to track and manage data changes in a transaction. XMIN and XMAX are special columns that are automatically added to every table when the transaction is started.
XMIN is used to track the transaction that inserted a row into the table, while XMAX is used to track the transaction that deleted or updated the row. When a transaction is committed, the XMIN and XMAX values are set to the transaction's ID.
Example code
BEGIN;
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
SELECT * FROM table_name;
Output example
id | xmin | xmax | column1 | column2
----+------+------+---------+---------
1 | 0 | 0 | value1 | value2
The code above starts a transaction, inserts a row into the table, and then selects all the rows from the table. The output shows that the XMIN and XMAX values are both set to 0, indicating that the row was inserted in the current transaction.
When the transaction is committed, the XMIN and XMAX values will be set to the transaction's ID:
COMMIT;
SELECT * FROM table_name;
Output example
id | xmin | xmax | column1 | column2
----+------+------+---------+---------
1 | 42 | 0 | value1 | value2
The output shows that the XMIN value is set to the transaction's ID (42), and the XMAX value is still 0, indicating that the row was inserted in the current transaction.
Code explanation
- BEGIN;: Starts a new transaction.
- INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);: Inserts a row into the table.
- SELECT * FROM table_name;: Selects all the rows from the table.
- COMMIT;: Commits the transaction.
Helpful links
More of Postgresql
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can Zalando use PostgreSQL to improve its software development?
- How do I parse XML data using PostgreSQL?
- How do I set the PostgreSQL work_mem parameter?
- How can I use PostgreSQL with YAML?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I set a PostgreSQL interval to zero?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How do I set a timestamp in PostgreSQL?
See more codes...