Skip to content

Improve C++/Postgres error handling

Krzysztof Leśniak requested to merge pg_cxx_interop-improve into develop
  • Add call to FlushErrorState. This is required for deeply nested calls to pg_call_cxx/cxx_call_pg.
  • Do not switch back to errorContext at the end of PG_CATCH. Looking at existing Postgres code this is being done only before calling PG_RE_THROW.

Unfortunately, we cannot construct a test for this currently, because we use mock implementation or Postgres' error handling functions in our tests, which do no expose the bad behaviour.

I checked this by implementing a C function that calls a deeply nested calls to pg_call_cxx/cxx_call_pg. This is unlikely scenario, but it kills the backend process with PANIC error when happens.

PsqlTools::PsqlUtils::pg_call_cxx([](){
  return PsqlTools::PsqlUtils::cxx_call_pg([]() -> int {
    PsqlTools::PsqlUtils::pg_call_cxx([](){
      return PsqlTools::PsqlUtils::cxx_call_pg([]() -> int {
        PsqlTools::PsqlUtils::pg_call_cxx([](){
          return PsqlTools::PsqlUtils::cxx_call_pg([]() -> int {
            PsqlTools::PsqlUtils::pg_call_cxx([](){
              return PsqlTools::PsqlUtils::cxx_call_pg([]() -> int {
                PsqlTools::PsqlUtils::pg_call_cxx([](){
                  return PsqlTools::PsqlUtils::cxx_call_pg([]() -> int {
                    PsqlTools::PsqlUtils::pg_call_cxx([](){
                      return PsqlTools::PsqlUtils::cxx_call_pg([]() -> int {
                        ereport( ERROR, (errcode( ERRCODE_DATA_EXCEPTION ), errmsg( "some error" )) );
                      });
                    });
                  });
                });
              });
            });
          });
        });
      });
    });
  });
});

Merge request reports