Posts

Showing posts with the label primary key

Data Integrity

Image
Introduction This is a term referring to the accuracy, completeness, consistency and reliability of data throughout its lifecycle, from when it is entered, stored, processed until it is deleted. Data with Integrity is data that correctly reflects objective reality and is not distorted, biased or contaminated due to system errors, human errors or hacker destruction. In database management systems like PostgreSQL, Data Integrity acts like strict rules, preventing any behavior that intentionally or unintentionally makes data absurd. To ensure data is always clean and correct, PostgreSQL provides the following core constraints: Entity Integrity When creating a table with a primary key, using a Unique Constraint or Unique Index means that the values in this column must be unique Ensures that the system can always distinguish between different entities, there is no such thing as two completely identical data rows or an "anonymous" data row existing. If you do not use the above meth...

How Index and B-Tree Index Work

Image
Introduction Index When creating an Index, PostgreSQL creates a separate physical file on the disk. Each index has a Relfilenode which is a unique identifier number. An index does not store the entire information of a row but only contains Index Entries, each entry including: Key: The value of the column you index. TID (Tuple Identifier): A physical pointer consisting of a BlockNumber and an OffsetNumber, used for reference to point to the location of that row in the main table (HEAP). When executing a query, the Query Planner Cost Model in Postgres will calculate to choose between reading data from the index or retrieving it directly from the HEAP (Sequential Scan). {Index Scan Cost} = {Index file reading cost} + {Random block reading cost in the table} {Seq Scan Cost} = {Sequential block reading cost in the table} Because the Index only contains the TID to point to the data in the main table, after loading the index content and finding the necessary values, it must perform random I...