diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c
index e03d7ce..cfe7f78 100644
--- a/src/pl/plpython/plpython.c
+++ b/src/pl/plpython/plpython.c
@@ -6,6 +6,21 @@
  *********************************************************************
  */
 
+#include "postgres.h"
+
+/*
+ * Save settings the Python headers might override 
+ */
+#ifdef _POSIX_C_SOURCE
+#define _PGSAVE_POSIX_C_SOURCE _POSIX_C_SOURCE
+#undef _POSIX_C_SOURCE
+#endif
+
+#ifdef _XOPEN_SOURCE
+#define _PGSAVE_XOPEN_SOURCE _XOPEN_SOURCE
+#undef _XOPEN_SOURCE
+#endif
+
 #if defined(_MSC_VER) && defined(_DEBUG)
 /* Python uses #pragma to bring in a non-default libpython on VC++ if
  * _DEBUG is defined */
@@ -84,7 +99,20 @@ typedef int Py_ssize_t;
 		PyObject_HEAD_INIT(type) size,
 #endif
 
-#include "postgres.h"
+/*
+ * Restore settings the Python headers might have overridden.
+ */
+#ifdef _PGSAVE_POSIX_C_SOURCE
+#undef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE _PGSAVE_POSIX_C_SOURCE
+#undef _PGSAVE_POSIX_C_SOURCE
+#endif
+
+#ifdef _PGSAVE_XOPEN_SOURCE
+#undef _XOPEN_SOURCE
+#define _XOPEN_SOURCE _PGSAVE_XOPEN_SOURCE
+#undef _PGSAVE_XOPEN_SOURCE
+#endif
 
 /* system stuff */
 #include <unistd.h>
