Rakesh Mehta
Rakesh Mehta

Reputation: 85

libpq ERROR: insufficient data left in message

I am using libpq to interact with PostgreSQL DB. I'm getting below error while trying to update the table.

ERROR: insufficient data left in message

I have created a sample code with same parameters and it also is giving me the same error Below is the sample code

    std::string sQuery("update client_backlog_period set starttime = $1, endtime = $2, unavailable = $3 where periodid = $4");

    int nElem = 4;
    
    timestamp start_time = 736260060000000, end_time = 736260900000000;
    short nUnavailable = 0;
    long nPeriodid = 1;

    start_time = boost::endian::native_to_big(start_time);
    end_time = boost::endian::native_to_big(end_time);
    nUnavailable = boost::endian::native_to_big(nUnavailable);
    nPeriodid = boost::endian::native_to_big(nPeriodid);

    const char* paramValues[4] = { (char*)&start_time, (char*)&end_time, (char*)&nUnavailable, (char*)&nPeriodid};
    Oid paramTypes[4] = { TIMESTAMPOID, TIMESTAMPOID, INT2OID , INT8OID };

    int paramLengths[4] = { sizeof(start_time), sizeof(end_time),sizeof(nUnavailable), sizeof(nPeriodid) };
    int paramFormats[4] = { 1,1,1,1 };
    PGresult* res = PQexecParams(db.m_pPGConn,
            sQuery.c_str(),
            nElem,
            paramTypes,
            paramValues,
            paramLengths,
            paramFormats,
            1
        );

    ExecStatusType status = PQresultStatus(res);
    if (status != PGRES_COMMAND_OK)
    {
        std::cerr << "\n Update Query failed, Error: " << PQerrorMessage(res) << ",status: " << status << std::endl;
PQclear(res);
    }

PQerrorMessage(res) is returning ERROR: insufficient data left in message
status is PGRES_FATAL_ERROR

The insertion and select stmt are working fine.

table description

Upvotes: 0

Views: 908

Answers (1)

Stephen Wood
Stephen Wood

Reputation: 1

This might be down to a few things (referenced in the below post) I'll summarize them here.

  1. There is a datatype mismatch between the value you are attempting to update and the datatype defined for those columns within the table (starttime,endtime or unavailable in your case)
  2. Your data has a null character in it "\0"
  3. An issue to do with DB encoding, change to UTF-8 and this works.

Postgres: org.postgresql.util.PSQLException: ERROR: insufficient data left in message

Upvotes: 0

Related Questions