PostgreSQL table variable

Use a temporary table in PostgreSQL. For your example: CREATE TEMP TABLE product_totals ( product_id int , revenue money ); The manual about CREATE TABLE: If specified, the table is created as a temporary table. Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction … Read more

Loop on tables with PL/pgSQL in Postgres 9.0+

I can’t remember the last time I actually needed to use an explicit cursor for looping in PL/pgSQL. Use the implicit cursor of a FOR loop, that’s much cleaner: DO $$ DECLARE rec record; nbrow bigint; BEGIN FOR rec IN SELECT * FROM pg_tables WHERE tablename NOT LIKE ‘pg\_%’ ORDER BY tablename LOOP EXECUTE ‘SELECT … Read more

No function matches the given name and argument types

Your function has a couple of smallint parameters. But in the call, you are using numeric literals that are presumed to be type integer. A string literal or string constant (‘123’) is not typed immediately. It remains type “unknown” until assigned or cast explicitly. However, a numeric literal or numeric constant is typed immediately. The … Read more

SELECT INTO with more than one attribution

In PL/pgSQL you can SELECT INTO as many variables at once as you like directly. You just had the syntax backwards: SELECT INTO unsolvedNodes, lengths array_agg(DISTINCT idDestination), array_agg(length) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); You have the keyword INTO followed by a list of target variables, and you have a … Read more

Function with SQL query has no destination for result data

Do it as plain SQL CREATE OR REPLACE FUNCTION tst_dates_func() RETURNS TABLE( date_value date, date_id int, date_desc varchar) as $BODY$ select a.date_value, a.date_id, a.date_desc from dates_tbl a; $BODY$ LANGUAGE sql; If you really need plpgsql use return query CREATE OR REPLACE FUNCTION tst_dates_func() RETURNS TABLE( date_value date, date_id int, date_desc varchar) as $BODY$ BEGIN perform … Read more

PostgreSQL cannot begin/end transactions in PL/pgSQL

A plpgsql function automatically runs inside a transaction. It all succeeds or it all fails. The manual: Functions and trigger procedures are always executed within a transaction established by an outer query — they cannot start or commit that transaction, since there would be no context for them to execute in. However, a block containing … Read more

How to create sequence if not exists

Postgres 9.5 or later IF NOT EXISTS was added to CREATE SEQUENCE in Postgres 9.5. That’s the simple solution now: CREATE SEQUENCE IF NOT EXISTS myschema.myseq; But consider details of the outdated answer anyway … And you know about serial or IDENTITY columns, right? Auto increment table column Postgres 9.4 or older Sequences share the … Read more

PL/pgSQL perform vs execute

PERFORM is plpgsql command used for calls of void functions. PLpgSQL is careful about useless SELECT statements – the SELECT without INTO clause is not allowed. But sometimes you need to call a function and you don’t need to store result (or functions has no result). The function in SQL is called with SELECT statement. … Read more

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