Posts

Showing posts with the label sql

PostgreSQL Practice Series

Image
Introduction PostgreSQL is a powerful open-source relational database management system, developed with many powerful features, high performance and famous for its reliability applied across various diverse projects. The outstanding advantages include: High data integrity and full ACID compliance. Powerful extensibility, allowing users to define their own data types, functions and custom indexes. Support for a wide variety of data types from structured (SQL) to unstructured (JSON/JSONB, XML). A strong development community, continuously updated and optimally secure. Detail Installing PostgreSQL with Docker Explanation of PostgreSQL Operations TOAST Storage Strategies Common PostgreSQL Statements Data Type Numeric Types Character Types Date and Time Types Boolean, Binary and UUID Types Casting Design Schema Normalization and Denormalization Data Integrity OLTP and OLAP Table relationships Aggregate Constraint Index B-Tree B-Tree Index B-Tree Index with order Using Hash Index GIN Index G...

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...

Boolean, Binary and UUID Types

Image
Introduction Next, we will explore Boolean, Binary and UUID Types as follows. Boolean Type BOOLEAN is a logical data type used to store true/false states, supporting three-valued logic such as TRUE , FALSE and NULL (unknown or missing data). Storage size is 1 byte . Commonly used to toggle features, activation status and soft delete flags ( is_deleted ). Postgres supports flexible INSERT syntax, so you do not necessarily have to write TRUE or FALSE . You can use equivalent keywords such as: TRUE state: 'true', 't', 'yes', 'y', '1', 1 FALSE state: 'false', 'f', 'no', 'n', '0', 0 Binary Data Types BYTEA (byte array) is used to store raw binary data without text processing, such as image files, PDF files, audio files, encryption certificates, RSA keys and more. Storage size is highly flexible. For short binary strings: Data size + 1 byte overhead. For long binary strings (over 2 KB): Uses compression ...

Pivot in PostgreSQL

Image
Introduction Pivot (or Pivot Table / Crosstab) is a technique used to transform data from rows to columns. This technique is very popular in data analysis and reporting, helping group scattered data rows into an easy-to-read summary table. Example of initial data (Long format): | Year | Quarter | Revenue | | : --- | : ------ | : ------ | | 2023 | Q1 | 100 | | 2023 | Q2 | 150 | | 2024 | Q1 | 120 | | 2024 | Q2 | 180 | Data after Pivot (Wide format): | Year | Q1 | Q2 | | : --- | : --- | : --- | | 2023 | 100 | 150 | | 2024 | 120 | 180 | How to Implement Unlike SQL Server or Oracle (which have native PIVOT syntax), PostgreSQL does not have a direct PIVOT keyword. You can accomplish this using the following common approaches: Using conditional aggregate functions ( SUM + FILTER or CASE ) is the simplest method, requiring no additional extensions and optimizing performance well. Using the tablefunc extension and the crosstab() fun...