Interacting with transactions

Let's take as an example the f_insert_tags() function of Listing 24. This splits a string into an array of tags and inserts each tag into the tags table. It is possible to convert the function into a procedure in order to ensure each tuple is committed and does not get lost if the function unexpectedly aborts. The procedure shown in Listing 28 performs the exact same operations as the function in Listing 24, so it will not be re-detailed here. However, as you can see, after every INSERT, an explicit COMMIT is issued so that each single tuple is stored even if the very next loop iteration aborts:

testdb=> CREATE OR REPLACE PROCEDUREp_insert_tags( tag_string text, separator text DEFAULT '>' )AS $code$  DECLARE

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.