59 minutes ago · Tech · 0 comments

VIEWs should be the cleanest abstraction SQL, and therefore Postgres, has on offer. I love the concept. The promise of decoupling logical intent from physical storage is perfect on paper. In practice, few things in the database world trigger such a heated debate or carry as much historical baggage. VIEWs mix big promises with false hopes, and the promises rarely survive contact with production. The appeal is straightforward. Abstract "active customer" once and reuse it everywhere. Every query, report and dashboard uses the same definition. The "active customer" then becomes the foundation of a "customer orders" view, which in turn powers an operational "customer summary" view. -- layer 1: who counts as an active customer? CREATE VIEW active_customers AS SELECT c.* FROM customers c WHERE c.deleted_at IS NULL AND c.status = 'active' AND c.last_login_at > now() - interval '90 days'; -- layer 2: active customers with their recent orders CREATE VIEW customer_orders AS SELECT ac.*, o.id AS…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.