From aa731ed8433914641e42f32fec0fcf27f01aab7e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 2 Oct 2005 23:50:16 +0000 Subject: Change nextval and other sequence functions to specify their sequence argument as a 'regclass' value instead of a text string. The frontend conversion of text string to pg_class OID is now encapsulated as an implicitly-invocable coercion from text to regclass. This provides backwards compatibility to the old behavior when the sequence argument is explicitly typed as 'text'. When the argument is just an unadorned literal string, it will be taken as 'regclass', which means that the stored representation will be an OID. This solves longstanding problems with renaming sequences that are referenced in default expressions, as well as new-in-8.1 problems with renaming such sequences' schemas or moving them to another schema. All per recent discussion. Along the way, fix some rather serious problems in dbmirror's support for mirroring sequence operations (int4 vs int8 confusion for instance). --- doc/src/sgml/datatype.sgml | 14 ++++++++- doc/src/sgml/func.sgml | 78 +++++++++++++++++++++++++++++++++++----------- doc/src/sgml/release.sgml | 21 ++++++++++++- 3 files changed, 92 insertions(+), 21 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index 099f5e8ab67..48caaa2994f 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -1,5 +1,5 @@ @@ -3113,6 +3113,18 @@ SELECT * FROM pg_attribute operand. + + An additional property of the OID alias types is that if a + constant of one of these types appears in a stored expression + (such as a column default expression or view), it creates a dependency + on the referenced object. For example, if a column has a default + expression nextval('my_seq'::regclass), + PostgreSQL + understands that the default expression depends on the sequence + my_seq; the system will not let the sequence be dropped + without first removing the default expression. + + Another identifier type used by the system is xid, or transaction (abbreviated xact) identifier. This is the data type of the system columns diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index c6c9d87e8a5..a641db7ee74 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,5 +1,5 @@ @@ -6875,12 +6875,12 @@ SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT - nextval(text) + nextval(regclass) bigint Advance sequence and return new value - currval(text) + currval(regclass) bigint Return value most recently obtained with nextval for specified sequence @@ -6891,12 +6891,12 @@ SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT Return value most recently obtained with nextval - setval(text, bigint) + setval(regclass, bigint) bigint Set sequence's current value - setval(text, bigint, boolean) + setval(regclass, bigint, boolean) bigint Set sequence's current value and is_called flag @@ -6905,11 +6905,15 @@ SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT - For largely historical reasons, the sequence to be operated on by a - sequence-function call is specified by a text-string argument. To + The sequence to be operated on by a sequence-function call is specified by + a regclass argument, which is just the OID of the sequence in the + pg_class system catalog. You do not have to look up the + OID by hand, however, since the regclass datatype's input + converter will do the work for you. Just write the sequence name enclosed + in single quotes, so that it looks like a literal constant. To achieve some compatibility with the handling of ordinary - SQL names, the sequence functions convert their - argument to lowercase unless the string is double-quoted. Thus + SQL names, the string will be converted to lowercase + unless it contains double quotes around the sequence name. Thus nextval('foo') operates on sequence foo nextval('FOO') operates on sequence foo @@ -6921,10 +6925,46 @@ nextval('myschema.foo') operates on myschema.foosame as above nextval('foo') searches search path for foo - Of course, the text argument can be the result of an expression, - not only a simple literal, which is occasionally useful. + See for more information about + regclass. + + + Before PostgreSQL 8.1, the arguments of the + sequence functions were of type text, not regclass, and + the above-described conversion from a text string to an OID value would + happen at runtime during each call. For backwards compatibility, this + facility still exists, but internally it is now handled as an implicit + coercion from text to regclass before the function is + invoked. + + + + When you write the argument of a sequence function as an unadorned + literal string, it becomes a constant of type regclass. + Since this is really just an OID, it will track the originally + identified sequence despite later renaming, schema reassignment, + etc. This early binding behavior is usually desirable for + sequence references in column defaults and views. But sometimes you will + want late binding where the sequence reference is resolved + at runtime. To get late-binding behavior, force the constant to be + stored as a text constant instead of regclass: + +nextval('foo'::text) foo is looked up at runtime + + Note that late binding was the only behavior supported in + PostgreSQL releases before 8.1, so you + may need to do this to preserve the semantics of old applications. + + + + Of course, the argument of a sequence function can be an expression + as well as a constant. If it is a text expression then the implicit + coercion will result in a run-time lookup. + + + The available sequence functions are: @@ -7001,6 +7041,14 @@ SELECT setval('foo', 42, false); Next nextval wi + + If a sequence object has been created with default parameters, + nextval calls on it will return successive values + beginning with 1. Other behaviors can be obtained by using + special parameters in the command; + see its command reference page for more information. + + To avoid blocking of concurrent transactions that obtain numbers from the @@ -7013,14 +7061,6 @@ SELECT setval('foo', 42, false); Next nextval wi - - If a sequence object has been created with default parameters, - nextval calls on it will return successive values - beginning with 1. Other behaviors can be obtained by using - special parameters in the command; - see its command reference page for more information. - - diff --git a/doc/src/sgml/release.sgml b/doc/src/sgml/release.sgml index 092179f01bc..37d67a94797 100644 --- a/doc/src/sgml/release.sgml +++ b/doc/src/sgml/release.sgml @@ -1,5 +1,5 @@