agentleFS
charanjit-singhcharanjit-singh/lovable-skills/skills/postgres-triggers-and-functions/SKILL.md

postgres-triggers-and-functions

Skill10 starsChanged 6 months agoMITView on GitHub

Use when adding Postgres triggers, database functions (RPC), generated columns, or moving invariant logic into the database. Not for edge functions (use edge-functions-and-webhooks) or client-only validation.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Keep business workflows that call external APIs in edge functions, not triggers.

    line 17

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Postgres triggers and functions

When to use DB logic

  • Invariants that must hold regardless of client (e.g. updated_at, audit fields).
  • Derived fields on insert/update.
  • Profile creation on auth.users insert.

Keep business workflows that call external APIs in edge functions, not triggers.

updated_at trigger

create or replace function public.set_updated_at()
returns trigger language plpgsql as $$
begin
  new.updated_at = now();
  return new;
end;
$$;

create trigger posts_set_updated_at
  before update on public.posts
  for each row execute function public.set_updated_at();

Auth signup hook (profile row)

Triggers on auth.users require sufficient privileges. Run via the Supabase SQL editor (which runs as postgres) or a migration applied with the management CLI — not from a user-scoped session.

create or replace function public.handle_new_user()
returns trigger language plpgsql security definer set search_path = public as $$
begin
  insert into public.profiles (id, email, display_name)
  values (new.id, new.email, coalesce(new.raw_user_meta_data->>'full_name', ''))
  on conflict (id) do nothing;
  return new;
end;
$$;

drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute function public.handle_new_user();

on conflict do nothing keeps the trigger safe if the profile is also created elsewhere (e.g. first sign-in upsert as a fallback).

RPC (supabase.rpc)

Expose safe, parameterized functions:

create or replace function public.transfer_credits(
  p_to_user uuid,
  p_amount int
) returns void language plpgsql security definer as $$
-- validate caller, deduct/add with checks
$$;
  • Mark security definer only when needed; set search_path = public.
  • Grant execute to authenticated selectively.
  • Validate auth.uid() inside function for user-scoped actions.

Avoid

  • Triggers calling HTTP (use edge + queue).
  • security definer without access checks.
  • Duplicating RLS rules in triggers instead of policies.

Checklist

  • Trigger tested on insert/update/delete paths
  • RPC documented; typed in generated types if exposed
  • Migrations reversible or follow migration-playbook

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.