Skip to content

Commit 3de19ca

Browse files
committed
drop foreign tables in drop_partitions() func; test FDW features
1 parent 56bfc94 commit 3de19ca

File tree

3 files changed

+143
-18
lines changed

3 files changed

+143
-18
lines changed

init.sql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ DECLARE
498498
v_rows INTEGER;
499499
v_part_count INTEGER := 0;
500500
conf_num_del INTEGER;
501+
v_relkind CHAR;
501502

502503
BEGIN
503504
PERFORM @extschema@.validate_relname(parent_relid);
@@ -531,7 +532,20 @@ BEGIN
531532
RAISE NOTICE '% rows copied from %', v_rows, v_rec.tbl;
532533
END IF;
533534

534-
EXECUTE format('DROP TABLE %s', v_rec.tbl);
535+
/*
536+
* Determine the kind of child relation. It can be either regular
537+
* table (r) or foreign table (f). Depending on relkind we use
538+
* DROP TABLE or DROP FOREIGN TABLE
539+
*/
540+
EXECUTE format('SELECT relkind FROM pg_class WHERE oid = ''%s''::regclass', v_rec.tbl)
541+
INTO v_relkind;
542+
543+
IF v_relkind = 'f' THEN
544+
EXECUTE format('DROP FOREIGN TABLE %s', v_rec.tbl);
545+
ELSE
546+
EXECUTE format('DROP TABLE %s', v_rec.tbl);
547+
END IF;
548+
535549
v_part_count := v_part_count + 1;
536550
END LOOP;
537551

tests/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Tests
2+
3+
This directory contains script to tests some features which cannot be tested
4+
with only regression tests
5+
6+
## Running
7+
8+
First of all you need to install `testgres` python module which contains useful
9+
functions to start postgres clusters and make queries:
10+
11+
```
12+
pip install testgres
13+
```
14+
15+
To run tests execute:
16+
17+
```
18+
python -m unittest partitioning_test
19+
```
20+
21+
from current directory. If you want to run a specific postgres build then
22+
you should specify the path to your pg_config executable by setting PG_CONFIG
23+
environment variable:
24+
25+
```
26+
export PG_CONFIG=/path/to/pg_config
27+
```
28+
29+
Tests concerning FDW features are disabled by default. To test FDW features
30+
you need to install postgres_fdw contrib module first and then set the TEST_FDW
31+
environment variable:
32+
33+
```
34+
export TEST_FDW=1
35+
```

tests/partitioning_test.py

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@
1212
import os
1313

1414

15+
def test_fdw(func):
16+
"""To run tests with FDW support set environment variable TEST_FDW=1"""
17+
def wrapper(*args, **kwargs):
18+
if os.environ.get('TEST_FDW'):
19+
func(*args, **kwargs)
20+
else:
21+
print('Warning: FDW features tests are disabled, skipping...')
22+
return wrapper
23+
24+
1525
class PartitioningTests(unittest.TestCase):
1626

1727
def setUp(self):
1828
self.setup_cmd = [
19-
'create extension pg_pathman',
29+
# 'create extension pg_pathman',
2030
'create table abc(id serial, t text)',
2131
'insert into abc select generate_series(1, 300000)',
2232
'select create_hash_partitions(\'abc\', \'id\', 3, partition_data := false)',
@@ -26,6 +36,16 @@ def tearDown(self):
2636
stop_all()
2737
# clean_all()
2838

39+
def start_new_pathman_cluster(self, name='test', allows_streaming=False):
40+
node = get_new_node(name)
41+
node.init(allows_streaming=allows_streaming)
42+
node.append_conf(
43+
'postgresql.conf',
44+
'shared_preload_libraries=\'pg_pathman\'\n')
45+
node.start()
46+
node.psql('postgres', 'create extension pg_pathman')
47+
return node
48+
2949
def init_test_data(self, node):
3050
"""Initialize pg_pathman extension and test data"""
3151
for cmd in self.setup_cmd:
@@ -42,17 +62,12 @@ def catchup_replica(self, master, replica):
4262
def printlog(self, logfile):
4363
with open(logfile, 'r') as log:
4464
for line in log.readlines():
45-
print line
65+
print(line)
4666

4767
def test_concurrent(self):
4868
"""Tests concurrent partitioning"""
49-
node = get_new_node('test')
5069
try:
51-
node.init()
52-
node.append_conf(
53-
'postgresql.conf',
54-
'shared_preload_libraries=\'pg_pathman\'\n')
55-
node.start()
70+
node = self.start_new_pathman_cluster()
5671
self.init_test_data(node)
5772

5873
node.psql(
@@ -95,11 +110,7 @@ def test_replication(self):
95110

96111
try:
97112
# initialize master server
98-
node.init(allows_streaming=True)
99-
node.append_conf(
100-
'postgresql.conf',
101-
'shared_preload_libraries=\'pg_pathman\'\n')
102-
node.start()
113+
node = self.start_new_pathman_cluster(allows_streaming=True)
103114
node.backup('my_backup')
104115

105116
# initialize replica from backup
@@ -238,8 +249,8 @@ def add_partition(node, flag, query):
238249
con.commit()
239250

240251
# Now wait until each thread finishes
241-
for i in range(3):
242-
threads[i].join()
252+
for thread in threads:
253+
thread.join()
243254

244255
# Check flags, it should be true which means that threads are
245256
# finished
@@ -277,11 +288,11 @@ def check_tablespace(node, tablename, tablespace):
277288
'postgresql.conf',
278289
'shared_preload_libraries=\'pg_pathman\'\n')
279290
node.start()
280-
path = os.path.join(node.data_dir, 'test_space_location')
281-
os.mkdir(path)
282291
node.psql('postgres', 'create extension pg_pathman')
283292

284293
# create tablespace
294+
path = os.path.join(node.data_dir, 'test_space_location')
295+
os.mkdir(path)
285296
node.psql(
286297
'postgres',
287298
'create tablespace test_space location \'{}\''.format(path))
@@ -330,6 +341,71 @@ def check_tablespace(node, tablename, tablespace):
330341
self.assertTrue(check_tablespace(node, 'abc_prepended_2', 'pg_default'))
331342
self.assertTrue(check_tablespace(node, 'abc_added_2', 'pg_default'))
332343

344+
@test_fdw
345+
def test_foreign_table(self):
346+
"""Test foreign tables"""
347+
348+
# Start master server
349+
master = get_new_node('test')
350+
master.init()
351+
master.append_conf(
352+
'postgresql.conf',
353+
'shared_preload_libraries=\'pg_pathman, postgres_fdw\'\n')
354+
master.start()
355+
master.psql('postgres', 'create extension pg_pathman')
356+
master.psql('postgres', 'create extension postgres_fdw')
357+
master.psql(
358+
'postgres',
359+
'''create table abc(id serial, name text);
360+
select create_range_partitions('abc', 'id', 0, 10, 2)''')
361+
362+
# Current user name (needed for user mapping)
363+
username = master.execute('postgres', 'select current_user')[0][0]
364+
365+
# Start foreign server
366+
fserv = get_new_node('fserv')
367+
fserv.init().start()
368+
fserv.safe_psql('postgres', 'create table ftable(id serial, name text)')
369+
fserv.safe_psql('postgres', 'insert into ftable values (25, \'foreign\')')
370+
371+
# Create foreign table and attach it to partitioned table
372+
master.safe_psql(
373+
'postgres',
374+
'''create server fserv
375+
foreign data wrapper postgres_fdw
376+
options (dbname 'postgres', host '127.0.0.1', port '{}')'''.format(fserv.port)
377+
)
378+
master.safe_psql(
379+
'postgres',
380+
'''create user mapping for {0}
381+
server fserv
382+
options (user '{0}')'''.format(username)
383+
)
384+
master.safe_psql(
385+
'postgres',
386+
'''import foreign schema public limit to (ftable)
387+
from server fserv into public'''
388+
)
389+
master.safe_psql(
390+
'postgres',
391+
'select attach_range_partition(\'abc\', \'ftable\', 20, 30)')
392+
393+
# Check that table attached to partitioned table
394+
self.assertEqual(
395+
master.safe_psql('postgres', 'select * from ftable'),
396+
'25|foreign\n'
397+
)
398+
399+
# Check that we can successfully insert new data into foreign partition
400+
master.safe_psql('postgres', 'insert into abc values (26, \'part\')')
401+
self.assertEqual(
402+
master.safe_psql('postgres', 'select * from ftable order by id'),
403+
'25|foreign\n26|part\n'
404+
)
405+
406+
# Testing drop partitions (including foreign partitions)
407+
master.safe_psql('postgres', 'select drop_partitions(\'abc\')')
408+
333409

334410
if __name__ == "__main__":
335411
unittest.main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy