PostgreSQL – How to Limit Results for a User

postgresql

I would like to grant a Postgres user access to a public_data view and allow them to execute user defined SQL queries against it.

However, the view is quite large (5M rows), so I'd like to prevent users from executing queries that could accidentally return huge results sets.

CREATE VIEW public_data AS SELECT * FROM data limit 10; only seems to give me the first 10 results no matter what query I run against public_data.

Ideally, I'd like users to be able to run SELECT * FROM public_data WHERE ...; but somehow have LIMIT 10 automatically included prior to executing the statement.

Is there any way to do this without creating a custom function?

Best Answer

Alas, no. You cannot parameterize views like that, you need a function to do it.

I rather hope someone proves me wrong, as I much prefer views to set-returning functions. But I doubt they will.