Using triggers to modify data on the fly

In this section, we will look at how a trigger can be used to modify manipulated data according to specific rules. For example, what happens if the type of the file (f_type) is not specified when a tuple is inserted or updated? It might be possible to infer the file type from its name extension. In order to do this, a row-level trigger must be implemented; the trigger will fire before any INSERT or UPDATE completes, and this will allow you to modify each tuple on the fly, inspect the current value of the incoming or updating f_name value, and extract the file type from its extension.

The trigger function is shown in Listing 7. First, it checks its running context to ensure the function has not been ...

Get PostgreSQL 11 Server Side Programming Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.