Debug PostgreSQL function using pgAdmin

You have to enable debugging in two places. On PGAdmin and on the database itself. That article you referenced does a wonderful job explaining it but there were some nuances. PGAdmin When updating your postgresql.conf file to load the debugging library, I was running PGAdmin on Windows so the file was here: C:\Program Files\PostgreSQL\9.4\data\postgresql.conf And … Read more

Loop over array dimension in plpgsql

Since PostgreSQL 9.1 There is the convenient FOREACH which can loop over slices of arrays. The manual: The target variable must be an array, and it receives successive slices of the array value, where each slice is of the number of dimensions specified by SLICE. DO $do$ DECLARE m text[]; arr text[] := ‘{{key1,val1},{key2,val2}}’; — … Read more

How do I get the primary key(s) of a table from Postgres via plpgsql?

The query above is very bad as it is really slow. I would recommend this official version: http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns if schema is needed the query is as follows SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute, pg_namespace WHERE pg_class.oid = ‘foo’::regclass AND indrelid = pg_class.oid AND nspname=”public” AND pg_class.relnamespace = pg_namespace.oid AND pg_attribute.attrelid = pg_class.oid AND … Read more

How to write function for optional parameters in postgresql?

You can define optional parameters by supplying a default value. create function foo(p_one integer default null, p_two integer default 42, p_three varchar default ‘foo’) returns text as $$ begin return format(‘p_one=%s, p_two=%s, p_three=%s’, p_one, p_two, p_three); end; $$ language plpgsql; You can “leave out” parameters from the end, so foo(), foo(1) or foo(1,2) are valid. … Read more

How do I do large non-blocking updates in PostgreSQL?

Column / Row … I don’t need the transactional integrity to be maintained across the entire operation, because I know that the column I’m changing is not going to be written to or read during the update. Any UPDATE in PostgreSQL’s MVCC model writes a new version of the whole row. If concurrent transactions change … Read more

Difference between language sql and language plpgsql in PostgreSQL functions

SQL functions … are the better choice: For simple scalar queries. Not much to plan, better save any overhead. For single (or very few) calls per session. Nothing to gain from plan caching via prepared statements that PL/pgSQL has to offer. See below. If they are typically called in the context of bigger queries and … Read more

Return multiple fields as a record in PostgreSQL with PL/pgSQL

Don’t use CREATE TYPE to return a polymorphic result. Use and abuse the RECORD type instead. Check it out: CREATE FUNCTION test_ret(a TEXT, b TEXT) RETURNS RECORD AS $$ DECLARE ret RECORD; BEGIN — Arbitrary expression to change the first parameter IF LENGTH(a) < LENGTH(b) THEN SELECT TRUE, a || b, ‘a shorter than b’ … Read more

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