1

[Disclaimer: This question was posed by one of our YugabyteDB users on our yugabyte.com/slack channel]

The documentation for triggers below gives an example of attaching a trigger to the update of an employee table.

https://docs.yugabyte.com/latest/explore/ysql-language-features/triggers/

Suppose you have a procedure that only allows a manager to transfer an employee if the employee is a direct report to the manager, something like the following:

#transfer_employee(manager_no, employee_no, department)
CREATE OR REPLACE PROCEDURE transfer_employee(integer, integer, text)
LANGUAGE plpgsql
AS $$
BEGIN
  -- IF employee reports to mgr, allow mgr to transfer person
  IF EXISTS (SELECT employee_no FROM mgr_table where mgr_id = $1 and employee_no = $2)
  UPDATE employees
  SET department = $3
  WHERE employee_no = $2;
  COMMIT;
END;
$$;

Is there a way in YugabyteDB to have the trigger gain access, or pass the state variables of the stored procedure, to the trigger so that you could log in a table the variables such as manager_id who made the change, and not just the new or old department (e.g. this is a variable that only exists in the context of the stored procedure)?

If it is possible, the syntax to do this is unclear to me from this example.

2 Answers 2

3

There is nothing here that I can see that is unique to yugabyte, so this is all postgres. This also means you can use the postgres documentation.

The postgres documentation states that the trigger function must be declared with no arguments. (https://www.postgresql.org/docs/11/plpgsql-trigger.html) In other words: a variable cannot be passed as an argument to the trigger function.

Since the body of the trigger is a function, the possibilities are reasonably unlimited. But I would strongly advise to think about making sure data and logic are not configured in such a way that make it non-obvious.

So to provide an answer to your question: yes, that is possible, here is a stackoverflow answer that provides the means to arrange the requested functionality: How to use variable settings in trigger functions? (using custom variables set in the sessions setting table)

Please do not stop reading here. By doing this, the only way to make the triggered functionality working is by manually crafting the session state as the trigger desires. That makes it really hard (if not: impossible) to work with the data in general. Especially if this type of trigger is added in more places.

In general the way to do this is by creating procedures that an application must use (an API) in order to manipulate the data, so that any rules that require more information than a table level function like a trigger can see, can be handled there.

That way the database objects can have all their data scope rules (primary/foreign keys, check constraints, not null), but do not require anything beyond the tables theirselves to do data and database administration.

2

Adding to Frits's answer:

In general the way to do this is by creating procedures that an application must use (an API) in order to manipulate the data… 

Yes—100% agree. Stepping up a level, the question becomes “How to model session state in PostgreSQL”. In Oracle Database, the usual paradigm is to use package state (by all means visible only though a setter procedure and a getter function). Sad-to-say, there’s no simple way to do this in PostgreSQL (which has no packages). The only choices are tables or what the stack exchange piece that Frits referred to describes.

Tables are problematic: performance; distinguishing your rows from other sessions’ rows in a regular table; no such thing as a global temporary table and the PG, so session temporary table needs somehowsla to be created at the start of a session.

Here’s how I built a stopwatch for use across several server calls. Cumbersome. But it does work.

create procedure admin.start_stopwatch()
language plpgsql
as $body$
declare
 -- Make a memo of the current wall-clock time.
 start_time constant text not null := clock_timestamp()::text;
begin
 execute 'set stopwatch.start_time to '''||start_time||'''';
end;
$body$;

create function admin.stopwatch_reading()
 returns text
 -- It's critical to use "volatile". Else wrong results.
 volatile
language plpgsql
as $body$
declare
 -- Read the starting wall-clock time from the memo.
 start_time constant timestamptz not null := current_setting('stopwatch.start_time');

 -- Read the current wall-clock time.
 curr_time constant timestamptz not null := clock_timestamp();
 diff constant interval not null := curr_time - start_time;
begin
 return ...
end;
$body$;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.