In addition, the
PostgreSQL source code
contains several working examples in the
contrib directory. If you rely on these
- examples you will make your modules dependent on the documentation
+ examples you will make your modules dependent on the availability
of the
PostgreSQL source code, however.
executables: first the source files are compiled into object files,
then the object files are linked together. The object files need to
be created as position-independent code
- (
PIC), which conceptually means that
it can be
- placed at an arbitrary location in memory when it is loaded by the
+ (
PIC), which conceptually means that
they can be
+ placed at an arbitrary location in memory when they are loaded by the
executable. (Object files intended for executables are not compiled
that way.) The command to link a shared library contains special
flags to distinguish it from linking an executable. --- At least
-
+
Functions and Operators
to_char(125, '999')
|
- to_char(float, text)
+ to_char(double precision, text)
text
- convert float4/float8 to string
+ convert real/double precision to string
to_char(125.8, '999D9')
|
|
date_part(text,timestamp)
- float8
+ double precision
portion of date
date_part('dow',timestamp 'now')
|
date_part(text,interval)
- float8
+ double precision
portion of time
date_part('hour',interval '4 hrs 3 mins')
|
+
Translation
- '((0,0),(1,1))'::box + '(2.0,0)'::point
+ box '((0,0),(1,1))' + point '(2.0,0)'
|
-
Translation
- '((0,0),(1,1))'::box - '(2.0,0)'::point
+ box '((0,0),(1,1))' - point '(2.0,0)'
|
*
Scaling/rotation
- '((0,0),(1,1))'::box * '(2.0,0)'::point
+ box '((0,0),(1,1))' * point '(2.0,0)'
|
/
Scaling/rotation
- '((0,0),(2,2))'::box / '(2.0,0)'::point
+ box '((0,0),(2,2))' / point '(2.0,0)'
|
#
|
##
Point of closest proximity
- '(0,0)'::point ## '((2,0),(0,2))'::lseg
+ point '(0,0)' ## lseg '((2,0),(0,2))'
|
&&
Overlaps?
- '((0,0),(1,1))'::box && '((0,0),(2,2))'::box
+ box '((0,0),(1,1))' && box '((0,0),(2,2))'
|
&<
Overlaps to left?
- '((0,0),(1,1))'::box &< '((0,0),(2,2))'::box
+ box '((0,0),(1,1))' &< box '((0,0),(2,2))'
|
&>
Overlaps to right?
- '((0,0),(3,3))'::box &> '((0,0),(2,2))'::box
+ box '((0,0),(3,3))' &> box '((0,0),(2,2))'
|
<->
Distance between
- '((0,0),1)'::circle <-> '((5,0),1)'::circle
+ circle '((0,0),1)' <-> circle '((5,0),1)'
|
<<
Left of?
- '((0,0),1)'::circle << '((5,0),1)'::circle
+ circle '((0,0),1)' << circle '((5,0),1)'
|
<^
Is below?
- '((0,0),1)'::circle <^ '((0,5),1)'::circle
+ circle '((0,0),1)' <^ circle '((0,5),1)'
|
>>
Is right of?
- '((5,0),1)'::circle >> '((0,0),1)'::circle
+ circle '((5,0),1)' >> circle '((0,0),1)'
|
>^
Is above?
- '((0,5),1)'::circle >^ '((0,0),1)'::circle
+ circle '((0,5),1)' >^ circle '((0,0),1)'
|
?#
Intersects or overlaps
- '((-1,0),(1,0))'::lseg ?# '((-2,-2),(2,2))'::box;
+ lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))';
|
?-
Is horizontal?
- '(1,0)'::point ?- '(0,0)'::point
+ point '(1,0)' ?- point '(0,0)'
|
?-|
Is perpendicular?
- '((0,0),(0,1))'::lseg ?-| '((0,0),(1,0))'::lseg
+ lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'
|
@-@
Length or circumference
- @-@ '((0,0),(1,0))'::path
+ @-@ path '((0,0),(1,0))'
|
?|
Is vertical?
- '(0,1)'::point ?| '(0,0)'::point
+ point '(0,1)' ?| point '(0,0)'
|
?||
Is parallel?
- '((-1,0),(1,0))'::lseg ?|| '((-1,2),(1,2))'::lseg
+ lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'
|
@
Contained or on
- '(1,1)'::point @ '((0,0),2)'::circle
+ point '(1,1)' @ circle '((0,0),2)'
|
@@
Center of
- @@ '((0,0),10)'::circle
+ @@ circle '((0,0),10)'
|
~=
Same as
- '((0,0),(1,1))'::polygon ~= '((1,1),(0,0))'::polygon
+ polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'
|
area(object)
- float8
+ double precision
area of item
area(box '((0,0),(1,1))')
|
- box(box,box)
+ box(box, box)
box
intersection box
box(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')
|
diameter(circle)
- float8
+ double precision
diameter of circle
diameter(circle '((0,0),2.0)')
|
height(box)
- float8
+ double precision
vertical size of box
height(box '((0,0),(1,1))')
|
length(object)
- float8
+ double precision
length of item
length(path '((-1,0),(1,0))')
|
radius(circle)
- float8
+ double precision
radius of circle
radius(circle '((0,0),2.0)')
|
width(box)
- float8
+ double precision
horizontal size
width(box '((0,0),(1,1))')
box(circle)
box
circle to box
- box('((0,0),2.0)'::circle)
+ box(circle '((0,0),2.0)')
|
- box(point,point)
+ box(point, point)
box
points to box
- box('(0,0)'::point,'(1,1)'::point)
+ box(point '(0,0)', point '(1,1)')
|
box(polygon)
box
polygon to box
- box('((0,0),(1,1),(2,0))'::polygon)
+ box(polygon '((0,0),(1,1),(2,0))')
|
circle(box)
circle
to circle
- circle('((0,0),(1,1))'::box)
+ circle(box '((0,0),(1,1))')
|
- circle(point,float8)
+ circle(point, double precision)
circle
point to circle
- circle('(0,0)'::point,2.0)
+ circle(point '(0,0)', 2.0)
|
lseg(box)
lseg
box diagonal to lseg
- lseg('((-1,0),(1,0))'::box)
+ lseg(box '((-1,0),(1,0))')
|
- lseg(point,point)
+ lseg(point, point)
lseg
points to lseg
- lseg('(-1,0)'::point,'(1,0)'::point)
+ lseg(point '(-1,0)', point '(1,0)')
|
path(polygon)
point
polygon to path
- path('((0,0),(1,1),(2,0))'::polygon)
+ path(polygon '((0,0),(1,1),(2,0))')
|
point(circle)
point
center
- point('((0,0),2.0)'::circle)
+ point(circle '((0,0),2.0)')
|
- point(lseg,lseg)
+ point(lseg, lseg)
point
intersection
- point('((-1,0),(1,0))'::lseg, '((-2,-2),(2,2))'::lseg)
+ point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')
|
point(polygon)
point
center
- point('((0,0),(1,1),(2,0))'::polygon)
+ point(polygon '((0,0),(1,1),(2,0))')
|
polygon(box)
polygon
12 point polygon
- polygon('((0,0),(1,1))'::box)
+ polygon(box '((0,0),(1,1))')
|
polygon(circle)
polygon
12-point polygon
- polygon('((0,0),2.0)'::circle)
+ polygon(circle '((0,0),2.0)')
|
- polygon(npts,circle)
+ polygon(npts, circle)
polygon
npts polygon
- polygon(12,'((0,0),2.0)'::circle)
+ polygon(12, circle '((0,0),2.0)')
|
polygon(path)
polygon
path to polygon
- polygon('((0,0),(1,1),(2,0))'::path)
+ polygon(path '((0,0),(1,1),(2,0))')
PgDatabase data;
-data.Exec("create table foo (a int4, b char(16), d float8)");
+data.Exec("create table foo (a int4, b char(16), d double precision)");
data.Exec("copy foo from stdin");
data.PutLine("3\tHello World\t4.5\n");
data.PutLine("4\tGoodbye World\t7.11\n");
As an example:
-PQexec(conn, "create table foo (a int4, b char(16), d float8)");
+PQexec(conn, "create table foo (a int4, b char(16), d double precision)");
PQexec(conn, "copy foo from stdin");
PQputline(conn, "3\thello world\t4.5\n");
PQputline(conn,"4\tgoodbye world\t7.11\n");
*
* populate a database by doing the following:
*
- * CREATE TABLE test1 (i int4, d float4, p polygon);
+ * CREATE TABLE test1 (i int4, d real, p polygon);
*
- * INSERT INTO test1 values (1, 3.567, '(3.0, 4.0, 1.0,
- * 2.0)'::polygon);
+ * INSERT INTO test1 values (1, 3.567, polygon '(3.0, 4.0, 1.0, 2.0)');
*
- * INSERT INTO test1 values (2, 89.05, '(4.0, 3.0, 2.0,
- * 1.0)'::polygon);
+ * INSERT INTO test1 values (2, 89.05, polygon '(4.0, 3.0, 2.0, 1.0)');
*
* the expected output is:
*
must also have a default value specified.
- The default value is evaluated ever time the function is called. So
+ The default value is evaluated every time the function is called. So
assigning 'now' to a variable of type
- <replaceable>datetimee> causes the variable to have the
+ <type>timestampe> causes the variable to have the
time of the actual function call, not when the function was
precompiled into its bytecode.
All expressions used in PL/pgSQL statements are processed using
the backend's executor. Expressions that appear to contain
constants may in fact require run-time evaluation (e.g. 'now' for the
- datetime type) so
+ timestamp type) so
it is impossible for the PL/pgSQL parser
to identify real constant values other than the NULL keyword. All
expressions are evaluated internally by executing a query
is a difference between what the two functions
-CREATE FUNCTION logfunc1 (text) RETURNS datetime AS '
+CREATE FUNCTION logfunc1 (text) RETURNS timestamp AS '
DECLARE
logtxt ALIAS FOR $1;
BEGIN
and
-CREATE FUNCTION logfunc2 (text) RETURNS datetime AS '
+CREATE FUNCTION logfunc2 (text) RETURNS timestamp AS '
DECLARE
logtxt ALIAS FOR $1;
- curtime datetime;
+ curtime timestamp;
BEGIN
curtime := ''now'';
INSERT INTO logtable VALUES (logtxt, curtime);
do. In the case of logfunc1(), the
Postgres
main parser
knows when preparing the plan for the INSERT, that the string 'now'
- should be interpreted as datetime because the target field of logtable
+ should be interpreted as timestamp because the target field of logtable
is of that type. Thus, it will make a constant from it at this time
and this constant value is then used in all invocations of logfunc1()
during the lifetime of the backend. Needless to say that this isn't what the
what type 'now' should become and therefore it returns a data type of
text containing the string 'now'. During the assignment
to the local variable curtime, the PL/pgSQL interpreter casts this
- string to the datetime type by calling the text_out() and datetime_in()
+ string to the timestamp type by calling the text_out() and timestamp_in()
functions for the conversion.
counterparts from the C language function discussion.
-CREATE FUNCTION add_one (int4) RETURNS int4 AS '
+CREATE FUNCTION add_one (integer) RETURNS integer AS '
BEGIN
RETURN $1 + 1;
END;
The C functions.
-CREATE FUNCTION c_overpaid (EMP, int4) RETURNS bool AS '
+CREATE FUNCTION c_overpaid (EMP, integer) RETURNS bool AS '
DECLARE
emprec ALIAS FOR $1;
sallim ALIAS FOR $2;
CREATE TABLE emp (
empname text,
- salary int4,
- last_date datetime,
- last_user name);
+ salary integer,
+ last_date timestamp,
+ last_user text);
CREATE FUNCTION emp_stamp () RETURNS OPAQUE AS '
BEGIN
-- Remember who changed the payroll when
NEW.last_date := ''now'';
- NEW.last_user := getpgusername();
+ NEW.last_user := current_user;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
COMMENT ON TYPE my_type IS 'Complex Number support';
COMMENT ON VIEW my_view IS 'View of departmental costs';
COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
-COMMENT ON AGGREGATE my_aggregate float8 IS 'Computes sample variance';
-COMMENT ON FUNCTION my_function (datetime) IS 'Returns Roman Numeral';
+COMMENT ON AGGREGATE my_aggregate (double precision) IS 'Computes sample variance';
+COMMENT ON FUNCTION my_function (timestamp) IS 'Returns Roman Numeral';
COMMENT ON OPERATOR ^ (text, text) IS 'Performs intersection of two text';
COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
CREATE AGGREGATE
allows a user or programmer to extend
Postgres
functionality by defining new aggregate functions. Some aggregate functions
- for base types such as min(int4)
- and avg(float8) are already provided in the base
+ for base types such as min(integer)
+ and avg(double precision) are already provided in the base
distribution. If one defines new types or needs an aggregate function not
already provided, then CREATE AGGREGATE
can be used to provide the desired features.
CREATE TABLE shoelace_log (
sl_name char(10), -- shoelace changed
sl_avail integer, -- new available value
- log_who name, -- who did it
- log_when datetime -- when
+ log_who text, -- who did it
+ log_when timestamp -- when
);
CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
DO INSERT INTO shoelace_log VALUES (
NEW.sl_name,
NEW.sl_avail,
- getpgusername(),
- 'now'::text
+ current_user,
+ current_timestamp
);
-
- One interesting detail is the casting of 'now' in the rules
- INSERT action to type text. Without that, the parser would see
- at CREATE RULE time, that the target type in shoelace_log
- is a datetime and tries to make a constant from it - with success.
- So a constant datetime value would be stored in the rule action
- and all log entries would have the time of the CREATE RULE statement.
- Not exactly what we want. The casting causes the parser to
- construct a datetime('now'::text) expression and this will be
- evaluated when the rule is executed. (Another way to do this is to
- use the function now() instead of a literal constant.)
INSERT INTO shoelace_log VALUES(
*NEW*.sl_name, *NEW*.sl_avail,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data *NEW*, shoelace_data *OLD*;
INSERT INTO shoelace_log VALUES(
*NEW*.sl_name, *NEW*.sl_avail,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data *NEW*, shoelace_data *OLD*,
shoelace_data shoelace_data;
INSERT INTO shoelace_log VALUES(
*NEW*.sl_name, *NEW*.sl_avail,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data *NEW*, shoelace_data *OLD*,
shoelace_data shoelace_data
WHERE int4ne(*NEW*.sl_avail, *OLD*.sl_avail);
INSERT INTO shoelace_log VALUES(
*NEW*.sl_name, *NEW*.sl_avail,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data *NEW*, shoelace_data *OLD*,
shoelace_data shoelace_data
WHERE int4ne(*NEW*.sl_avail, *OLD*.sl_avail)
INSERT INTO shoelace_log VALUES(
shoelace_data.sl_name, 6,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data *NEW*, shoelace_data *OLD*,
shoelace_data shoelace_data
WHERE int4ne(6, *OLD*.sl_avail)
INSERT INTO shoelace_log VALUES(
shoelace_data.sl_name, 6,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data *NEW*, shoelace_data *OLD*,
shoelace_data shoelace_data
WHERE int4ne(6, shoelace_data.sl_avail)
INSERT INTO shoelace_log VALUES(
shoelace_data.sl_name, 6,
- getpgusername(), datetime('now'::text))
+ current_user, current_timestamp
FROM shoelace_data
WHERE 6 != shoelace_data.sl_avail
AND shoelace_data.sl_name = 'sl7';
INSERT INTO shoelace_log VALUES(
shoelace_data.sl_name, shoelace_data.sl_avail,
- getpgusername(), 'now')
+ current_user, current_timestamp)
FROM shoelace_data
WHERE shoelace_data.sl_avail != shoelace_data.sl_avail
AND shoelace_data.sl_name = 'sl7';
INSERT INTO shoelace_log SELECT
shoelace_data.sl_name, 0,
- getpgusername(), 'now'
+ current_user, current_timestamp
FROM shoelace_data
WHERE 0 != shoelace_data.sl_avail
AND shoelace_data.sl_color = 'black';
INSERT INTO shoelace_log SELECT
s.sl_name,
int4pl(s.sl_avail, shoelace_arrive.arr_quant),
- getpgusername(),
- datetime('now'::text)
+ current_user,
+ current_timestamp
FROM shoelace_arrive shoelace_arrive, shoelace_ok shoelace_ok,
shoelace_ok *OLD*, shoelace_ok *NEW*,
shoelace shoelace, shoelace *OLD*,
INSERT INTO shoelace_log SELECT
s.sl_name,
s.sl_avail + shoelace_arrive.arr_quant,
- getpgusername(),
- 'now'
+ current_user,
+ current_timestamp
FROM shoelace_arrive shoelace_arrive, shoelace_data shoelace_data,
shoelace_data s
WHERE s.sl_name = shoelace_arrive.arr_name
There is only one exponentiation
-operator defined in the catalog, and it takes float8 arguments.
+operator defined in the catalog, and it takes arguments of type
+double precision.
The scanner assigns an initial type of int4 to both arguments
of this query expression:
is equivalent to
-tgl=> select float8(2) ^ float8(3) AS "Exp";
+tgl=> select CAST(2 AS double precision) ^ CAST(3 AS double precision) AS "Exp";
Exp
-----
8
-tgl=> SELECT 1.2 AS "Float8" UNION SELECT 1;
- Float8
+tgl=> SELECT 1.2 AS "Double" UNION SELECT 1;
+ Double
--------
1
1.2
tgl=> SELECT 1 AS "All integers"
-tgl-> UNION SELECT '2.2'::float4;
+tgl-> UNION SELECT CAST('2.2' AS REAL);
All integers
--------------
1
-Since float4 is not a preferred type, the parser sees no reason to select it
-over int4, and instead falls back on the use-the-first-alternative rule.
+Since REAL is not a preferred type, the parser sees no reason
+to select it over INTEGER (which is what the 1 is), and instead
+falls back on the use-the-first-alternative rule.
This example demonstrates that the preferred-type mechanism doesn't encode
as much information as we'd like. Future versions of
Postgres may support a more general notion of
SELECT name, double_salary(EMP) AS dream
FROM EMP
- WHERE EMP.cubicle ~= '(2,1)'::point;
+ WHERE EMP.cubicle ~= point '(2,1)';
+-----+-------+
CREATE FUNCTION new_emp()
RETURNS EMP
- AS 'SELECT \'None\'::text AS name,
+ AS 'SELECT text ''None'' AS name,
1000 AS salary,
25 AS age,
- \'(2,2)\'::point AS cubicle'
+ point ''(2,2)'' AS cubicle'
LANGUAGE 'sql';
- You must typecast the expressions (using ::) to match the
+ You must typecast the expressions to match the
composite type's definition, or you will get errors like this:
- Compiling and loading your object code so that
+ Compiling and linking your object code so that
it can be dynamically loaded into
always requires special flags.