2000-02-15 20:49:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* variable.c
|
2002-05-17 01:19:19 +00:00
|
|
|
* Routines for handling specialized SET variables.
|
|
|
|
*
|
2000-02-15 20:49:31 +00:00
|
|
|
*
|
2011-01-01 13:18:15 -05:00
|
|
|
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
|
2000-02-15 20:49:31 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
1997-04-17 13:50:57 +00:00
|
|
|
*
|
2000-02-15 20:49:31 +00:00
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/commands/variable.c
|
1997-04-17 13:50:57 +00:00
|
|
|
*
|
2000-02-15 20:49:31 +00:00
|
|
|
*-------------------------------------------------------------------------
|
1997-04-17 13:50:57 +00:00
|
|
|
*/
|
|
|
|
|
2000-10-25 19:44:44 +00:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1997-06-20 17:17:03 +00:00
|
|
|
#include <ctype.h>
|
1999-07-16 05:00:38 +00:00
|
|
|
|
|
|
|
#include "access/xact.h"
|
2005-06-28 05:09:14 +00:00
|
|
|
#include "catalog/pg_authid.h"
|
1998-01-05 18:43:18 +00:00
|
|
|
#include "commands/variable.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "miscadmin.h"
|
2005-07-25 22:12:34 +00:00
|
|
|
#include "utils/acl.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "utils/builtins.h"
|
2002-05-17 01:19:19 +00:00
|
|
|
#include "utils/syscache.h"
|
2008-03-26 18:48:59 +00:00
|
|
|
#include "utils/snapmgr.h"
|
1998-07-26 04:31:41 +00:00
|
|
|
#include "mb/pg_wchar.h"
|
2000-02-15 20:49:31 +00:00
|
|
|
|
1998-10-14 05:10:12 +00:00
|
|
|
/*
|
2002-05-17 01:19:19 +00:00
|
|
|
* DATESTYLE
|
1998-10-14 05:10:12 +00:00
|
|
|
*/
|
1997-04-24 15:41:37 +00:00
|
|
|
|
1998-10-14 05:10:12 +00:00
|
|
|
/*
|
2002-05-17 01:19:19 +00:00
|
|
|
* assign_datestyle: GUC assign_hook for datestyle
|
1998-10-14 05:10:12 +00:00
|
|
|
*/
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
2004-01-19 19:04:40 +00:00
|
|
|
assign_datestyle(const char *value, bool doit, GucSource source)
|
1997-06-02 11:00:57 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
int newDateStyle = DateStyle;
|
2003-07-29 00:03:19 +00:00
|
|
|
int newDateOrder = DateOrder;
|
2005-06-09 21:52:07 +00:00
|
|
|
bool have_style = false;
|
|
|
|
bool have_order = false;
|
2002-05-17 01:19:19 +00:00
|
|
|
bool ok = true;
|
|
|
|
char *rawstring;
|
|
|
|
char *result;
|
|
|
|
List *elemlist;
|
2004-05-26 04:41:50 +00:00
|
|
|
ListCell *l;
|
1997-06-20 17:17:03 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/* Need a modifiable copy of string */
|
|
|
|
rawstring = pstrdup(value);
|
1997-11-07 06:43:16 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/* Parse string into list of identifiers */
|
|
|
|
if (!SplitIdentifierString(rawstring, ',', &elemlist))
|
1997-06-02 11:00:57 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
/* syntax error in list */
|
|
|
|
pfree(rawstring);
|
2004-05-26 04:41:50 +00:00
|
|
|
list_free(elemlist);
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2005-10-15 02:49:52 +00:00
|
|
|
errmsg("invalid list syntax for parameter \"datestyle\"")));
|
2002-05-17 01:19:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(l, elemlist)
|
|
|
|
{
|
|
|
|
char *tok = (char *) lfirst(l);
|
|
|
|
|
1997-04-17 13:50:57 +00:00
|
|
|
/* Ugh. Somebody ought to write a table driven version -- mjl */
|
1997-06-20 17:17:03 +00:00
|
|
|
|
2004-05-07 00:24:59 +00:00
|
|
|
if (pg_strcasecmp(tok, "ISO") == 0)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_style && newDateStyle != USE_ISO_DATES)
|
|
|
|
ok = false; /* conflicting styles */
|
2002-05-17 01:19:19 +00:00
|
|
|
newDateStyle = USE_ISO_DATES;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_style = true;
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(tok, "SQL") == 0)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_style && newDateStyle != USE_SQL_DATES)
|
|
|
|
ok = false; /* conflicting styles */
|
2002-05-17 01:19:19 +00:00
|
|
|
newDateStyle = USE_SQL_DATES;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_style = true;
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strncasecmp(tok, "POSTGRES", 8) == 0)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_style && newDateStyle != USE_POSTGRES_DATES)
|
|
|
|
ok = false; /* conflicting styles */
|
2002-05-17 01:19:19 +00:00
|
|
|
newDateStyle = USE_POSTGRES_DATES;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_style = true;
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(tok, "GERMAN") == 0)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_style && newDateStyle != USE_GERMAN_DATES)
|
|
|
|
ok = false; /* conflicting styles */
|
2002-05-17 01:19:19 +00:00
|
|
|
newDateStyle = USE_GERMAN_DATES;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_style = true;
|
2003-07-29 00:03:19 +00:00
|
|
|
/* GERMAN also sets DMY, unless explicitly overridden */
|
2005-06-09 21:52:07 +00:00
|
|
|
if (!have_order)
|
2003-07-29 00:03:19 +00:00
|
|
|
newDateOrder = DATEORDER_DMY;
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(tok, "YMD") == 0)
|
1997-12-04 23:17:13 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_order && newDateOrder != DATEORDER_YMD)
|
|
|
|
ok = false; /* conflicting orders */
|
2003-07-29 00:03:19 +00:00
|
|
|
newDateOrder = DATEORDER_YMD;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_order = true;
|
1997-12-04 23:17:13 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(tok, "DMY") == 0 ||
|
|
|
|
pg_strncasecmp(tok, "EURO", 4) == 0)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_order && newDateOrder != DATEORDER_DMY)
|
|
|
|
ok = false; /* conflicting orders */
|
2003-07-29 00:03:19 +00:00
|
|
|
newDateOrder = DATEORDER_DMY;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_order = true;
|
2003-07-29 00:03:19 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(tok, "MDY") == 0 ||
|
|
|
|
pg_strcasecmp(tok, "US") == 0 ||
|
|
|
|
pg_strncasecmp(tok, "NONEURO", 7) == 0)
|
2003-07-29 00:03:19 +00:00
|
|
|
{
|
2005-06-09 21:52:07 +00:00
|
|
|
if (have_order && newDateOrder != DATEORDER_MDY)
|
|
|
|
ok = false; /* conflicting orders */
|
2003-07-29 00:03:19 +00:00
|
|
|
newDateOrder = DATEORDER_MDY;
|
2005-06-09 21:52:07 +00:00
|
|
|
have_order = true;
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(tok, "DEFAULT") == 0)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Easiest way to get the current DEFAULT state is to fetch the
|
|
|
|
* DEFAULT string from guc.c and recursively parse it.
|
2002-05-17 01:19:19 +00:00
|
|
|
*
|
2005-11-22 18:17:34 +00:00
|
|
|
* We can't simply "return assign_datestyle(...)" because we need
|
|
|
|
* to handle constructs like "DEFAULT, ISO".
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
|
|
|
int saveDateStyle = DateStyle;
|
2003-07-29 00:03:19 +00:00
|
|
|
int saveDateOrder = DateOrder;
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *subval;
|
|
|
|
|
|
|
|
subval = assign_datestyle(GetConfigOptionResetString("datestyle"),
|
2004-01-19 19:04:40 +00:00
|
|
|
true, source);
|
2005-06-09 21:52:07 +00:00
|
|
|
if (!have_style)
|
2003-07-29 00:03:19 +00:00
|
|
|
newDateStyle = DateStyle;
|
2005-06-09 21:52:07 +00:00
|
|
|
if (!have_order)
|
2003-07-29 00:03:19 +00:00
|
|
|
newDateOrder = DateOrder;
|
2002-05-17 01:19:19 +00:00
|
|
|
DateStyle = saveDateStyle;
|
2003-07-29 00:03:19 +00:00
|
|
|
DateOrder = saveDateOrder;
|
2002-05-17 01:19:19 +00:00
|
|
|
if (!subval)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Here we know that our own return value is always malloc'd */
|
|
|
|
/* when doit is true */
|
|
|
|
free((char *) subval);
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1997-04-17 13:50:57 +00:00
|
|
|
else
|
2002-05-17 01:19:19 +00:00
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unrecognized \"datestyle\" key word: \"%s\"",
|
|
|
|
tok)));
|
2002-05-17 01:19:19 +00:00
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
1997-06-02 11:00:57 +00:00
|
|
|
}
|
1997-06-20 17:17:03 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
pfree(rawstring);
|
2004-05-26 04:41:50 +00:00
|
|
|
list_free(elemlist);
|
2001-10-18 17:30:21 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
if (!ok)
|
2002-04-21 19:12:46 +00:00
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("conflicting \"datestyle\" specifications")));
|
2002-05-17 01:19:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-04-21 19:12:46 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* If we aren't going to do the assignment, just return OK indicator.
|
|
|
|
*/
|
|
|
|
if (!doit)
|
|
|
|
return value;
|
2002-04-21 19:12:46 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
2002-09-04 20:31:48 +00:00
|
|
|
* Prepare the canonical string to return. GUC wants it malloc'd.
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
|
|
|
result = (char *) malloc(32);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
2002-04-21 19:12:46 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
switch (newDateStyle)
|
|
|
|
{
|
|
|
|
case USE_ISO_DATES:
|
|
|
|
strcpy(result, "ISO");
|
|
|
|
break;
|
|
|
|
case USE_SQL_DATES:
|
|
|
|
strcpy(result, "SQL");
|
|
|
|
break;
|
|
|
|
case USE_GERMAN_DATES:
|
2003-07-15 19:19:56 +00:00
|
|
|
strcpy(result, "German");
|
2002-05-17 01:19:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-15 19:19:56 +00:00
|
|
|
strcpy(result, "Postgres");
|
2002-05-17 01:19:19 +00:00
|
|
|
break;
|
2002-04-21 19:12:46 +00:00
|
|
|
}
|
2003-07-29 00:03:19 +00:00
|
|
|
switch (newDateOrder)
|
|
|
|
{
|
|
|
|
case DATEORDER_YMD:
|
|
|
|
strcat(result, ", YMD");
|
|
|
|
break;
|
|
|
|
case DATEORDER_DMY:
|
|
|
|
strcat(result, ", DMY");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcat(result, ", MDY");
|
|
|
|
break;
|
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Finally, it's safe to assign to the global variables; the assignment
|
|
|
|
* cannot fail now.
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
|
|
|
DateStyle = newDateStyle;
|
2003-07-29 00:03:19 +00:00
|
|
|
DateOrder = newDateOrder;
|
2001-10-18 17:30:21 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
return result;
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
|
|
|
|
2000-02-19 22:10:47 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* TIMEZONE
|
1997-11-10 15:24:56 +00:00
|
|
|
*/
|
|
|
|
|
1998-10-14 05:10:12 +00:00
|
|
|
/*
|
2002-05-17 01:19:19 +00:00
|
|
|
* assign_timezone: GUC assign_hook for timezone
|
1997-11-10 15:37:15 +00:00
|
|
|
*/
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
2004-01-19 19:04:40 +00:00
|
|
|
assign_timezone(const char *value, bool doit, GucSource source)
|
1997-10-30 16:52:11 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
char *result;
|
|
|
|
char *endptr;
|
|
|
|
double hours;
|
1997-10-30 16:52:11 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* Check for INTERVAL 'foo'
|
|
|
|
*/
|
2004-05-07 00:24:59 +00:00
|
|
|
if (pg_strncasecmp(value, "interval", 8) == 0)
|
1997-10-30 16:52:11 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *valueptr = value;
|
|
|
|
char *val;
|
|
|
|
Interval *interval;
|
|
|
|
|
|
|
|
valueptr += 8;
|
|
|
|
while (isspace((unsigned char) *valueptr))
|
|
|
|
valueptr++;
|
|
|
|
if (*valueptr++ != '\'')
|
|
|
|
return NULL;
|
|
|
|
val = pstrdup(valueptr);
|
|
|
|
/* Check and remove trailing quote */
|
|
|
|
endptr = strchr(val, '\'');
|
|
|
|
if (!endptr || endptr[1] != '\0')
|
2001-10-18 17:30:21 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
pfree(val);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*endptr = '\0';
|
2002-09-04 20:31:48 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* Try to parse it. XXX an invalid interval format will result in
|
2009-06-11 14:49:15 +00:00
|
|
|
* ereport(ERROR), which is not desirable for GUC. We did what we
|
2007-12-28 00:23:23 +00:00
|
|
|
* could to guard against this in flatten_set_variable_args, but a
|
|
|
|
* string coming in from postgresql.conf might contain anything.
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
|
|
|
interval = DatumGetIntervalP(DirectFunctionCall3(interval_in,
|
2005-10-15 02:49:52 +00:00
|
|
|
CStringGetDatum(val),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1)));
|
2004-08-30 02:54:42 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
pfree(val);
|
|
|
|
if (interval->month != 0)
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid interval value for time zone: month not allowed")));
|
2002-05-17 01:19:19 +00:00
|
|
|
pfree(interval);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-07-20 16:42:32 +00:00
|
|
|
if (interval->day != 0)
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2009-06-11 14:49:15 +00:00
|
|
|
errmsg("invalid interval value for time zone: day not allowed")));
|
2005-07-20 16:42:32 +00:00
|
|
|
pfree(interval);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
if (doit)
|
|
|
|
{
|
2003-07-17 00:55:37 +00:00
|
|
|
/* Here we change from SQL to Unix sign convention */
|
2005-06-05 01:48:34 +00:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
CTimeZone = -(interval->time / USECS_PER_SEC);
|
|
|
|
#else
|
2003-08-04 00:43:34 +00:00
|
|
|
CTimeZone = -interval->time;
|
2005-06-05 01:48:34 +00:00
|
|
|
#endif
|
2004-08-30 02:54:42 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
HasCTZSet = true;
|
|
|
|
}
|
|
|
|
pfree(interval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Try it as a numeric number of hours (possibly fractional).
|
|
|
|
*/
|
|
|
|
hours = strtod(value, &endptr);
|
|
|
|
if (endptr != value && *endptr == '\0')
|
|
|
|
{
|
|
|
|
if (doit)
|
2001-10-18 17:30:21 +00:00
|
|
|
{
|
2003-07-17 00:55:37 +00:00
|
|
|
/* Here we change from SQL to Unix sign convention */
|
2005-07-21 03:56:25 +00:00
|
|
|
CTimeZone = -hours * SECS_PER_HOUR;
|
2002-05-17 01:19:19 +00:00
|
|
|
HasCTZSet = true;
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
}
|
2004-05-07 00:24:59 +00:00
|
|
|
else if (pg_strcasecmp(value, "UNKNOWN") == 0)
|
2002-05-17 01:19:19 +00:00
|
|
|
{
|
2001-10-25 05:50:21 +00:00
|
|
|
/*
|
2003-08-04 00:43:34 +00:00
|
|
|
* UNKNOWN is the value shown as the "default" for TimeZone in
|
2004-05-21 05:08:06 +00:00
|
|
|
* guc.c. We interpret it as being a complete no-op; we don't
|
|
|
|
* change the timezone setting. Note that if there is a known
|
2005-10-15 02:49:52 +00:00
|
|
|
* timezone setting, we will return that name rather than UNKNOWN
|
|
|
|
* as the canonical spelling.
|
2004-05-21 05:08:06 +00:00
|
|
|
*
|
2005-11-22 18:17:34 +00:00
|
|
|
* During GUC initialization, since the timezone library isn't set
|
|
|
|
* up yet, pg_get_timezone_name will return NULL and we will leave
|
2005-10-15 02:49:52 +00:00
|
|
|
* the setting as UNKNOWN. If this isn't overridden from the
|
|
|
|
* config file then pg_timezone_initialize() will eventually
|
|
|
|
* select a default value from the environment.
|
2001-10-25 05:50:21 +00:00
|
|
|
*/
|
2005-08-08 23:39:01 +00:00
|
|
|
if (doit)
|
|
|
|
{
|
2007-08-04 01:26:54 +00:00
|
|
|
const char *curzone = pg_get_timezone_name(session_timezone);
|
2003-05-18 01:06:26 +00:00
|
|
|
|
2005-08-08 23:39:01 +00:00
|
|
|
if (curzone)
|
|
|
|
value = curzone;
|
|
|
|
}
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
2005-04-19 03:13:59 +00:00
|
|
|
* Otherwise assume it is a timezone name, and try to load it.
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
2005-10-15 02:49:52 +00:00
|
|
|
pg_tz *new_tz;
|
2003-05-18 01:06:26 +00:00
|
|
|
|
2005-04-19 03:13:59 +00:00
|
|
|
new_tz = pg_tzset(value);
|
2003-05-18 01:06:26 +00:00
|
|
|
|
2005-04-19 03:13:59 +00:00
|
|
|
if (!new_tz)
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
2005-04-19 03:13:59 +00:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unrecognized time zone name: \"%s\"",
|
|
|
|
value)));
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-05-18 01:06:26 +00:00
|
|
|
|
2005-04-19 03:13:59 +00:00
|
|
|
if (!tz_acceptable(new_tz))
|
2001-10-18 17:30:21 +00:00
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
2005-04-19 03:13:59 +00:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2005-10-15 02:49:52 +00:00
|
|
|
errmsg("time zone \"%s\" appears to use leap seconds",
|
|
|
|
value),
|
|
|
|
errdetail("PostgreSQL does not support leap seconds.")));
|
2005-04-19 03:13:59 +00:00
|
|
|
return NULL;
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
2005-04-19 03:13:59 +00:00
|
|
|
|
|
|
|
if (doit)
|
2003-05-18 01:06:26 +00:00
|
|
|
{
|
2005-04-19 03:13:59 +00:00
|
|
|
/* Save the changed TZ */
|
2007-08-04 01:26:54 +00:00
|
|
|
session_timezone = new_tz;
|
2005-04-19 03:13:59 +00:00
|
|
|
HasCTZSet = false;
|
2003-05-18 01:06:26 +00:00
|
|
|
}
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
1997-10-30 16:52:11 +00:00
|
|
|
}
|
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* If we aren't going to do the assignment, just return OK indicator.
|
|
|
|
*/
|
|
|
|
if (!doit)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
/*
|
2002-09-04 20:31:48 +00:00
|
|
|
* Prepare the canonical string to return. GUC wants it malloc'd.
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
|
|
|
if (HasCTZSet)
|
2004-05-21 05:08:06 +00:00
|
|
|
{
|
|
|
|
result = (char *) malloc(64);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
snprintf(result, 64, "%.5f",
|
2005-10-15 02:49:52 +00:00
|
|
|
(double) (-CTimeZone) / (double) SECS_PER_HOUR);
|
2004-05-21 05:08:06 +00:00
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
else
|
2004-05-21 05:08:06 +00:00
|
|
|
result = strdup(value);
|
2002-05-17 01:19:19 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
1997-10-30 16:52:11 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* show_timezone: GUC show_hook for timezone
|
|
|
|
*/
|
|
|
|
const char *
|
2000-10-25 19:44:44 +00:00
|
|
|
show_timezone(void)
|
1997-10-30 16:52:11 +00:00
|
|
|
{
|
2004-08-29 05:07:03 +00:00
|
|
|
const char *tzn;
|
2001-10-18 17:30:21 +00:00
|
|
|
|
|
|
|
if (HasCTZSet)
|
|
|
|
{
|
2005-10-15 02:49:52 +00:00
|
|
|
Interval interval;
|
1997-10-30 16:52:11 +00:00
|
|
|
|
2005-06-05 01:48:34 +00:00
|
|
|
interval.month = 0;
|
2005-07-20 16:42:32 +00:00
|
|
|
interval.day = 0;
|
2005-06-05 01:48:34 +00:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
interval.time = -(CTimeZone * USECS_PER_SEC);
|
|
|
|
#else
|
|
|
|
interval.time = -CTimeZone;
|
|
|
|
#endif
|
2001-10-18 17:30:21 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
tzn = DatumGetCString(DirectFunctionCall1(interval_out,
|
2005-10-15 02:49:52 +00:00
|
|
|
IntervalPGetDatum(&interval)));
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
|
|
|
else
|
2007-08-04 01:26:54 +00:00
|
|
|
tzn = pg_get_timezone_name(session_timezone);
|
|
|
|
|
|
|
|
if (tzn != NULL)
|
|
|
|
return tzn;
|
|
|
|
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LOG_TIMEZONE
|
|
|
|
*
|
|
|
|
* For log_timezone, we don't support the interval-based methods of setting a
|
|
|
|
* zone, which are only there for SQL spec compliance not because they're
|
|
|
|
* actually useful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* assign_log_timezone: GUC assign_hook for log_timezone
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
assign_log_timezone(const char *value, bool doit, GucSource source)
|
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (pg_strcasecmp(value, "UNKNOWN") == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* UNKNOWN is the value shown as the "default" for log_timezone in
|
2007-11-15 21:14:46 +00:00
|
|
|
* guc.c. We interpret it as being a complete no-op; we don't change
|
|
|
|
* the timezone setting. Note that if there is a known timezone
|
|
|
|
* setting, we will return that name rather than UNKNOWN as the
|
|
|
|
* canonical spelling.
|
2007-08-04 01:26:54 +00:00
|
|
|
*
|
2007-11-15 21:14:46 +00:00
|
|
|
* During GUC initialization, since the timezone library isn't set up
|
|
|
|
* yet, pg_get_timezone_name will return NULL and we will leave the
|
|
|
|
* setting as UNKNOWN. If this isn't overridden from the config file
|
|
|
|
* then pg_timezone_initialize() will eventually select a default
|
|
|
|
* value from the environment.
|
2007-08-04 01:26:54 +00:00
|
|
|
*/
|
|
|
|
if (doit)
|
|
|
|
{
|
|
|
|
const char *curzone = pg_get_timezone_name(log_timezone);
|
|
|
|
|
|
|
|
if (curzone)
|
|
|
|
value = curzone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Otherwise assume it is a timezone name, and try to load it.
|
|
|
|
*/
|
|
|
|
pg_tz *new_tz;
|
|
|
|
|
|
|
|
new_tz = pg_tzset(value);
|
|
|
|
|
|
|
|
if (!new_tz)
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
2007-08-04 01:26:54 +00:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unrecognized time zone name: \"%s\"",
|
|
|
|
value)));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tz_acceptable(new_tz))
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
2007-08-04 01:26:54 +00:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("time zone \"%s\" appears to use leap seconds",
|
|
|
|
value),
|
|
|
|
errdetail("PostgreSQL does not support leap seconds.")));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doit)
|
|
|
|
{
|
|
|
|
/* Save the changed TZ */
|
|
|
|
log_timezone = new_tz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we aren't going to do the assignment, just return OK indicator.
|
|
|
|
*/
|
|
|
|
if (!doit)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare the canonical string to return. GUC wants it malloc'd.
|
|
|
|
*/
|
|
|
|
result = strdup(value);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* show_log_timezone: GUC show_hook for log_timezone
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
show_log_timezone(void)
|
|
|
|
{
|
|
|
|
const char *tzn;
|
|
|
|
|
|
|
|
tzn = pg_get_timezone_name(log_timezone);
|
1997-10-30 16:52:11 +00:00
|
|
|
|
2001-10-18 17:30:21 +00:00
|
|
|
if (tzn != NULL)
|
2002-05-17 01:19:19 +00:00
|
|
|
return tzn;
|
1997-10-30 16:52:11 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
return "unknown";
|
|
|
|
}
|
2000-02-15 20:49:31 +00:00
|
|
|
|
2000-03-17 05:29:07 +00:00
|
|
|
|
2011-01-22 20:51:32 -05:00
|
|
|
/*
|
|
|
|
* SET TRANSACTION READ ONLY and SET TRANSACTION READ WRITE
|
|
|
|
*
|
|
|
|
* We allow idempotent changes (r/w -> r/w and r/o -> r/o) at any time, and
|
|
|
|
* we also always allow changes from read-write to read-only. However,
|
|
|
|
* read-only to read-write may be changed only when source == PGC_S_OVERRIDE
|
|
|
|
* (i.e. we're aborting a read only transaction and restoring the previous
|
|
|
|
* setting) or in a top-level transaction that has not yet taken an initial
|
|
|
|
* snapshot.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
assign_transaction_read_only(bool newval, bool doit, GucSource source)
|
|
|
|
{
|
|
|
|
if (source != PGC_S_OVERRIDE && newval == false && XactReadOnly)
|
|
|
|
{
|
|
|
|
/* Can't go to r/w mode inside a r/o transaction */
|
|
|
|
if (IsSubTransaction())
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("cannot set transaction read-write mode inside a read-only transaction")));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Top level transaction can't change to r/w after first snapshot. */
|
|
|
|
if (FirstSnapshotSet)
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
|
|
|
|
errmsg("transaction read-write mode must be set before any query")));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Can't go to r/w mode while recovery is still active */
|
|
|
|
if (RecoveryInProgress())
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("cannot set transaction read-write mode during recovery")));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-10-18 17:30:21 +00:00
|
|
|
/*
|
2002-05-17 01:19:19 +00:00
|
|
|
* SET TRANSACTION ISOLATION LEVEL
|
2011-01-22 20:51:32 -05:00
|
|
|
*
|
|
|
|
* We allow idempotent changes at any time, but otherwise this can only be
|
|
|
|
* changed from a toplevel transaction that has not yet taken a snapshot, or
|
|
|
|
* when source == PGC_S_OVERRIDE (i.e. we're aborting a transaction and
|
|
|
|
* restoring the previously set value).
|
2001-10-18 17:30:21 +00:00
|
|
|
*/
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
2004-01-19 19:04:40 +00:00
|
|
|
assign_XactIsoLevel(const char *value, bool doit, GucSource source)
|
2000-02-15 20:49:31 +00:00
|
|
|
{
|
2011-01-21 21:49:19 -05:00
|
|
|
/* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
|
2011-01-22 20:51:32 -05:00
|
|
|
if (source != PGC_S_OVERRIDE && strcmp(value, XactIsoLevel_string) != 0)
|
2004-07-01 00:52:04 +00:00
|
|
|
{
|
2011-01-21 21:49:19 -05:00
|
|
|
if (FirstSnapshotSet)
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
|
|
|
|
errmsg("SET TRANSACTION ISOLATION LEVEL must be called before any query")));
|
2004-08-31 19:28:51 +00:00
|
|
|
return NULL;
|
2011-01-21 21:49:19 -05:00
|
|
|
}
|
|
|
|
/* We ignore a subtransaction setting it to the existing value. */
|
2011-01-22 20:51:32 -05:00
|
|
|
if (IsSubTransaction())
|
2011-01-21 21:49:19 -05:00
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
|
|
|
|
errmsg("SET TRANSACTION ISOLATION LEVEL must not be called in a subtransaction")));
|
2004-08-31 19:28:51 +00:00
|
|
|
return NULL;
|
2011-01-21 21:49:19 -05:00
|
|
|
}
|
Implement genuine serializable isolation level.
Until now, our Serializable mode has in fact been what's called Snapshot
Isolation, which allows some anomalies that could not occur in any
serialized ordering of the transactions. This patch fixes that using a
method called Serializable Snapshot Isolation, based on research papers by
Michael J. Cahill (see README-SSI for full references). In Serializable
Snapshot Isolation, transactions run like they do in Snapshot Isolation,
but a predicate lock manager observes the reads and writes performed and
aborts transactions if it detects that an anomaly might occur. This method
produces some false positives, ie. it sometimes aborts transactions even
though there is no anomaly.
To track reads we implement predicate locking, see storage/lmgr/predicate.c.
Whenever a tuple is read, a predicate lock is acquired on the tuple. Shared
memory is finite, so when a transaction takes many tuple-level locks on a
page, the locks are promoted to a single page-level lock, and further to a
single relation level lock if necessary. To lock key values with no matching
tuple, a sequential scan always takes a relation-level lock, and an index
scan acquires a page-level lock that covers the search key, whether or not
there are any matching keys at the moment.
A predicate lock doesn't conflict with any regular locks or with another
predicate locks in the normal sense. They're only used by the predicate lock
manager to detect the danger of anomalies. Only serializable transactions
participate in predicate locking, so there should be no extra overhead for
for other transactions.
Predicate locks can't be released at commit, but must be remembered until
all the transactions that overlapped with it have completed. That means that
we need to remember an unbounded amount of predicate locks, so we apply a
lossy but conservative method of tracking locks for committed transactions.
If we run short of shared memory, we overflow to a new "pg_serial" SLRU
pool.
We don't currently allow Serializable transactions in Hot Standby mode.
That would be hard, because even read-only transactions can cause anomalies
that wouldn't otherwise occur.
Serializable isolation mode now means the new fully serializable level.
Repeatable Read gives you the old Snapshot Isolation level that we have
always had.
Kevin Grittner and Dan Ports, reviewed by Jeff Davis, Heikki Linnakangas and
Anssi Kääriäinen
2011-02-07 23:46:51 +02:00
|
|
|
/* Can't go to serializable mode while recovery is still active */
|
|
|
|
if (RecoveryInProgress() && strcmp(value, "serializable") == 0)
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("cannot use serializable mode in a hot standby"),
|
|
|
|
errhint("You can use REPEATABLE READ instead.")));
|
|
|
|
return false;
|
|
|
|
}
|
2004-07-01 00:52:04 +00:00
|
|
|
}
|
2000-02-15 20:49:31 +00:00
|
|
|
|
2001-09-19 15:19:12 +00:00
|
|
|
if (strcmp(value, "serializable") == 0)
|
2002-09-04 20:31:48 +00:00
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
XactIsoLevel = XACT_SERIALIZABLE;
|
|
|
|
}
|
2003-11-06 22:08:15 +00:00
|
|
|
else if (strcmp(value, "repeatable read") == 0)
|
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
XactIsoLevel = XACT_REPEATABLE_READ;
|
|
|
|
}
|
2001-09-19 15:19:12 +00:00
|
|
|
else if (strcmp(value, "read committed") == 0)
|
2002-09-04 20:31:48 +00:00
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
XactIsoLevel = XACT_READ_COMMITTED;
|
|
|
|
}
|
2003-11-06 22:08:15 +00:00
|
|
|
else if (strcmp(value, "read uncommitted") == 0)
|
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
XactIsoLevel = XACT_READ_UNCOMMITTED;
|
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
else if (strcmp(value, "default") == 0)
|
2002-09-04 20:31:48 +00:00
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
XactIsoLevel = DefaultXactIsoLevel;
|
|
|
|
}
|
2000-02-15 20:49:31 +00:00
|
|
|
else
|
2002-05-17 01:19:19 +00:00
|
|
|
return NULL;
|
2000-02-15 20:49:31 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
return value;
|
2000-02-15 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
2000-10-25 19:44:44 +00:00
|
|
|
show_XactIsoLevel(void)
|
2000-02-15 20:49:31 +00:00
|
|
|
{
|
2003-11-06 22:08:15 +00:00
|
|
|
switch (XactIsoLevel)
|
|
|
|
{
|
|
|
|
case XACT_READ_UNCOMMITTED:
|
|
|
|
return "read uncommitted";
|
|
|
|
case XACT_READ_COMMITTED:
|
|
|
|
return "read committed";
|
|
|
|
case XACT_REPEATABLE_READ:
|
|
|
|
return "repeatable read";
|
|
|
|
case XACT_SERIALIZABLE:
|
|
|
|
return "serializable";
|
|
|
|
default:
|
|
|
|
return "bogus";
|
|
|
|
}
|
2000-02-15 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
Implement genuine serializable isolation level.
Until now, our Serializable mode has in fact been what's called Snapshot
Isolation, which allows some anomalies that could not occur in any
serialized ordering of the transactions. This patch fixes that using a
method called Serializable Snapshot Isolation, based on research papers by
Michael J. Cahill (see README-SSI for full references). In Serializable
Snapshot Isolation, transactions run like they do in Snapshot Isolation,
but a predicate lock manager observes the reads and writes performed and
aborts transactions if it detects that an anomaly might occur. This method
produces some false positives, ie. it sometimes aborts transactions even
though there is no anomaly.
To track reads we implement predicate locking, see storage/lmgr/predicate.c.
Whenever a tuple is read, a predicate lock is acquired on the tuple. Shared
memory is finite, so when a transaction takes many tuple-level locks on a
page, the locks are promoted to a single page-level lock, and further to a
single relation level lock if necessary. To lock key values with no matching
tuple, a sequential scan always takes a relation-level lock, and an index
scan acquires a page-level lock that covers the search key, whether or not
there are any matching keys at the moment.
A predicate lock doesn't conflict with any regular locks or with another
predicate locks in the normal sense. They're only used by the predicate lock
manager to detect the danger of anomalies. Only serializable transactions
participate in predicate locking, so there should be no extra overhead for
for other transactions.
Predicate locks can't be released at commit, but must be remembered until
all the transactions that overlapped with it have completed. That means that
we need to remember an unbounded amount of predicate locks, so we apply a
lossy but conservative method of tracking locks for committed transactions.
If we run short of shared memory, we overflow to a new "pg_serial" SLRU
pool.
We don't currently allow Serializable transactions in Hot Standby mode.
That would be hard, because even read-only transactions can cause anomalies
that wouldn't otherwise occur.
Serializable isolation mode now means the new fully serializable level.
Repeatable Read gives you the old Snapshot Isolation level that we have
always had.
Kevin Grittner and Dan Ports, reviewed by Jeff Davis, Heikki Linnakangas and
Anssi Kääriäinen
2011-02-07 23:46:51 +02:00
|
|
|
/*
|
|
|
|
* SET TRANSACTION [NOT] DEFERRABLE
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
assign_transaction_deferrable(bool newval, bool doit, GucSource source)
|
|
|
|
{
|
|
|
|
/* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
|
|
|
|
if (source == PGC_S_OVERRIDE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsSubTransaction())
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
|
|
|
|
errmsg("SET TRANSACTION [NOT] DEFERRABLE cannot be called within a subtransaction")));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FirstSnapshotSet)
|
|
|
|
{
|
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
|
|
|
|
errmsg("SET TRANSACTION [NOT] DEFERRABLE must be called before any query")));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2000-02-15 20:49:31 +00:00
|
|
|
|
2000-04-07 13:40:45 +00:00
|
|
|
/*
|
|
|
|
* Random number seed
|
|
|
|
*/
|
2002-04-21 19:12:46 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
bool
|
2004-01-19 19:04:40 +00:00
|
|
|
assign_random_seed(double value, bool doit, GucSource source)
|
2000-04-07 13:40:45 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
/* Can't really roll back on error, so ignore non-interactive setting */
|
2004-01-19 19:04:40 +00:00
|
|
|
if (doit && source >= PGC_S_INTERACTIVE)
|
2002-05-17 01:19:19 +00:00
|
|
|
DirectFunctionCall1(setseed, Float8GetDatum(value));
|
|
|
|
return true;
|
2000-04-07 13:40:45 +00:00
|
|
|
}
|
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
|
|
|
show_random_seed(void)
|
2000-04-07 13:40:45 +00:00
|
|
|
{
|
2002-05-17 01:19:19 +00:00
|
|
|
return "unavailable";
|
2000-04-07 13:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-25 19:44:44 +00:00
|
|
|
/*
|
I have committed many support files for CREATE CONVERSION. Default
conversion procs and conversions are added in initdb. Currently
supported conversions are:
UTF-8(UNICODE) <--> SQL_ASCII, ISO-8859-1 to 16, EUC_JP, EUC_KR,
EUC_CN, EUC_TW, SJIS, BIG5, GBK, GB18030, UHC,
JOHAB, TCVN
EUC_JP <--> SJIS
EUC_TW <--> BIG5
MULE_INTERNAL <--> EUC_JP, SJIS, EUC_TW, BIG5
Note that initial contents of pg_conversion system catalog are created
in the initdb process. So doing initdb required is ideal, it's
possible to add them to your databases by hand, however. To accomplish
this:
psql -f your_postgresql_install_path/share/conversion_create.sql your_database
So I did not bump up the version in cataversion.h.
TODO:
Add more conversion procs
Add [CASCADE|RESTRICT] to DROP CONVERSION
Add tuples to pg_depend
Add regression tests
Write docs
Add SQL99 CONVERT command?
--
Tatsuo Ishii
2002-07-18 02:02:30 +00:00
|
|
|
* encoding handling functions
|
2000-10-25 19:44:44 +00:00
|
|
|
*/
|
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
2004-01-19 19:04:40 +00:00
|
|
|
assign_client_encoding(const char *value, bool doit, GucSource source)
|
2000-10-25 19:44:44 +00:00
|
|
|
{
|
2001-10-25 05:50:21 +00:00
|
|
|
int encoding;
|
2001-10-18 17:30:21 +00:00
|
|
|
|
2000-10-25 19:44:44 +00:00
|
|
|
encoding = pg_valid_client_encoding(value);
|
|
|
|
if (encoding < 0)
|
2002-05-17 01:19:19 +00:00
|
|
|
return NULL;
|
2002-09-04 20:31:48 +00:00
|
|
|
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Note: if we are in startup phase then SetClientEncoding may not be able
|
|
|
|
* to really set the encoding. In this case we will assume that the
|
|
|
|
* encoding is okay, and InitializeClientEncoding() will fix things once
|
|
|
|
* initialization is complete.
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
I have committed many support files for CREATE CONVERSION. Default
conversion procs and conversions are added in initdb. Currently
supported conversions are:
UTF-8(UNICODE) <--> SQL_ASCII, ISO-8859-1 to 16, EUC_JP, EUC_KR,
EUC_CN, EUC_TW, SJIS, BIG5, GBK, GB18030, UHC,
JOHAB, TCVN
EUC_JP <--> SJIS
EUC_TW <--> BIG5
MULE_INTERNAL <--> EUC_JP, SJIS, EUC_TW, BIG5
Note that initial contents of pg_conversion system catalog are created
in the initdb process. So doing initdb required is ideal, it's
possible to add them to your databases by hand, however. To accomplish
this:
psql -f your_postgresql_install_path/share/conversion_create.sql your_database
So I did not bump up the version in cataversion.h.
TODO:
Add more conversion procs
Add [CASCADE|RESTRICT] to DROP CONVERSION
Add tuples to pg_depend
Add regression tests
Write docs
Add SQL99 CONVERT command?
--
Tatsuo Ishii
2002-07-18 02:02:30 +00:00
|
|
|
if (SetClientEncoding(encoding, doit) < 0)
|
2000-10-25 19:44:44 +00:00
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("conversion between %s and %s is not supported",
|
|
|
|
value, GetDatabaseEncodingName())));
|
2002-05-17 01:19:19 +00:00
|
|
|
return NULL;
|
2000-10-25 19:44:44 +00:00
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
return value;
|
2000-10-26 17:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
/*
|
|
|
|
* SET SESSION AUTHORIZATION
|
|
|
|
*
|
2003-02-01 18:31:28 +00:00
|
|
|
* When resetting session auth after an error, we can't expect to do catalog
|
2005-06-28 05:09:14 +00:00
|
|
|
* lookups. Hence, the stored form of the value must provide a numeric oid
|
2003-02-01 18:31:28 +00:00
|
|
|
* that can be re-used directly. We store the string in the form of
|
2003-06-27 19:08:38 +00:00
|
|
|
* NAMEDATALEN 'x's, followed by T or F to indicate superuserness, followed
|
2005-06-28 05:09:14 +00:00
|
|
|
* by the numeric oid, followed by a comma, followed by the role name.
|
|
|
|
* This cannot be confused with a plain role name because of the NAMEDATALEN
|
2004-08-11 21:10:37 +00:00
|
|
|
* limit on names, so we can tell whether we're being passed an initial
|
2006-02-12 22:32:43 +00:00
|
|
|
* role name or a saved/restored value. (NOTE: we rely on guc.c to have
|
|
|
|
* properly truncated any incoming value, but not to truncate already-stored
|
2006-10-04 00:30:14 +00:00
|
|
|
* values. See GUC_IS_NAME processing.)
|
2002-05-17 01:19:19 +00:00
|
|
|
*/
|
2004-08-29 05:07:03 +00:00
|
|
|
extern char *session_authorization_string; /* in guc.c */
|
2004-08-11 21:10:37 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
2004-01-19 19:04:40 +00:00
|
|
|
assign_session_authorization(const char *value, bool doit, GucSource source)
|
2002-05-06 19:47:30 +00:00
|
|
|
{
|
2005-10-15 02:49:52 +00:00
|
|
|
Oid roleid = InvalidOid;
|
2003-06-27 19:08:38 +00:00
|
|
|
bool is_superuser = false;
|
2005-06-28 05:09:14 +00:00
|
|
|
const char *actual_rolename = NULL;
|
2002-05-17 01:19:19 +00:00
|
|
|
char *result;
|
2002-05-06 19:47:30 +00:00
|
|
|
|
2003-06-27 19:08:38 +00:00
|
|
|
if (strspn(value, "x") == NAMEDATALEN &&
|
|
|
|
(value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'))
|
2002-05-17 01:19:19 +00:00
|
|
|
{
|
2004-08-11 21:10:37 +00:00
|
|
|
/* might be a saved userid string */
|
2005-10-15 02:49:52 +00:00
|
|
|
Oid savedoid;
|
2003-02-01 18:31:28 +00:00
|
|
|
char *endptr;
|
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);
|
2003-02-01 18:31:28 +00:00
|
|
|
|
2004-08-11 21:10:37 +00:00
|
|
|
if (endptr != value + NAMEDATALEN + 1 && *endptr == ',')
|
2003-02-01 18:31:28 +00:00
|
|
|
{
|
2004-08-11 21:10:37 +00:00
|
|
|
/* syntactically valid, so break out the data */
|
2005-06-28 05:09:14 +00:00
|
|
|
roleid = savedoid;
|
2003-06-27 19:08:38 +00:00
|
|
|
is_superuser = (value[NAMEDATALEN] == 'T');
|
2005-06-28 05:09:14 +00:00
|
|
|
actual_rolename = endptr + 1;
|
2003-02-01 18:31:28 +00:00
|
|
|
}
|
2002-05-17 01:19:19 +00:00
|
|
|
}
|
2003-02-01 18:31:28 +00:00
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
if (roleid == InvalidOid)
|
2001-10-18 17:30:21 +00:00
|
|
|
{
|
2003-02-01 18:31:28 +00:00
|
|
|
/* not a saved ID, so look it up */
|
2005-06-28 05:09:14 +00:00
|
|
|
HeapTuple roleTup;
|
2000-09-22 15:34:31 +00:00
|
|
|
|
2003-08-04 00:43:34 +00:00
|
|
|
if (!IsTransactionState())
|
2003-06-06 16:25:35 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Can't do catalog lookups, so fail. The upshot of this is that
|
|
|
|
* session_authorization cannot be set in postgresql.conf, which
|
|
|
|
* seems like a good thing anyway.
|
2003-06-06 16:25:35 +00:00
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-14 18:42:19 +00:00
|
|
|
roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(value));
|
2005-06-28 05:09:14 +00:00
|
|
|
if (!HeapTupleIsValid(roleTup))
|
2002-04-21 19:12:46 +00:00
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("role \"%s\" does not exist", value)));
|
2002-05-17 01:19:19 +00:00
|
|
|
return NULL;
|
2002-04-21 19:12:46 +00:00
|
|
|
}
|
2001-10-18 17:30:21 +00:00
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
roleid = HeapTupleGetOid(roleTup);
|
|
|
|
is_superuser = ((Form_pg_authid) GETSTRUCT(roleTup))->rolsuper;
|
|
|
|
actual_rolename = value;
|
2003-08-04 00:43:34 +00:00
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
ReleaseSysCache(roleTup);
|
2001-10-18 17:30:21 +00:00
|
|
|
}
|
2001-03-22 04:01:46 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
if (doit)
|
2005-06-28 05:09:14 +00:00
|
|
|
SetSessionAuthorization(roleid, is_superuser);
|
2002-05-17 01:19:19 +00:00
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
result = (char *) malloc(NAMEDATALEN + 32 + strlen(actual_rolename));
|
2002-05-17 01:19:19 +00:00
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
2003-02-01 18:31:28 +00:00
|
|
|
memset(result, 'x', NAMEDATALEN);
|
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
sprintf(result + NAMEDATALEN, "%c%u,%s",
|
2004-08-11 21:10:37 +00:00
|
|
|
is_superuser ? 'T' : 'F',
|
2005-06-28 05:09:14 +00:00
|
|
|
roleid,
|
|
|
|
actual_rolename);
|
2002-05-17 01:19:19 +00:00
|
|
|
|
|
|
|
return result;
|
2001-03-22 04:01:46 +00:00
|
|
|
}
|
1997-06-20 17:17:03 +00:00
|
|
|
|
2002-05-17 01:19:19 +00:00
|
|
|
const char *
|
|
|
|
show_session_authorization(void)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2003-02-01 18:31:28 +00:00
|
|
|
/*
|
2004-08-11 21:10:37 +00:00
|
|
|
* Extract the user name from the stored string; see
|
2003-02-01 18:31:28 +00:00
|
|
|
* assign_session_authorization
|
|
|
|
*/
|
2004-08-11 21:10:37 +00:00
|
|
|
const char *value = session_authorization_string;
|
2005-10-15 02:49:52 +00:00
|
|
|
Oid savedoid;
|
2004-08-11 21:10:37 +00:00
|
|
|
char *endptr;
|
|
|
|
|
2010-09-23 16:53:16 -04:00
|
|
|
/* If session_authorization hasn't been set in this process, return "" */
|
|
|
|
if (value == NULL || value[0] == '\0')
|
|
|
|
return "";
|
|
|
|
|
2004-08-11 21:10:37 +00:00
|
|
|
Assert(strspn(value, "x") == NAMEDATALEN &&
|
|
|
|
(value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'));
|
|
|
|
|
2005-06-28 05:09:14 +00:00
|
|
|
savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);
|
2004-08-11 21:10:37 +00:00
|
|
|
|
|
|
|
Assert(endptr != value + NAMEDATALEN + 1 && *endptr == ',');
|
|
|
|
|
|
|
|
return endptr + 1;
|
2001-03-22 04:01:46 +00:00
|
|
|
}
|
2005-07-25 22:12:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SET ROLE
|
|
|
|
*
|
|
|
|
* When resetting session auth after an error, we can't expect to do catalog
|
|
|
|
* lookups. Hence, the stored form of the value must provide a numeric oid
|
|
|
|
* that can be re-used directly. We implement this exactly like SET
|
|
|
|
* SESSION AUTHORIZATION.
|
|
|
|
*
|
|
|
|
* The SQL spec requires "SET ROLE NONE" to unset the role, so we hardwire
|
|
|
|
* a translation of "none" to InvalidOid.
|
|
|
|
*/
|
|
|
|
extern char *role_string; /* in guc.c */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
assign_role(const char *value, bool doit, GucSource source)
|
|
|
|
{
|
2005-10-15 02:49:52 +00:00
|
|
|
Oid roleid = InvalidOid;
|
2005-07-25 22:12:34 +00:00
|
|
|
bool is_superuser = false;
|
|
|
|
const char *actual_rolename = value;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (strspn(value, "x") == NAMEDATALEN &&
|
|
|
|
(value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'))
|
|
|
|
{
|
|
|
|
/* might be a saved userid string */
|
2005-10-15 02:49:52 +00:00
|
|
|
Oid savedoid;
|
2005-07-25 22:12:34 +00:00
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);
|
|
|
|
|
|
|
|
if (endptr != value + NAMEDATALEN + 1 && *endptr == ',')
|
|
|
|
{
|
|
|
|
/* syntactically valid, so break out the data */
|
|
|
|
roleid = savedoid;
|
|
|
|
is_superuser = (value[NAMEDATALEN] == 'T');
|
|
|
|
actual_rolename = endptr + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (roleid == InvalidOid &&
|
|
|
|
strcmp(actual_rolename, "none") != 0)
|
|
|
|
{
|
|
|
|
/* not a saved ID, so look it up */
|
|
|
|
HeapTuple roleTup;
|
|
|
|
|
|
|
|
if (!IsTransactionState())
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Can't do catalog lookups, so fail. The upshot of this is that
|
|
|
|
* role cannot be set in postgresql.conf, which seems like a good
|
|
|
|
* thing anyway.
|
2005-07-25 22:12:34 +00:00
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-14 18:42:19 +00:00
|
|
|
roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(value));
|
2005-07-25 22:12:34 +00:00
|
|
|
if (!HeapTupleIsValid(roleTup))
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("role \"%s\" does not exist", value)));
|
2005-07-25 22:12:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
roleid = HeapTupleGetOid(roleTup);
|
|
|
|
is_superuser = ((Form_pg_authid) GETSTRUCT(roleTup))->rolsuper;
|
|
|
|
|
|
|
|
ReleaseSysCache(roleTup);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that session user is allowed to become this role
|
|
|
|
*/
|
|
|
|
if (!is_member_of_role(GetSessionUserId(), roleid))
|
|
|
|
{
|
2007-12-28 00:23:23 +00:00
|
|
|
ereport(GUC_complaint_elevel(source),
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied to set role \"%s\"",
|
|
|
|
value)));
|
2005-07-25 22:12:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doit)
|
|
|
|
SetCurrentRoleId(roleid, is_superuser);
|
|
|
|
|
|
|
|
result = (char *) malloc(NAMEDATALEN + 32 + strlen(actual_rolename));
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(result, 'x', NAMEDATALEN);
|
|
|
|
|
|
|
|
sprintf(result + NAMEDATALEN, "%c%u,%s",
|
|
|
|
is_superuser ? 'T' : 'F',
|
|
|
|
roleid,
|
|
|
|
actual_rolename);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
show_role(void)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Extract the role name from the stored string; see assign_role
|
2005-07-25 22:12:34 +00:00
|
|
|
*/
|
|
|
|
const char *value = role_string;
|
2005-10-15 02:49:52 +00:00
|
|
|
Oid savedoid;
|
2005-07-25 22:12:34 +00:00
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
/* This special case only applies if no SET ROLE has been done */
|
|
|
|
if (value == NULL || strcmp(value, "none") == 0)
|
|
|
|
return "none";
|
|
|
|
|
|
|
|
Assert(strspn(value, "x") == NAMEDATALEN &&
|
|
|
|
(value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'));
|
|
|
|
|
|
|
|
savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);
|
|
|
|
|
|
|
|
Assert(endptr != value + NAMEDATALEN + 1 && *endptr == ',');
|
|
|
|
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Check that the stored string still matches the effective setting, else
|
|
|
|
* return "none". This is a kluge to deal with the fact that SET SESSION
|
|
|
|
* AUTHORIZATION logically resets SET ROLE to NONE, but we cannot set the
|
|
|
|
* GUC role variable from assign_session_authorization (because we haven't
|
|
|
|
* got enough info to call set_config_option).
|
2005-07-25 22:12:34 +00:00
|
|
|
*/
|
|
|
|
if (savedoid != GetCurrentRoleId())
|
|
|
|
return "none";
|
|
|
|
|
|
|
|
return endptr + 1;
|
|
|
|
}
|