SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Downloaden Sie, um offline zu lesen
Greenplum Fundamentals
Module 1



            Greenplum
    Concepts, Features & Benefits



                 Greenplum Confidential
2
Module 4


     Joining Tables
    Types & Methods




          Greenplum Confidential
3
Join Types




      Inner Join        Left Outer Join            Right Outer Join




      Full Outer Join     Cross Join




                          Greenplum Confidential
4
Inner Join
    The syntax for an INNER JOIN is simple!
    Also known as a SIMPLE JOIN or EQUI-JOIN.

    SELECT
               EMP.EMPLOYEE_ID
             , EMP.EMPLOYEE_NAME
             , DPT.DEPT_NAME
    FROM
               EMPLOYEES   EMP
             , DEPARTMENTS DPT
    WHERE
              EMP.DPT_ID = DPT.DPT_ID;

    This query only retrieves rows based on the following join condition:
    a corresponding row must exist in each table,
    thus the term ―INNER JOIN‖.




                                         Greenplum Confidential
5
Left OUTER Join
    LEFT OUTER JOIN returns all rows from the ―left‖ table, and only those rows from
    the ―right‖ table that match the left one.

    This is handy when there may be missing values in the ―left‖ table, but you want
    to get a description or other information from the ―right‖ table.

    SELECT
             t.transid
             , c.custname
    FROM
             facts.transaction t
             LEFT OUTER JOIN dimensions.customer c
             ON c.customerid = t.customerid;

    You may also abbreviate LEFT OUTER JOIN to LEFT JOIN.
    …
    FROM
            facts.transaction t
            LEFT JOIN dimensions.customer c
            ON c.customerid = t.customerid;



                                         Greenplum Confidential
6
Right OUTER Join
    RIGHT OUTER JOIN is the inverse of the LEFT OUTER JOIN.

    There is no difference in the functionality of these two. Just be careful when
    designating the left and right tables!

    SELECT
             t.transid
             , c.custname
    FROM
             dimensions.customer c
             RIGHT OUTER JOIN facts.transaction t
             ON c.customerid = t.customerid;


    You may also abbreviate RIGHT OUTER JOIN to RIGHT JOIN.
    …
    FROM dimensions.customer c
            RIGHT JOIN facts.transaction t
            ON c.customerid = t.customerid;




                                          Greenplum Confidential
7
Full OUTER Join
    This interesting join is used when you want to retrieve every row, whether there is
    a match or not based on the join criteria! When would you use this? An example
    is detecting ―true changes‖ for a table.

    SELECT
                 COALESCE( ORIG.transid, STG.transid) AS transid
               , CASE
                  WHEN ORIG.transid IS NULL AND STG.transid IS NOT NULL THEN 'INSERT'
                   WHEN ORIG.transid IS NOT NULL AND STG.transid IS NOT NULL THEN
    'UPDATE'
                  WHEN ORIG.transid IS NOT NULL AND STG.transid IS NULL THEN 'DELETE‘
                 ELSE 'UNKNOWN‘
                END AS Process_type
    FROM
               facts.transaction                             ORIG
               FULL OUTER JOIN staging.transaction_w         STG
               ON STG.transid = ORIG.transid;


    The COALESCE function insures that we have a transaction id
    to work with in subsequent processing.
                                                                            ORIG   STG




                                            Greenplum Confidential
8
CROSS JOIN (Cartesian Product)
    We see these joins, generally, when they are unintentionally created!

    •   Every row in the ―left‖ table is joined with every row in the ―right‖
        table!
    •   So, if you have a billion row table and CROSS JOIN it to a 100 row
        table, your answer set will have 100 billion rows!
    •   There are uses for deliberate CROSS JOINs and will be shown later.

    SELECT
         t.transid
       , t.transdate
       , COALESCE( c.custname, ‘Unknown Customer‘) AS custname
    FROM
         facts.transaction t
       , dimensions.customer c;


    Q: What is wrong with this query?
       Why will it perform a CROSS JOIN?




                                           Greenplum Confidential
9
Module 5


     PostgreSQL Database
          Essentials




             Greenplum Confidential
10
PostgreSQL Schemas – the Search Path
       PostgreSQL will search for tables that are named without
     qualification (schema name not included) by use of the
     search_path variable.


       You can change the search_path parameter using SET
     command:
            SET search_path TO myschema,public;


            SHOW search_path;
            search_path
            --------------
            myschema,public


                                  Greenplum Confidential
11
PostgreSQL – Roles
     • Database roles are conceptually completely separate from
       operating system users.

     • Database roles are global across all databases on the same
       cluster installation.

     • Database user is a role with a LOGIN privilege.

     • It is frequently convenient to group users together to ease
       management of privileges. That way, privileges can be granted
       to, or revoked from, one group instead of may users separately.




                                  Greenplum Confidential
12
PostgreSQL – Roles and Privileges
     • When an object is created, it is assigned an owner. The default
       owner is the role that executed the creation statement.
     • For most kinds of objects, the initial state is that only the owner (or
       a superuser) can do anything with it.
     • To allow other roles to use it, privileges must be granted.
     • There are several different kinds of privileges:
        SELECT       Read only, unable to create new data or modify
        INSERT       Permission to add rows to the object
        UPDATE       Permission to make changes to existing data
        DELETE       Permission to remove data from the object
        CREATE       Permission to create objects
        CONNECT      Permission to connect (applies to a database)
        EXECUTE      Permission to execute functions / procedures
        USAGE        Permission to use objects (applies to a schema)

                                     Greenplum Confidential
13
PostgreSQL – Users
     • In PostgreSQL a user is a role with login privileges.
     • Privileges on database objects should be granted to a group role
       rather than a user role.
     • Users may have special (system) privileges not associated with
       database objects.
       SUPERUSER
       PASSWORD
       CREATEDB
       CREATEROLE


     Tip: It is good practice to create a role that has the CREATEDB and CREATEROLE
     privileges, but is not a superuser, and then use this role for all routine management
     of databases and roles. This approach avoids the dangers of operating as a
     superuser for tasks that do not really require it.
                                           Greenplum Confidential
14
PostgreSQL Table Basics - Constraints
     • PRIMARY KEY constraint insures row uniqueness.

       A primary key indicates that a column or group of columns can be used as
       a unique identifier for rows in the table.

       Technically, a primary key constraint is simply a combination of a UNIQUE
       constraint and a NOT NULL constraint.

     • FOREIGN KEY constraint specifies that values in a column (or a
       group of columns) of a given table must match the values
       appearing in another table. * NOT SUPPORTED IN GREENPLUM




                                        Greenplum Confidential
15
PostgreSQL DDL – CREATE TABLE
     • Supports standard ANSI CREATE TABLE syntax:

     CREATE TABLE dimensions.store
     (
       storeId     SMALLINT    NOT NULL,
       storeName   VARCHAR(40) NOT NULL,
       address     VARCHAR(50) NOT NULL,
       city        VARCHAR(40) NOT NULL,
       state       CHAR(2)     NOT NULL,
       zipcode     CHAR(8)     NOT NULL
     );



                              Greenplum Confidential
16
PostgreSQL DDL – MODIFY TABLE
     • You can modify certain attributes of a table:
         Rename columns
         Rename tables
         Add/Remove columns
           - ALTER TABLE product   ADD COLUMN description text;

         Add/Remove constraints
           - ALTER TABLE product ALTER       COLUMN prod_no SET NOT NULL;

         Change default values
           - ALTER TABLE product   ALTER COLUMN prod_no SET DEFAULT -999;

         Change column data types
           - ALTER TABLE products ALTER         COLUMN price TYPE numeric(10,2);



                                     Greenplum Confidential
17
PostgreSQL DDL – DROP TABLE
     • Table can be removed from the database with the DROP
       TABLE statement:

       DROP TABLE dimensions.customer_old;


     • If the table you are dropping does not exists, you can
       avoid error messages if you add ―IF EXISTS‖ to the
       DROP TABLE statement:


       DROP TABLE IF EXISTS dimensions.customer_old;


                               Greenplum Confidential
18
PostgreSQL Column Basics - Constraints
     • A column can be assigned a DEFAULT value.

     • CHECK constraint is the most generic constraint type. It allows
       you to specify that the value in a certain column must satisfy a
       Boolean expression. EXAMPLE: The column price must be greater than zero:
              price numeric CHECK (price > 0),

     • NOT NULL constraint simply specifies that a column must not
       assume the null value. EXAMPLE: The column transactionid may not be NULL:
              transactionid NOT NULL,

     • UNIQUE constraints ensure that the data contained in a column or
       a group of columns is unique with respect to all the rows in the
       table.


                                       Greenplum Confidential
19
PostgreSQL Indexes
     • PostgreSQL provides several index types:

         B-tree
         Hash
         GiST (Generalized Search Tree)
         GIN (Generalized Inverted Index)

     • Each index type uses a different algorithm that is best suited
       to different types of queries.

     • By default, CREATE INDEX command creates a B-tree index.




                                  Greenplum Confidential
20
Constants in PostgreSQL
     • There are three kinds of implicitly-typed constants in
       PostgreSQL:
          Strings
          Bit strings
          Numbers


     • Constants can also be specified with explicit types, which can
       enable more accurate representation and more efficient
       handling by the system.




                                   Greenplum Confidential
21
Casting Data Types in PostgreSQL
     • A constant of an arbitrary type can be entered using any one of the
       following explicit notations:
             type ’string ’                          REAL ‘2.117902’
             ’string ’::type           ‘2.117902’::REAL
             CAST ( ’string ’ AS type ) CAST(‘2.117902’ AS REAL)
        The result is a constant of the indicated type.

     • If there is no ambiguity the explicit type cast can be omitted (for
       example, when constant it is assigned directly to a table column), in
       which case it is automatically converted to the column type.

     • The string constant can be written using either regular SQL notation or
       dollar-quoting.



                                        Greenplum Confidential
22
PostgreSQL Comments
     • Standard SQL Comments
     -- This is a comment


     • “C” style Comments
     /* This is a comment block
         that ends with the */




                         Greenplum Confidential
23
PostgreSQL Data Types




                     Greenplum Confidential
24
PostgreSQL Data Types




                     Greenplum Confidential
25
PostgreSQL Data Types




                     Greenplum Confidential
26
PostgreSQL Functions & Operators

     • PostgreSQL has a wide range of built-in functions and
       operators for all data types.


     • Users can define custom operators and functions.
       Use caution, new operators or functions may adversely effect
       Greenplum MPP functionality.




                                      Greenplum Confidential
27
Built-In Functions (select function)
     CURRENT_DATE returns the current system date
                            Example: 2006-11-06
     CURRENT_TIME returns the current system time
                             Example: 16:50:54
     CURRENT_TIMESTAMP returns the current system date and time
                           Example: 2008-01-06 16:51:44.430000+00:00
     LOCALTIME returns the current system time with time zone adjustment
                               Example: 19:50:54
     LOCALTIMESTAMP returns the current system date and time with time
     zone adjustment
                             Example: 2008-01-06 19:51:44.430000+00:00
     CURRENT_ROLE or USER returns the current database user
                           Example: jdoe


                                    Greenplum Confidential
28
PostgreSQL Comparison Operators
     The usual comparison operators are available:

             Operator                       Description
             =                              Equal to
             != OR <>                       NOT Equal to
             >                              Greater than
             >=                             Greater than or equal to
             <                              Less than
             <=                             Less than or equal to
             x BETWEEN y AND z              Short hand for
                                            x >= y and x <= z
             x IS NULL                      True if x has NO VALUE
             ‗abc‘ LIKE ‗%abcde%‘           Pattern Matching
             POSIX Comparisons              POSIX Pattern Matching
                                    Greenplum Confidential
29
PostgreSQL Mathematical Functions
     Commonly used Mathematical functions:

      Function    Returns Description                         Example   Results
      + - * /     same      Add, Subtract,                    1 + 1     2
                            Multiply & Divide
      %           Integer   Modulo                            10%2      0

      ^           Same      Exponentiation                    2^2       4

      |/          Numeric   Square Root                       |/9       3

      ||/         Numeric   Cube Root                         ||/8      2

      !           Numeric   Factorial                         !3        6

      & | # ~     Numeric   Bitwise                           91 & 15   11
                            And, Or, XOR, Not
      << >>       Numeric   Bitwise                           1 << 4    16
                            Shift left, right                 8 >> 2    2




                                     Greenplum Confidential
30
PostgreSQL Mathematical Functions
     Handy Mathematical functions:

      Function    Returns Description                        Example         Results
      Abs         same      Absolute Value                   Abs(-998.2)     998.2

      Ceiling     Numeric   Returns smallest                 Ceiling(48.2)   49
      (numeric)             integer not less
                            than argument
      Floor       Numeric   Returns largest                  Floor(48.2)     48
      (numeric)             integer not
                            greater than
                            argument

      Pi()        Numeric   The π constant                   Pi()            3.1419…

      Random()    Numeric   Random value                     Random()        .87663
                            between 0.0 and
                            1.0
      Round()     Numeric   Round to nearest                 Round(22.7)     23
                            integer


                                    Greenplum Confidential
31
PostgreSQL String Functions
     Commonly used string functions:

      Function        Returns Description                        Example            Results
      String ||       Text      String                           ‘my’ || ‘my’       ‘mymy’
      String                    concatenation
      Char_length     Integer   number of chars in               Char_length(‘mym   4
      (string)                  string                           y’)
      Position        Integer   Location of                      Position(‘my’ in   3
      (string in                specified sub-                   ‘ohmy’)
      string)                   string
      Lower(string)   Text      Converts to lower                Lower(‘MYMY’)      ‘mymy’
                                case
      Upper(string)   Text      Converts to upper                Upper(‘mymy’)      ‘MYMY’
                                case
      Substring       Text      Displays portion                 Substring(‘myohm   ‘oh’
      (string from              of string                        y’ from 3 for 2)
      n for n)
      Trim(both,lea   Text      Remove leading                   Trim(‘    mymy     ‘mymy’
      ding,trailing             and/or trailing                  ‘)
      from string)              characters

                                        Greenplum Confidential
32
PostgreSQL String Functions
     Handy string functions:

      Function      Returns Description                        Example         Results
      Initcap       Text      Changes case                     Initcap         ‘My My’
      (string)                                                 (‘my my’)
      Length        Integer   Returns string                   Length          4
      (string)                length                           (‘mymy’)
      Split_part    Text      Separates                        Split_part(‘o   ‘two’
      (string,                delimited list                   ne|two|three’
      delimiter,                                               ,’|’,2)
      occurrence)




                                      Greenplum Confidential
33
PostgreSQL Date Functions
     Commonly used date functions:
      Function      Returns Description                     Example          Results
      Age           Timesta Difference in                   Age(‘2008-08-    0 years 1
      (timestam,t   mp      years, months                   12’ timestamp,   month 11
      imestamp)             and days                        current_timest   days
                                                            amp)

      Extract       Integer Returns year,                   Extract( day     11
      (field from           month, day,                     from
      timestamp)            hour, minute or                 current_date)
                            second

      Now()         Timesta Returns current                 Now()            2008-09-22
                    mp      date & time                                      11:00:01

      Overlaps      Boolean Simplifies                      WHERE            TRUE
                            comparing date                  (‘2008-01-
                            ranges                          01’,’2008-02-
                                                            11’) overlaps
                                                            (‘2008-02-
                                                            01’,’2008-09-
                                                            11’)



                                   Greenplum Confidential
34
Module 6


     Greenplum SQL




          Greenplum Confidential
35
Set Operations – EXCEPT – Example
                Returns all rows from the first SELECT statement
                except for those also returned by the second SELECT.

     SELECT     t.transid
               c.custname
     FROM      facts.transaction t
          JOIN dimensions.customer c
            ON c.customerid = t.customerid


       EXCEPT


     SELECT t.transid
            c.custname
     FROM   facts.transaction t
       JOIN dimensions.customer c
         ON c.customerid = t.customerid
                                                                 TABLE A
     WHERE t.transdate BETWEEN
            ‘2008-01-01’ AND ‘2008-01-21’                                  RESULTS




                                                                 TABLE B   TABLE A
                                                                            MINUS
                                                                           TABLE B




                                               Greenplum Confidential
36
Set Operations – UNION – Example

     SELECT t.transid
             c.custname
     FROM    facts.transaction t
       JOIN dimensions.customer c
          ON c.customerid = t.customerid
     WHERE t.transdate BETWEEN
               ‘2008-01-01’ AND ‘2008-05-17’


      UNION

     SELECT t.transid                                                   TABLE A             TABLE B

            c.custname
     FROM      facts.transaction t
       JOIN    dimensions.customer c
          ON   c.customerid = t.customerid
     WHERE     t.transdate BETWEEN
               ‘2008-01-01’ AND ‘2008-01-21’                                      RESULTS




     The UNION operation DOES NOT ALLOW DUPLICATE ROWS
                                               Greenplum Confidential
37
Set Operations – UNION ALL - Example

     SELECT t.transid
             c.custname
     FROM    facts.transaction t
       JOIN dimensions.customer c
          ON c.customerid = t.customerid
     WHERE t.transdate BETWEEN
               ‘2008-01-01’ AND ‘2008-05-17’


      UNION ALL

     SELECT t.transid
            c.custname
     FROM      facts.transaction t                                      TABLE A             TABLE B
       JOIN    dimensions.customer c
          ON   c.customerid = t.customerid
     WHERE     t.transdate BETWEEN
               ‘2008-01-01’ AND ‘2008-01-21’
                                                                                  RESULTS




     The UNION ALL operation MAY CREATE DUPLICATE ROWS
                                               Greenplum Confidential
38
Greenplum allows Sub-Queries
     IN clause is fully supported …

     ORIGINAL QUERY:
          SELECT     *
          FROM      facts.transaction t
          WHERE     t.customerid IN (SELECT customerid
                                        FROM dimensions.customer c);


     HOWEVER, THIS WILL PERFORM BETTER IN MOST CASES:
                    SELECT t.*
                    FROM   facts.transaction t
                    INNER JOIN     dimensions.customer c
                                   ON c.customerid = t.customerid;




                                  Greenplum Confidential
39
Module 10

     PostGREs Functions




            Greenplum Confidential
40
Types of Functions
     Greenplum supports several function types.
      query language functions (functions written in
       SQL)
      procedural language functions (functions
       written in for example, PL/pgSQL or PL/Tcl)
      internal functions
      C-language functions
      This class will only present the first two types of functions:
      Query Language and Procedural Language



                                         Greenplum Confidential
41
Query Language Function – More Rules
     • May contain SELECT statements
     • May contain DML statements
       Insert, Update or Delete statements
     • May not contain:
        Rollback, Savepoint, Begin or Commit commands
     • Last statement must be SELECT unless the
      return type is void.




                              Greenplum Confidential
42
Query Language Function – Example
     This function has no parameters and no return
     set:
     CREATE FUNCTION public.clean_customer()
       RETURNS void AS ‘DELETE FROM dimensions.customer
         WHERE state = ‘‘NA’’; ’
      LANGUAGE SQL;
     It is executed as a SELECT statement:
     SELECT public.clean_customer();

     clean_customer
     -----------
     (1 row)

                              Greenplum Confidential
43
Query Language Function – With
      Parameter
     CREATE FUNCTION public.clean_specific_customer(which
       char(2))
       RETURNS void AS ‘DELETE FROM dimensions.customer
         WHERE state = $1; ’
      LANGUAGE SQL;
     It is executed as a SELECT statement:
     SELECT public.clean_specific_customer(‘NA’);

     clean_specific_customer
     -----------------------

     (1 row)


                               Greenplum Confidential
44
SQL Functions Returning Sets
     When an SQL function is declared as returning SETOF sometype, the
     function‘s final SELECT query is executed to completion, and each
     row it outputs is returned as an element of the result set.
     CREATE FUNCTION dimensions.getstatecust(char)
              RETURNS SETOF customer AS $$
        SELECT *
            FROM dimensions.customer WHERE state = $1;
     $$ LANGUAGE SQL;

     SELECT *,UPPER(city)
     FROM dimensions.getstatecust(‘WA’);


     This function returns all qualifying rows.

                                     Greenplum Confidential
45
Procedural Language Functions
     • PL/pgSQL Procedural Language for Greenplum
       Can be used to create functions and procedures
       Adds control structures to the SQL language
       Can perform complex computations
       Inherits all user-defined types, functions, and
        operators
       Can be defined to be trusted by the server
       Is (relatively) easy to use.




                              Greenplum Confidential
46
Structure of PL/pgSQL
     • PL/pgSQL is a block-structured language.
     CREATE FUNCTION somefunc() RETURNS integer AS $$
     DECLARE quantity integer := 30;
     BEGIN
     RAISE NOTICE ’Quantity here is %’, quantity;
     -- Prints 30 quantity := 50;
     -- Create a subblock
        DECLARE quantity integer := 80;
        BEGIN
             RAISE NOTICE ’Quantity here is %’, quantity;
             -- Prints 80
             RAISE NOTICE ’Outer quantity here is %’, outerblock.quantity;
           -- Prints 50
        END;
     RAISE NOTICE ’Quantity here is %’, quantity;
     -- Prints 50
     RETURN quantity;
     END;
     $$ LANGUAGE plpgsql;
                                         Greenplum Confidential
47
PL/pgSQL Declarations
     • All variables used in a block must be declared in the declarations
       section of the block.
     • SYNTAX:
     name [ CONSTANT ] type [ NOT NULL ] [ { DEFAULT | := } expression ];
     •    EXAMPLES:
     user_id integer;
     quantity numeric(5);
     url varchar;
     myrow tablename%ROWTYPE;
     myfield tablename.columnname%TYPE;
     somerow RECORD;




                                           Greenplum Confidential
48
PL/pgSQL %Types
     • %TYPE provides the data type of a variable or
       table column.
     • You can use this to declare variables that will
       hold database values.
       SYNTAX: variable%TYPE
       EXAMPLES:
         custid customer.customerid%TYPE
        tid    transaction.transid%TYPE


                            Greenplum Confidential
49
PL/pgSQL Basic Statements
     • Assignment
     variable := expression;
     • Executing DML (no RETURN)
       PERFORM myfunction(myparm1,myparm2);
     • Single row results
       Use SELECT … INTO mytarget%TYPE;




                            Greenplum Confidential
50
PL/pgSQL – Executing Dynamic SQL
     • Oftentimes you will want to generate dynamic
       commands inside your PL/pgSQL functions
     • This is for commands that will involve different
       tables or different data types each time they are
       executed.
     • To handle this sort of problem, the EXECUTE
       statement is provided:
     EXECUTE command-string [ INTO [STRICT] target ];




                              Greenplum Confidential
51
PL/pgSQL – Dynamic SQL Example
     Dynamic values that are to be inserted into the constructed query
     require careful handling since they might themselves contain quote
     characters.

     EXECUTE   ’UPDATE tbl SET ’
          ||   quote_ident(colname)
          ||   ’ = ’
          ||   quote_literal(newvalue)
          ||   ’ WHERE key = ’
          ||   quote_literal(keyvalue);




                                    Greenplum Confidential
52
PL/pgSQL – FOUND
     • FOUND is set by:
        A SELECT INTO statement sets FOUND true if a row is assigned, false if
         no row is returned.
        A PERFORM statement sets FOUND true if it produces (and discards)
         one or more rows, false if no row is produced.
        UPDATE, INSERT, and DELETE statements set FOUND true if at least
         one row is affected, false if no row is affected.
        A FETCH statement sets FOUND true if it returns a row, false if no row
         is returned.
        A MOVE statement sets FOUND true if it successfully repositions the
         cursor, false otherwise.
        A FOR statement sets FOUND true if it iterates one or more times, else
         false.


                                      Greenplum Confidential
53
PL/pgSQL – Returning from a Function
     • RETURN NEXT and RETURN QUERY do not actually return from the
        function — they simply append zero or more rows to the function‘s result
        set.
     EXAMPLE:
     CREATE OR REPLACE FUNCTION getAllStores()
     RETURNS SETOF store AS
     $BODY$
     DECLARE r store%rowtype;
     BEGIN
     FOR r IN SELECT * FROM store WHERE storeid > 0 LOOP
        -- can do some processing here
        RETURN NEXT r;
       -- return current row of SELECT
     END LOOP;
     RETURN;
     END
     $BODY$
     LANGUAGE plpgsql;

                                         Greenplum Confidential
54
PL/pgSQL – Conditionals
     IF – THEN – ELSE conditionals lets you execute commands based on
     certain conditions.
     IF boolean-expression THEN
            statements
     [ ELSIF boolean-expression THEN
            statements
     [ ELSIF boolean-expression THEN
        statements
     [ ELSE statements ]
     END IF;


     TIP: Put the most commonly occurring condition first!




                                           Greenplum Confidential
55
PL/pgSQL – Simple Loops
     • With the LOOP, EXIT, CONTINUE, WHILE, and FOR statements,
       you can arrange for your PL/pgSQL function to repeat a series of
       commands.

     LOOP SYNTAX:
     [ <<label>> ]
     LOOP
        statements
     END LOOP [ label ];




                                    Greenplum Confidential
56
PL/pgSQL – Simple Loops
     EXIT EXAMPLES:
     LOOP
       -- some computations
       IF count > 0
         THEN EXIT;
         -- exit loop
       END IF;
     END LOOP;
     LOOP
        -- some computations
        EXIT WHEN count > 0;
        -- same result as previous example
     END LOOP;
     BEGIN
      -- some computations
        IF stocks > 100000
           THEN EXIT;
      -- causes exit from the BEGIN block
     END IF;
     END;
                                         Greenplum Confidential
57
PL/pgSQL – Simple Loops
     CONTINUE can be used within all loops.

     CONTINUE EXAMPLE:
     LOOP
         -- some computations
            EXIT WHEN count > 100;
            CONTINUE WHEN count < 50;
            -- some computations for count IN [50 .. 100]
     END LOOP;




                                   Greenplum Confidential
58
PL/pgSQL – Simple Loops
     The WHILE statement repeats a sequence of statements so long as
     the boolean-expression evaluates to true. The expression is checked
     just before each entry to the loop body.

     WHILE EXAMPLE:
     WHILE customerid < 50 LOOP
         -- some computations
     END LOOP;




                                   Greenplum Confidential
59
PL/pgSQL – Simple For (integer) Loops
     This form of FOR creates a loop that iterates over a range of integer
     values.
     EXAMPLE:
     FOR i IN 1..3 LOOP
       -- i will take on the values 1,2,3 within the loop
     END LOOP;

     FOR i IN REVERSE 3..1 LOOP
       -- i will take on the values 3,2,1 within the loop
     END LOOP;

     FOR i IN REVERSE 8..1 BY 2 LOOP
       -- i will take on the values 8,6,4,2 within the loop
     END LOOP;
                                     Greenplum Confidential
60
PL/pgSQL – Simple For (Query) Loops
     Using a different type of FOR loop, you can iterate through the results
     of a query and manipulate that data accordingly.
     EXAMPLE:
      CREATE FUNCTION nukedimensions() RETURNS integer AS $$
      DECLARE mdims RECORD;
      BEGIN
       FOR mdims IN SELECT * FROM pg_class WHERE schema=‘dimensions’ LOOP
        -- Now ”mdims" has one record from pg_class
        EXECUTE ’TRUNCATE TABLE ’ || quote_ident(mdimss.relname);
       END LOOP;
     RETURN 1;
     END;
     $$ LANGUAGE plpgsql;




                                     Greenplum Confidential
61
PL/pgSQL – Trapping Errors
     • By default, any error occurring in a PL/pgSQL function aborts
       execution of the function, and indeed of the surrounding
       transaction as well.
     • You can trap errors and recover from them by using a BEGIN block
       with an EXCEPTION clause.
        BEGIN
            statements
            EXCEPTION WHEN condition [OR condition ... ] THEN
              handler_statements
                     [ WHEN condition [ OR condition ... ] THEN
              handler_statements ... ]
        END;


                                   Greenplum Confidential
62
PL/pgSQL – Common Exception
      Conditions

     NO_DATA                           No data matches predicates
     CONNECTION_FAILURE                SQL cannot connect
     DIVISION_BY_ZERO                  Invalid division operation 0 in divisor
     INTERVAL_FIELD_OVERFLOW           Insufficient precision for timestamp
     NULL_VALUE_VIOLATION              Tried to insert NULL into NOT NULL
                                       column
     RAISE_EXCEPTION                   Error raising an exception
     PLPGSQL_ERROR                     PL/pgSQL error encountered at run
                                       time
     NO_DATA_FOUND                     Query returned zero rows
     TOO_MANY_ROWS                     Anticipated single row return,
                                       received more than 1 row.



                               Greenplum Confidential
63

Weitere ähnliche Inhalte

Was ist angesagt?

Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Intro to tsql unit 3
Intro to tsql   unit 3Intro to tsql   unit 3
Intro to tsql unit 3
Syed Asrarali
 
Sql basics
Sql basicsSql basics
Sql basics
Kumar
 

Was ist angesagt? (14)

Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Oracle: DML
Oracle: DMLOracle: DML
Oracle: DML
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Intro to tsql unit 3
Intro to tsql   unit 3Intro to tsql   unit 3
Intro to tsql unit 3
 
Sql operator
Sql operatorSql operator
Sql operator
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
 
Ch3
Ch3Ch3
Ch3
 
Sql basics
Sql basicsSql basics
Sql basics
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
SQL Views
SQL ViewsSQL Views
SQL Views
 

Andere mochten auch

Database Introduction - Join Query
Database Introduction - Join QueryDatabase Introduction - Join Query
Database Introduction - Join Query
Dudy Ali
 
Greenplum: Driving the future of Data Warehousing and Analytics
Greenplum: Driving the future of Data Warehousing and AnalyticsGreenplum: Driving the future of Data Warehousing and Analytics
Greenplum: Driving the future of Data Warehousing and Analytics
eaiti
 
Agile Business Intelligence
Agile Business IntelligenceAgile Business Intelligence
Agile Business Intelligence
Don Jackson
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
pramode_ce
 

Andere mochten auch (20)

Joins
JoinsJoins
Joins
 
Building a heterogeneous Hadoop Olap system with Microsoft BI stack. PABLO DO...
Building a heterogeneous Hadoop Olap system with Microsoft BI stack. PABLO DO...Building a heterogeneous Hadoop Olap system with Microsoft BI stack. PABLO DO...
Building a heterogeneous Hadoop Olap system with Microsoft BI stack. PABLO DO...
 
Hadoop Data Warehouse
Hadoop Data WarehouseHadoop Data Warehouse
Hadoop Data Warehouse
 
Greenplum feature
Greenplum featureGreenplum feature
Greenplum feature
 
Demonstrating the Future of Data Science
Demonstrating the Future of Data ScienceDemonstrating the Future of Data Science
Demonstrating the Future of Data Science
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Analyzing SQL Server wait stats, hands-on!
Analyzing SQL Server wait stats, hands-on!Analyzing SQL Server wait stats, hands-on!
Analyzing SQL Server wait stats, hands-on!
 
Introduction to Greenplum
Introduction to GreenplumIntroduction to Greenplum
Introduction to Greenplum
 
Database Introduction - Join Query
Database Introduction - Join QueryDatabase Introduction - Join Query
Database Introduction - Join Query
 
Greenplum: Driving the future of Data Warehousing and Analytics
Greenplum: Driving the future of Data Warehousing and AnalyticsGreenplum: Driving the future of Data Warehousing and Analytics
Greenplum: Driving the future of Data Warehousing and Analytics
 
Greenplum Architecture
Greenplum ArchitectureGreenplum Architecture
Greenplum Architecture
 
SQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and ProfitSQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and Profit
 
Agile Business Intelligence
Agile Business IntelligenceAgile Business Intelligence
Agile Business Intelligence
 
Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 
Sql básico - Teoria e prática: Um grande resumo
Sql básico - Teoria e prática: Um grande resumoSql básico - Teoria e prática: Um grande resumo
Sql básico - Teoria e prática: Um grande resumo
 
Agile Business Intelligence
Agile Business IntelligenceAgile Business Intelligence
Agile Business Intelligence
 
Whitepaper : Working with Greenplum Database using Toad for Data Analysts
Whitepaper : Working with Greenplum Database using Toad for Data Analysts Whitepaper : Working with Greenplum Database using Toad for Data Analysts
Whitepaper : Working with Greenplum Database using Toad for Data Analysts
 
Greenplum Database Overview
Greenplum Database Overview Greenplum Database Overview
Greenplum Database Overview
 
Hadoop & Greenplum: Why Do Such a Thing?
Hadoop & Greenplum: Why Do Such a Thing?Hadoop & Greenplum: Why Do Such a Thing?
Hadoop & Greenplum: Why Do Such a Thing?
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 

Ähnlich wie Green plum培训材料

Sydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexesSydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexes
paulguerin
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 

Ähnlich wie Green plum培训材料 (20)

SQL LECTURE.pptx
SQL LECTURE.pptxSQL LECTURE.pptx
SQL LECTURE.pptx
 
Sydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexesSydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexes
 
Sql
SqlSql
Sql
 
SQL
SQLSQL
SQL
 
Sql
SqlSql
Sql
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
IR SQLite Session #1
IR SQLite Session #1IR SQLite Session #1
IR SQLite Session #1
 
Mandatory sql functions for beginners
Mandatory sql functions for beginnersMandatory sql functions for beginners
Mandatory sql functions for beginners
 
Dbms
DbmsDbms
Dbms
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
 
Goldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL QueriesGoldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL Queries
 
Postgresql
PostgresqlPostgresql
Postgresql
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Mysql
MysqlMysql
Mysql
 
Dbms &amp; oracle
Dbms &amp; oracleDbms &amp; oracle
Dbms &amp; oracle
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Sql for dbaspresentation
Sql for dbaspresentationSql for dbaspresentation
Sql for dbaspresentation
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
 

Mehr von 锐 张

长尾理论(The longtail)版
长尾理论(The longtail)版长尾理论(The longtail)版
长尾理论(The longtail)版
锐 张
 
Openstack starter-guide-diablo
Openstack starter-guide-diabloOpenstack starter-guide-diablo
Openstack starter-guide-diablo
锐 张
 
基于My sql的分布式数据库实践
基于My sql的分布式数据库实践基于My sql的分布式数据库实践
基于My sql的分布式数据库实践
锐 张
 
Redis中文入门手册
Redis中文入门手册Redis中文入门手册
Redis中文入门手册
锐 张
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
锐 张
 
Redis内存存储结构分析
Redis内存存储结构分析Redis内存存储结构分析
Redis内存存储结构分析
锐 张
 
淘宝前端优化
淘宝前端优化淘宝前端优化
淘宝前端优化
锐 张
 
Sun jdk 1.6内存管理 -实现篇 -毕玄
Sun jdk 1.6内存管理 -实现篇 -毕玄Sun jdk 1.6内存管理 -实现篇 -毕玄
Sun jdk 1.6内存管理 -实现篇 -毕玄
锐 张
 
Sun jdk 1.6内存管理 -调优篇-毕玄
Sun jdk 1.6内存管理 -调优篇-毕玄Sun jdk 1.6内存管理 -调优篇-毕玄
Sun jdk 1.6内存管理 -调优篇-毕玄
锐 张
 
Sun jdk 1.6内存管理 -使用篇-毕玄
Sun jdk 1.6内存管理 -使用篇-毕玄Sun jdk 1.6内存管理 -使用篇-毕玄
Sun jdk 1.6内存管理 -使用篇-毕玄
锐 张
 
Sun jdk-1.6-gc
Sun jdk-1.6-gcSun jdk-1.6-gc
Sun jdk-1.6-gc
锐 张
 
Redis介绍
Redis介绍Redis介绍
Redis介绍
锐 张
 
Redis深入浅出
Redis深入浅出Redis深入浅出
Redis深入浅出
锐 张
 
Greenplum技术
Greenplum技术Greenplum技术
Greenplum技术
锐 张
 
新时代的分析型云数据库 Greenplum
新时代的分析型云数据库 Greenplum新时代的分析型云数据库 Greenplum
新时代的分析型云数据库 Greenplum
锐 张
 
服务器端性能优化 提升Qps、rt
服务器端性能优化 提升Qps、rt服务器端性能优化 提升Qps、rt
服务器端性能优化 提升Qps、rt
锐 张
 
新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)
新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)
新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)
锐 张
 
新浪云计算公开课第一期:Let’s run @ sae(丛磊)
新浪云计算公开课第一期:Let’s run @ sae(丛磊)新浪云计算公开课第一期:Let’s run @ sae(丛磊)
新浪云计算公开课第一期:Let’s run @ sae(丛磊)
锐 张
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计
锐 张
 
亚马逊云计算Aws
亚马逊云计算Aws亚马逊云计算Aws
亚马逊云计算Aws
锐 张
 

Mehr von 锐 张 (20)

长尾理论(The longtail)版
长尾理论(The longtail)版长尾理论(The longtail)版
长尾理论(The longtail)版
 
Openstack starter-guide-diablo
Openstack starter-guide-diabloOpenstack starter-guide-diablo
Openstack starter-guide-diablo
 
基于My sql的分布式数据库实践
基于My sql的分布式数据库实践基于My sql的分布式数据库实践
基于My sql的分布式数据库实践
 
Redis中文入门手册
Redis中文入门手册Redis中文入门手册
Redis中文入门手册
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
 
Redis内存存储结构分析
Redis内存存储结构分析Redis内存存储结构分析
Redis内存存储结构分析
 
淘宝前端优化
淘宝前端优化淘宝前端优化
淘宝前端优化
 
Sun jdk 1.6内存管理 -实现篇 -毕玄
Sun jdk 1.6内存管理 -实现篇 -毕玄Sun jdk 1.6内存管理 -实现篇 -毕玄
Sun jdk 1.6内存管理 -实现篇 -毕玄
 
Sun jdk 1.6内存管理 -调优篇-毕玄
Sun jdk 1.6内存管理 -调优篇-毕玄Sun jdk 1.6内存管理 -调优篇-毕玄
Sun jdk 1.6内存管理 -调优篇-毕玄
 
Sun jdk 1.6内存管理 -使用篇-毕玄
Sun jdk 1.6内存管理 -使用篇-毕玄Sun jdk 1.6内存管理 -使用篇-毕玄
Sun jdk 1.6内存管理 -使用篇-毕玄
 
Sun jdk-1.6-gc
Sun jdk-1.6-gcSun jdk-1.6-gc
Sun jdk-1.6-gc
 
Redis介绍
Redis介绍Redis介绍
Redis介绍
 
Redis深入浅出
Redis深入浅出Redis深入浅出
Redis深入浅出
 
Greenplum技术
Greenplum技术Greenplum技术
Greenplum技术
 
新时代的分析型云数据库 Greenplum
新时代的分析型云数据库 Greenplum新时代的分析型云数据库 Greenplum
新时代的分析型云数据库 Greenplum
 
服务器端性能优化 提升Qps、rt
服务器端性能优化 提升Qps、rt服务器端性能优化 提升Qps、rt
服务器端性能优化 提升Qps、rt
 
新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)
新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)
新浪云计算公开课第二期:Sae平台的灵活应用(吕毅、魏世江)
 
新浪云计算公开课第一期:Let’s run @ sae(丛磊)
新浪云计算公开课第一期:Let’s run @ sae(丛磊)新浪云计算公开课第一期:Let’s run @ sae(丛磊)
新浪云计算公开课第一期:Let’s run @ sae(丛磊)
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计
 
亚马逊云计算Aws
亚马逊云计算Aws亚马逊云计算Aws
亚马逊云计算Aws
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Kürzlich hochgeladen (20)

SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

Green plum培训材料

  • 2. Module 1 Greenplum Concepts, Features & Benefits Greenplum Confidential 2
  • 3. Module 4 Joining Tables Types & Methods Greenplum Confidential 3
  • 4. Join Types Inner Join Left Outer Join Right Outer Join Full Outer Join Cross Join Greenplum Confidential 4
  • 5. Inner Join The syntax for an INNER JOIN is simple! Also known as a SIMPLE JOIN or EQUI-JOIN. SELECT EMP.EMPLOYEE_ID , EMP.EMPLOYEE_NAME , DPT.DEPT_NAME FROM EMPLOYEES EMP , DEPARTMENTS DPT WHERE EMP.DPT_ID = DPT.DPT_ID; This query only retrieves rows based on the following join condition: a corresponding row must exist in each table, thus the term ―INNER JOIN‖. Greenplum Confidential 5
  • 6. Left OUTER Join LEFT OUTER JOIN returns all rows from the ―left‖ table, and only those rows from the ―right‖ table that match the left one. This is handy when there may be missing values in the ―left‖ table, but you want to get a description or other information from the ―right‖ table. SELECT t.transid , c.custname FROM facts.transaction t LEFT OUTER JOIN dimensions.customer c ON c.customerid = t.customerid; You may also abbreviate LEFT OUTER JOIN to LEFT JOIN. … FROM facts.transaction t LEFT JOIN dimensions.customer c ON c.customerid = t.customerid; Greenplum Confidential 6
  • 7. Right OUTER Join RIGHT OUTER JOIN is the inverse of the LEFT OUTER JOIN. There is no difference in the functionality of these two. Just be careful when designating the left and right tables! SELECT t.transid , c.custname FROM dimensions.customer c RIGHT OUTER JOIN facts.transaction t ON c.customerid = t.customerid; You may also abbreviate RIGHT OUTER JOIN to RIGHT JOIN. … FROM dimensions.customer c RIGHT JOIN facts.transaction t ON c.customerid = t.customerid; Greenplum Confidential 7
  • 8. Full OUTER Join This interesting join is used when you want to retrieve every row, whether there is a match or not based on the join criteria! When would you use this? An example is detecting ―true changes‖ for a table. SELECT COALESCE( ORIG.transid, STG.transid) AS transid , CASE WHEN ORIG.transid IS NULL AND STG.transid IS NOT NULL THEN 'INSERT' WHEN ORIG.transid IS NOT NULL AND STG.transid IS NOT NULL THEN 'UPDATE' WHEN ORIG.transid IS NOT NULL AND STG.transid IS NULL THEN 'DELETE‘ ELSE 'UNKNOWN‘ END AS Process_type FROM facts.transaction ORIG FULL OUTER JOIN staging.transaction_w STG ON STG.transid = ORIG.transid; The COALESCE function insures that we have a transaction id to work with in subsequent processing. ORIG STG Greenplum Confidential 8
  • 9. CROSS JOIN (Cartesian Product) We see these joins, generally, when they are unintentionally created! • Every row in the ―left‖ table is joined with every row in the ―right‖ table! • So, if you have a billion row table and CROSS JOIN it to a 100 row table, your answer set will have 100 billion rows! • There are uses for deliberate CROSS JOINs and will be shown later. SELECT t.transid , t.transdate , COALESCE( c.custname, ‘Unknown Customer‘) AS custname FROM facts.transaction t , dimensions.customer c; Q: What is wrong with this query? Why will it perform a CROSS JOIN? Greenplum Confidential 9
  • 10. Module 5 PostgreSQL Database Essentials Greenplum Confidential 10
  • 11. PostgreSQL Schemas – the Search Path PostgreSQL will search for tables that are named without qualification (schema name not included) by use of the search_path variable. You can change the search_path parameter using SET command: SET search_path TO myschema,public; SHOW search_path; search_path -------------- myschema,public Greenplum Confidential 11
  • 12. PostgreSQL – Roles • Database roles are conceptually completely separate from operating system users. • Database roles are global across all databases on the same cluster installation. • Database user is a role with a LOGIN privilege. • It is frequently convenient to group users together to ease management of privileges. That way, privileges can be granted to, or revoked from, one group instead of may users separately. Greenplum Confidential 12
  • 13. PostgreSQL – Roles and Privileges • When an object is created, it is assigned an owner. The default owner is the role that executed the creation statement. • For most kinds of objects, the initial state is that only the owner (or a superuser) can do anything with it. • To allow other roles to use it, privileges must be granted. • There are several different kinds of privileges: SELECT Read only, unable to create new data or modify INSERT Permission to add rows to the object UPDATE Permission to make changes to existing data DELETE Permission to remove data from the object CREATE Permission to create objects CONNECT Permission to connect (applies to a database) EXECUTE Permission to execute functions / procedures USAGE Permission to use objects (applies to a schema) Greenplum Confidential 13
  • 14. PostgreSQL – Users • In PostgreSQL a user is a role with login privileges. • Privileges on database objects should be granted to a group role rather than a user role. • Users may have special (system) privileges not associated with database objects. SUPERUSER PASSWORD CREATEDB CREATEROLE Tip: It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it. Greenplum Confidential 14
  • 15. PostgreSQL Table Basics - Constraints • PRIMARY KEY constraint insures row uniqueness. A primary key indicates that a column or group of columns can be used as a unique identifier for rows in the table. Technically, a primary key constraint is simply a combination of a UNIQUE constraint and a NOT NULL constraint. • FOREIGN KEY constraint specifies that values in a column (or a group of columns) of a given table must match the values appearing in another table. * NOT SUPPORTED IN GREENPLUM Greenplum Confidential 15
  • 16. PostgreSQL DDL – CREATE TABLE • Supports standard ANSI CREATE TABLE syntax: CREATE TABLE dimensions.store ( storeId SMALLINT NOT NULL, storeName VARCHAR(40) NOT NULL, address VARCHAR(50) NOT NULL, city VARCHAR(40) NOT NULL, state CHAR(2) NOT NULL, zipcode CHAR(8) NOT NULL ); Greenplum Confidential 16
  • 17. PostgreSQL DDL – MODIFY TABLE • You can modify certain attributes of a table:  Rename columns  Rename tables  Add/Remove columns - ALTER TABLE product ADD COLUMN description text;  Add/Remove constraints - ALTER TABLE product ALTER COLUMN prod_no SET NOT NULL;  Change default values - ALTER TABLE product ALTER COLUMN prod_no SET DEFAULT -999;  Change column data types - ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2); Greenplum Confidential 17
  • 18. PostgreSQL DDL – DROP TABLE • Table can be removed from the database with the DROP TABLE statement: DROP TABLE dimensions.customer_old; • If the table you are dropping does not exists, you can avoid error messages if you add ―IF EXISTS‖ to the DROP TABLE statement: DROP TABLE IF EXISTS dimensions.customer_old; Greenplum Confidential 18
  • 19. PostgreSQL Column Basics - Constraints • A column can be assigned a DEFAULT value. • CHECK constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean expression. EXAMPLE: The column price must be greater than zero: price numeric CHECK (price > 0), • NOT NULL constraint simply specifies that a column must not assume the null value. EXAMPLE: The column transactionid may not be NULL: transactionid NOT NULL, • UNIQUE constraints ensure that the data contained in a column or a group of columns is unique with respect to all the rows in the table. Greenplum Confidential 19
  • 20. PostgreSQL Indexes • PostgreSQL provides several index types:  B-tree  Hash  GiST (Generalized Search Tree)  GIN (Generalized Inverted Index) • Each index type uses a different algorithm that is best suited to different types of queries. • By default, CREATE INDEX command creates a B-tree index. Greenplum Confidential 20
  • 21. Constants in PostgreSQL • There are three kinds of implicitly-typed constants in PostgreSQL:  Strings  Bit strings  Numbers • Constants can also be specified with explicit types, which can enable more accurate representation and more efficient handling by the system. Greenplum Confidential 21
  • 22. Casting Data Types in PostgreSQL • A constant of an arbitrary type can be entered using any one of the following explicit notations: type ’string ’ REAL ‘2.117902’ ’string ’::type ‘2.117902’::REAL CAST ( ’string ’ AS type ) CAST(‘2.117902’ AS REAL) The result is a constant of the indicated type. • If there is no ambiguity the explicit type cast can be omitted (for example, when constant it is assigned directly to a table column), in which case it is automatically converted to the column type. • The string constant can be written using either regular SQL notation or dollar-quoting. Greenplum Confidential 22
  • 23. PostgreSQL Comments • Standard SQL Comments -- This is a comment • “C” style Comments /* This is a comment block that ends with the */ Greenplum Confidential 23
  • 24. PostgreSQL Data Types Greenplum Confidential 24
  • 25. PostgreSQL Data Types Greenplum Confidential 25
  • 26. PostgreSQL Data Types Greenplum Confidential 26
  • 27. PostgreSQL Functions & Operators • PostgreSQL has a wide range of built-in functions and operators for all data types. • Users can define custom operators and functions. Use caution, new operators or functions may adversely effect Greenplum MPP functionality. Greenplum Confidential 27
  • 28. Built-In Functions (select function) CURRENT_DATE returns the current system date Example: 2006-11-06 CURRENT_TIME returns the current system time Example: 16:50:54 CURRENT_TIMESTAMP returns the current system date and time Example: 2008-01-06 16:51:44.430000+00:00 LOCALTIME returns the current system time with time zone adjustment Example: 19:50:54 LOCALTIMESTAMP returns the current system date and time with time zone adjustment Example: 2008-01-06 19:51:44.430000+00:00 CURRENT_ROLE or USER returns the current database user Example: jdoe Greenplum Confidential 28
  • 29. PostgreSQL Comparison Operators The usual comparison operators are available: Operator Description = Equal to != OR <> NOT Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to x BETWEEN y AND z Short hand for x >= y and x <= z x IS NULL True if x has NO VALUE ‗abc‘ LIKE ‗%abcde%‘ Pattern Matching POSIX Comparisons POSIX Pattern Matching Greenplum Confidential 29
  • 30. PostgreSQL Mathematical Functions Commonly used Mathematical functions: Function Returns Description Example Results + - * / same Add, Subtract, 1 + 1 2 Multiply & Divide % Integer Modulo 10%2 0 ^ Same Exponentiation 2^2 4 |/ Numeric Square Root |/9 3 ||/ Numeric Cube Root ||/8 2 ! Numeric Factorial !3 6 & | # ~ Numeric Bitwise 91 & 15 11 And, Or, XOR, Not << >> Numeric Bitwise 1 << 4 16 Shift left, right 8 >> 2 2 Greenplum Confidential 30
  • 31. PostgreSQL Mathematical Functions Handy Mathematical functions: Function Returns Description Example Results Abs same Absolute Value Abs(-998.2) 998.2 Ceiling Numeric Returns smallest Ceiling(48.2) 49 (numeric) integer not less than argument Floor Numeric Returns largest Floor(48.2) 48 (numeric) integer not greater than argument Pi() Numeric The π constant Pi() 3.1419… Random() Numeric Random value Random() .87663 between 0.0 and 1.0 Round() Numeric Round to nearest Round(22.7) 23 integer Greenplum Confidential 31
  • 32. PostgreSQL String Functions Commonly used string functions: Function Returns Description Example Results String || Text String ‘my’ || ‘my’ ‘mymy’ String concatenation Char_length Integer number of chars in Char_length(‘mym 4 (string) string y’) Position Integer Location of Position(‘my’ in 3 (string in specified sub- ‘ohmy’) string) string Lower(string) Text Converts to lower Lower(‘MYMY’) ‘mymy’ case Upper(string) Text Converts to upper Upper(‘mymy’) ‘MYMY’ case Substring Text Displays portion Substring(‘myohm ‘oh’ (string from of string y’ from 3 for 2) n for n) Trim(both,lea Text Remove leading Trim(‘ mymy ‘mymy’ ding,trailing and/or trailing ‘) from string) characters Greenplum Confidential 32
  • 33. PostgreSQL String Functions Handy string functions: Function Returns Description Example Results Initcap Text Changes case Initcap ‘My My’ (string) (‘my my’) Length Integer Returns string Length 4 (string) length (‘mymy’) Split_part Text Separates Split_part(‘o ‘two’ (string, delimited list ne|two|three’ delimiter, ,’|’,2) occurrence) Greenplum Confidential 33
  • 34. PostgreSQL Date Functions Commonly used date functions: Function Returns Description Example Results Age Timesta Difference in Age(‘2008-08- 0 years 1 (timestam,t mp years, months 12’ timestamp, month 11 imestamp) and days current_timest days amp) Extract Integer Returns year, Extract( day 11 (field from month, day, from timestamp) hour, minute or current_date) second Now() Timesta Returns current Now() 2008-09-22 mp date & time 11:00:01 Overlaps Boolean Simplifies WHERE TRUE comparing date (‘2008-01- ranges 01’,’2008-02- 11’) overlaps (‘2008-02- 01’,’2008-09- 11’) Greenplum Confidential 34
  • 35. Module 6 Greenplum SQL Greenplum Confidential 35
  • 36. Set Operations – EXCEPT – Example Returns all rows from the first SELECT statement except for those also returned by the second SELECT. SELECT t.transid c.custname FROM facts.transaction t JOIN dimensions.customer c ON c.customerid = t.customerid EXCEPT SELECT t.transid c.custname FROM facts.transaction t JOIN dimensions.customer c ON c.customerid = t.customerid TABLE A WHERE t.transdate BETWEEN ‘2008-01-01’ AND ‘2008-01-21’ RESULTS TABLE B TABLE A MINUS TABLE B Greenplum Confidential 36
  • 37. Set Operations – UNION – Example SELECT t.transid c.custname FROM facts.transaction t JOIN dimensions.customer c ON c.customerid = t.customerid WHERE t.transdate BETWEEN ‘2008-01-01’ AND ‘2008-05-17’ UNION SELECT t.transid TABLE A TABLE B c.custname FROM facts.transaction t JOIN dimensions.customer c ON c.customerid = t.customerid WHERE t.transdate BETWEEN ‘2008-01-01’ AND ‘2008-01-21’ RESULTS The UNION operation DOES NOT ALLOW DUPLICATE ROWS Greenplum Confidential 37
  • 38. Set Operations – UNION ALL - Example SELECT t.transid c.custname FROM facts.transaction t JOIN dimensions.customer c ON c.customerid = t.customerid WHERE t.transdate BETWEEN ‘2008-01-01’ AND ‘2008-05-17’ UNION ALL SELECT t.transid c.custname FROM facts.transaction t TABLE A TABLE B JOIN dimensions.customer c ON c.customerid = t.customerid WHERE t.transdate BETWEEN ‘2008-01-01’ AND ‘2008-01-21’ RESULTS The UNION ALL operation MAY CREATE DUPLICATE ROWS Greenplum Confidential 38
  • 39. Greenplum allows Sub-Queries IN clause is fully supported … ORIGINAL QUERY: SELECT * FROM facts.transaction t WHERE t.customerid IN (SELECT customerid FROM dimensions.customer c); HOWEVER, THIS WILL PERFORM BETTER IN MOST CASES: SELECT t.* FROM facts.transaction t INNER JOIN dimensions.customer c ON c.customerid = t.customerid; Greenplum Confidential 39
  • 40. Module 10 PostGREs Functions Greenplum Confidential 40
  • 41. Types of Functions Greenplum supports several function types.  query language functions (functions written in SQL)  procedural language functions (functions written in for example, PL/pgSQL or PL/Tcl)  internal functions  C-language functions This class will only present the first two types of functions: Query Language and Procedural Language Greenplum Confidential 41
  • 42. Query Language Function – More Rules • May contain SELECT statements • May contain DML statements Insert, Update or Delete statements • May not contain:  Rollback, Savepoint, Begin or Commit commands • Last statement must be SELECT unless the return type is void. Greenplum Confidential 42
  • 43. Query Language Function – Example This function has no parameters and no return set: CREATE FUNCTION public.clean_customer() RETURNS void AS ‘DELETE FROM dimensions.customer WHERE state = ‘‘NA’’; ’ LANGUAGE SQL; It is executed as a SELECT statement: SELECT public.clean_customer(); clean_customer ----------- (1 row) Greenplum Confidential 43
  • 44. Query Language Function – With Parameter CREATE FUNCTION public.clean_specific_customer(which char(2)) RETURNS void AS ‘DELETE FROM dimensions.customer WHERE state = $1; ’ LANGUAGE SQL; It is executed as a SELECT statement: SELECT public.clean_specific_customer(‘NA’); clean_specific_customer ----------------------- (1 row) Greenplum Confidential 44
  • 45. SQL Functions Returning Sets When an SQL function is declared as returning SETOF sometype, the function‘s final SELECT query is executed to completion, and each row it outputs is returned as an element of the result set. CREATE FUNCTION dimensions.getstatecust(char) RETURNS SETOF customer AS $$ SELECT * FROM dimensions.customer WHERE state = $1; $$ LANGUAGE SQL; SELECT *,UPPER(city) FROM dimensions.getstatecust(‘WA’); This function returns all qualifying rows. Greenplum Confidential 45
  • 46. Procedural Language Functions • PL/pgSQL Procedural Language for Greenplum Can be used to create functions and procedures Adds control structures to the SQL language Can perform complex computations Inherits all user-defined types, functions, and operators Can be defined to be trusted by the server Is (relatively) easy to use. Greenplum Confidential 46
  • 47. Structure of PL/pgSQL • PL/pgSQL is a block-structured language. CREATE FUNCTION somefunc() RETURNS integer AS $$ DECLARE quantity integer := 30; BEGIN RAISE NOTICE ’Quantity here is %’, quantity; -- Prints 30 quantity := 50; -- Create a subblock DECLARE quantity integer := 80; BEGIN RAISE NOTICE ’Quantity here is %’, quantity; -- Prints 80 RAISE NOTICE ’Outer quantity here is %’, outerblock.quantity; -- Prints 50 END; RAISE NOTICE ’Quantity here is %’, quantity; -- Prints 50 RETURN quantity; END; $$ LANGUAGE plpgsql; Greenplum Confidential 47
  • 48. PL/pgSQL Declarations • All variables used in a block must be declared in the declarations section of the block. • SYNTAX: name [ CONSTANT ] type [ NOT NULL ] [ { DEFAULT | := } expression ]; • EXAMPLES: user_id integer; quantity numeric(5); url varchar; myrow tablename%ROWTYPE; myfield tablename.columnname%TYPE; somerow RECORD; Greenplum Confidential 48
  • 49. PL/pgSQL %Types • %TYPE provides the data type of a variable or table column. • You can use this to declare variables that will hold database values. SYNTAX: variable%TYPE EXAMPLES: custid customer.customerid%TYPE tid transaction.transid%TYPE Greenplum Confidential 49
  • 50. PL/pgSQL Basic Statements • Assignment variable := expression; • Executing DML (no RETURN) PERFORM myfunction(myparm1,myparm2); • Single row results Use SELECT … INTO mytarget%TYPE; Greenplum Confidential 50
  • 51. PL/pgSQL – Executing Dynamic SQL • Oftentimes you will want to generate dynamic commands inside your PL/pgSQL functions • This is for commands that will involve different tables or different data types each time they are executed. • To handle this sort of problem, the EXECUTE statement is provided: EXECUTE command-string [ INTO [STRICT] target ]; Greenplum Confidential 51
  • 52. PL/pgSQL – Dynamic SQL Example Dynamic values that are to be inserted into the constructed query require careful handling since they might themselves contain quote characters. EXECUTE ’UPDATE tbl SET ’ || quote_ident(colname) || ’ = ’ || quote_literal(newvalue) || ’ WHERE key = ’ || quote_literal(keyvalue); Greenplum Confidential 52
  • 53. PL/pgSQL – FOUND • FOUND is set by:  A SELECT INTO statement sets FOUND true if a row is assigned, false if no row is returned.  A PERFORM statement sets FOUND true if it produces (and discards) one or more rows, false if no row is produced.  UPDATE, INSERT, and DELETE statements set FOUND true if at least one row is affected, false if no row is affected.  A FETCH statement sets FOUND true if it returns a row, false if no row is returned.  A MOVE statement sets FOUND true if it successfully repositions the cursor, false otherwise.  A FOR statement sets FOUND true if it iterates one or more times, else false. Greenplum Confidential 53
  • 54. PL/pgSQL – Returning from a Function • RETURN NEXT and RETURN QUERY do not actually return from the function — they simply append zero or more rows to the function‘s result set. EXAMPLE: CREATE OR REPLACE FUNCTION getAllStores() RETURNS SETOF store AS $BODY$ DECLARE r store%rowtype; BEGIN FOR r IN SELECT * FROM store WHERE storeid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END $BODY$ LANGUAGE plpgsql; Greenplum Confidential 54
  • 55. PL/pgSQL – Conditionals IF – THEN – ELSE conditionals lets you execute commands based on certain conditions. IF boolean-expression THEN statements [ ELSIF boolean-expression THEN statements [ ELSIF boolean-expression THEN statements [ ELSE statements ] END IF; TIP: Put the most commonly occurring condition first! Greenplum Confidential 55
  • 56. PL/pgSQL – Simple Loops • With the LOOP, EXIT, CONTINUE, WHILE, and FOR statements, you can arrange for your PL/pgSQL function to repeat a series of commands. LOOP SYNTAX: [ <<label>> ] LOOP statements END LOOP [ label ]; Greenplum Confidential 56
  • 57. PL/pgSQL – Simple Loops EXIT EXAMPLES: LOOP -- some computations IF count > 0 THEN EXIT; -- exit loop END IF; END LOOP; LOOP -- some computations EXIT WHEN count > 0; -- same result as previous example END LOOP; BEGIN -- some computations IF stocks > 100000 THEN EXIT; -- causes exit from the BEGIN block END IF; END; Greenplum Confidential 57
  • 58. PL/pgSQL – Simple Loops CONTINUE can be used within all loops. CONTINUE EXAMPLE: LOOP -- some computations EXIT WHEN count > 100; CONTINUE WHEN count < 50; -- some computations for count IN [50 .. 100] END LOOP; Greenplum Confidential 58
  • 59. PL/pgSQL – Simple Loops The WHILE statement repeats a sequence of statements so long as the boolean-expression evaluates to true. The expression is checked just before each entry to the loop body. WHILE EXAMPLE: WHILE customerid < 50 LOOP -- some computations END LOOP; Greenplum Confidential 59
  • 60. PL/pgSQL – Simple For (integer) Loops This form of FOR creates a loop that iterates over a range of integer values. EXAMPLE: FOR i IN 1..3 LOOP -- i will take on the values 1,2,3 within the loop END LOOP; FOR i IN REVERSE 3..1 LOOP -- i will take on the values 3,2,1 within the loop END LOOP; FOR i IN REVERSE 8..1 BY 2 LOOP -- i will take on the values 8,6,4,2 within the loop END LOOP; Greenplum Confidential 60
  • 61. PL/pgSQL – Simple For (Query) Loops Using a different type of FOR loop, you can iterate through the results of a query and manipulate that data accordingly. EXAMPLE: CREATE FUNCTION nukedimensions() RETURNS integer AS $$ DECLARE mdims RECORD; BEGIN FOR mdims IN SELECT * FROM pg_class WHERE schema=‘dimensions’ LOOP -- Now ”mdims" has one record from pg_class EXECUTE ’TRUNCATE TABLE ’ || quote_ident(mdimss.relname); END LOOP; RETURN 1; END; $$ LANGUAGE plpgsql; Greenplum Confidential 61
  • 62. PL/pgSQL – Trapping Errors • By default, any error occurring in a PL/pgSQL function aborts execution of the function, and indeed of the surrounding transaction as well. • You can trap errors and recover from them by using a BEGIN block with an EXCEPTION clause. BEGIN statements EXCEPTION WHEN condition [OR condition ... ] THEN handler_statements [ WHEN condition [ OR condition ... ] THEN handler_statements ... ] END; Greenplum Confidential 62
  • 63. PL/pgSQL – Common Exception Conditions NO_DATA No data matches predicates CONNECTION_FAILURE SQL cannot connect DIVISION_BY_ZERO Invalid division operation 0 in divisor INTERVAL_FIELD_OVERFLOW Insufficient precision for timestamp NULL_VALUE_VIOLATION Tried to insert NULL into NOT NULL column RAISE_EXCEPTION Error raising an exception PLPGSQL_ERROR PL/pgSQL error encountered at run time NO_DATA_FOUND Query returned zero rows TOO_MANY_ROWS Anticipated single row return, received more than 1 row. Greenplum Confidential 63