Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 38000: External Routine Exception Fix

· 6 min read · Home Automation

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 38000 (external routine exception) means a function written in a procedural language such as PL/Python, PL/Perl, PL/Java or another external handler raised an exception that PostgreSQL could not map to a more specific SQLSTATE code. The fix is almost always inside the function itself: find which function is failing, capture the real error message, and correct the external code or its environment. This guide walks through that process from start to finish.

What You'll Need

  • Access to the PostgreSQL server with a role that can query the catalogs (for example via psql)
  • The ability to read the server logs (a superuser or a user with permission to read the log files)
  • The source of the failing function, or permission to inspect it
  • Any external dependencies the function uses (Python modules, Perl modules, files, network access) installed and working

Step-by-Step

  1. Reproduce the error. Run the query or call that triggers the failure and note the exact message. PostgreSQL reports the failing function name in the error context, for example "PL/Python function \"my_function\"". Write down the function name and schema.
  2. Get the full details from the server log. Open the PostgreSQL server log and find the entry for the failure. The log shows the underlying exception from the external language runtime, such as a Python traceback, which is far more informative than the 38000 code itself. The default log location varies by system; check the log_directory and data_directory settings with SHOW log_directory; if you are unsure where to look.
  3. Inspect the function definition. In psql, run \ef schema_name.function_name to view and edit the source, or query it directly: SELECT prosrc FROM pg_proc WHERE proname = 'function_name';. You can also check which language it uses: SELECT lanname FROM pg_language WHERE oid = (SELECT prolang FROM pg_proc WHERE proname = 'function_name');
  4. Identify the external cause. Common culprits revealed by the log include: a missing module or library in the external language (for example an uninstalled Python package), a syntax or runtime error in the function source, a file path that does not exist on the server, a permission problem (the PostgreSQL server process runs as its own OS user and may not be able to read a file), or a network call that failed.
  5. Test the external code outside PostgreSQL. Copy the function body into a standalone script and run it with the same interpreter the server uses (for PL/Python, the server's GDAL of Python version, visible via SELECT plpython3_python_version(); on recent versions, or simply plpython_version()). This confirms whether the bug is in the code or in the database environment.
  6. Fix the code or environment. Correct the bug in the function source, install the missing dependency so the server's OS user can access it, fix file paths and permissions, or adjust the environment variables the external runtime needs. If you install a system package, remember the PostgreSQL server process must be able to see it; a module installed only in your personal user account will not be found.
  7. Recreate the function. Apply the corrected source with CREATE OR REPLACE FUNCTION .... Note that you cannot change the return type or argument list this way; if those must change, drop and recreate the function instead, which requires re-granting permissions on it.
  8. Test again. Call the function with the same input that previously failed, then with a range of other inputs to make sure the fix holds. Watch the server log during the test for any warnings.
  9. Add error handling for the future. Wrap risky parts of the external code in the language's own exception handling and raise a clearer error deliberately. In PL/Python you can plpy.error('descriptive message') from an except block; in PL/pgSQL you would use RAISE EXCEPTION. This converts opaque 38000 failures into actionable messages for the next person.

Common Mistakes

  • Only reading the client-side message and ignoring the server log, which contains the actual traceback or cause.
  • Installing a missing library as your own OS user instead of system-wide, so the postgres server process still cannot find it.
  • Assuming 38000 is a PostgreSQL core bug. It is nearly always the external function's own failure; PostgreSQL just has no more specific code for it.
  • Editing the function without recording the original source first, making it hard to roll back if the change makes things worse.
  • Forgetting that CREATE OR REPLACE FUNCTION cannot change the argument list or return type.

Tips

  • Set client_min_messages = DEBUG; in your session (if you have permission) while debugging to surface more detail in the client, not just the log.
  • If the same function runs fine on one server but not another, compare the external runtime versions and installed modules on both machines; a version mismatch is a very common cause.
  • Keep external functions small and testable. The more logic lives in an unlogged script, the harder 38000 errors are to pin down.
  • After fixing, add a regression test that calls the function, so the failure is caught early if the environment changes.

Troubleshooting

The log shows nothing useful. Make sure log_min_messages is not set too high (it defaults to WARNING, which is usually fine) and that you are reading the current log file, not a rotated one.

The error only happens with certain data. Feed those exact inputs into the standalone script from step 5. Look for null handling, encoding issues, or numeric edge cases in the external code.

The function worked before and stopped by itself. Something in the environment changed: an OS update removed or upgraded a library, a file was moved or deleted, a certificate expired, or a network endpoint became unreachable. Check recent system changes on the server.

You cannot see the function source. The function owner or a superuser can read pg_proc; ask them to run the inspection queries for you if your role lacks access.

Everything looks correct but it still fails. Test the interpreter path the server actually uses. Some distributions ship multiple Python or Perl versions, and the procedural language may be bound to a different one than the one on your PATH.

Frequently Asked Questions

What does SQLSTATE 38000 mean?

It is the generic class code for "external routine exception". It means a function in an external language raised an error and no more specific SQLSTATE code (such as 38001 or 38004) applied.

Does 38000 mean my database is corrupted?

No. It almost always points to a bug or environment problem in an external function, not to data corruption or a broken server.

Can PL/pgSQL raise 38000?

Rarely. PL/pgSQL is internal to PostgreSQL and usually produces specific SQLSTATE codes. 38000 typically comes from truly external handlers such as PL/Python, PL/Perl, PL/Java, PL/R or similar languages.

How do I find which function caused it?

The error context names the function, and the server log entry contains the underlying exception with a traceback or message from the external runtime.

Will restarting PostgreSQL fix it?

Usually not. If the cause was a freshly installed library that the process had not picked up, a restart can help, but in general the fix is in the function code or its environment.

How can I replace 38000 with a clearer error?

Add exception handling inside the external function that catches failures and raises a descriptive error with the language's mechanism, such as plpy.error() in PL/Python or RAISE EXCEPTION with ERRCODE in PL/pgSQL.