LAMP was originally Linux, Apache, MySQL, PHP. While the L & A have parts have become more flexible, most still use MySQL. With the recent acquisition by Oracle there's no better time to demystify PostgreSQL. For years PostgreSQL has had a reputation of being difficult, but this is the furthest from the truth.
This presentation by Asher Snyder will cover installation, basic queries, stored procedures, triggers, and full-text search.
Presentation that I gave as a guest lecture for a summer intensive development course at nod coworking in Dallas, TX. The presentation targets beginning web developers with little, to no experience in databases, SQL, or PostgreSQL. I cover the creation of a database, creating records, reading/querying records, updating records, destroying records, joining tables, and a brief introduction to transactions.
Presentation that I gave as a guest lecture for a summer intensive development course at nod coworking in Dallas, TX. The presentation targets beginning web developers with little, to no experience in databases, SQL, or PostgreSQL. I cover the creation of a database, creating records, reading/querying records, updating records, destroying records, joining tables, and a brief introduction to transactions.
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...DataStax
The 3.0 storage engine re-write is the biggest and most exciting change to ever happen in Apache Cassandra. The new storage engine can efficiently store and read data from disk using the same concepts present in the CQL 3 language. This has delivered large space savings, and creates new performance characteristics.
In this talk Aaron Morton, Co Founder at The Last Pickle and Apache Cassandra Committer, will discuss the 3.0 storage engine, it's layout and performance characteristics.
About the Speaker
Aaron Morton CEO, The Last Pickle
Aaron Morton is the Co Founder & CEO at The Last Pickle (thelastpickle.com). A professional services company that works with clients to deliver and improve Apache Cassandra based solutions. He's based in New Zealand, is an Apache Cassandra Committer and a DataStax MVP for Apache Cassandra.
This presentation is for those who are familiar with databases and SQL, but want to learn how to move processing from their applications into the database to improve consistency, administration, and performance. Topics covered include advanced SQL features like referential integrity constraints, ANSI joins, views, rules, and triggers. The presentation also explains how to create server-side functions, operators, and custom data types in PostgreSQL.
An Introduction to Basics of Search and Relevancy with Apache SolrLucidworks (Archived)
The open source Apache Solr open source search engine provides powerful, versatile search application development technology so you to take full control of your search needs. Solr’s rich interfaces and convenient server packaging of the underlying Apache Lucene search libraries into web service interfaces, and near limitless customizability let you take control of your search. From e-commerce to content management and endless variations in between, Solr is the right tool at the right time to turn ever growing volume and variety of data and documents to the advantage of your business.http://www.lucidimagination.com/blog/2009/12/01/webinar-an-introduction-to-basics-of-search-and-relevancy-with-apache-solr/
En base a algoritmos de clasificación se muestra un ejemplo implementado en weka. Trata de predecir el tipo de fármaco (droga) que se debe administrar a un paciente afectado de rinitis alérgica según distintos parámetros/variables y ver qué fármacos son más comunes en general para ver si todos son igualmente efectivos.
Connection Pooling in PostgreSQL using pgbouncer Sameer Kumar
The presentation was presented at 5th Postgres User Group, Singapore.
It explain how to setup pgbouncer and also shows a few demonstration graphs comparing the advantages/gains in performance when using pgbouncer instead of direct connections to PostgreSQL database.
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...DataStax
The 3.0 storage engine re-write is the biggest and most exciting change to ever happen in Apache Cassandra. The new storage engine can efficiently store and read data from disk using the same concepts present in the CQL 3 language. This has delivered large space savings, and creates new performance characteristics.
In this talk Aaron Morton, Co Founder at The Last Pickle and Apache Cassandra Committer, will discuss the 3.0 storage engine, it's layout and performance characteristics.
About the Speaker
Aaron Morton CEO, The Last Pickle
Aaron Morton is the Co Founder & CEO at The Last Pickle (thelastpickle.com). A professional services company that works with clients to deliver and improve Apache Cassandra based solutions. He's based in New Zealand, is an Apache Cassandra Committer and a DataStax MVP for Apache Cassandra.
This presentation is for those who are familiar with databases and SQL, but want to learn how to move processing from their applications into the database to improve consistency, administration, and performance. Topics covered include advanced SQL features like referential integrity constraints, ANSI joins, views, rules, and triggers. The presentation also explains how to create server-side functions, operators, and custom data types in PostgreSQL.
An Introduction to Basics of Search and Relevancy with Apache SolrLucidworks (Archived)
The open source Apache Solr open source search engine provides powerful, versatile search application development technology so you to take full control of your search needs. Solr’s rich interfaces and convenient server packaging of the underlying Apache Lucene search libraries into web service interfaces, and near limitless customizability let you take control of your search. From e-commerce to content management and endless variations in between, Solr is the right tool at the right time to turn ever growing volume and variety of data and documents to the advantage of your business.http://www.lucidimagination.com/blog/2009/12/01/webinar-an-introduction-to-basics-of-search-and-relevancy-with-apache-solr/
En base a algoritmos de clasificación se muestra un ejemplo implementado en weka. Trata de predecir el tipo de fármaco (droga) que se debe administrar a un paciente afectado de rinitis alérgica según distintos parámetros/variables y ver qué fármacos son más comunes en general para ver si todos son igualmente efectivos.
Connection Pooling in PostgreSQL using pgbouncer Sameer Kumar
The presentation was presented at 5th Postgres User Group, Singapore.
It explain how to setup pgbouncer and also shows a few demonstration graphs comparing the advantages/gains in performance when using pgbouncer instead of direct connections to PostgreSQL database.
In recent years Zalando has adopted a decentralized setup for applications and databases. This has impacted our database engineers by transferring responsibility to small teams, each of which manages its own infrastructure. Decentralization is great for team autonomy, but can present challenges in terms of how to easily manage lots of PostgreSQL clusters. That’s why our team created Spilo: an open source HA-cluster (highly available PostgreSQL cluster). This talk will show how Spilo simplifies Postgres cluster management while preserving team autonomy. By building upon streaming replication, Spilo provides a set of clusters that require no human interaction for many administration tasks and most failure scenarios; takes care of managing the number of servers (adding and removing them); and creates backups. It implements our own version of Patroni (https://github.com/zalando/patroni): a process, derived from Compose’s Governor, that governs the Postgres cluster (promoting and demoting) and updates information in etcd (the distributed consensus key/value store created by CoreOS). I’ll explore the architecture of Patroni implemented with Spilo; a live demo will show some failovers as they occur. Finally, I’ll show how Spilo combines Patroni with cloud infrastructure architecture components (for example, AWS), adding autoscaling to run a HA-cluster and allowing AWS power users to create a new HA-cluster with very little effort. Spilo relies upon STUPS, Zalando’s open-source platform as a service (PaaS) for enabling multiple, autonomous teams to use AWS while remaining audit-compliant. By using Spilo and STUPS together, our engineers can create a new HA-cluster with just a few commands. After attending this talk the audience will understand how they can also use Spilo, Patroni and STUPS to manage their Postgres clusters more efficiently while working autonomously.
Managing Databases In A DevOps Environment 2016Robert Treat
Given at #pgdayphilly2016, this talk covers how configuration management, monitoring, and rapid deployments are impacting how we think about database management.
Rust was initially designed as a systems language. The ecosystem has grown fast, and we can now develop full webaps with it. This talk walks through the various components that make up a simple webapp and how they fit nicely together with the language features.
This talk was given at the DevFest conference in Toulouse, France, in november 2018.
AWS re:Invent 2016: How Citus Enables Scalable PostgreSQL on AWS (DAT207)Amazon Web Services
Join the principal engineer of Citus Cloud for a brief overview of Citus, best use cases for it, and a drill down into how it's run and managed as a hosted service on top of AWS. The orchestration of Citus is homegrown, but comes from years of experience of running millions of PostgreSQL databases on top of AWS. Even if you aren't looking to leverage Citus to help you scale out, in this session you'll gain insights applicable to running and managing your stateful services on top of AWS. Citus is a PostgreSQL extension that transforms the database into a distributed, horizontally scalable database. Companies like Cloudflare use Citus to process 40 TB per day. With Citus MX, applications can take advantage of every node in the cluster for writes and yielding near-linear write scaling. Citus MX provide up to 500,000 durable writes per second.
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuRedis Labs
Postgres and Redis Sitting in a Tree | In today’s world of polyglot persistence, it’s likely that companies will be using multiple data stores for storing and working with data based on the use case. Typically a company will
start with a relational database like Postgres and then add Redis for more high velocity use-cases. What if you could tie the two systems together to enable so much more?
If you've ever run into issues with your database failing, database size, table/row sizes, and inheriting DNN sites - this presentation was built for you. During this presentation, we discuss these topics and more... Tips & tricks that were gathered over several years of managing DNN websites.
BASE is an open source python framework based on tornado asynchronous web framework, which represents an upgrade by adding a decorators library that enables fast and simple development of REST-API based applications and it reduces the need for coding frequently used segments of code. Besides that, BASE can be used as an engine for web sites.
Basic of and Unix and Command. More presentation you can find on www.scmGalaxy.com.
scmGalaxy.com is dedicated to software configuration, build and Release management. This covers CVS, VSS (Visual Source Safe),Perforce, SVN(Subversion) MKS Integrity, ClearCase,TFS,CM Synergy, Best Practices ,AnthillPro, Apache Ant, Maven, Bamboo, Cruise Control and many more tools.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
Are you looking to streamline your workflows and boost your projects’ efficiency? Do you find yourself searching for ways to add flexibility and control over your FME workflows? If so, you’re in the right place.
Join us for an insightful dive into the world of FME parameters, a critical element in optimizing workflow efficiency. This webinar marks the beginning of our three-part “Essentials of Automation” series. This first webinar is designed to equip you with the knowledge and skills to utilize parameters effectively: enhancing the flexibility, maintainability, and user control of your FME projects.
Here’s what you’ll gain:
- Essentials of FME Parameters: Understand the pivotal role of parameters, including Reader/Writer, Transformer, User, and FME Flow categories. Discover how they are the key to unlocking automation and optimization within your workflows.
- Practical Applications in FME Form: Delve into key user parameter types including choice, connections, and file URLs. Allow users to control how a workflow runs, making your workflows more reusable. Learn to import values and deliver the best user experience for your workflows while enhancing accuracy.
- Optimization Strategies in FME Flow: Explore the creation and strategic deployment of parameters in FME Flow, including the use of deployment and geometry parameters, to maximize workflow efficiency.
- Pro Tips for Success: Gain insights on parameterizing connections and leveraging new features like Conditional Visibility for clarity and simplicity.
We’ll wrap up with a glimpse into future webinars, followed by a Q&A session to address your specific questions surrounding this topic.
Don’t miss this opportunity to elevate your FME expertise and drive your projects to new heights of efficiency.
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
A tale of scale & speed: How the US Navy is enabling software delivery from l...sonjaschweigert1
Rapid and secure feature delivery is a goal across every application team and every branch of the DoD. The Navy’s DevSecOps platform, Party Barge, has achieved:
- Reduction in onboarding time from 5 weeks to 1 day
- Improved developer experience and productivity through actionable findings and reduction of false positives
- Maintenance of superior security standards and inherent policy enforcement with Authorization to Operate (ATO)
Development teams can ship efficiently and ensure applications are cyber ready for Navy Authorizing Officials (AOs). In this webinar, Sigma Defense and Anchore will give attendees a look behind the scenes and demo secure pipeline automation and security artifacts that speed up application ATO and time to production.
We will cover:
- How to remove silos in DevSecOps
- How to build efficient development pipeline roles and component templates
- How to deliver security artifacts that matter for ATO’s (SBOMs, vulnerability reports, and policy evidence)
- How to streamline operations with automated policy checks on container images
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfPeter Spielvogel
Building better applications for business users with SAP Fiori.
• What is SAP Fiori and why it matters to you
• How a better user experience drives measurable business benefits
• How to get started with SAP Fiori today
• How SAP Fiori elements accelerates application development
• How SAP Build Code includes SAP Fiori tools and other generative artificial intelligence capabilities
• How SAP Fiori paves the way for using AI in SAP apps
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
PHP Frameworks: I want to break free (IPC Berlin 2024)Ralf Eggert
In this presentation, we examine the challenges and limitations of relying too heavily on PHP frameworks in web development. We discuss the history of PHP and its frameworks to understand how this dependence has evolved. The focus will be on providing concrete tips and strategies to reduce reliance on these frameworks, based on real-world examples and practical considerations. The goal is to equip developers with the skills and knowledge to create more flexible and future-proof web applications. We'll explore the importance of maintaining autonomy in a rapidly changing tech landscape and how to make informed decisions in PHP development.
This talk is aimed at encouraging a more independent approach to using PHP frameworks, moving towards a more flexible and future-proof approach to PHP development.
State of ICS and IoT Cyber Threat Landscape Report 2024 previewPrayukth K V
The IoT and OT threat landscape report has been prepared by the Threat Research Team at Sectrio using data from Sectrio, cyber threat intelligence farming facilities spread across over 85 cities around the world. In addition, Sectrio also runs AI-based advanced threat and payload engagement facilities that serve as sinks to attract and engage sophisticated threat actors, and newer malware including new variants and latent threats that are at an earlier stage of development.
The latest edition of the OT/ICS and IoT security Threat Landscape Report 2024 also covers:
State of global ICS asset and network exposure
Sectoral targets and attacks as well as the cost of ransom
Global APT activity, AI usage, actor and tactic profiles, and implications
Rise in volumes of AI-powered cyberattacks
Major cyber events in 2024
Malware and malicious payload trends
Cyberattack types and targets
Vulnerability exploit attempts on CVEs
Attacks on counties – USA
Expansion of bot farms – how, where, and why
In-depth analysis of the cyber threat landscape across North America, South America, Europe, APAC, and the Middle East
Why are attacks on smart factories rising?
Cyber risk predictions
Axis of attacks – Europe
Systemic attacks in the Middle East
Download the full report from here:
https://sectrio.com/resources/ot-threat-landscape-reports/sectrio-releases-ot-ics-and-iot-security-threat-landscape-report-2024/
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionAggregage
Join Maher Hanafi, VP of Engineering at Betterworks, in this new session where he'll share a practical framework to transform Gen AI prototypes into impactful products! He'll delve into the complexities of data collection and management, model selection and optimization, and ensuring security, scalability, and responsible use.
2. What is PostgreSQL?
• Completely Open Source Database System
– Started in 1995
– Open source project
– Not owned by any one company
– Controlled by the community
– Can’t be bought (Looking at you ORACLE)
• ORDMBS
– Object-relational database management system?
• RDBMS with object-oriented database model
– That’s right, it’s not just an RDMBS
– You can create your own objects
– You can inherit
• Fully ACID compliant
– Atomicity, Consistency, Isolation, Durability. Guarantees that
database transactions are processed reliably.
• ANSI SQL compliant
3. Notable Features
• Transactions
• Functions (Stored procedures)
• Rules
• Views
• Triggers
• Inheritance
• Custom Types
• Referential Integrity
• Array Data Types
• Schemas
• Hot Standby (as of 9.0)
• Streaming Replication (as of 9.0)
4. Support
• Excellent Personal Support!
– Vibrant Community
• Active Mailing List
• Active IRC - #postgres on irc.freenode.net
– Absolutely amazing
• Complete, Extensive and Detailed Documentation
– http://www.postgresql.org/docs/
• Regular and frequent releases and updates
– Public Roadmap
• Support for older builds
– Currently support and release updates to builds as
old as 5 years
5. Support
• Numerous Shared Hosts
– Such as A2hosting (http://www.a2hosting.com)
• Numerous GUI Administration Tools
– pgAdmin (http://www.pgadmin.org/)
– php pgAdmin
(http://phppgadmin.sourceforge.net/)
– Notable commercial tools
• Navicat (http://www.navicat.com)
• EMS (http://sqlmanager.net)
– Many many more
• http://wiki.postgresql.org/wiki/Community_Guide_to_
PostgreSQL_GUI_Tools
6. Installation
• Despite what you’ve heard
Postgres is NOT hard to install
$ apt-get install postgresql
On Ubuntu or Debian:
$ emerge postgresql
On Gentoo:
$ adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
Manual Installation:
7. Installation (cont)
• Regardless of what installation method
you choose. Make sure to modify
postgresql.conf and pg_hba.conf
configuration files if you want to allow
outside access.
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 md5
pg_hba.conf - Controls which hosts are allowed to connect
Allows for connection from any outside connection with md5 verification
# - Connection Settings
listen_addresses = '*' # IP address to listen on
#listen_addresses = 'localhost' # default
postgresql.conf - PostgreSQL configuration file
Allows PostgreSQL to listen on any address
8. Getting Started
$ /etc/init.d/postgresql start
pg_hba.conf - Controls which hosts are allowed to connect
$ /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
alternatively, you can start it manually
Start from distro
9. Creating Your Database
$ psql
Launch psql
CREATE DATABASE first_db;
Create your first PostgreSQL database
c first_db
Connect to your newly created database
11. Tables - Creating
CREATE TABLE users (
"user_id" SERIAL,
"email" TEXT,
"firstname" TEXT,
"lastname" TEXT,
"password" TEXT,
PRIMARY KEY("user_id")
);
• What’s a SERIAL?
– Short for INTEGER NOT NULL with default value of
nextval('users_user_id_seq')
• Similar to AUTO_INCREMENT property of other databases.
12. Tables - Inserting
INSERT INTO users (email, firstname, lastname, password)
VALUES ('asnyder@noloh.com', 'Asher', 'Snyder', 'Pass');
We can explicitly define each field and the value associated with it.
This will set user_id to the default value.
INSERT INTO users VALUES (DEFAULT, 'john@example.com',
'John', 'Doe', 'OtherPass');
Alternatively, we can not specify the column names and insert
based on the column order, using DEFAULT for the user_id.
SELECT * FROM users;
Lets see the results
user_id | email | firstname | lastname | password
--------+-------------------+-----------+------------------+-----------+----------------------------
1 | asnyder@noloh.com | Ash | Snyder | Pass
2 | John | Doe | john@example.com | OtherPass
14. Views
CREATE VIEW v_get_all_users AS
SELECT * FROM users;
• Views allow you store a query for easy retrieval later.
– Query a view as if it were a table
– Allows you to name your query
– Use views as types
– Abstract & encapsulate table structure changes
• Allows for easier modification & extension of your database
Create basic view
Query the view
SELECT * FROM v_get_all_users;
user_id | email | firstname | lastname | password
--------+-------------------+-----------+------------------+-----------+----------------------------
1 | asnyder@noloh.com | Ash | Snyder | Pass
2 | John | Doe | john@example.com | OtherPass
15. Views (cont)
ALTER TABLE users ADD COLUMN "time_created"
TIMESTAMP WITHOUT TIME ZONE DEFAULT
Alter Table
Now, if we were to query the table we would see a timestamp showing us when
the user was created.
SELECT * FROM users;
As you can see, this is not very useful for humans. This is where a view can
come in to make your life easier.
user_id | email | firstname | lastname | password | time_created
--------+-------------------+-----------+------------------+-----------+----------------------------
1 | asnyder@noloh.com | Ash | Snyder | Pass | 2010-10-27 14:30:07.335936
2 | John | Doe | john@example.com | OtherPass | 2010-10-27 14:30:07.335936
16. Views (cont)
CREATE OR REPLACE VIEW v_get_all_users
AS
SELECT user_id,
email,
firstname,
lastname,
password,
time_created,
to_char(time_created, 'FMMonth
FMDDth, YYYY FMHH12:MI:SS AM') as
friendly_time
FROM users;
Alter View
17. Views (cont)
Now, when we query the view we can actually interpret time_created
SELECT * FROM v_get_all_users;
user_id | email | firstname | lastname | password | time_created
---------+-------------------+-----------+------------------+-----------+----------------------------
1 | asnyder@noloh.com | Ash | Snyder | Pass | 2010-10-27 14:30:07.335936
2 | John | Doe | john@example.com | OtherPass | 2010-10-27 15:20:05.235936
| friendly_time
+-------------------------------
October 27th, 2010 2:30:07 PM
October 27th, 2010 3:20:05 PM
18. Views (cont) – Joined View
Finally, lets create a joined view
CREATE TABLE companies (
"company_id" SERIAL,
"company_name" TEXT,
PRIMARY KEY("company_id")
);
Create companies table
INSERT INTO companies VALUES(DEFAULT, 'NOLOH LLC.');
Add company
ALTER TABLE users ADD COLUMN company_id INTEGER;
Add company_id to users
UPDATE users SET company_id = 1;
Update users
19. Views (cont) – Joined View
CREATE OR REPLACE VIEW v_get_all_users
AS
SELECT user_id,
email,
firstname,
lastname,
password,
time_created,
to_char(time_created, 'FMMonth FMDDth, YYYY
FMHH12:MI:SS AM') as friendly_time,
t2.company_id,
t2.company_name
FROM users t1
LEFT JOIN companies t2 ON (t1.company_id =
t2.company_id);
Alter view
20. Views (cont)
SELECT * FROM v_get_all_users;
user_id | email | firstname | lastname | password | time_created
---------+-------------------+-----------+------------------+-----------+----------------------------
1 | asnyder@noloh.com | Ash | Snyder | SomePass | 2010-10-27 14:30:07.335936
2 | John | Doe | john@example.com | OtherPass | 2010-10-27 15:20:05.235936
| friendly_time | company_id | company_name
+------------------------------+------------+--------------
October 27th, 2010 2:30:07 PM | 1 | NOLOH LLC.
October 27th, 2010 3:20:05 PM | 1 | NOLOH LLC.
Query view
Nice! Now instead of having to modify a query each time we can just use
v_get_all_users. We can even use this VIEW as a return type when
creating your own database functions.
22. Functions
• Also known as Stored Procedures
• Allows you to carry out operations that would normally
take several queries and round-trips in a single function
within the database
• Allows database re-use as other applications can interact
directly with your stored procedures instead of a middle-
tier or duplicating code
• Can be used in other functions
• First class citizens
– Query functions like tables
– Create functions in language of your choice
• SQL, PL/pgSQL, C, Python, etc.
– Allowed to modify tables and perform multiple operations
– Defaults
– In/Out parameters
23. Functions (cont)
CREATE FUNCTION f_add_company(p_name TEXT) RETURNS
INTEGER
AS
$func$
INSERT INTO companies (company_name) VALUES ($1)
RETURNING company_id;
$func$
LANGUAGE SQL;
Create basic function
Call f_add_company
f_add_company
---------------
2
(1 row)
SELECT f_add_company('Google');
24. Functions (cont) – Syntax Explained
• Return type
– In this case we used INTEGER
• Can be anything you like including your views and own custom types
– Can even return an ARRAY of types, such as INTEGER[]
• Do not confusing this with returning a SET of types.
• Returning multiple rows of a particular types is done
through RETURN SETOF. For example, RETURN SETOF
v_get_all_users.
• $func$ is our delimiter separating the function body. It
can be any string you like. For example $body$ instead of
$func$.
• $1 refers to parameter corresponding to that number. We
can also use the parameter name instead. In our example
this would be 'p_name'.
25. Functions (cont) – PL/pgSQL
• If you’re using PostgreSQL < 9.0 make
sure you add PL/pgSQL support
CREATE TRUSTED PROCEDURAL LANGUAGE "plpgsql"
HANDLER "plpgsql_call_handler"
VALIDATOR "plpgsql_validator";
26. Functions (cont) – PL/pgSQL
CREATE OR REPLACE FUNCTION f_add_company(p_name TEXT) RETURNS INTEGER
AS
$func$
DECLARE
return_var INTEGER;
BEGIN
SELECT INTO return_var company_id FROM companies WHERE
lower(company_name) = lower($1);
IF NOT FOUND THEN
INSERT INTO companies (company_name) VALUES ($1) RETURNING
company_id INTO return_var;
END IF;
RETURN return_var;
END;
$func$
LANGUAGE plpgsql;
27. Functions (cont)
Call f_add_company
f_add_company
---------------
3
(1 row)
SELECT f_add_company('Zend');
Call f_add_company with repeated entry
f_add_company
---------------
2
(1 row)
SELECT f_add_company('Google');
• We can see that using functions in our database allows us
to integrate safeguards and business logic into our
functions allowing for increased modularity and re-use.
29. Triggers
• Specifies a function to be called BEFORE or AFTER any
INSERT, UPDATE, or DELETE operation
– Similar to an event handler (but clearly confined) in event
based programming
– BEFORE triggers fire before the data is actually inserted.
– AFTER triggers fire after the statement is executed and
data is inserted into the row
• Function takes NO parameters and returns type
TRIGGER
• Most commonly used for logging or validation
• Can be defined on entire statement or a per-row basis
– Statement level triggers should always return NULL
• As of 9.0 can be specified for specific columns and
specific WHEN conditions
30. Triggers (cont)
CREATE TABLE logs (
"log_id" SERIAL,
"log" TEXT,
PRIMARY KEY("log_id")
);
Create logs table
CREATE FUNCTION "tr_log_handler"()
RETURNS trigger AS
$func$
DECLARE
log_string TEXT;
BEGIN
log_string := 'User ' || OLD.user_id || ' changed ' || CURRENT_TIMESTAMP;
IF NEW.email != OLD.email THEN
log_string := log_string || 'email changed from '
|| OLD.email || ' to ' || NEW.email || '. ';
END IF;
IF NEW.firstname != OLD.firstname THEN
log_string := log_string || 'firstname changed from '
|| OLD.firstname || ' to ' || NEW.firstname || '. ';
END IF;
IF NEW.lastname != OLD.lastname THEN
log_string := log_string || 'lastname changed from '
|| OLD.lastname || ' to ' || NEW.lastname || '. ';
END IF;
INSERT INTO logs (log) VALUES (log_string);
RETURN NEW;
END;
$func$
LANGUAGE plpgsql;
Create trigger handler
31. Triggers (cont)
CREATE TRIGGER "tr_log" AFTER UPDATE
ON users FOR EACH ROW
EXECUTE PROCEDURE "tr_log_handler"();
Create trigger
log_id | log
--------+---------------------------------------------------------------------------------
1 | User 1 changed 2010-08-30 23:43:23.771347-04 firstname changed from Asher to Ash.
(1 row)
SELECT * FROM logs;
Update user
UPDATE users SET firstname = 'Ash' WHERE user_id = 1;
Display logs
33. Full-Text Search
• Allows documents to be preprocessed
• Search through text to find matches
• Sort matches based on relevance
– Apply weights to certain attributes to
increase/decrease relevance
• Faster than LIKE, ILIKE, and LIKE with regular
expressions
• Define your own dictionaries
– Define your own words, synonyms, phrase
relationships, word variations
34. Full-Text Search
• tsvector
– Vectorized text data
• tsquery
– Search predicate
• Converted to Normalized lexemes
– Ex. run, runs, ran and running are forms of the same lexeme
• @@
– Match operator
35. Full-Text Search (cont)
SELECT 'phpbarcelona 2010 is a great conference'::tsvector
@@ 'conference & great'::tsquery;
Match Test 1
SELECT 'phpbarcelona 2010 is a great conference'
@@ 'conference & bad'
Match Test 2
?column?
---------
t
(1 row)
?column?
---------
f
(1 row)
36. Full-Text Search (cont) – to_
SELECT to_tsvector('phpbarcelona 2010 is a great conference‘)
@@ plainto_tsquery('conference & great') ;
to_tsvector & plainto_tsquery
SELECT * FROM companies WHERE to_tsvector(company_name) @@
to_tsquery('unlimited');
Search through tables
?column?
---------
t
(1 row)
company_id | company_name
------------+--------------
(0 rows)
37. Full-Text Search
• setweight
– Possible weights are A, B, C, D
• equivalent 1.0, 0.4, 0.2, 0.1 respectively
• ts_rank
– Normal ranking function
• ts_rank_cd
– uses the cover density method of ranking, as
specified in Clarke, Cormack, and Tudhope’s
“Relevance Ranking for One to Three Term
Queries” in the journal, “Information Processing
and Management”, 1999.
38. Full-Text Search (cont) - Ranking
SELECT case_id, file_name,
ts_rank_cd(setweight(to_tsvector(coalesce(t1.file_name)), 'A') ||
setweight(to_tsvector(coalesce(t1.contents)), 'B'), query) AS rank
FROM cases.cases t1, plainto_tsquery('copyright') query
WHERE to_tsvector(file_name || ' ' || contents) @@ query
ORDER BY rank DESC LIMIT 10
setweight & ts_rank_cd
case_id | file_name | rank
---------+------------------------------------------------------+------
101 | Harper & Row v Nation Enter 471 US 539.pdf | 84.8
113 | IN Re Literary Works Elec Databases 509 F3d 522.pdf | 76
215 | Lexmark Intl v Static Control 387 F3d 522.pdf | 75.2
283 | Feist Pubs v Rural Tel 499 US 340.pdf | 67.2
216 | Lucks Music Library v Ashcroft 321 F Supp 2d 107.pdf | 59.2
342 | Blue Nile v Ice 478 F Supp 2d 1240.pdf | 50.8
374 | Perfect 10 v Amazon 487 F3d 701.pdf | 43.6
85 | Pillsbury v Milky Way 215 USPQ 124.pdf | 43.6
197 | St Lukes Cataract v Sanderson 573 F3d 1186.pdf | 42
272 | Religious Technology v Netcom 907 F Supp 1361.pdf | 42
(10 rows)