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.
In this repository56 other files it gives its agents.
- accessibility-passSkill
- analytics-eventsSkill
- api-error-handlingSkill
- audit-logging-backendSkill
- background-jobs-and-cronSkill
- dark-mode-and-themingSkill
- dashboard-charts-rechartsSkill
- edge-functions-and-webhooksSkill
- email-and-notificationsSkill
- error-states-and-empty-uiSkill
- feature-flags-and-togglesSkill
- file-upload-storageSkill
- i18n-multi-languageSkill
- lovable-prompting-playbookSkill
- lovable-ship-checklistSkill
- migration-playbookSkill
- multi-tenant-backendSkill
- onboarding-copySkill
- optimistic-updatesSkill
- payment-failed-and-recoverySkill
- payment-webhook-idempotencySkill
- payment-webhook-testingSkill
- performance-budgetSkill
- pg-cron-recipesSkill
- pg-cron-scheduled-jobsSkill
- pg-cron-with-pg-net-edgeSkill
- postgres-full-text-searchSkill
- pricing-and-billingSkill
- programmatic-seo-content-qualitySkill
- programmatic-seo-pagesSkill
- rate-limiting-edgeSkill
- realtime-and-subscriptionsSkill
- refactor-safe-diffSkill
- release-notesSkill
- seo-core-web-vitalsSkill
- seo-landing-pageSkill
- seo-meta-tags-spaSkill
- seo-sitemap-generationSkill
- seo-ssr-and-prerenderingSkill
- seo-structured-dataSkill
- server-input-validationSkill
- shadcn-patternsSkill
- soft-delete-and-archivingSkill
- stripe-one-time-payment-webhooksSkill
- stripe-payment-webhooksSkill
- supabase-auth-flowsSkill
- supabase-database-designSkill
- supabase-rls-and-authSkill
- support-replySkill
- tanstack-infinite-queriesSkill
- tanstack-mutations-and-invalidationSkill
- tanstack-prefetch-and-hydrationSkill
- tanstack-query-alternativeSkill
- tanstack-router-data-loadersSkill
- tanstack-start-server-routesSkill
- typed-api-hooks-formsSkill
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
- Distinctive: line 17
Keep business workflows that call external APIs in edge functions, not triggers.
≈ “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.
Files
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.usersinsert.
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 defineronly when needed; setsearch_path = public. - Grant
executetoauthenticatedselectively. - Validate
auth.uid()inside function for user-scoped actions.
Avoid
- Triggers calling HTTP (use edge + queue).
security definerwithout 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
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
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.

