-#ifndef _REGEX_H_
-#define _REGEX_H_              /* never again */
+#ifndef _PG_REGEX_H_
+#define _PG_REGEX_H_           /* never again */
 /*
  * regular expressions
  *
  * src/include/regex/regex.h
  */
 
+/*
+ * This is an implementation of POSIX regex_t, so it clashes with the
+ * system-provided <regex.h> header.  That header might be unintentionally
+ * included already, so we force that to happen now on all systems to show that
+ * we can cope and that we completely replace the system regex interfaces.
+ *
+ * Note that we avoided using _REGEX_H_ as an include guard, as that confuses
+ * matters on BSD family systems including macOS that use the same include
+ * guard.
+ */
+#ifndef _WIN32
+#include <regex.h>
+#endif
+
+/* Avoid redefinition errors due to the system header. */
+#undef REG_UBACKREF
+#undef REG_ULOOKAROUND
+#undef REG_UBOUNDS
+#undef REG_UBRACES
+#undef REG_UBSALNUM
+#undef REG_UPBOTCH
+#undef REG_UBBS
+#undef REG_UNONPOSIX
+#undef REG_UUNSPEC
+#undef REG_UUNPORT
+#undef REG_ULOCALE
+#undef REG_UEMPTYMATCH
+#undef REG_UIMPOSSIBLE
+#undef REG_USHORTEST
+#undef REG_BASIC
+#undef REG_EXTENDED
+#undef REG_ADVF
+#undef REG_ADVANCED
+#undef REG_QUOTE
+#undef REG_NOSPEC
+#undef REG_ICASE
+#undef REG_NOSUB
+#undef REG_EXPANDED
+#undef REG_NLSTOP
+#undef REG_NLANCH
+#undef REG_NEWLINE
+#undef REG_PEND
+#undef REG_EXPECT
+#undef REG_BOSONLY
+#undef REG_DUMP
+#undef REG_FAKE
+#undef REG_PROGRESS
+#undef REG_NOTBOL
+#undef REG_NOTEOL
+#undef REG_STARTEND
+#undef REG_FTRACE
+#undef REG_MTRACE
+#undef REG_SMALL
+#undef REG_OKAY
+#undef REG_NOMATCH
+#undef REG_BADPAT
+#undef REG_ECOLLATE
+#undef REG_ECTYPE
+#undef REG_EESCAPE
+#undef REG_ESUBREG
+#undef REG_EBRACK
+#undef REG_EPAREN
+#undef REG_EBRACE
+#undef REG_BADBR
+#undef REG_ERANGE
+#undef REG_ESPACE
+#undef REG_BADRPT
+#undef REG_ASSERT
+#undef REG_INVARG
+#undef REG_MIXED
+#undef REG_BADOPT
+#undef REG_ETOOBIG
+#undef REG_ECOLORS
+#undef REG_ATOI
+#undef REG_ITOA
+#undef REG_PREFIX
+#undef REG_EXACT
+
 /*
  * Add your own defines, if needed, here.
  */
  * regoff_t has to be large enough to hold either off_t or ssize_t,
  * and must be signed; it's only a guess that long is suitable.
  */
-typedef long regoff_t;
+typedef long pg_regoff_t;
 
 /*
  * other interface types
    /* the rest is opaque pointers to hidden innards */
    char       *re_guts;        /* `char *' is more portable than `void *' */
    char       *re_fns;
-} regex_t;
+} pg_regex_t;
 
 /* result reporting (may acquire more fields later) */
 typedef struct
 {
-   regoff_t    rm_so;          /* start of substring */
-   regoff_t    rm_eo;          /* end of substring */
-} regmatch_t;
+   pg_regoff_t rm_so;          /* start of substring */
+   pg_regoff_t rm_eo;          /* end of substring */
+} pg_regmatch_t;
 
 /* supplementary control and reporting */
 typedef struct
 {
-   regmatch_t  rm_extend;      /* see REG_EXPECT */
+   pg_regmatch_t rm_extend;    /* see REG_EXPECT */
 } rm_detail_t;
 
 
 #define REG_EXACT  (-2)        /* identified an exact match */
 
 
+/* Redirect the standard typenames to our typenames. */
+#define regoff_t pg_regoff_t
+#define regex_t pg_regex_t
+#define regmatch_t pg_regmatch_t
+
 
 /*
  * the prototypes for exported functions
                                   int cflags, Oid collation,
                                   int nmatch, regmatch_t *pmatch);
 
-#endif                         /* _REGEX_H_ */
+#endif                         /* _PG_REGEX_H_ */