Code Room
Vibe codingHardvc-g023
Subject Ai code reviewLevel Senior–Staff~18 minCommon in Security · Databases & SQL interviewsIndustries Technology, Software development

Question

An AI agent generated this Postgres PL/pgSQL function so an analytics service can fetch a metric for a tenant, with a caller-supplied column to sort by. It runs and sorts correctly in testing. Review it for production.

sql
CREATE FUNCTION get_metrics(tenant TEXT, sort_col TEXT)RETURNS SETOF metrics AS $$BEGIN  RETURN QUERY EXECUTE    'SELECT * FROM metrics WHERE tenant_id = ''' || tenant ||    ''' ORDER BY ' || sort_col;END;$$ LANGUAGE plpgsql;
What a strong answer looks like

Treat the AI’s output as a draft to verify, not an answer to trust. Name the specific flaw and the input that triggers it, say how you’d catch it — tests, edge cases, reading critically — and how you’d re-prompt or decompose to get it right.

Describe your solution

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.