SlideShare a Scribd company logo
Powering Rails 
Application with 
PostgreSQL 
Nidhi Sarvaiya
Who am I? 
➢ Developer at Icicle technologies from last 7 
years. 
➢ Ruby/Ruby on Rails developer since 2009 
➢ Official Handle @ice_on_rails 
➢ Personal Handle @sarvaiya_nidhi
What Are We Covering Today? 
➢ Why PostgreSQL 
➢ Advance Inbuilt Data Types 
➢ UUID 
➢ Full Text Search 
➢ Indexes 
➢ Third Party Gems
Why PostgreSQL? 
➢ First stable version was 6.0 
released in 1996 
➢ Latest stable version 9.3.5 
➢ Fast and reliable 
➢ Free, community support
Rails 4 Handshakes with PostgreSQL 
➢ As of Rails 4, many 
PostgreSQL features 
are supported out of box 
by Active Record
Built In Data Types 
➢ JSON 
➢ Arrays 
➢ hstore 
➢ Range Types 
➢ PostGis 
➢ Network Types
JSON 
Scenario 
Storing different social media information in your Rails 
Application. 
Traditional Approach 
Each social media sites returns different type of information 
so we usually go for NoSQL database 
Alternative 
Store data in JSON format in PostgreSQL
JSON Implementation 
Migration 
create_table :socials do |t| 
t.json 'profile' 
end 
Querying 
Social.first.profile['name'] => nidhi 
Social.select(“profile”) 
Sample Twitter JSON 
{ 
“username” :“sarvaiya_nidhi”, 
“name”:”nidhi”, 
“followers”:80 
}
Array 
Scenario 
Need to add multiple tags and ratings for Book 
Traditional Approach 
Add Separate table and map it with Book 
Alternative 
Add array field in Book table to map multiple tags and 
ratings
Array Implementation 
Migration 
create_table :books do |t| 
t.string :title 
t.string :tags, array: true 
end 
Querying 
Get Books with specific tag 
Book.where("’fiction’ = ANY (tags)") 
Get Books with all tags or multiple tags 
Book.where("’fiction’ = ALL (tags)") 
Book.where("tags @> ARRAY[?]::varchar[]", 
["fantasy", "fiction"]) 
Sampe Book Data 
Book 1 
<Book id: 1, title: "Brave New World", tags: 
["fantasy", "fiction"] > 
Book 2 
<Book id: 2, title: "The Hit", tags: 
["suspense", "thriller"] >
Hstore 
HStore is a key value store within Postgres. 
Scenario 
Need to apply different configuration for different clients 
Traditional Approach 
Maintain multiple entries in settings table by adding 
Key/Value details for each client 
Alternative 
Use Hstore to maintain all setting for client in single record
HStore Implementation 
Migration 
def self.up 
execute "CREATE EXTENSION hstore" 
end 
create_table :clients do |t| 
t.hstore 'settings' 
end 
Querying 
client = Client.first 
client.settings # => { "background-color" => "blue", 
“font-size”:”14px" } 
Get Specific value - client.settings["-background-color"] 
= "blue" 
Sample Hstore Data 
settings: { "background-color" => 
"blue", 
“font-size”:”14px" }
Range Types 
➢ daterange 
➢ int4range 
➢ int8range 
➢numrange 
➢ tsrange 
➢ tstzrange 
Let’s look at some of the them with example ->
Range Type - daterange 
Scenario 
Implement functionality of booking hotels room for specific 
duration 
Traditional Approach 
Need to maintain two different fields capturing start date 
and end date for booking duration 
Alternative 
Use daterange data type to store duration details
daterange Implementation 
Migration 
create_table :rooms do |t| 
t.daterange 'duration' 
end 
Querying 
All Events on a given date: 
Room.where("duration @> ?::date", 
Date.new(2014, 8, 14)) 
Sample Data 
Room1 
10/08/2014 - 14/08/2014 
Room1 
16/08/2014 - 21/08/2014
Range Type - int4range 
Scenario 
Implement functionality of capturing weather forecast 
Traditional Approach 
Need to maintain two different fields capturing high and low 
temperature details 
Alternative 
Use int4range data type to store temperature details
int4range Implementation 
Migration 
create_table :forecasts do |t| 
t.string :city 
t.int4range :hi_lo 
end 
Querying 
Forecast.create!(city: “mumbai”, hi_lo: 
29..32) 
Forecast.first.hi..lo => 29..32 
Sample Data 
Mumbai: 29..32 
Delhi: 31..24
PostGis 
➢ Geospatial extension to Postgres 
➢Support for Geographic objects 
➢New data types 
➢Functions to work with those data types 
➢ Spatial Index Support
Setup & Configuration 
➢ Installing Postgis 
➢ Add gems to support postgis with Active Record 
○ activerecord-postgis-adaptor 
○ rgeo 
➢ Add postgis adaptor in database.yml 
Reference 
http://daniel-azuma.com/articles/georails
Postgis Data Types 
➢ geometry - Any geometric type 
➢ point - Point data 
➢ line_string - LineString data 
➢ polygon - Polygon data 
➢ geometry_collection - Any collection type 
➢ multi_point - A collection of Points 
➢ multi_line_string - A collection of LineStrings 
➢ multi_polygon - A collection of Polygons
Network DataType 
➢ PostgreSQL comes with column types exclusively for IPv4, IPv6, 
and MAC addresses. 
➢ Active Record datatypes - inet, cidr and macaddre 
Migration 
create_table :network_addresses do |t| 
t.inet :inet_address 
t.cidr :cidr_address 
t.macaddr :mac_address 
end
UUID (Universally Unique Identifier) 
➢ Most of the applications now have API, so use UUID rather than 
integer for the ids 
➢ The uuid column type represents a universally unique identifier 
(UUID), a 128-bit value that is generated by an algorithm that 
makes it highly unlikely that the same value can be generated 
twice. 
➢ Sharding would be easier. Even re-merging all the data into a 
single database, if that's something you need at some point.
UUID ImplementaTion 
Enabling UUID Support 
def change 
enable_extension 'uuid-ossp' 
end 
Migration 
create_table :users, id: false do |t| 
t.primary_key :id, :uuid 
end 
or 
create_table :revisions do |t| 
t.column :identifier, :uuid 
end
Full Text Search 
➢ Since PostgreSQL 8.3 
➢ TSVECTOR to represent text data 
➢ TSQUERY to represent search predicates 
➢ Use pg_search gem for Rails application
Indexes Support in PostgreSQL 
➢B-Tree Indexes 
○ Unique Index 
○ Sorted Index 
○ Partial Indexes 
➢ Functional Indexes 
➢ Multi Column Indexes 
➢ GIST & GIN indexes
Gems 
➢ Full Text Search Gem 
○ pg_search - https://github.com/Casecommons/pg_search 
➢Database Insight 
○ pghero - https://github.com/ankane/pghero 
➢ Postgres Extensions 
○ postgres_ext - https://github.com/dockyard/postgres_ext
References 
http://edgeguides.rubyonrails.org/active_record 
_postgresql.html 
http://www.informit.com/articles/article.aspx?p= 
2220311&seqNum=13
THANK 
YOU

More Related Content

What's hot

Apache Cassandra Lunch #70: Basics of Apache Cassandra
Apache Cassandra Lunch #70: Basics of Apache CassandraApache Cassandra Lunch #70: Basics of Apache Cassandra
Apache Cassandra Lunch #70: Basics of Apache Cassandra
Anant Corporation
 
2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb
antoinegirbal
 
Handle 08
Handle 08Handle 08
Handle 08
Tony Hammond
 
Data Binding in Grails
Data Binding in GrailsData Binding in Grails
Data Binding in Grails
TO THE NEW | Technology
 
Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)
Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)
Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)
Kyle Davis
 
Андрей Козлов (Altoros): Оптимизация производительности Cassandra
Андрей Козлов (Altoros): Оптимизация производительности CassandraАндрей Козлов (Altoros): Оптимизация производительности Cassandra
Андрей Козлов (Altoros): Оптимизация производительности Cassandra
Olga Lavrentieva
 
GAAD Database presentation
GAAD Database presentationGAAD Database presentation
GAAD Database presentation
Md. Tariqul Islam
 
Qtp training session II
Qtp training session IIQtp training session II
Qtp training session II
Aisha Mazhar
 
What’s new in Alluxio 2: from seamless operations to structured data management
What’s new in Alluxio 2: from seamless operations to structured data managementWhat’s new in Alluxio 2: from seamless operations to structured data management
What’s new in Alluxio 2: from seamless operations to structured data management
Alluxio, Inc.
 
MongoDB World 2016: The Best IoT Analytics with MongoDB
MongoDB World 2016: The Best IoT Analytics with MongoDBMongoDB World 2016: The Best IoT Analytics with MongoDB
MongoDB World 2016: The Best IoT Analytics with MongoDB
MongoDB
 
Mongodb in-anger-boston-rb-2011
Mongodb in-anger-boston-rb-2011Mongodb in-anger-boston-rb-2011
Mongodb in-anger-boston-rb-2011
bostonrb
 
Need for Time series Database
Need for Time series DatabaseNeed for Time series Database
Need for Time series Database
Pramit Choudhary
 
ADO.net control
ADO.net controlADO.net control
ADO.net control
Paneliya Prince
 
ElasticSearch Basics
ElasticSearch BasicsElasticSearch Basics
ElasticSearch Basics
Amresh Singh
 
Time Series Data in a Time Series World
Time Series Data in a Time Series WorldTime Series Data in a Time Series World
Time Series Data in a Time Series World
MapR Technologies
 
Elastic Search
Elastic SearchElastic Search
Elastic Search
NexThoughts Technologies
 
Whirlwind tour of Hadoop and HIve
Whirlwind tour of Hadoop and HIveWhirlwind tour of Hadoop and HIve
Whirlwind tour of Hadoop and HIve
Edward Capriolo
 
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
Gianfranco Palumbo
 
Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...
Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...
Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...
Trivadis
 
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
leifwalsh
 

What's hot (20)

Apache Cassandra Lunch #70: Basics of Apache Cassandra
Apache Cassandra Lunch #70: Basics of Apache CassandraApache Cassandra Lunch #70: Basics of Apache Cassandra
Apache Cassandra Lunch #70: Basics of Apache Cassandra
 
2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb
 
Handle 08
Handle 08Handle 08
Handle 08
 
Data Binding in Grails
Data Binding in GrailsData Binding in Grails
Data Binding in Grails
 
Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)
Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)
Probabilistic Data Structures (Edmonton Data Science Meetup, March 2018)
 
Андрей Козлов (Altoros): Оптимизация производительности Cassandra
Андрей Козлов (Altoros): Оптимизация производительности CassandraАндрей Козлов (Altoros): Оптимизация производительности Cassandra
Андрей Козлов (Altoros): Оптимизация производительности Cassandra
 
GAAD Database presentation
GAAD Database presentationGAAD Database presentation
GAAD Database presentation
 
Qtp training session II
Qtp training session IIQtp training session II
Qtp training session II
 
What’s new in Alluxio 2: from seamless operations to structured data management
What’s new in Alluxio 2: from seamless operations to structured data managementWhat’s new in Alluxio 2: from seamless operations to structured data management
What’s new in Alluxio 2: from seamless operations to structured data management
 
MongoDB World 2016: The Best IoT Analytics with MongoDB
MongoDB World 2016: The Best IoT Analytics with MongoDBMongoDB World 2016: The Best IoT Analytics with MongoDB
MongoDB World 2016: The Best IoT Analytics with MongoDB
 
Mongodb in-anger-boston-rb-2011
Mongodb in-anger-boston-rb-2011Mongodb in-anger-boston-rb-2011
Mongodb in-anger-boston-rb-2011
 
Need for Time series Database
Need for Time series DatabaseNeed for Time series Database
Need for Time series Database
 
ADO.net control
ADO.net controlADO.net control
ADO.net control
 
ElasticSearch Basics
ElasticSearch BasicsElasticSearch Basics
ElasticSearch Basics
 
Time Series Data in a Time Series World
Time Series Data in a Time Series WorldTime Series Data in a Time Series World
Time Series Data in a Time Series World
 
Elastic Search
Elastic SearchElastic Search
Elastic Search
 
Whirlwind tour of Hadoop and HIve
Whirlwind tour of Hadoop and HIveWhirlwind tour of Hadoop and HIve
Whirlwind tour of Hadoop and HIve
 
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
 
Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...
Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...
Trivadis TechEvent 2016 Polybase challenges Hive relational access to non-rel...
 
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
 

Similar to Powering Rails Application With PostgreSQL

Siddhi - cloud-native stream processor
Siddhi - cloud-native stream processorSiddhi - cloud-native stream processor
Siddhi - cloud-native stream processor
Sriskandarajah Suhothayan
 
Cascading introduction
Cascading introductionCascading introduction
Cascading introduction
Alex Su
 
Big data week presentation
Big data week presentationBig data week presentation
Big data week presentation
Joseph Adler
 
Real-time Big Data Processing with Storm
Real-time Big Data Processing with StormReal-time Big Data Processing with Storm
Real-time Big Data Processing with Storm
viirya
 
Rails israel 2013
Rails israel 2013Rails israel 2013
Rails israel 2013
Reuven Lerner
 
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
GeeksLab Odessa
 
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
NoSQLmatters
 
Brightstar DB
Brightstar DBBrightstar DB
Brightstar DB
Connected Data World
 
WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...
WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...
WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...
WSO2
 
WSO2 Analytics Platform: The one stop shop for all your data needs
WSO2 Analytics Platform: The one stop shop for all your data needsWSO2 Analytics Platform: The one stop shop for all your data needs
WSO2 Analytics Platform: The one stop shop for all your data needs
Sriskandarajah Suhothayan
 
Big Data Technologies - Hadoop
Big Data Technologies - HadoopBig Data Technologies - Hadoop
Big Data Technologies - Hadoop
Talentica Software
 
Coding serbia
Coding serbiaCoding serbia
Coding serbia
Dusan Zamurovic
 
Elasticsearch an overview
Elasticsearch   an overviewElasticsearch   an overview
Elasticsearch an overview
Amit Juneja
 
Big Data, Data Lake, Fast Data - Dataserialiation-Formats
Big Data, Data Lake, Fast Data - Dataserialiation-FormatsBig Data, Data Lake, Fast Data - Dataserialiation-Formats
Big Data, Data Lake, Fast Data - Dataserialiation-Formats
Guido Schmutz
 
Microsoft Azure Big Data Analytics
Microsoft Azure Big Data AnalyticsMicrosoft Azure Big Data Analytics
Microsoft Azure Big Data Analytics
Mark Kromer
 
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
DicodingEvent
 
AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)
Paul Chao
 
Storm real-time processing
Storm real-time processingStorm real-time processing
Storm real-time processing
Michael Vogiatzis
 
WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...
WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...
WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...
WSO2
 
Modeling with Document Database: 5 Key Patterns
Modeling with Document Database: 5 Key PatternsModeling with Document Database: 5 Key Patterns
Modeling with Document Database: 5 Key Patterns
Dan Sullivan, Ph.D.
 

Similar to Powering Rails Application With PostgreSQL (20)

Siddhi - cloud-native stream processor
Siddhi - cloud-native stream processorSiddhi - cloud-native stream processor
Siddhi - cloud-native stream processor
 
Cascading introduction
Cascading introductionCascading introduction
Cascading introduction
 
Big data week presentation
Big data week presentationBig data week presentation
Big data week presentation
 
Real-time Big Data Processing with Storm
Real-time Big Data Processing with StormReal-time Big Data Processing with Storm
Real-time Big Data Processing with Storm
 
Rails israel 2013
Rails israel 2013Rails israel 2013
Rails israel 2013
 
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
 
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
 
Brightstar DB
Brightstar DBBrightstar DB
Brightstar DB
 
WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...
WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...
WSO2Con ASIA 2016: WSO2 Analytics Platform: The One Stop Shop for All Your Da...
 
WSO2 Analytics Platform: The one stop shop for all your data needs
WSO2 Analytics Platform: The one stop shop for all your data needsWSO2 Analytics Platform: The one stop shop for all your data needs
WSO2 Analytics Platform: The one stop shop for all your data needs
 
Big Data Technologies - Hadoop
Big Data Technologies - HadoopBig Data Technologies - Hadoop
Big Data Technologies - Hadoop
 
Coding serbia
Coding serbiaCoding serbia
Coding serbia
 
Elasticsearch an overview
Elasticsearch   an overviewElasticsearch   an overview
Elasticsearch an overview
 
Big Data, Data Lake, Fast Data - Dataserialiation-Formats
Big Data, Data Lake, Fast Data - Dataserialiation-FormatsBig Data, Data Lake, Fast Data - Dataserialiation-Formats
Big Data, Data Lake, Fast Data - Dataserialiation-Formats
 
Microsoft Azure Big Data Analytics
Microsoft Azure Big Data AnalyticsMicrosoft Azure Big Data Analytics
Microsoft Azure Big Data Analytics
 
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
 
AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)
 
Storm real-time processing
Storm real-time processingStorm real-time processing
Storm real-time processing
 
WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...
WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...
WSO2Con USA 2015: WSO2 Analytics Platform - The One Stop Shop for All Your Da...
 
Modeling with Document Database: 5 Key Patterns
Modeling with Document Database: 5 Key PatternsModeling with Document Database: 5 Key Patterns
Modeling with Document Database: 5 Key Patterns
 

Recently uploaded

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 

Recently uploaded (20)

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 

Powering Rails Application With PostgreSQL

  • 1. Powering Rails Application with PostgreSQL Nidhi Sarvaiya
  • 2. Who am I? ➢ Developer at Icicle technologies from last 7 years. ➢ Ruby/Ruby on Rails developer since 2009 ➢ Official Handle @ice_on_rails ➢ Personal Handle @sarvaiya_nidhi
  • 3. What Are We Covering Today? ➢ Why PostgreSQL ➢ Advance Inbuilt Data Types ➢ UUID ➢ Full Text Search ➢ Indexes ➢ Third Party Gems
  • 4. Why PostgreSQL? ➢ First stable version was 6.0 released in 1996 ➢ Latest stable version 9.3.5 ➢ Fast and reliable ➢ Free, community support
  • 5. Rails 4 Handshakes with PostgreSQL ➢ As of Rails 4, many PostgreSQL features are supported out of box by Active Record
  • 6. Built In Data Types ➢ JSON ➢ Arrays ➢ hstore ➢ Range Types ➢ PostGis ➢ Network Types
  • 7. JSON Scenario Storing different social media information in your Rails Application. Traditional Approach Each social media sites returns different type of information so we usually go for NoSQL database Alternative Store data in JSON format in PostgreSQL
  • 8. JSON Implementation Migration create_table :socials do |t| t.json 'profile' end Querying Social.first.profile['name'] => nidhi Social.select(“profile”) Sample Twitter JSON { “username” :“sarvaiya_nidhi”, “name”:”nidhi”, “followers”:80 }
  • 9. Array Scenario Need to add multiple tags and ratings for Book Traditional Approach Add Separate table and map it with Book Alternative Add array field in Book table to map multiple tags and ratings
  • 10. Array Implementation Migration create_table :books do |t| t.string :title t.string :tags, array: true end Querying Get Books with specific tag Book.where("’fiction’ = ANY (tags)") Get Books with all tags or multiple tags Book.where("’fiction’ = ALL (tags)") Book.where("tags @> ARRAY[?]::varchar[]", ["fantasy", "fiction"]) Sampe Book Data Book 1 <Book id: 1, title: "Brave New World", tags: ["fantasy", "fiction"] > Book 2 <Book id: 2, title: "The Hit", tags: ["suspense", "thriller"] >
  • 11. Hstore HStore is a key value store within Postgres. Scenario Need to apply different configuration for different clients Traditional Approach Maintain multiple entries in settings table by adding Key/Value details for each client Alternative Use Hstore to maintain all setting for client in single record
  • 12. HStore Implementation Migration def self.up execute "CREATE EXTENSION hstore" end create_table :clients do |t| t.hstore 'settings' end Querying client = Client.first client.settings # => { "background-color" => "blue", “font-size”:”14px" } Get Specific value - client.settings["-background-color"] = "blue" Sample Hstore Data settings: { "background-color" => "blue", “font-size”:”14px" }
  • 13. Range Types ➢ daterange ➢ int4range ➢ int8range ➢numrange ➢ tsrange ➢ tstzrange Let’s look at some of the them with example ->
  • 14. Range Type - daterange Scenario Implement functionality of booking hotels room for specific duration Traditional Approach Need to maintain two different fields capturing start date and end date for booking duration Alternative Use daterange data type to store duration details
  • 15. daterange Implementation Migration create_table :rooms do |t| t.daterange 'duration' end Querying All Events on a given date: Room.where("duration @> ?::date", Date.new(2014, 8, 14)) Sample Data Room1 10/08/2014 - 14/08/2014 Room1 16/08/2014 - 21/08/2014
  • 16. Range Type - int4range Scenario Implement functionality of capturing weather forecast Traditional Approach Need to maintain two different fields capturing high and low temperature details Alternative Use int4range data type to store temperature details
  • 17. int4range Implementation Migration create_table :forecasts do |t| t.string :city t.int4range :hi_lo end Querying Forecast.create!(city: “mumbai”, hi_lo: 29..32) Forecast.first.hi..lo => 29..32 Sample Data Mumbai: 29..32 Delhi: 31..24
  • 18. PostGis ➢ Geospatial extension to Postgres ➢Support for Geographic objects ➢New data types ➢Functions to work with those data types ➢ Spatial Index Support
  • 19. Setup & Configuration ➢ Installing Postgis ➢ Add gems to support postgis with Active Record ○ activerecord-postgis-adaptor ○ rgeo ➢ Add postgis adaptor in database.yml Reference http://daniel-azuma.com/articles/georails
  • 20. Postgis Data Types ➢ geometry - Any geometric type ➢ point - Point data ➢ line_string - LineString data ➢ polygon - Polygon data ➢ geometry_collection - Any collection type ➢ multi_point - A collection of Points ➢ multi_line_string - A collection of LineStrings ➢ multi_polygon - A collection of Polygons
  • 21. Network DataType ➢ PostgreSQL comes with column types exclusively for IPv4, IPv6, and MAC addresses. ➢ Active Record datatypes - inet, cidr and macaddre Migration create_table :network_addresses do |t| t.inet :inet_address t.cidr :cidr_address t.macaddr :mac_address end
  • 22. UUID (Universally Unique Identifier) ➢ Most of the applications now have API, so use UUID rather than integer for the ids ➢ The uuid column type represents a universally unique identifier (UUID), a 128-bit value that is generated by an algorithm that makes it highly unlikely that the same value can be generated twice. ➢ Sharding would be easier. Even re-merging all the data into a single database, if that's something you need at some point.
  • 23. UUID ImplementaTion Enabling UUID Support def change enable_extension 'uuid-ossp' end Migration create_table :users, id: false do |t| t.primary_key :id, :uuid end or create_table :revisions do |t| t.column :identifier, :uuid end
  • 24. Full Text Search ➢ Since PostgreSQL 8.3 ➢ TSVECTOR to represent text data ➢ TSQUERY to represent search predicates ➢ Use pg_search gem for Rails application
  • 25. Indexes Support in PostgreSQL ➢B-Tree Indexes ○ Unique Index ○ Sorted Index ○ Partial Indexes ➢ Functional Indexes ➢ Multi Column Indexes ➢ GIST & GIN indexes
  • 26. Gems ➢ Full Text Search Gem ○ pg_search - https://github.com/Casecommons/pg_search ➢Database Insight ○ pghero - https://github.com/ankane/pghero ➢ Postgres Extensions ○ postgres_ext - https://github.com/dockyard/postgres_ext
  • 27. References http://edgeguides.rubyonrails.org/active_record _postgresql.html http://www.informit.com/articles/article.aspx?p= 2220311&seqNum=13