26

The PostgreSql documentation does not seem to mention a special class range for custom error codes. Has any been defined or is there an unofficial agreement on a specific class range?

5
  • 3
    Very good question, I'll raise it on the mailing list. Commented Mar 24, 2014 at 1:39
  • 1
    Love this question - I'm currently trying to work with both PostgreSQL and Oracle, where Oracle uses and supports returning custom error codes via SQLException#getErrorCode(), while PostgreSQL always return 0 from that method. I've looked pretty hard and found nothing discussing custom errors via SQLException#getSQLState(). Commented Feb 18, 2016 at 4:21
  • Hopefully we will see an answer one day. Commented Feb 10, 2020 at 19:52
  • 1
    See also Custom error code class and numbers for Postgres stored functions on Database Administrators Commented Apr 11, 2023 at 22:45
  • @Bergi This answers the question conclusively. Thanks! Commented Mar 22 at 4:52

1 Answer 1

20

At the bottom of the first section on this page there is some oblique, and indeed rather unspecific, reference to custom error codes. My approach for custom error codes:

  • Start with a capital letter but not F (predefined config file errors), H (fdw), P (PL/pgSQL) or X (internal).
  • Do not use 0 (zero) or P in the 3rd column. Predefined error codes use these commonly.
  • Use a capital letter in the 4th position. No predefined error codes have this.

As an example, start with a character for your app: "T". Then a two-char error class: "3G". Then a sequential code "A0"-"A9", "B0"-"B9", etc. Yields T3GA0, T3GA1, etc.

But as Craig Ringer indicated in his comment to the OP, some better guidance on the topic might be useful. Keep in mind though that such guidance would hardly solve the issue, unless a global registry is made of error ranges for common extensions. I am doubtful that such a registry will be made, however.

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

3 Comments

Yeah, the only robust mechanism is to do the "follow the DNS" trick, with namespaced values like com.mycompany.myproject.ErrorCode. Which isn't possible with an SQLState.
@Patrick et al, 6 years later, I have a question... Since we have no crystal ball, starting with an as-yet unused letter strikes me as perhaps only slightly less risky than starting with an as-yet unused number. It appears that starting with a '9' also places your errors in an as-yet unused bucket. Does anyone see problems with this approach?
same here. I finaly use the "ZZ999" as my all-in-one custom error code.

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.