|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_lsn.c |
| 4 | + * Internal PostgreSQL LSN operations |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * src/backend/utils/adt/pg_lsn.c |
| 11 | + * |
| 12 | + *------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | +#include"postgres.h" |
| 15 | + |
| 16 | +#include"funcapi.h" |
| 17 | +#include"libpq/pqformat.h" |
| 18 | +#include"utils/builtins.h" |
| 19 | +#include"utils/pg_lsn.h" |
| 20 | + |
| 21 | +#defineMAXPG_LSNLEN17 |
| 22 | +#defineMAXPG_LSNCOMPONENT8 |
| 23 | + |
| 24 | +/*---------------------------------------------------------- |
| 25 | + * Formatting and conversion routines. |
| 26 | + *---------------------------------------------------------*/ |
| 27 | + |
| 28 | +Datum |
| 29 | +pg_lsn_in(PG_FUNCTION_ARGS) |
| 30 | +{ |
| 31 | +char*str=PG_GETARG_CSTRING(0); |
| 32 | +intlen1,len2; |
| 33 | +uint32id,off; |
| 34 | +XLogRecPtrresult; |
| 35 | + |
| 36 | +/* Sanity check input format. */ |
| 37 | +len1=strspn(str,"0123456789abcdefABCDEF"); |
| 38 | +if (len1<1||len1>MAXPG_LSNCOMPONENT||str[len1]!='/') |
| 39 | +ereport(ERROR, |
| 40 | +(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 41 | +errmsg("invalid input syntax for transaction log location: \"%s\"",str))); |
| 42 | +len2=strspn(str+len1+1,"0123456789abcdefABCDEF"); |
| 43 | +if (len2<1||len2>MAXPG_LSNCOMPONENT||str[len1+1+len2]!='\0') |
| 44 | +ereport(ERROR, |
| 45 | +(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 46 | +errmsg("invalid input syntax for transaction log location: \"%s\"",str))); |
| 47 | + |
| 48 | +/* Decode result. */ |
| 49 | +id= (uint32)strtoul(str,NULL,16); |
| 50 | +off= (uint32)strtoul(str+len1+1,NULL,16); |
| 51 | +result= (XLogRecPtr) ((uint64)id <<32) |off; |
| 52 | + |
| 53 | +PG_RETURN_PG_LSN(result); |
| 54 | +} |
| 55 | + |
| 56 | +Datum |
| 57 | +pg_lsn_out(PG_FUNCTION_ARGS) |
| 58 | +{ |
| 59 | +XLogRecPtrlsn= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 60 | +charbuf[MAXPG_LSNLEN+1]; |
| 61 | +char*result; |
| 62 | +uint32id,off; |
| 63 | + |
| 64 | +/* Decode ID and offset */ |
| 65 | +id= (uint32) (lsn >>32); |
| 66 | +off= (uint32)lsn; |
| 67 | + |
| 68 | +snprintf(buf,sizeofbuf,"%X/%X",id,off); |
| 69 | +result=pstrdup(buf); |
| 70 | +PG_RETURN_CSTRING(result); |
| 71 | +} |
| 72 | + |
| 73 | +Datum |
| 74 | +pg_lsn_recv(PG_FUNCTION_ARGS) |
| 75 | +{ |
| 76 | +StringInfobuf= (StringInfo)PG_GETARG_POINTER(0); |
| 77 | +XLogRecPtrresult; |
| 78 | + |
| 79 | +result=pq_getmsgint64(buf); |
| 80 | +PG_RETURN_PG_LSN(result); |
| 81 | +} |
| 82 | + |
| 83 | +Datum |
| 84 | +pg_lsn_send(PG_FUNCTION_ARGS) |
| 85 | +{ |
| 86 | +XLogRecPtrlsn= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 87 | +StringInfoDatabuf; |
| 88 | + |
| 89 | +pq_begintypsend(&buf); |
| 90 | +pq_sendint64(&buf,lsn); |
| 91 | +PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +/*---------------------------------------------------------- |
| 96 | + *Operators for PostgreSQL LSNs |
| 97 | + *---------------------------------------------------------*/ |
| 98 | + |
| 99 | +Datum |
| 100 | +pg_lsn_eq(PG_FUNCTION_ARGS) |
| 101 | +{ |
| 102 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 103 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 104 | + |
| 105 | +PG_RETURN_BOOL(lsn1==lsn2); |
| 106 | +} |
| 107 | + |
| 108 | +Datum |
| 109 | +pg_lsn_ne(PG_FUNCTION_ARGS) |
| 110 | +{ |
| 111 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 112 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 113 | + |
| 114 | +PG_RETURN_BOOL(lsn1!=lsn2); |
| 115 | +} |
| 116 | + |
| 117 | +Datum |
| 118 | +pg_lsn_lt(PG_FUNCTION_ARGS) |
| 119 | +{ |
| 120 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 121 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 122 | + |
| 123 | +PG_RETURN_BOOL(lsn1<lsn2); |
| 124 | +} |
| 125 | + |
| 126 | +Datum |
| 127 | +pg_lsn_gt(PG_FUNCTION_ARGS) |
| 128 | +{ |
| 129 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 130 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 131 | + |
| 132 | +PG_RETURN_BOOL(lsn1>lsn2); |
| 133 | +} |
| 134 | + |
| 135 | +Datum |
| 136 | +pg_lsn_le(PG_FUNCTION_ARGS) |
| 137 | +{ |
| 138 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 139 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 140 | + |
| 141 | +PG_RETURN_BOOL(lsn1 <=lsn2); |
| 142 | +} |
| 143 | + |
| 144 | +Datum |
| 145 | +pg_lsn_ge(PG_FUNCTION_ARGS) |
| 146 | +{ |
| 147 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 148 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 149 | + |
| 150 | +PG_RETURN_BOOL(lsn1 >=lsn2); |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +/*---------------------------------------------------------- |
| 155 | + *Arithmetic operators on PostgreSQL LSNs. |
| 156 | + *---------------------------------------------------------*/ |
| 157 | + |
| 158 | +Datum |
| 159 | +pg_lsn_mi(PG_FUNCTION_ARGS) |
| 160 | +{ |
| 161 | +XLogRecPtrlsn1= (XLogRecPtr)PG_GETARG_PG_LSN(0); |
| 162 | +XLogRecPtrlsn2= (XLogRecPtr)PG_GETARG_PG_LSN(1); |
| 163 | +charbuf[256]; |
| 164 | +Datumresult; |
| 165 | + |
| 166 | +/* Negative results are not allowed. */ |
| 167 | +if (lsn1<lsn2) |
| 168 | +ereport(ERROR, |
| 169 | +(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |
| 170 | +errmsg("transaction log location out of range"))); |
| 171 | + |
| 172 | +/* Convert to numeric. */ |
| 173 | +snprintf(buf,sizeofbuf,UINT64_FORMAT,lsn1-lsn2); |
| 174 | +result=DirectFunctionCall3(numeric_in, |
| 175 | +CStringGetDatum(buf), |
| 176 | +ObjectIdGetDatum(0), |
| 177 | +Int32GetDatum(-1)); |
| 178 | + |
| 179 | +returnresult; |
| 180 | +} |