I ran into the same problem after compiling postgresql 8.1.4 with gcc 4.9.3.
The problem is that postgres uses to represent variable-length arrays:
typedef struct { int32 size; int ndim; int flags; Oid elemtype; int dim1; int lbound1; int2 values[1]; } int2vector;
In some cases, for loops referring to "values", GCC assumes that they will do one iteration at most. Loops like the following (extracted from postgres source code):
ii->ii_NumIndexAttrs = numKeys; for (i = 0; i < numKeys; i++) ii->ii_KeyAttrNumbers[i] = indexStruct->indkey.values[i];
can be reduced to something like:
ii->ii_NumIndexAttrs = numKeys; if (numKeys) ii->ii_KeyAttrNumbers[0] = indexStruct->indkey.values[0];
as shown in the assembler generated for it:
.L161: testl %r12d, %r12d movl %r12d, 4(%rbx) jle .L162 movzwl 40(%r13), %eax movw %ax, 8(%rbx) .L162:
The problem disappeared after recompiling postgres with optimization disabled using -fno-aggressive optimization loops.
source share