Is SELECT or INSERT in a function prone to race conditions?

It’s the recurring problem of SELECT or INSERT under possible concurrent write load, related to (but different from) UPSERT (which is INSERT or UPDATE). This PL/pgSQL function uses UPSERT (INSERT … ON CONFLICT ..) to INSERT or SELECT a single row: CREATE OR REPLACE FUNCTION f_tag_id(_tag text, OUT _tag_id int) LANGUAGE plpgsql AS $func$ BEGIN … Read more

How to create guid in PostgreSQL

PostgreSQL has the uuid-ossp extension which ships with the standard distributions and it has 5 standard algorithms for generating uuids. Note that a guid is the Microsoft version of a uuid, conceptually they are the same thing. CREATE EXTENSION “uuid-ossp”; Then: SELECT uuid_generate_v4(); Note also that, once you installed the extension, PostgreSQL has an actual … Read more

How to prevent ‘invalid input syntax for type json’ in Postgres, when records contain a mix of json or strings

If you want to skip the rows with invalid JSON, you must first test if the text is valid JSON. You can do this by creating a function which will attempt to parse the value, and catch the exception for invalid JSON values. CREATE OR REPLACE FUNCTION is_json(input_text varchar) RETURNS boolean AS $$ DECLARE maybe_json … Read more

Liquibase error [Postgresql]: unterminated dollar-quoted string at or near “$BODY$

I just encountered the same issue days ago. It does not work if we add the changeset into changelog.xml file using the format below: <include file=”path/to/sqlfile” /> To work around, I use another format: <changeSet author=”authour_name” id=”your_id”> <sqlFile path=”path/to/sqlfile” splitStatements=”false”/> </changeSet> Here is the link which gives a brief explanation to Problem with dollar-quoted-in-postgresql.

FOR EACH STATEMENT trigger example

OLD and NEW are null or not defined in a statement-level trigger. Per documentation: NEW Data type RECORD; variable holding the new database row for INSERT/UPDATE operations in row-level triggers. This variable is null in statement-level triggers and for DELETE operations. OLD Data type RECORD; variable holding the old database row for UPDATE/DELETE operations in … Read more

SELECT .. INTO to create a table in PL/pgSQL

Try CREATE TEMP TABLE mytable AS SELECT * FROM orig_table; Per http://www.postgresql.org/docs/current/static/sql-selectinto.html CREATE TABLE AS is functionally similar to SELECT INTO. CREATE TABLE AS is the recommended syntax, since this form of SELECT INTO is not available in ECPG or PL/pgSQL, because they interpret the INTO clause differently. Furthermore, CREATE TABLE AS offers a superset … Read more

INSERT with dynamic table name in trigger function

Modern PostgreSQL format() has a built-in way to escape identifiers. Simpler than before: CREATE OR REPLACE FUNCTION foo_before() RETURNS trigger LANGUAGE plpgsql AS $func$ BEGIN EXECUTE format(‘INSERT INTO %I.%I SELECT $1.*’ , TG_TABLE_SCHEMA, TG_TABLE_NAME || ‘shadow’) USING OLD; RETURN OLD; END $func$; Works with a VALUES expression as well. db<>fiddle here Old sqlfiddle Major points … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)