Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Intro to Stored Procedures in PostgreSQL
개발팀
Why use Stored Procedures?
• Reusability
– Avoid rewriting subqueries and improve readability.
– If you can't store a quer...
Why NOT use Stored Procedures?
• Views may be all you need.
• An object-relational mapper (ORM) can help write queries saf...
Writing a Stored Procedure in SQL
Writing Stored Procedure in PL/pgSQL
Executing get_last_name()
Triggers
• Triggers can be used to call a stored procedure before or after an INSERT, UPDATE, or DELETE
statement on a tab...
Automatic Variables in Triggers
• NEW: The new row for INSERT/UPDATE statements. It is NULL in statement-level triggers.
•...
Writing a Trigger in PL/pgSQL
Watching Triggers in Action
Views are Actually Rules
Example Rule for Updates
Granting Access through Views
Views and Rules Summary
• Relations (tables/views) that are used due to rules get checked against the privileges of the ru...
http://www.day32.com/MySQL/Meetup/Presentations/postgresql_stored_procedures.pdf
References
http://www.postgresql.org/docs...
Postgresql stored procedure
Upcoming SlideShare
Loading in …5
×

Postgresql stored procedure

441 views

Published on

Postgresql stored procedure

Published in: Technology
  • I have always found it hard to meet the requirements of being a student. Ever since my years of high school, I really have no idea what professors are looking for to give good grades. After some google searching, I found this service HelpWriting.net who helped me write my research paper. The final result was amazing, and I highly recommend HelpWriting.net to anyone in the same mindset as me.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Postgresql stored procedure

  1. 1. Intro to Stored Procedures in PostgreSQL 개발팀
  2. 2. Why use Stored Procedures? • Reusability – Avoid rewriting subqueries and improve readability. – If you can't store a query in a library that all the applications can access, you can put that query in a stored procedure. • Separation of duties – You don't trust non-DBA's to write queries. • Data integrity – Use triggers or constraints to prevent bad data from entering. – Run several interdependent queries in a transaction in a single stored procedure. • Event handling – Log changes. – Notify other systems of new data.
  3. 3. Why NOT use Stored Procedures? • Views may be all you need. • An object-relational mapper (ORM) can help write queries safely. • Difficult to version control stored procedures. • Software rollouts may require more DB changes. • Could slow software development process.
  4. 4. Writing a Stored Procedure in SQL
  5. 5. Writing Stored Procedure in PL/pgSQL
  6. 6. Executing get_last_name()
  7. 7. Triggers • Triggers can be used to call a stored procedure before or after an INSERT, UPDATE, or DELETE statement on a table. • Triggers can be called once per each row affected or once per each INSERT, UPDATE, or DELETE statement. • Triggers on many different tables can share the same stored procedure.
  8. 8. Automatic Variables in Triggers • NEW: The new row for INSERT/UPDATE statements. It is NULL in statement-level triggers. • OLD: The old row for UPDATE/DELETE statements. It is NULL in statement-level triggers. • TG_NAME: Name of the trigger. • TG_WHEN: 'BEFORE' or 'AFTER'. • TG_LEVEL: 'ROW' or 'STATEMENT'. • TG_OP: 'INSERT', 'UPDATE', or 'DELETE' • TG_TABLE_NAME: Name of the table that invoked the trigger. • TG_TABLE_SCHEMA: Schema for the table in TG_TABLE_NAME.
  9. 9. Writing a Trigger in PL/pgSQL
  10. 10. Watching Triggers in Action
  11. 11. Views are Actually Rules
  12. 12. Example Rule for Updates
  13. 13. Granting Access through Views
  14. 14. Views and Rules Summary • Relations (tables/views) that are used due to rules get checked against the privileges of the rule owner, not the user invoking the rule. • One of the things that cannot be implemented by rules are some kinds of constraints, especially foreign keys. • A trigger that is fired on INSERT on a view can do the same as a rule: put the data somewhere else and suppress the insert in the view. But it cannot do the same thing on UPDATE or DELETE, because there is no real data in the view relation that could be scanned, and thus the trigger would never get called. • A rule may be more efficient than row-level triggers for bulk updates or deletes. • A rule may be easier to write than a statement-level triggers, since the rule will rewrite complex queries for you.
  15. 15. http://www.day32.com/MySQL/Meetup/Presentations/postgresql_stored_procedures.pdf References http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html#PLPGSQL-ADVANTAGES http://coffeenix.net/doc/db/pgsql_sql_091a/pgsql_sql_091a.html#toc5

×