|
| 1 | +/* |
| 2 | + * dblink |
| 3 | + * |
| 4 | + * Functions returning results from a remote database |
| 5 | + * |
| 6 | + * Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001; |
| 7 | + * |
| 8 | + * Permission to use, copy, modify, and distribute this software and its |
| 9 | + * documentation for any purpose, without fee, and without a written agreement |
| 10 | + * is hereby granted, provided that the above copyright notice and this |
| 11 | + * paragraph and the following two paragraphs appear in all copies. |
| 12 | + * |
| 13 | + * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR |
| 14 | + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING |
| 15 | + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS |
| 16 | + * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE |
| 17 | + * POSSIBILITY OF SUCH DAMAGE. |
| 18 | + * |
| 19 | + * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
| 20 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 21 | + * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | + * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO |
| 23 | + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +Version 0.2 (29 May, 2001): |
| 28 | + Function to test returning data set from remote database |
| 29 | + Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.1 and 7.2devel |
| 30 | + |
| 31 | +Release Notes: |
| 32 | + |
| 33 | + Version 0.2 |
| 34 | + - initial release |
| 35 | + |
| 36 | +Installation: |
| 37 | + Place these files in a directory called 'dblink' under 'contrib' in the PostgreSQL source tree. Then run: |
| 38 | + |
| 39 | + make |
| 40 | + make install |
| 41 | + |
| 42 | + You can use dblink.sql to create the functions in your database of choice, e.g. |
| 43 | + |
| 44 | + psql -U postgres template1 < dblink.sql |
| 45 | + |
| 46 | + installs following functions into database template1: |
| 47 | + |
| 48 | + dblink() - returns a pointer to results from remote query |
| 49 | + dblink_tok() - extracts and returns individual field results |
| 50 | + |
| 51 | +Documentation |
| 52 | +================================================================== |
| 53 | +Name |
| 54 | + |
| 55 | +dblink -- Returns a pointer to a data set from a remote database |
| 56 | + |
| 57 | +Synopsis |
| 58 | + |
| 59 | +dblink(text connstr, text sql) |
| 60 | + |
| 61 | +Inputs |
| 62 | + |
| 63 | + connstr |
| 64 | + |
| 65 | + standard libpq format connection srting, |
| 66 | + e.g. "hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd" |
| 67 | + |
| 68 | + sql |
| 69 | + |
| 70 | + sql statement that you wish to execute on the remote host |
| 71 | + e.g. "select * from pg_class" |
| 72 | + |
| 73 | +Outputs |
| 74 | + |
| 75 | + Returns setof int (pointer) |
| 76 | + |
| 77 | +Example usage |
| 78 | + |
| 79 | + select dblink('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd','select f1, f2 from mytable'); |
| 80 | + |
| 81 | + |
| 82 | +================================================================== |
| 83 | + |
| 84 | +Name |
| 85 | + |
| 86 | +dblink_tok -- Returns individual select field results from a dblink remote query |
| 87 | + |
| 88 | +Synopsis |
| 89 | + |
| 90 | +dblink_tok(int pointer, int fnumber) |
| 91 | + |
| 92 | +Inputs |
| 93 | + |
| 94 | + pointer |
| 95 | + |
| 96 | + a pointer returned by a call to dblink() |
| 97 | + |
| 98 | + fnumber |
| 99 | + |
| 100 | + the ordinal position (zero based) of the field to be returned from the dblink result set |
| 101 | + |
| 102 | +Outputs |
| 103 | + |
| 104 | + Returns text |
| 105 | + |
| 106 | +Example usage |
| 107 | + |
| 108 | + select dblink_tok(t1.dblink_p,0) as f1, dblink_tok(t1.dblink_p,1) as f2 |
| 109 | + from (select dblink('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd' |
| 110 | + ,'select f1, f2 from mytable') as dblink_p) as t1; |
| 111 | + |
| 112 | + |
| 113 | +================================================================== |
| 114 | + |
| 115 | +NOTE: If you need to provide selection criteria in a WHERE clause, it is necessary |
| 116 | +to 'fake' a UNION, e.g. |
| 117 | + |
| 118 | + select |
| 119 | + dblink_tok(t1.dblink_p,0) as f1 |
| 120 | + ,dblink_tok(t1.dblink_p,1) as f2 |
| 121 | + from |
| 122 | + ( |
| 123 | + select dblink( |
| 124 | + 'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd' |
| 125 | + ,'select f1, f2 from mytable' |
| 126 | + ) as dblink_p |
| 127 | + union |
| 128 | + select null,null where false |
| 129 | + ) as t1 |
| 130 | + where |
| 131 | + f1 = 'mycriteria'; |
| 132 | + |
| 133 | +in order to work around an issue with the query optimizer. A more convenient way to approach |
| 134 | +this problem is to create a view: |
| 135 | + |
| 136 | + create view myremotetable as |
| 137 | + select |
| 138 | + dblink_tok(t1.dblink_p,0) as f1 |
| 139 | + ,dblink_tok(t1.dblink_p,1) as f2 |
| 140 | + from |
| 141 | + ( |
| 142 | + select dblink( |
| 143 | + 'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd' |
| 144 | + ,'select f1, f2 from mytable' |
| 145 | + ) as dblink_p |
| 146 | + union |
| 147 | + select null,null where false |
| 148 | + ) as t1; |
| 149 | + |
| 150 | +Then you can simply write: |
| 151 | + |
| 152 | + select f1,f2 from myremotetable where f1 = 'mycriteria'; |
| 153 | + |
| 154 | +================================================================== |
| 155 | + |
| 156 | +-- Joe Conway |
| 157 | + |
0 commit comments