automatically to all the backends without need to know their pids.
and it will stop automatically all the backends.
the cache.
mixing of different data types in the same expression.
Postgres has extensive facilities for
evaluating mixed-type expressions.
+
In many cases a user will not need
can affect the apparent results of a query, and these results
can be tailored by a user or programmer
using explicit type coersion.
+
This chapter introduces the
Postgres
type conversion mechanisms and conventions.
Refer to the relevant sections in the User's Guide and Programmer's Guide
for more information on specific data types and allowed functions and operators.
+
The Programmer's Guide has more details on the exact algorithms used for
implicit type conversion and coersion.
+
Overview
Hence, most type conversion behavior in
Postgres
should be governed by general rules rather than by ad-hoc heuristics to allow
mixed-type expressions to be meaningful, even with user-defined types.
+
The
Postgres scanner/parser decodes lexical elements
has two strings, of type text and point.
If a type is not specified, then the placeholder type unknown
is assigned initially, to be resolved in later stages as described below.
+
There are four fundamental
SQL constructs requiring
distinct type conversion rules in the
Postgres
parser:
+
Postgres allows expressions with
left- and right-unary (one argument) operators,
as well as binary (two argument) operators.
+
Much of the
Postgres type system is built around a rich set of
functions. Function calls have one or more arguments which, for any specific query,
must be matched to the functions available in the system catalog.
+
SQL INSERT statements place the results of query into a table. The expressions
in the query must be matched up with, and perhaps converted to, the target columns of the insert.
+
Since all select results from a UNION SELECT statement must appear in a single set of columns, the types
of each SELECT clause must be matched up and converted to a uniform set.
+
There are some heuristics included in the conversion rules to better support
conventions for the
SQL92 standard native types such as
smallint, integer, and float.
+
The
Postgres parser uses the convention that all
by the parser as such. This simple assumption gives the parser the power
to explore type conversion possibilities without hardcoding, allowing
extended user-defined types to use these same features transparently.
+
An additional heuristic is provided in the parser to allow better guesses
expressions (those with multiple candidate parsing solutions)
with only one user-defined type can resolve to a single best choice, while those with
multiple user-defined types will remain ambiguous and throw an error.
+
Ambiguous expressions which have candidate solutions within only one type category are
likely to resolve, while ambiguous expressions with candidates spanning multiple
categories are likely to throw an error and ask for clarification from the user.
+
Guidelines
Implicit conversions should never have suprising or unpredictable outcomes.
+
+
User-defined types, of which the parser has no apriori knowledge, should be
"higher" in the type heirarchy. In mixed-type expressions, native types shall always
be converted to a user-defined type (of course, only if conversion is necessary).
+
+
does not have information available to it on relationships between types, other than
hardcoded heuristics for built-in types and implicit relationships based on available functions
in the catalog.
+
+
That is, if a query is well formulated and the types already match up, then the query should proceed
without spending extra time in the parser and without introducing unnecessary implicit conversion
functions into the query.
+
Additionally, if a query usually requires an implicit conversion for a function, and
if then the user defines an explicit function with the correct argument types, the parser
should use this new function and will no longer do the implicit conversion using the old function.
+
+
+
+
+
Operators
Conversion Procedure
Operator Evaluation
Check for an exact match in the pg_operator system catalog.
+
If one argument of a binary operator is unknown,
then assume it is the same type as the other argument.
-
+
+
Reverse the arguments, and look for an exact match with an operator which
points to itself as being commutative.
If found, then reverse the arguments in the parse tree and use this operator.
-
+
+
+
Look for the best match.
-
+
Make a list of all operators of the same name.
-
+
+
If only one operator is in the list, use it if the input type can be coerced,
and throw an error if the type cannot be coerced.
-
+
+
Keep all operators with the most explicit matches for types. Keep all if there
are no explicit matches and move to the next step.
If only one candidate remains, use it if the type can be coerced.
-
+
+
If any input arguments are "unknown", categorize the input candidates as
the correct choice cannot be deduced without more clues.
If only one category is present, then assign the "preferred type"
to the input column which had been previously "unknown".
-
+
+
Choose the candidate with the most exact type matches, and which matches
the "preferred type" for each column category from the previous step.
If there is still more than one candidate, or if there are none,
then throw an error.
+
+
-
+
+
Examples
This last form has the least overhead, since no functions are called to do
implicit type conversion. This is not an issue for small queries, but may
have an impact on the performance of queries involving large tables.
+
+
+
String Concatenation
A string-like syntax is used for working with string types as well as for
working with complex extended types.
Strings with unspecified type are matched with likely operator candidates.
+
One unspecified argument:
abcdef
(1 row)
+
In this case the parser looks to see if there is an operator taking text
for both arguments. Since there is, it assumes that the second argument should
be interpreted as of type text.
+
Concatenation on unspecified types:
abcdef
(1 row)
+
In this case there is no initial hint for which type to use, since no types
are specified in the query. So, the parser looks for all candidate operators
and finds that all arguments for all the candidates are string types. It chooses
the "preferred type" for strings, text, for this query.
+
If a user defines a new type and defines an operator ||
to work
with it, then this query would no longer succeed as written. The parser would
now have candidate types from two categories, and could not decide which to use.
+
+
Factorial
to be a tool for data manipulation. If a user chooses to take the
factorial of a floating point number,
Postgres
will try to oblige.
+
+
+
+
+
Functions
-
Function Evaluation
Check for an exact match in the pg_proc system catalog.
-
+
Look for the best match.
-
+
Make a list of all functions of the same name with the same number of arguments.
-
+
If only one function is in the list, use it if the input types can be coerced,
and throw an error if the types cannot be coerced.
-
+
Keep all functions with the most explicit matches for types. Keep all if there
are no explicit matches and move to the next step.
If only one candidate remains, use it if the type can be coerced.
-
+
If any input arguments are "unknown", categorize the input candidate arguments as
the correct choice cannot be deduced without more clues.
If only one category is present, then assign the "preferred type"
to the input column which had been previously "unknown".
-
+
Choose the candidate with the most exact type matches, and which matches
the "preferred type" for each column category from the previous step.
If there is still more than one candidate, or if there are none,
then throw an error.
+
-
+
-
Examples
24
(1 row)
+
+
Substring Function
There are two substr functions declared in pg_proc. However,
only one takes two arguments, of types text and int4.
+
If called with a string constant of unspecified type, the type is matched up
34
(1 row)
+
If the string is declared to be of type varchar, as might be the case
34
(1 row)
+
There are some heuristics in the parser to optimize the relationship between the
char, varchar, and text types.
For this case, substr is called directly with the varchar string
rather than inserting an explicit conversion call.
+
34
(1 row)
+
+
+
+
Query Targets
-
Target Evaluation
Check for an exact match with the target.
-
+
Try to coerce the expression directly to the target type if necessary.
+
declared with a length) then try to find a sizing function of the same name
as the type taking two arguments, the first the type name and the second an
integer length.
+
abcd
(1 row)
-
+
+
+
+
UNION Queries
The UNION construct is somewhat different in that it must match up
possibly dissimilar types to become a single result set.
-
+
UNION Evaluation
Check for identical types for all results.
+
Coerce each result from the UNION clauses to match the type of the
first SELECT clause or the target column.
-
+
b
(2 rows)
+
+
Simple UNION
1.2
(2 rows)
+
+
Transposed UNION
3
(3 rows)
-
+
An alternate parser strategy could be to choose the "best" type of the bunch, but
this is more difficult because of the nice recursion technique used in the
3.3
(3 rows)
-
+
+
+
+