Zhemin Zhou <Zhemin.Zhou@xxxxxxxxxxxxx> writes: > We met a problem after running the website for one week. We used a > function to convert and save binary files into the database (as bytea). > This function worked well in the old version but sometimes makes the new > version of postgres crash. This random crash is not file specific. AFAICT, it's pure luck that it didn't crash the older system too. You're allocating the output buffer too small, at least for cases where "size" isn't a multiple of 3: > bytea *result = (bytea *) > palloc(VARHDRSZ+sizeof(char)*(4*(size)/3+15)); For example, if size = 2, 4*2/3 is only 2, but the loop will write 4 bytes of data. So the function sometimes clobbers bytes beyond what it allocated, which unsurprisingly corrupts malloc's data structures. You need to round up not truncate in this division. regards, tom lane