Question

I am trying to figure out how to write a trigger function in PL/pgSQL that can select values into host variables in an Embedded SQL/C program. I am new to SQL, so I apologize if my terminology is a bit off. Basically, on a change to the table, I want the function to trigger a view in order to update the values stored in host variables.

This is the view I have created and tested to work:

EXEC SQL CREATE VIEW idfound AS SELECT id, num FROM newtable WHERE id = 1;

This is how I have called the view, using host variables dbID and dbNum:

EXEC SQL SELECT * INTO :dbID, :dbNum FROM idfound;

Here is the trigger I have created:

EXEC SQL CREATE idTrigger AFTER INSERT OR UPDATE ON newtable 
FOR EACH ROW EXECUTE PROCEDURE idFunc();

And here is the trigger function I would like to use:

EXEC SQL CREATE FUNCTION idFunc() RETURNS TRIGGER AS $idTrigger$
    BEGIN
    SELECT * INTO :dbID, :dbNum FROM idfound;
    END;
$idTrigger$ LANGUAGE plpgsql;

For testing purposes, I am using pgAdmin III. When I change the function to perform any action that doesn't use the host variables (such as update a value in a predefined row), I can see that it is created. However, I am unable to get it to create the trigger or trigger function using the host variables.

Right now, I am just trying to figure out how to achieve this in general, so I am aware that my current code doesn't actually do much. The general idea is that I want my trigger function to call a predefined view that uses host variables in C. If there is a better way of implementing the trigger, that advice would be greatly appreciated as well.

Was it helpful?

Solution

Trigger functions are executed in the server environment and cannot access the memory space of the client environment, neither for reading nor writing. That's a fundamental point of the client-server architecture, so there's no workaround, this is just not possible.

The closer workable equivalent may be for the trigger to NOTIFY and for the client to LISTEN and consume the notifications and the associated payload.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top