Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
CREATE EXTENSION http;
set http.timeout_msec = 10000;
SELECT http_set_curlopt('CURLOPT_TIMEOUT', '10');
 http_set_curlopt 
------------------
 t
(1 row)
 
-- Status code
SELECT status
FROM http_get('http://localhost:9080/status/202');
 status 
--------
    202
(1 row)
 
-- Headers
SELECT lower(field) AS field, value
FROM (
    SELECT (unnest(headers)).*
    FROM http_get('http://localhost:9080/response-headers?Abcde=abcde')
) a
WHERE field ILIKE 'Abcde';
 field | value 
-------+-------
 abcde | abcde
(1 row)
 
-- GET
SELECT status,
content::json->'args'->>'foo' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_get('http://localhost:9080/anything?foo=bar');
 status | args |                  url                   | method 
--------+------+----------------------------------------+--------
    200 | bar  | http://localhost:9080/anything?foo=bar | GET
(1 row)
 
-- GET with data
SELECT status,
content::json->'args'->>'this' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_get('http://localhost:9080/anything', jsonb_build_object('this', 'that'));
 status | args |                   url                    | method 
--------+------+------------------------------------------+--------
    200 | that | http://localhost:9080/anything?this=that | GET
(1 row)
 
-- GET with data
SELECT status,
content::json->>'args' as args,
(content::json)->>'data' as data,
content::json->>'url' as url,
content::json->>'method' as method
FROM http(('GET', 'http://localhost:9080/anything', NULL, 'application/json', '{"search": "toto"}', NULL, NULL));
 status | args |        data        |              url               | method 
--------+------+--------------------+--------------------------------+--------
    200 | {}   | {"search": "toto"} | http://localhost:9080/anything | GET
(1 row)
 
-- DELETE
SELECT status,
content::json->'args'->>'foo' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_delete('http://localhost:9080/anything?foo=bar');
 status | args |                  url                   | method 
--------+------+----------------------------------------+--------
    200 | bar  | http://localhost:9080/anything?foo=bar | DELETE
(1 row)
 
-- DELETE with payload
SELECT status,
content::json->'args'->>'foo' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method,
content::json->>'data' AS data
FROM http_delete('http://localhost:9080/anything?foo=bar', 'payload', 'text/plain');
 status | args |                  url                   | method |  data   
--------+------+----------------------------------------+--------+---------
    200 | bar  | http://localhost:9080/anything?foo=bar | DELETE | payload
(1 row)
 
-- PUT
SELECT status,
content::json->>'data' AS data,
content::json->'args'->>'foo' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_put('http://localhost:9080/anything?foo=bar','payload','text/plain');
 status |  data   | args |                  url                   | method 
--------+---------+------+----------------------------------------+--------
    200 | payload | bar  | http://localhost:9080/anything?foo=bar | PUT
(1 row)
 
-- PATCH
SELECT status,
content::json->>'data' AS data,
content::json->'args'->>'foo' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_patch('http://localhost:9080/anything?foo=bar','{"this":"that"}','application/json');
 status |      data       | args |                  url                   | method 
--------+-----------------+------+----------------------------------------+--------
    200 | {"this":"that"} | bar  | http://localhost:9080/anything?foo=bar | PATCH
(1 row)
 
-- POST
SELECT status,
content::json->>'data' AS data,
content::json->'args'->>'foo' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_post('http://localhost:9080/anything?foo=bar','payload','text/plain');
 status |  data   | args |                  url                   | method 
--------+---------+------+----------------------------------------+--------
    200 | payload | bar  | http://localhost:9080/anything?foo=bar | POST
(1 row)
 
SET client_min_messages TO DEBUG1;
-- GET with custom timeout and retries
SELECT status,
       content::json->>'url' AS url,
       content::json->>'method' AS method
FROM http_get('http://localhost:5050/anything?ping=test', 2000, 10);
LOG:  Q.DataBase: Could not stat the license file.
DEBUG:  pgsql-http: http_retry was set '10'
DEBUG:  retries left 9
DEBUG:  retries left 8
DEBUG:  retries left 7
DEBUG:  retries left 6
DEBUG:  retries left 5
DEBUG:  retries left 4
DEBUG:  retries left 3
DEBUG:  retries left 2
DEBUG:  retries left 1
DEBUG:  retries left 0
ERROR:  Failed to connect to localhost port 5050 after 0 ms: Connection refused
-- POST with payload and custom timeout/retries
SELECT status,
       content::json->>'data' AS data,
       content::json->>'url' AS url,
       content::json->>'method' AS method
FROM http_post('http://localhost:5050/anything', 'payload_data', 'text/plain', 3000, 5);
LOG:  Q.DataBase: Could not stat the license file.
DEBUG:  pgsql-http: http_retry was set '5'
DEBUG:  retries left 4
DEBUG:  retries left 3
DEBUG:  retries left 2
DEBUG:  retries left 1
DEBUG:  retries left 0
ERROR:  Failed to connect to localhost port 5050 after 0 ms: Connection refused
-- PUT with payload and custom timeout/retries
SELECT status,
       content::json->>'data' AS data,
       content::json->>'url' AS url,
       content::json->>'method' AS method
FROM http_put('http://localhost:9080/anything', 'updated_data', 'text/plain', 1500, 2);
LOG:  Q.DataBase: Could not stat the license file.
DEBUG:  pgsql-http: http_retry was set '2'
 status |     data     |              url               | method 
--------+--------------+--------------------------------+--------
    200 | updated_data | http://localhost:9080/anything | PUT
(1 row)
 
-- GET with NULL timeout and NULL retries → fallback to default
SELECT status,
       content::json->>'url' AS url,
       content::json->>'method' AS method
FROM http_get('http://localhost:5050/anything?nulls=test', NULL, NULL);
LOG:  Q.DataBase: Could not stat the license file.
DEBUG:  retries left 0
ERROR:  Failed to connect to localhost port 5050 after 0 ms: Connection refused
-- GET with negative timeout and retries → fallback to default
SELECT status,
       content::json->>'url' AS url,
       content::json->>'method' AS method
FROM http_get('http://localhost:5050/anything?negative=test', -500, -3);
LOG:  Q.DataBase: Could not stat the license file.
DEBUG:  pgsql-http: http_retry was set '1'
DEBUG:  retries left 0
ERROR:  Failed to connect to localhost port 5050 after 0 ms: Connection refused
SET client_min_messages TO WARNING;
-- POST with json data
SELECT status,
content::json->'form'->>'this' AS args,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_post('http://localhost:9080/anything', jsonb_build_object('this', 'that'));
 status | args |              url               | method 
--------+------+--------------------------------+--------
    200 | that | http://localhost:9080/anything | POST
(1 row)
 
-- POST with data
SELECT status,
content::json->'form'->>'key1' AS key1,
content::json->'form'->>'key2' AS key2,
content::json->>'url' AS url,
content::json->>'method' AS method
FROM http_post('http://localhost:9080/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded');
 status |  key1  |  key2  |              url               | method 
--------+--------+--------+--------------------------------+--------
    200 | value1 | value2 | http://localhost:9080/anything | POST
(1 row)
 
-- HEAD
SELECT lower(field) AS field, value
FROM (
    SELECT (unnest(headers)).*
    FROM http_head('http://localhost:9080/response-headers?Abcde=abcde')
) a
WHERE field ILIKE 'Abcde';
 field | value 
-------+-------
 abcde | abcde
(1 row)
 
-- Follow redirect
SELECT status,
(content::json)->>'url' AS url
FROM http_get('http://localhost:9080/redirect-to?url=get');
 status |            url            
--------+---------------------------
    200 | http://localhost:9080/get
(1 row)
 
-- Request image
WITH
  http AS (
    SELECT * FROM http_get('http://localhost:9080/image/png')
  ),
  headers AS (
    SELECT (unnest(headers)).* FROM http
  )
SELECT
  http.content_type,
  length(text_to_bytea(http.content)) AS length_binary
FROM http, headers
WHERE field ilike 'Content-Type';
 content_type | length_binary 
--------------+---------------
 image/png    |          8090
(1 row)
 
-- Alter options and and reset them and throw errors
SELECT http_set_curlopt('CURLOPT_PROXY', '127.0.0.1');
 http_set_curlopt 
------------------
 t
(1 row)
 
-- Error because proxy is not there
DO $$
BEGIN
    SELECT status FROM http_get('http://localhost:9080/status/555');
EXCEPTION
    WHEN OTHERS THEN
        RAISE WARNING 'Failed to connect';
END;
$$;
WARNING:  Failed to connect
-- Still an error
DO $$
BEGIN
    SELECT status FROM http_get('http://localhost:9080/status/555');
EXCEPTION
    WHEN OTHERS THEN
        RAISE WARNING 'Failed to connect';
END;
$$;
WARNING:  Failed to connect
-- Reset options
SELECT http_reset_curlopt();
 http_reset_curlopt 
--------------------
 t
(1 row)
 
-- Now it should work
SELECT status FROM http_get('http://localhost:9080/status/555');
 status 
--------
    555
(1 row)
 
-- Alter the default timeout and then run a query that is longer than
-- the default (5s), but shorter than the new timeout
SELECT http_set_curlopt('CURLOPT_TIMEOUT_MS', '10000');
 http_set_curlopt 
------------------
 t
(1 row)
 
SELECT status FROM http_get('http://localhost:9080/delay/7');
 status 
--------
    200
(1 row)
 
-- Check that statement interruption works
SET statement_timeout = 200;
CREATE TEMPORARY TABLE timer AS
  SELECT now() AS start;
SELECT *
  FROM http_get('http://localhost:9080/delay/7');
ERROR:  canceling statement due to user request
SELECT round(extract(epoch FROM now() - start) * 10) AS m
  FROM timer;
 m 
---
 2
(1 row)
 
DROP TABLE timer;
DROP EXTENSION http;