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 can I use PostgreSQL and ZFS snapshots together?
- How can I set a PostgreSQL interval to zero?
- How can Zalando use PostgreSQL to improve its software development?
- How can I use PostgreSQL's "zero if null" feature?
- How can I use PostgreSQL with Zabbix?
- How do I use PostgreSQL and ZFS together?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I integrate PostgreSQL with Yii2?
- How can I use PostgreSQL with YAML?
See more codes...