2003-05-13 01:08:52 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* xid.c
|
|
|
|
* POSTGRES transaction identifier and command identifier datatypes.
|
|
|
|
*
|
2011-01-01 19:18:15 +01:00
|
|
|
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
|
2003-05-13 01:08:52 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/xid.c
|
2003-05-13 01:08:52 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
2003-05-13 01:08:52 +02:00
|
|
|
#include "access/xact.h"
|
|
|
|
#include "libpq/pqformat.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
#define PG_GETARG_TRANSACTIONID(n) DatumGetTransactionId(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_RETURN_TRANSACTIONID(x) return TransactionIdGetDatum(x)
|
|
|
|
|
|
|
|
#define PG_GETARG_COMMANDID(n) DatumGetCommandId(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_RETURN_COMMANDID(x) return CommandIdGetDatum(x)
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xidin(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
|
|
|
|
|
|
|
PG_RETURN_TRANSACTIONID((TransactionId) strtoul(str, NULL, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
xidout(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
|
|
|
|
|
|
|
|
/* maximum 32 bit unsigned integer representation takes 10 chars */
|
|
|
|
char *str = palloc(11);
|
|
|
|
|
|
|
|
snprintf(str, 11, "%lu", (unsigned long) transactionId);
|
|
|
|
|
|
|
|
PG_RETURN_CSTRING(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xidrecv - converts external binary format to xid
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
xidrecv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
|
|
|
|
PG_RETURN_TRANSACTIONID((TransactionId) pq_getmsgint(buf, sizeof(TransactionId)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xidsend - converts xid to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
xidsend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TransactionId arg1 = PG_GETARG_TRANSACTIONID(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendint(&buf, arg1, sizeof(arg1));
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xideq - are two xids equal?
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
xideq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TransactionId xid1 = PG_GETARG_TRANSACTIONID(0);
|
|
|
|
TransactionId xid2 = PG_GETARG_TRANSACTIONID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(TransactionIdEquals(xid1, xid2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xid_age - compute age of an XID (relative to current xact)
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
xid_age(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TransactionId xid = PG_GETARG_TRANSACTIONID(0);
|
2004-09-16 18:58:44 +02:00
|
|
|
TransactionId now = GetTopTransactionId();
|
2003-05-13 01:08:52 +02:00
|
|
|
|
|
|
|
/* Permanent XIDs are always infinitely old */
|
|
|
|
if (!TransactionIdIsNormal(xid))
|
|
|
|
PG_RETURN_INT32(INT_MAX);
|
|
|
|
|
|
|
|
PG_RETURN_INT32((int32) (now - xid));
|
|
|
|
}
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
|
|
|
* xidComparator
|
|
|
|
* qsort comparison function for XIDs
|
|
|
|
*
|
|
|
|
* We can't use wraparound comparison for XIDs because that does not respect
|
|
|
|
* the triangle inequality! Any old sort order will do.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
xidComparator(const void *arg1, const void *arg2)
|
|
|
|
{
|
|
|
|
TransactionId xid1 = *(const TransactionId *) arg1;
|
|
|
|
TransactionId xid2 = *(const TransactionId *) arg2;
|
|
|
|
|
|
|
|
if (xid1 > xid2)
|
|
|
|
return 1;
|
|
|
|
if (xid1 < xid2)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-05-13 01:08:52 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* COMMAND IDENTIFIER ROUTINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cidin - converts CommandId to internal representation.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
cidin(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *s = PG_GETARG_CSTRING(0);
|
|
|
|
CommandId c;
|
|
|
|
|
|
|
|
c = atoi(s);
|
|
|
|
|
|
|
|
PG_RETURN_COMMANDID(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cidout - converts a cid to external representation.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
cidout(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
CommandId c = PG_GETARG_COMMANDID(0);
|
|
|
|
char *result = (char *) palloc(16);
|
|
|
|
|
|
|
|
snprintf(result, 16, "%u", (unsigned int) c);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cidrecv - converts external binary format to cid
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
cidrecv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
|
|
|
|
PG_RETURN_COMMANDID((CommandId) pq_getmsgint(buf, sizeof(CommandId)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cidsend - converts cid to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
cidsend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
CommandId arg1 = PG_GETARG_COMMANDID(0);
|
2003-05-13 01:08:52 +02:00
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendint(&buf, arg1, sizeof(arg1));
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
cideq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
CommandId arg1 = PG_GETARG_COMMANDID(0);
|
|
|
|
CommandId arg2 = PG_GETARG_COMMANDID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 == arg2);
|
|
|
|
}
|