

-- PostgreSQL: This works!BEGIN;ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE;SAVEPOINT after_column_add;CREATE INDEX idx_users_email_verified ON users(email_verified);-- Oops, index creation failed, but we can keep the columnROLLBACK TO SAVEPOINT after_column_add;COMMIT;

-- PostgreSQL: Create custom typesCREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');CREATE DOMAIN email AS TEXT CHECK (VALUE ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$');CREATE TABLE users (id SERIAL PRIMARY KEY,name TEXT,email email, -- Custom domain with validationcurrent_mood mood -- Custom enum type);

-- PostgreSQL: Arrays are first-class citizensCREATE TABLE articles (id SERIAL PRIMARY KEY,title TEXT,tags TEXT[], -- Array of tagsview_counts INTEGER[] -- Array of daily view counts);-- Query with array operationsSELECT * FROM articlesWHERE 'postgresql' = ANY(tags)AND array_length(view_counts, 1) > 7;

-- PostgreSQL: Recursive CTEs for hierarchical dataWITH RECURSIVE employee_hierarchy AS (-- Base case: top-level managersSELECT id, name, manager_id, 1 as levelFROM employeesWHERE manager_id IS NULLUNION ALL-- Recursive case: subordinatesSELECT e.id, e.name, e.manager_id, eh.level + 1FROM employees eJOIN employee_hierarchy eh ON e.manager_id = eh.id)SELECT * FROM employee_hierarchy ORDER BY level, name;

-- PostgreSQL: Enable RLS and create policiesALTER TABLE documents ENABLE ROW LEVEL SECURITY;-- Policy for user-specific dataCREATE POLICY user_documents ON documentsFOR ALL TO app_userUSING (owner_id = current_setting('app.current_user_id')::INTEGER);-- Policy for multi-tenant applicationsCREATE POLICY tenant_isolation ON documentsFOR ALL TO app_userUSING (tenant_id = current_setting('app.tenant_id')::INTEGER);-- Users can only see their own documents automaticallySELECT * FROM documents; -- Only returns user's documents in their tenant

-- PostgreSQL: Index only active usersCREATE INDEX idx_active_users ON users (last_login)WHERE status = 'active';-- Index only pending ordersCREATE INDEX idx_pending_orders ON orders (created_at)WHERE status = 'pending';

-- PostgreSQL with PostGIS: Advanced spatial queriesSELECT name,ST_Distance(location, ST_MakePoint(-73.935242, 40.730610)) as distance_kmFROM restaurantsWHERE ST_DWithin(location, ST_MakePoint(-73.935242, 40.730610), 1000) -- Within 1kmORDER BY location <-> ST_MakePoint(-73.935242, 40.730610) -- KNN searchLIMIT 10;

-- PostgreSQL with pgvector: Store and search embeddingsCREATE TABLE documents (id SERIAL PRIMARY KEY,content TEXT,embedding VECTOR(1536) -- OpenAI embedding dimension);-- Find similar documents using cosine similaritySELECT content, 1 - (embedding <=> '[0.1, 0.2, ...]'::vector) AS similarityFROM documentsORDER BY embedding <=> '[0.1, 0.2, ...]'::vectorLIMIT 5;


pgsql-hackers:https://www.postgresql.org/list/pgsql-hackers
虚拟生成列:https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
NOT VALID NOT NULL:https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a379061a22a8fdf421e1a457cc6af8503def6252

「开源、开放标准,最重要的是,不被任何单一供应商所拥有。」

MySQL 阵营又一大将投奔 Postgres,加入 Supabase 打造全新分布式数据库
DBA 视角:Postgres 18 新版本,带来了哪些变化?



文章转载自Bytebase,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




