0

Is the usage of format() in cases like this generally interchangeable?

exec_str := format('UPDATE ' || TG_ARGV[0] || 
                    ' SET username = current_user,
                              time = current_timestamp::timestamp(0);'
                  );
EXECUTE exec_str;

vs.

exec_str := 'UPDATE ' || TG_ARGV[0] || 
                    ' SET username = current_user,
                              time = current_timestamp::timestamp(0);'
                  ;
EXECUTE format(exec_str);
2
  • Did you try each? Commented Aug 29, 2016 at 10:53
  • yes, both work. I just would like to know if one of the variants could produce errors. I just did not have any problems so far. Better be safe than sorry ;) Commented Aug 29, 2016 at 10:54

1 Answer 1

2

The primary benefit of the function format() is that you can use parameters:

execute format('
    UPDATE %I 
    SET username = current_user,
        time = current_timestamp::timestamp(0);',
    TG_ARGV[0]);

Read more in the documentation.

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

2 Comments

Wouldn't it be better to use EXECUTE ... USING ... for that?
@LaurenzAlbe USING can only bind values, not identifiers (like in the original question).

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.