|
158 | 158 | specified, the file size (in bytes) is shown as well.
|
159 | 159 | </para>
|
160 | 160 |
|
| 161 | + <example> |
| 162 | + <title id="csvlog-fdw">Create a Foreign Table for PostgreSQL CSV Logs</title> |
| 163 | + |
| 164 | + <para> |
| 165 | + One of the obvious uses for the <literal>file_fdw</> is to make |
| 166 | + the PostgreSQL activity log available as a table for querying. To |
| 167 | + do this, first you must be logging to a CSV file, which here we |
| 168 | + will call <literal>pglog.csv</>. First, install <literal>file_fdw</> |
| 169 | + as an extension: |
| 170 | + </para> |
| 171 | + |
| 172 | +<programlisting> |
| 173 | +CREATE EXTENSION file_fdw; |
| 174 | +</programlisting> |
| 175 | + |
| 176 | + <para> |
| 177 | + Next, create the foreign data wrapper: |
| 178 | + |
| 179 | +<programlisting> |
| 180 | +CREATE FOREIGN DATA WRAPPER file_fdw HANDLER file_fdw_handler; |
| 181 | +</programlisting> |
| 182 | + </para> |
| 183 | + |
| 184 | + <para> |
| 185 | + Then create a foreign data server: |
| 186 | + |
| 187 | +<programlisting> |
| 188 | +CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw; |
| 189 | +</programlisting> |
| 190 | + </para> |
| 191 | + |
| 192 | + <para> |
| 193 | + Now you are ready to create the foreign data table. Using the |
| 194 | + <command>CREATE FOREIGN TABLE</> command, you will need to define |
| 195 | + the columns for the table, the CSV filename, and its format: |
| 196 | + |
| 197 | +<programlisting> |
| 198 | +CREATE FOREIGN TABLE pglog ( |
| 199 | + log_time timestamp(3) with time zone, |
| 200 | + user_name text, |
| 201 | + database_name text, |
| 202 | + process_id integer, |
| 203 | + connection_from text, |
| 204 | + session_id text, |
| 205 | + session_line_num bigint, |
| 206 | + command_tag text, |
| 207 | + session_start_time timestamp with time zone, |
| 208 | + virtual_transaction_id text, |
| 209 | + transaction_id bigint, |
| 210 | + error_severity text, |
| 211 | + sql_state_code text, |
| 212 | + message text, |
| 213 | + detail text, |
| 214 | + hint text, |
| 215 | + internal_query text, |
| 216 | + internal_query_pos integer, |
| 217 | + context text, |
| 218 | + query text, |
| 219 | + query_pos integer, |
| 220 | + location text, |
| 221 | + application_name text |
| 222 | +) SERVER pglog |
| 223 | +OPTIONS ( filename '/home/josh/9.1/data/pg_log/pglog.csv', format 'csv' ); |
| 224 | +</programlisting> |
| 225 | + </para> |
| 226 | + |
| 227 | + <para> |
| 228 | + That's it — now you can query your log directly. In production, of course, |
| 229 | + you would need to define some way to adjust to log rotation. |
| 230 | + </para> |
| 231 | + </example> |
| 232 | + |
161 | 233 | </sect1>
|
0 commit comments