BUG #17272: Incorrect syntax parsed

Started by PG Bug reporting formover 4 years ago2 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 17272
Logged by: László Nagy
Email address: nagylzs@gmail.com
PostgreSQL version: 12.8
Operating system: Ubuntu 20.04.3 LTS on amd64
Description:

Formally, the INTO should always precede the FROM keyword in a SELECT INTO
statement. ( https://www.postgresql.org/docs/12/sql-selectinto.html )

Today we accidentally wrote this in a plpsql function:

CREATE OR REPLACE FUNCTION wf.trg_after_update_workflow_station ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $function$
DECLARE
selected_user_id float;
BEGIN
IF (NEW.station_id IS DISTINCT FROM OLD.station_id) THEN
IF (NEW.station_id = 2105) THEN
select su.id from sys.sec_user su into selected_user_id inner
join kap.training_permit tp on
su.id = tp.sec_user_id inner join wf.workflow w on w.rec_id =
tp.id where w.rec_id = NEW.rec_id;
perform from sys.sec_user_group where user_id = selected_user_id
and group_id = 35;
IF NOT FOUND THEN
INSERT INTO sys.sec_user_group (id, group_id, user_id)
VALUES (nextval('sys.id_seq'), 35, selected_user_id);
END IF;
END IF;
<more code here>
END
$function$

As you can see, we have used it in this order:

SELECT <expression> FROM tablename INTO variable INNER JOIN .....

I was very surprised that it actually worked! I think this should be a
syntax error. (Or maybe if this is not a syntax error, then it should be
documented?)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: PG Bug reporting form (#1)
Re: BUG #17272: Incorrect syntax parsed

PG Bug reporting form <noreply@postgresql.org> writes:

Formally, the INTO should always precede the FROM keyword in a SELECT INTO
statement. ( https://www.postgresql.org/docs/12/sql-selectinto.html )

That is not the appropriate reference for plpgsql's version of
SELECT INTO. You should be reading

https://www.postgresql.org/docs/12/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW

which explains plpgsql's rules for where to put INTO.

regards, tom lane