C# Npgsql error: Backend sent unrecognized response type 'o' when calling PostgreSQL function

I’m encountering an unusual error while trying to invoke a PostgreSQL stored procedure from my C# application using Npgsql. The error message reads Backend sent unrecognized response type: o.

Here’s my PostgreSQL function:

CREATE OR REPLACE FUNCTION Fifty(field_a bigint, field_b bigint, field_c decimal, field_d decimal, field_e bigint)
RETURNS void AS $$
DECLARE variableA bigint;
BEGIN
IF (field_b != 0) THEN
    INSERT INTO table_a 
    SELECT fields FROM table_a 
    WHERE field_a = a AND field_b = b AND field_c = c;

    SELECT field_d INTO table FROM table_a 
    WHERE field_a = a AND field_b = b;

    UPDATE tb_history SET 
        field_a = a,
        value = b,
        his_value = d * 0.02,
        his_deb = 0.08,
        his_id = field_a
    WHERE field_a = a AND his_deb = b AND his_id = c;

    DELETE FROM table_a WHERE field_a = a AND field_b = b AND field_c = c;
ELSE
    DELETE FROM table_d WHERE field_a IN (SELECT ...);
    DELETE FROM table_d WHERE field_a IN (SELECT ...);
    DELETE FROM table_e WHERE field_a = a AND field_b = b AND field_c = c;
    DELETE FROM table_a WHERE field_a = a AND field_b = b AND field_c = c;
END IF;
END
$$ LANGUAGE plpgsql;

In my C# code, I’m using the following:

using (NpgsqlCommand command = new NpgsqlCommand())
{
    command.Connection = this.conn;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "Fifty";
    
    var parm = command.CreateParameter();
    parm.ParameterName = "...";
    parm.DbType = DbType.Int64;
    parm.Value = ...;
    command.Parameters.Add(parm);
    
    // Additional parameters setup
    
    command.ExecuteNonQuery();
}

Interestingly, the function executes perfectly in pgAdmin without any issues, so I suspect the problem may stem from the return type being void. I’m unsure what adjustments I need to make for successful execution.

Hmm, interesting. Have you tried wrapping the function call in a transaction block? Sometimes npgsql gets confused with complex procedures doing multiple operations. Also noticed your function has variable name mismatches - shouldn’t those be field_a, field_b instead of a, b, c? What PostgreSQL version are you running?

Update your npgsql version first - older versions had problems with complex stored procedures. Also check your function syntax - those variable references look wrong (you’re using ‘a’, ‘b’, ‘c’ instead of actual parameter names). That ‘o’ response type error usually means npgsql expects something different than what postgres is sending back.

This error usually happens when Npgsql can’t handle PostgreSQL’s response during stored procedure calls. I ran into the same thing with complex procedures that had multiple statement types. Switch from CommandType.StoredProcedure to CommandType.Text and call your function with SQL syntax instead. Change your command setup to: command.CommandType = CommandType.Text; command.CommandText = "SELECT Fifty(@param1, @param2, @param3, @param4, @param5)"; Use named parameters with the @ prefix rather than creating them manually. Even though your function returns void, PostgreSQL still needs the SELECT syntax for external function calls. This fixed the unrecognized response error for me, especially with functions doing multiple database operations.