Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Several applications and reporting tools read directly from your service's tables, which makes schema changes hard. Would you use database views as the contract for these consumers? Describe the design and its limits.

Yes, as a step forward: versioned views in a dedicated schema, such as reporting.orders_v1, give consumers a fixed read contract while we change the tables behind them, and granting access only to that schema makes the contract enforceable. Views do not remove the deeper coupling, because consumers still share our database's performance and can run expensive queries, and many versions cost effort to maintain. So we document the views as an API with owners, monitor their use, set end dates for old versions, and plan to move consumers to events or CDC-fed data products in the longer term.

Why this question is asked

Shared database access is one of the main reasons teams cannot change their own schema. A column rename that takes ten minutes in code takes three months of emails when a finance report, a partner extract and two other services read the table directly. Interviewers ask this design question to see whether we can find a practical middle step between the current mess and the ideal architecture, and whether we are honest about its limits. In this answer, we will see why direct table access blocks change, design a versioned view contract with access control, look at its limits, and plan the deprecation and the longer-term path.

1Why Direct Table Access Blocks Change

When consumers query our tables directly, every column name, type and table shape becomes part of an interface we never designed or documented. We cannot know who depends on what, so every change is risky. Renaming status to order_status, splitting orders into orders and order_lines, or changing an amount from integer cents to numeric might break a report nobody told us about.

The goal is to separate our internal model, which we want to change freely, from the shape we promise to others. In application code, an API does that. A view can do the same thing inside the database, which matters because these consumers speak SQL and are not going to switch to HTTP calls next week.

2Designing the View Contract

We create a separate schema for the contract, for example reporting, and define views in it that present the data in the shape consumers need. Each view has a version in its name. The views are the only objects consumers can see: their roles get USAGE on the reporting schema and SELECT on its views, and no privileges on the base tables. That makes the contract real rather than a polite request.

When the tables change, we update the view definitions so their output stays the same. When consumers need a different shape, we add orders_v2 next to orders_v1, rather than changing v1 under them.

Java
CREATE SCHEMA reporting;
CREATE VIEW reporting.orders_v1 AS
SELECT o.id AS order_id, o.customer_id, o.created_at,
o.total_cents / 100.0 AS total, -- the table changed, the contract did not
s.code AS status
FROM app.orders o
JOIN app.order_status s ON s.id = o.status_id;
COMMENT ON VIEW reporting.orders_v1 IS
'Owner: orders team. One row per order. Stable until 2027-06-30.';
CREATE ROLE finance_reports LOGIN;
GRANT USAGE ON SCHEMA reporting TO finance_reports;
GRANT SELECT ON reporting.orders_v1 TO finance_reports;
-- no grant on schema app: SELECT * FROM app.orders -> ERROR: permission denied for schema app
Step throughConsumers see only the contract
  1. Consumersfinance reports, partner extract, other services
  2. reporting schemaorders_v1, orders_v2: versioned, documented, granted
  3. app schemaorders, order_status: free to change

3 layers, from the top down. Step through them one at a time.

The view definitions are the only place that knows both shapes.

Notice that the view reads from the app schema with the privileges of its owner, which is the default for PostgreSQL views, so the consumer needs no access to app at all. We treat the views like a public API: they live in version control and go through review, each has a documented owner, meaning per column and stability promise, and breaking changes require a new version.

3The Limits of Views as a Contract

Views solve the shape problem, not the load problem. Consumers still run queries on our database, so a badly written report that scans five years of orders competes with our production traffic for CPU, memory and disk. Pointing consumers at a read replica helps, though they still depend on our replica's capacity and on the primary's schema.

Views also cannot stop consumers from writing expensive queries against them: a view hides a join but cannot force the consumer to filter. We can limit the damage with statement_timeout on consumer roles and per-role connection limits, but the coupling is still there. And versioning has a cost. Each version must be kept correct as the tables change, tested, and documented. Five consumers with three versions each is fifteen definitions to keep working, and complex view logic can block query optimisations, as when a window function prevents filter pushdown. So we keep the number of live versions small.

SQL
ALTER ROLE finance_reports SET statement_timeout = '60s';
ALTER ROLE finance_reports CONNECTION LIMIT 5;

4Deprecation: Knowing Who Uses What

A versioned contract is only manageable if old versions actually go away. We monitor usage with pg_stat_statements, the PostgreSQL extension that records statistics for every distinct query, grouped by the role that ran it. That tells us which consumers still query orders_v1 and how often. Each version gets an end date in its comment and documentation, consumers are told when a successor is published, and after the end date we revoke access on the old view before dropping it, so anyone we missed gets a clear permission error rather than silent wrong data.

SQL
SELECT r.rolname, s.calls, left(s.query, 50) AS query
FROM pg_stat_statements s JOIN pg_roles r ON r.oid = s.userid
WHERE s.query ILIKE '%reporting.orders_v1%'
ORDER BY s.calls DESC;
-- rolname | calls | query
-- finance_reports | 4210 | SELECT order_id, total FROM reporting.orders_v1 WHERE ...
-- partner_extract | 31 | SELECT * FROM reporting.orders_v1

5The Longer-Term Path: Events and Data Products

For the long term, the better decoupling is to stop sharing the database. We publish changes as events, or capture them with change data capture (CDC), a tool such as Debezium that reads the database's change log and streams every committed change, into a warehouse or a dedicated read store. Reporting consumers then query their own copy, with history and analytics tooling, and our database carries only our traffic. Operational consumers, other services, use our API.

The view contract is a good bridge to that state. Its documented shape becomes the schema of the published data product, so consumers who move to the warehouse find the same columns they used in orders_v1.

6Follow-up questions

Would you allow consumers to read from a replica instead of the primary?

Yes, it is a sensible first step, because it removes their load from the primary and limits the blast radius of a bad query. It does not remove the schema coupling, so the view contract still applies on the replica, and replica lag and long queries that conflict with replication need settings and monitoring.

Why not just give consumers an API?

For other services, we should. Reporting tools and analysts work in SQL and need joins and aggregations across large ranges, which an API serves poorly. Views keep their workflow while giving us a contract; the warehouse is the long-term answer for that kind of access.

Who owns a view in the reporting schema?

The team that owns the base tables, because only they can keep the view correct when the tables change. Consumers own their queries and must move off deprecated versions by the end date.

7Conclusion

In this answer, we used database views as a read contract for consumers that query our tables. Versioned views in a reporting schema, granted to consumer roles while the base tables are not, let us change tables behind a stable shape, and we document and own them like an API. Views do not remove load coupling or stop expensive queries, and every version costs maintenance, so we add timeouts, monitor usage with pg_stat_statements and set end dates. In the long run, publishing events or CDC-fed data products decouples consumers more completely.

What to say in the interviewAbout 90 seconds aloud

Open with

I would use views, but as a contract, not just a convenience. I would create a reporting schema with versioned views such as orders_v1 that present the shape consumers need, and grant consumer roles access only to that schema, with no privileges on the base tables.

Then, if they want more

Then I can rename, split or retype columns in the real tables and adjust the view definition so its output stays the same. If a consumer needs a different shape, I add orders_v2 rather than changing v1. The views live in version control, each has an owner, documented columns and an end date, so they are treated like any API.

I would be clear about the limits. Consumers still run queries on our database, so load and performance stay coupled; I would point them at a replica and set statement timeouts and connection limits on their roles. Views cannot stop someone from writing an expensive query, and each version costs maintenance, so I keep few versions alive.

For deprecation, I watch pg_stat_statements to see which roles still query old versions, announce end dates, revoke access first and then drop. Long term, I would move reporting consumers to a warehouse fed by CDC or events and other services to our API, and the view contract becomes the schema of that data product.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Several applications and reporting tools read directly from your service's tables, which...": https://scrivio-interview-library.netlify.app/databases/stored-code/0f0ffa1bc7/