mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-20 05:17:00 +08:00
Commit 8431e296ea reworked ProcArrayApplyRecoveryInfo to sort XIDs before adding them to KnownAssignedXids. But the XIDs are sorted using xidComparator, which compares the XIDs simply as uint32 values, not logically. KnownAssignedXidsAdd() however expects XIDs in logical order, and calls TransactionIdFollowsOrEquals() to enforce that. If there are XIDs for which the two orderings disagree, an error is raised and the recovery fails/restarts. Hitting this issue is fairly easy - you just need two transactions, one started before the 4B limit (e.g. XID 4294967290), the other sometime after it (e.g. XID 1000). Logically (4294967290 <= 1000) but when compared using xidComparator we try to add them in the opposite order. Which makes KnownAssignedXidsAdd() fail with an error like this: ERROR: out-of-order XID insertion in KnownAssignedXids This only happens during replica startup, while processing RUNNING_XACTS records to build the snapshot. Once we reach STANDBY_SNAPSHOT_READY, we skip these records. So this does not affect already running replicas, but if you restart (or create) a replica while there are transactions with XIDs for which the two orderings disagree, you may hit this. Long-running transactions and frequent replica restarts increase the likelihood of hitting this issue. Once the replica gets into this state, it can't be started (even if the old transactions are terminated). Fixed by sorting the XIDs logically - this is fine because we're dealing with normal XIDs (because it's XIDs assigned to backends) and from the same wraparound epoch (otherwise the backends could not be running at the same time on the primary node). So there are no problems with the triangle inequality, which is why xidComparator compares raw values. Investigation and root cause analysis by Abhijit Menon-Sen. Patch by me. This issue is present in all releases since 9.4, however releases up to 9.6 are EOL already so backpatch to 10 only. Reviewed-by: Abhijit Menon-Sen Reviewed-by: Alvaro Herrera Backpatch-through: 10 Discussion: https://postgr.es/m/36b8a501-5d73-277c-4972-f58a4dce088a%40enterprisedb.com
129 lines
4.3 KiB
C
129 lines
4.3 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* builtins.h
|
|
* Declarations for operations on built-in types.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/utils/builtins.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef BUILTINS_H
|
|
#define BUILTINS_H
|
|
|
|
#include "fmgr.h"
|
|
#include "nodes/nodes.h"
|
|
#include "utils/fmgrprotos.h"
|
|
|
|
/* Sign + the most decimal digits an 8-byte number could have */
|
|
#define MAXINT8LEN 20
|
|
|
|
/* bool.c */
|
|
extern bool parse_bool(const char *value, bool *result);
|
|
extern bool parse_bool_with_len(const char *value, size_t len, bool *result);
|
|
|
|
/* domains.c */
|
|
extern void domain_check(Datum value, bool isnull, Oid domainType,
|
|
void **extra, MemoryContext mcxt);
|
|
extern int errdatatype(Oid datatypeOid);
|
|
extern int errdomainconstraint(Oid datatypeOid, const char *conname);
|
|
|
|
/* encode.c */
|
|
extern uint64 hex_encode(const char *src, size_t len, char *dst);
|
|
extern uint64 hex_decode(const char *src, size_t len, char *dst);
|
|
|
|
/* int.c */
|
|
extern int2vector *buildint2vector(const int16 *int2s, int n);
|
|
|
|
/* name.c */
|
|
extern int namecpy(Name n1, const NameData *n2);
|
|
extern int namestrcpy(Name name, const char *str);
|
|
extern int namestrcmp(Name name, const char *str);
|
|
|
|
/* numutils.c */
|
|
extern int32 pg_atoi(const char *s, int size, int c);
|
|
extern int16 pg_strtoint16(const char *s);
|
|
extern int32 pg_strtoint32(const char *s);
|
|
extern void pg_itoa(int16 i, char *a);
|
|
extern int pg_ultoa_n(uint32 l, char *a);
|
|
extern int pg_ulltoa_n(uint64 l, char *a);
|
|
extern void pg_ltoa(int32 l, char *a);
|
|
extern void pg_lltoa(int64 ll, char *a);
|
|
extern char *pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth);
|
|
extern char *pg_ultostr(char *str, uint32 value);
|
|
extern uint64 pg_strtouint64(const char *str, char **endptr, int base);
|
|
|
|
/* oid.c */
|
|
extern oidvector *buildoidvector(const Oid *oids, int n);
|
|
extern Oid oidparse(Node *node);
|
|
extern int oid_cmp(const void *p1, const void *p2);
|
|
|
|
/* regexp.c */
|
|
extern char *regexp_fixed_prefix(text *text_re, bool case_insensitive,
|
|
Oid collation, bool *exact);
|
|
|
|
/* ruleutils.c */
|
|
extern bool quote_all_identifiers;
|
|
extern const char *quote_identifier(const char *ident);
|
|
extern char *quote_qualified_identifier(const char *qualifier,
|
|
const char *ident);
|
|
extern void generate_operator_clause(fmStringInfo buf,
|
|
const char *leftop, Oid leftoptype,
|
|
Oid opoid,
|
|
const char *rightop, Oid rightoptype);
|
|
|
|
/* varchar.c */
|
|
extern int bpchartruelen(char *s, int len);
|
|
|
|
/* popular functions from varlena.c */
|
|
extern text *cstring_to_text(const char *s);
|
|
extern text *cstring_to_text_with_len(const char *s, int len);
|
|
extern char *text_to_cstring(const text *t);
|
|
extern void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len);
|
|
|
|
#define CStringGetTextDatum(s) PointerGetDatum(cstring_to_text(s))
|
|
#define TextDatumGetCString(d) text_to_cstring((text *) DatumGetPointer(d))
|
|
|
|
/* xid.c */
|
|
extern int xidComparator(const void *arg1, const void *arg2);
|
|
extern int xidLogicalComparator(const void *arg1, const void *arg2);
|
|
|
|
/* inet_cidr_ntop.c */
|
|
extern char *pg_inet_cidr_ntop(int af, const void *src, int bits,
|
|
char *dst, size_t size);
|
|
|
|
/* inet_net_pton.c */
|
|
extern int pg_inet_net_pton(int af, const char *src,
|
|
void *dst, size_t size);
|
|
|
|
/* network.c */
|
|
extern double convert_network_to_scalar(Datum value, Oid typid, bool *failure);
|
|
extern Datum network_scan_first(Datum in);
|
|
extern Datum network_scan_last(Datum in);
|
|
extern void clean_ipv6_addr(int addr_family, char *addr);
|
|
|
|
/* numeric.c */
|
|
extern Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS);
|
|
|
|
/* format_type.c */
|
|
|
|
/* Control flags for format_type_extended */
|
|
#define FORMAT_TYPE_TYPEMOD_GIVEN 0x01 /* typemod defined by caller */
|
|
#define FORMAT_TYPE_ALLOW_INVALID 0x02 /* allow invalid types */
|
|
#define FORMAT_TYPE_FORCE_QUALIFY 0x04 /* force qualification of type */
|
|
extern char *format_type_extended(Oid type_oid, int32 typemod, bits16 flags);
|
|
|
|
extern char *format_type_be(Oid type_oid);
|
|
extern char *format_type_be_qualified(Oid type_oid);
|
|
extern char *format_type_with_typemod(Oid type_oid, int32 typemod);
|
|
|
|
extern int32 type_maximum_size(Oid type_oid, int32 typemod);
|
|
|
|
/* quote.c */
|
|
extern char *quote_literal_cstr(const char *rawstr);
|
|
|
|
#endif /* BUILTINS_H */
|