Documentos de Académico
Documentos de Profesional
Documentos de Cultura
03 Funciones
03 Funciones
Introduccin, Ejemplos
lvaro Herrera
13 de noviembre de 2004
lvaro Herrera
Contenidos
Lenguajes
Tipos de funciones
Creacin de funciones
I
I
tradicionales
SRFs
Indices Funcionales
lvaro Herrera
I
I
SQL es declarativo
se necesita poder expresivo distinto
lvaro Herrera
Los lenguajes
Hackers:
I
lvaro Herrera
Lenguajes: SQL
lvaro Herrera
Lenguajes: PL/pgSQL
I
I
PL por excelencia
Procedural
I
I
I
Fcil de usar
portar desde Oracle PL/SQL
sintaxis simple y apropiada
No particularmente veloz
I
lvaro Herrera
Lenguajes: C
Excelente rendimiento
Tiene acceso a todo
I
I
No es confiable (trusted)
lvaro Herrera
Lenguajes: scripting
Tcl
Perl
Python
PHP
otros
lvaro Herrera
Lenguajes: PL/R
Mencin especial
I
brillantemente mantenido
Propsito especfico
I
manejo estadstico
lvaro Herrera
Lenguajes: conclusiones
I
I
lvaro Herrera
Tipos de funciones
Especiales
I
I
Funciones normales
SRF (set-returning function)
Agregacin
trigger
language_handler
security definer
security invoker
lvaro Herrera
Funciones Tradicionales
I
I
escalar o tupla
tabla temporal
lvaro Herrera
lvaro Herrera
IMMUTABLE
I
I
STABLE
I
VOLATILE
I
I
lvaro Herrera
I
I
STRICT
RETURNS NULL ON NULL INPUT
I
lvaro Herrera
CREATE OPERATOR @+
(PROCEDURE = suma,
LEFTARG = INTEGER,
RIGHTARG = INTEGER);
SELECT 1 @+ 2 @+ NULL @+ 4;
?column?
---------7
(1 fila)
lvaro Herrera
lvaro Herrera
#include "postgres.h"
#include "fmgr.h"
PG_FUNCTION_INFO_V1(digitoVer);
Datum digitoVer(PG_FUNCTION_ARGS) {
int rut = PG_GETARG_INT32(0);
text *ret;
int M=0, S=1;
for (; rut; rut = rut / 10)
S = (S + rut % 10 * (9 - M++ % 6)) % 11;
ret = (text *) palloc(VARHDRSZ + 1);
VARATT_SIZEP(ret) = 5;
sprintf(VARDATA(ret), "%c", S ? S + 0 - 1 : K
PG_RETURN_TEXT_P(ret);
}
lvaro Herrera
lvaro Herrera
rut INTEGER,
dv TEXT CHECK (dv = digito_verificador(rut)),
);
lvaro Herrera
I
I
hacer JOIN
I
I
I
lvaro Herrera
SELECT * FROM
una_srf(arg1, arg2),
otra_srf(arg1, arg2)
WHERE una_srf.uno = otra_srf.uno
SELECT * FROM
una_srf(arg1, )
JOIN otra_srf( ) USING (columna)
JOIN una_tabla ON (condicion )
lvaro Herrera
SELECT * FROM
una_tabla,
una_srf(),
(SELECT tres, cuatro
FROM otra_srf()
WHERE )
WHERE
lvaro Herrera
I
I
I
un tipo
una tabla
una definicin annima
lvaro Herrera
lvaro Herrera
RETURN NEXT
END LOOP;
RETURN;
lvaro Herrera
DECLARE
inicio
valor
final
paso
ret
ALIAS FOR
TIMESTAMP
ALIAS FOR
ALIAS FOR
RECORD;
$1;
WITH TIME ZONE;
$2;
$3;
lvaro Herrera
BEGIN
valor := inicio;
LOOP
IF valor >= final THEN
RETURN;
END IF;
SELECT INTO ret valor, valor + paso;
RETURN NEXT ret;
valor := (valor + paso);
END LOOP;
END;
lvaro Herrera
fin
2003-01-01 03:00:00-03
2003-01-01 06:00:00-03
2003-01-01 09:00:00-03
2003-01-01 12:00:00-03
2003-01-01 15:00:00-03
2003-01-01 18:00:00-03
2003-01-01 21:00:00-03
2003-01-02 00:00:00-03
lvaro Herrera
lvaro Herrera
Privilegios
Funciones: EXECUTE
Lenguajes: USAGE
SECURITY INVOKER
I
SECURITY DEFINER
I
equivalente a setuid
lvaro Herrera
Ejemplo 7: Privilegios
lvaro Herrera
ndices Funcionales
lvaro Herrera
lvaro Herrera
Referencias:
http://www.postgresql.org
http://techdocs.postgresql.org
http://www.postgresql.cl
http://www.varlena.com/GeneralBits
http://archives.postgresql.org/pgsql-es-ayuda
lvaro Herrera
Preguntas?
lvaro Herrera