मुख्य सामग्रीमा जानुहोस्
~/writing

यो लेख अंग्रेजीमा छ।

डाटाबेस

Triggers in Postgres

/ २ मिनेट पढाइ / Ejan Shrestha

The trigger is a function that invokes automatically when an event associated with a table occurs like an update, delete, or insert.

Triggers can be defined to execute either at the statement level or the row level.

  1. Statement-level triggers: Statement-level triggers are executed once for each triggering SQL statement, regardless of the number of rows affected by the statement. Statement-level triggers are useful when the action to be taken by the trigger is independent of the number of rows being affected. Statement-level triggers are defined using the FOR EACH STATEMENT clause.

  2. Row-level triggers: Row-level triggers are executed once for each row affected by the triggering SQL statement. Row-level triggers are useful when the action to be taken by the trigger depends on the data in the affected rows. Row-level triggers are defined using the FOR EACH ROW clause.

Types of Trigger

  1. Before Trigger Example: Suppose we have a table called "employees" with columns "id", "name", and "salary", and we want to make sure that the salary of any employee being updated is not less than 1000. We can create a before trigger to perform this validation:
CREATE OR REPLACE FUNCTION validate_salary()
RETURNS trigger AS
$BODY$
BEGIN
    IF NEW.salary < 1000 THEN
        RAISE EXCEPTION 'Salary cannot be less than 1000';
    END IF;
    RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
 
CREATE TRIGGER check_salary
BEFORE UPDATE ON employees
FOR EACH ROW
EXECUTE FUNCTION validate_salary();

This trigger function will be executed before any update on the "employees" table, and will check if the new salary value is less than 1000. If so, it will raise an exception and prevent the update from occurring.

  1. After Trigger Example: Suppose we have a table called "orders" with columns "id", "customer_id", "total", and "date", and we want to log any changes made to this table. We can create an after trigger to perform this logging:
CREATE OR REPLACE FUNCTION log_changes()
RETURNS trigger AS
$BODY$
BEGIN
    IF TG_OP = 'INSERT' THEN
        INSERT INTO order_logs (order_id, event, date)
        VALUES (NEW.id, 'inserted', now());
    ELSIF TG_OP = 'UPDATE' THEN
        INSERT INTO order_logs (order_id, event, date)
        VALUES (NEW.id, 'updated', now());
    ELSIF TG_OP = 'DELETE' THEN
        INSERT INTO order_logs (order_id, event, date)
        VALUES (OLD.id, 'deleted', now());
    END IF;
    RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
 
CREATE TRIGGER orders_log
AFTER INSERT OR UPDATE OR DELETE ON orders
FOR EACH ROW
EXECUTE FUNCTION log_changes();

This trigger function will be executed after any insert, update, or delete on the "orders" table, and will log the event (inserted, updated, or deleted) and the current date/time into an "order_logs" table.

  1. Instead of Trigger Example: Suppose we have a view called "customers_orders" that combines the "customers" and "orders" tables to show the customer name, order total, and order date. We want to make sure that any updates to this view only affect the "orders" table, not the "customers" table. We can create an instead of trigger to handle this:
CREATE OR REPLACE FUNCTION update_orders()
RETURNS trigger AS
$BODY$
BEGIN
    UPDATE orders
    SET total = NEW.total, date = NEW.date
    WHERE id = NEW.id;
    RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql;
 
CREATE TRIGGER customers_orders_update
INSTEAD OF UPDATE ON customers_orders
FOR EACH ROW
EXECUTE FUNCTION update_orders();

This trigger function will be executed instead of any update on the "customers_orders" view, and will update the "orders" table based on the new values provided. It will return NULL to indicate that the view should not be updated directly. This way, any updates to the "customers_orders" view will only affect the "orders" table, not the "customers" table.
percona

Resume.pdf

PDF डाउनलोड

Ejan Shresthaइजन श्रेष्ठ

Senior Software Engineer, backend and .NET modernisation

Senior software engineer in Kathmandu with 4+ years shipping .NET for insurance and enterprise. I own reliability and modernisation for an insurance platform of 50 .NET projects on Kubernetes: structured logging and tracing across old and new services, the plan off end-of-life .NET to .NET 10, and the fix for socket exhaustion across ~390 call sites.

Selected impact

  • ~390Ending socket exhaustion across the platform. The error class disappeared from production logs, and outbound HTTP now has one place for retries and tracing.
  • 1 IDOne trace from gateway to database. One correlation ID now follows a request end to end.
  • 61%From end-of-life .NET to .NET 10. A sequenced roadmap with known risks; the first services already run on .NET 10.
  • v16A public website on Next.js 16. Shipped to production with a tested rollback path.
  • 4A release standard people follow. Releases are predictable enough to schedule.

Experience

  1. Senior Software Engineer, Himalayan Everest Insurance

    Jan 2025 – present

    Kathmandu, hybrid

    • Own backend reliability and modernisation for an insurance platform of 50 .NET projects, deployed as 28 Kubernetes workloads, across accounting, claims, reinsurance and reporting.
    • Leading the upgrade from .NET Core 2.2, 3.1 and 5 to .NET 10, including the IdentityServer4 replacement.
    • Built structured logging and tracing across every service.
    • Upgraded the public website and CMS to Next.js 16, React 19, Strapi 5 and Node 24.
  2. .NET Developer, EKbana Solutions

    Jul 2022 – Nov 2024

    Lalitpur, on-site

    • Designed backend services for enterprise insurance workflows with ASP.NET Core, PostgreSQL, EF Core and Dapper.
    • Built REST APIs and integrations for customer portals, payments and external partners.
    • Ran background workflows on Hangfire with idempotency, retries and failure recovery.
    • Shipped on Docker, Linux, Nginx/Traefik and Kubernetes; worked on IdentityServer, JWT and RBAC.
  3. Dotnet Developer, NBIT

    Jan 2022 – Jun 2022

    Lalitpur

    • Built and maintained .NET Framework applications for client projects.
  4. Data Specialist, CloudFactory

    Nov 2017 – Jun 2022

    Kathmandu

    • Delivered high-accuracy data work for international clients while completing my degree.