Posts

Showing posts with the label postgres

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

WRITE-BEHIND

Image
Introduction This article will guide you through implementing WRITE-BEHIND (WRITE-BACK) in NestJS with Redis and Postgres . Applying this mechanism will perform asynchronous writes to the Database via a Queue, so I will also use @nestjs/bullmq (BullMQ + Redis) , which is a standardized, powerful library for managing background jobs. As for theoretical content, you can review the previous article I mentioned. Detail Please install the following packages: bun add bullmq @nestjs/bullmq Create Drizzle Schema in drizzle-orm/schema/schema.ts as follows: import { bigserial, decimal, integer, pgTable, serial, text, timestamp, varchar, } from 'drizzle-orm/pg-core' export const products = pgTable ( 'products' , { id : bigserial ( 'id' , { mode : 'number' }). primaryKey (), name : varchar ( 'name' , { length : 255 }). notNull (), description : text ( 'description' ), price : decimal ( 'price' , { precision :...