1

This might be a stupid question but pardon me, I'm trying to convert one of my MariaDB database into a PostgreSQL database. Here I'm getting an error while executing this function. I cannot find what's wrong here,

create function tg_prodcut_insert()
returns trigger as '
    BEGIN
        SET NEW.id = CONCAT(1, LPAD(INSERT INTO product_seq VALUES (NULL) returning id, 6, 0));      
    END;
' LANGUAGE 'plpgsql';

Error is pointing to the 1 in CONCAT method, The type of id I'm trying to SET is char(7)

EDIT I also tried this, this won't work either,

create function tg_orders_insert()
returns trigger as '
    BEGIN
        INSERT INTO order_seq VALUES (NULL);
        SET NEW.id = CONCAT('1', LPAD(LAST_INSERT_ID(), 6, 0));       
    END;
' LANGUAGE 'plpgsql';

Thanks in advance.

12
  • Still not working. I also tried to_char(1, '9') and it also not working. Commented Aug 21, 2020 at 16:18
  • Nope. ERROR: syntax error at or near "(" Commented Aug 21, 2020 at 16:20
  • Hmm. What's wrong in LPAD ? It wasn't worked with LAST_INSERT_ID, so I moved the insert query in to the LPAD and returned the id. Is that wrong ? Commented Aug 21, 2020 at 16:21
  • I added the function I tried first. All I want is to get the last_insert_id. Commented Aug 21, 2020 at 16:26
  • You are looking for lastval() or currval() postgresql.org/docs/current/functions-sequence.html Commented Aug 21, 2020 at 16:26

1 Answer 1

3

It seems you are trying to simulate some kind of sequence with that code by inserting into a table and then getting the auto_increment value from that.

This can be done much more efficiently using a sequence in Postgres.

The error you get also isn't caused by the concat() function but because you are using the wrong syntax.

Value assignment is done using := in PL/pgSQL.

And there is also no last_insert_id() function in Postgres. To get the next value from a sequence use nextval(), to get the most recently generated value, you can use lastval() but that's not necessary here.

create sequence product_id_seq;

create function tg_product_insert()
returns trigger as 
$$
BEGIN
  NEW.id := concat('ORD', to_char(nextval('product_id_seq'), 'FM00000000'));
  return new;
END;
$$
LANGUAGE plpgsql;

you will need to create a before trigger for that to work:

create trigger product_seq_trigger
  before insert on product
  for each row
  execute procedure tg_product_insert();

Online example


But it would be a lot more efficient to switch to a proper identity column instead and get rid of the trigger.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the answer but now it error is pointing to :=
There were a few typos in there, but that is none of them.
FYI, assignment can also be done with = per link above. What is the error message?
@Dilshan: I fixed some typos, the code definitely works (now): dbfiddle.uk/…
@a_horse_with_no_name Thank you so much. It's working :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.