Posts

Showing posts with the label types

Character Types

Image
Introduction Overhead When you store a text string on disk, PostgreSQL cannot simply write those raw letters. If it only did that, upon reading the data back, Postgres would not know: How many bytes long is this string? Where does it end so it can read the next data column? Is this a short string, a long string, or compressed? Therefore, Postgres must spend a few extra bytes right at the beginning of the string to record this management information. This additional consumed capacity is called Overhead, which is the accompanying management cost. Header Structure PostgreSQL uses a common format named varlena (Variable-length array) to store all string types ( TEXT, VARCHAR, BYTEA, JSONB... ). This Overhead portion varies depending on the length of the data string: Very short strings (Under 127 bytes) Overhead: 1 byte . Mechanism: Postgres uses this first 1 byte ( 8 bits ) to store 7 bits for recording the actual length of the string, and 1 bit as a flag signaling that this is a s...

Date and Time Types

Image
Introduction Next, we will explore Date and Time Types as follows: DATE Only stores date, month and year. Completely ignores time or time zone. Used for birth dates, founding dates, holidays, etc. The INSERT method uses the standard ISO YYYY-MM-DD string format. TIME ( TIME without time zone) Only stores hours, minutes and seconds in a day without date and time zone. Suitable for store opening/closing hours, alarm times, etc. TIMETZ ( TIME with time zone) Stores time of day with a time zone offset. This data type is rarely used in practice because it only stores time and time zone without date information, making application scenarios scarce. TIMESTAMP ( TIMESTAMP without time zone) Stores date, month, year, hours, minutes, seconds and fractional seconds (up to 6 decimal places). Note that it DOES NOT store time zone information. If you input 2026-07-28 10:00:00+07 , Postgres will ignore +07 and store only 2026-07-28 10:00:00 . When retrieving data, regardless of where the D...

Numeric Types

Image
Introduction Postgres is famous for being a feature-rich database, supporting a large list of data types that can be divided into several groups with distinct functions. First, we will explore a very commonly used group, Numeric Types, which includes the following data types: smallint : 2 bytes, (+/-) 32,767 integer or int : 4 bytes, (+/-) 2,147,483,646 (~2.1 billion) bigint : 8 bytes, (+/-) 9,223,372,036,854,775,807 (~9.22 million billion) numeric : 10 bytes, (+/-) 10^131071 Used to store fixed-precision decimal numbers, never suffering from implicit rounding errors Highly suitable for financial, monetary and accounting data Flexible size Up to 131,072 digits before the decimal point and 16,383 digits after the decimal point 100% absolute accuracy based on the numeric(Precision, Scale) configuration Precision : The total count of digits that can be stored (including digits both before and after the decimal point, excluding negative/positive signs or decimal points), ranging from ...