Search⌘ K
AI Features

Views

Explore how PostgreSQL views enable dynamic server-side computations that remain transparent to clients. Understand how views simplify application code by embedding complex data calculations, ensuring consistent data retrieval and processing across multiple backends and programming languages.

We'll cover the following...

Views allow integrating server-side computations in the definition of a relation. The computing still happens dynamically at query time and is made transparent to the client. When using a view, there’s no problem with cache invalidation because nothing gets cached away.

PostgreSQL
create view tweet.message_with_counters
as
select messageid,
message.userid,
message.datetime,
message.message,
count(*) filter(where action = 'rt')
- count(*) filter(where action = 'de-rt')
as rts,
count(*) filter(where action = 'fav')
- count(*) filter(where action = 'de-fav')
as favs,
message.location,
message.lang,
message.url
from tweet.activity
join tweet.message using(messageid)
group by message.messageid, activity.messageid;

Note: This query is already executed. You can verify it by using the next query in the “ ...