2ndQuadrant is now part of EDB

Bringing together some of the world's top PostgreSQL experts.

2ndQuadrant | PostgreSQL
Mission Critical Databases
  • Contact us
  • EN
    • FR
    • IT
    • ES
    • DE
    • PT
  • Support & Services
  • Products
  • Downloads
    • Installers
      • Postgres Installer
      • 2UDA – Unified Data Analytics
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
  • Postgres Learning Center
    • Webinars
      • Upcoming Webinars
      • Webinar Library
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Blog
    • Training
      • Course Catalogue
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
    • Books
      • PostgreSQL 11 Administration Cookbook
      • PostgreSQL 10 Administration Cookbook
      • PostgreSQL High Availability Cookbook – 2nd Edition
      • PostgreSQL 9 Administration Cookbook – 3rd Edition
      • PostgreSQL Server Programming Cookbook – 2nd Edition
      • PostgreSQL 9 Cookbook – Chinese Edition
    • Videos
    • Events
    • PostgreSQL
      • PostgreSQL – History
      • Who uses PostgreSQL?
      • PostgreSQL FAQ
      • PostgreSQL vs MySQL
      • The Business Case for PostgreSQL
      • Security Information
      • Documentation
  • About Us
    • About 2ndQuadrant
    • 2ndQuadrant’s Passion for PostgreSQL
    • News
    • Careers
    • Team Profile
  • Blog
  • Menu Menu
You are here: Home1 / Blog2 / Greenplum3 / Using dblink in Greenplum
Carlo Ascani

Using dblink in Greenplum

September 28, 2011/1 Comment/in Greenplum /by Carlo Ascani

I’m going to demonstrate how it is possible to use dblink in Greenplum 4.0.4.0


What’s dblink?
——————
dblink is a PostgreSQL contrib module that allows to execute queries on another database.
Current PostgreSQL’s architeture requires users to connect to a specific database on a server. Therefore, it is not possible to natively perform an SQL query on a different database.
Normally we discourage using dblink to query remote databases.
Databases are self-contained objects in PostgreSQL, and they should be designed with that in mind.
We always advice users to work with schemas. But sometimes, this might not be enough.
Anyway, I’m writing this article to show you how PostgreSQL and Greenplum are related.
Requisities
————–
You need a working copy of Greenplum 4.0.4 and a directory containing the PostgreSQL 8.2 source code, I’ve tested it on a Linux operating system (CentOS 5 to be exact).
For this example, I assume that Greenplum is installed in /usr/local/greenplum-db-4.0.4.0 and PostgreSQL source directory is /home/gpadmin/postgresql-8.2.22.
I’m using PostgreSQL 8.2 because Greenplum is based on fork on that version.
You can obtain PostgreSQL 8.2 source from http://www.postgresql.org/ftp/source/v8.2.22/ or directly from git, please refer to http://wiki.postgresql.org/wiki/Working\_with\_git for instruction on how to use git with PostgreSQL.
Installing dblink
——————-
Installing dblink is as easy as installing a contrib module in PostgreSQL.
* as gpadmin user, source the greenplum_path.sh file:


$ source /usr/local/greenplum-db-4.0.4.0/greenplum_path.sh

You should have this line in your .bashrc file after the installation process.
* Check that pg_config --pgxs is the Greenplum one:


$ pg_config --pgxs
/usr/local/greenplum-db-4.0.4.0/lib/postgresql/pgxs/src/makefiles/pgxs.mk

* Enter the dblink directory:


$ cd /home/gpadmin/postgresql-8.2.22/contrib/dblink/

* Edit the Makefile and add -w to PG_CPPFLAGS, so that line number four looks like this:


PG_CPPFLAGS = -I$(libpq_srcdir) -w

This option tells gcc to ignore all warnings (that’s a _dirty_ hack but mandatory).
* Install the module directly in a database:


$ createdb my_db
$ make USE_PGXS=1 install
$ psql -f dblink.sql my_database

At this time, my_database contains dblink, so it is able to execute queries in remote databases.
Using dblink
—————
It is now time to test it, executing some queries.
* Create a database to query, with an example table:


$ createdb my_other_db
$ psql -c "CREATE TABLE t AS SELECT generate_series(1,1000) AS v" my_other_db

* Create the dblink connection to that database:


$ psql my_db
my_db=# SELECT dblink_connect('myconn', 'dbname=my_other_db');
dblink_connect
-------------------
OK
(1 row)

* Execute queries using that connection:


$ psql my_db
my_db=# SELECT * FROM dblink( 'myconn', 'SELECT v FROM t')
AS t1 ( v INTEGER ) LIMIT 5;

Please note that you must specify the expected set of columns in the calling query, because dblink returns a set of record (to be generic).
Conclusions
—————
Even though dblink would not be my preferred choice (also because it is not directly supported by Greenplum – or for Greenplum), this article shows you a simple method to use it in those cases where you can’t really do without it.

Tags: dblink, greenplum, postgres, PostgreSQL
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
1 reply
  1. Tamas
    Tamas says:
    July 3, 2012 at 10:47 am

    As there is no AUTONOMOUS TRANSACTIONS in greenplum, as there are several exceptions what greenplum cannot catch, the only way to have logging inside gp functions are the dblink or the output file + external table methods 🙁

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Get in touch with us!

Recent Posts

  • Random Data December 3, 2020
  • Webinar: COMMIT Without Fear – The Beauty of CAMO [Follow Up] November 13, 2020
  • Full-text search since PostgreSQL 8.3 November 5, 2020
  • Random numbers November 3, 2020
  • Webinar: Best Practices for Bulk Data Loading in PostgreSQL [Follow Up] November 2, 2020

Featured External Blogs

Tomas Vondra's Blog

Our Bloggers

  • Simon Riggs
  • Alvaro Herrera
  • Andrew Dunstan
  • Craig Ringer
  • Francesco Canovai
  • Gabriele Bartolini
  • Giulio Calacoci
  • Ian Barwick
  • Marco Nenciarini
  • Mark Wong
  • Pavan Deolasee
  • Petr Jelinek
  • Shaun Thomas
  • Tomas Vondra
  • Umair Shahid

PostgreSQL Cloud

2QLovesPG 2UDA 9.6 backup Barman BDR Business Continuity community conference database DBA development devops disaster recovery greenplum Hot Standby JSON JSONB logical replication monitoring OmniDB open source Orange performance PG12 pgbarman pglogical PG Phriday postgres Postgres-BDR postgres-xl PostgreSQL PostgreSQL 9.6 PostgreSQL10 PostgreSQL11 PostgreSQL 11 PostgreSQL 11 New Features postgresql repmgr Recovery replication security sql wal webinar webinars

Support & Services

24/7 Production Support

Developer Support

Remote DBA for PostgreSQL

PostgreSQL Database Monitoring

PostgreSQL Health Check

PostgreSQL Performance Tuning

Database Security Audit

Upgrade PostgreSQL

PostgreSQL Migration Assessment

Migrate from Oracle to PostgreSQL

Products

HA Postgres Clusters

Postgres-BDR®

2ndQPostgres

pglogical

repmgr

Barman

Postgres Cloud Manager

SQL Firewall

Postgres-XL

OmniDB

Postgres Installer

2UDA

Postgres Learning Center

Introducing Postgres

Blog

Webinars

Books

Videos

Training

Case Studies

Events

About Us

About 2ndQuadrant

What does 2ndQuadrant Mean?

News

Careers 

Team Profile

© 2ndQuadrant Ltd. All rights reserved. | Privacy Policy
  • Twitter
  • LinkedIn
  • Facebook
  • Youtube
  • Mail
Early bird registrations open for PGDay.IT 2011 ETL with Kettle and Greenplum – Part one: setting up your job.
Scroll to top
×