Skip to content

Commit 7e449c0

Browse files
committed
Simplify add_to_pathman_config, check hash function for expression for hash partitioning type.
1 parent 6b87455 commit 7e449c0

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

expected/pathman_permissions.out

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ GRANT SELECT ON permissions.user1_table TO user2;
1919
/* Should fail (don't own parent) */
2020
SET ROLE user2;
2121
SELECT create_range_partitions('permissions.user1_table', 'id', 1, 10, 2);
22+
WARNING: skipping "user1_table" --- only table or database owner can analyze it
2223
ERROR: only the owner or superuser can change partitioning configuration of table "user1_table"
2324
/* Should be ok */
2425
SET ROLE user1;

hash.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ BEGIN
3535
PERFORM @extschema@.common_relation_checks(parent_relid, expression);
3636

3737
/* Insert new entry to pathman config */
38-
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression, NULL, false);
38+
EXECUTE format('ANALYZE %s', parent_relid);
39+
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression, NULL);
3940

4041
/* Create partitions */
4142
PERFORM @extschema@.create_hash_partitions_internal(parent_relid,

init.sql

+1-2
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,8 @@ LANGUAGE C STRICT;
862862
*/
863863
CREATE OR REPLACE FUNCTION @extschema@.add_to_pathman_config(
864864
parent_relid REGCLASS,
865-
attname TEXT,
865+
expression TEXT,
866866
range_interval TEXT DEFAULT NULL,
867-
refresh_part_info BOOL DEFAULT TRUE,
868867
parttype INT4 DEFAULT 0
869868
)
870869
RETURNS BOOLEAN AS 'pg_pathman', 'add_to_pathman_config'

range.sql

+10-5
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ BEGIN
154154
END IF;
155155

156156
/* Insert new entry to pathman config */
157+
EXECUTE format('ANALYZE %s', parent_relid);
157158
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression,
158-
p_interval::TEXT, false);
159+
p_interval::TEXT);
159160

160161
/* Create sequence for child partitions names */
161162
PERFORM @extschema@.create_or_replace_sequence(parent_relid);
@@ -251,8 +252,9 @@ BEGIN
251252
END IF;
252253

253254
/* Insert new entry to pathman config */
255+
EXECUTE format('ANALYZE %s', parent_relid);
254256
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression,
255-
p_interval::TEXT, false);
257+
p_interval::TEXT);
256258

257259
/* Create sequence for child partitions names */
258260
PERFORM @extschema@.create_or_replace_sequence(parent_relid);
@@ -310,7 +312,8 @@ BEGIN
310312
bounds[array_length(bounds, 1) - 1]);
311313

312314
/* Insert new entry to pathman config */
313-
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression, NULL, false, 2);
315+
EXECUTE format('ANALYZE %s', parent_relid);
316+
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression, NULL, 2);
314317

315318
/* Create sequence for child partitions names */
316319
PERFORM @extschema@.create_or_replace_sequence(parent_relid);
@@ -360,8 +363,9 @@ BEGIN
360363
end_value);
361364

362365
/* Insert new entry to pathman config */
366+
EXECUTE format('ANALYZE %s', parent_relid);
363367
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression,
364-
p_interval::TEXT, false);
368+
p_interval::TEXT);
365369

366370
/* Create sequence for child partitions names */
367371
PERFORM @extschema@.create_or_replace_sequence(parent_relid);
@@ -417,8 +421,9 @@ BEGIN
417421
end_value);
418422

419423
/* Insert new entry to pathman config */
424+
EXECUTE format('ANALYZE %s', parent_relid);
420425
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression,
421-
p_interval::TEXT, false);
426+
p_interval::TEXT);
422427

423428
/* Create sequence for child partitions names */
424429
PERFORM @extschema@.create_or_replace_sequence(parent_relid);

src/pl_funcs.c

+14-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "access/nbtree.h"
2323
#include "access/htup_details.h"
2424
#include "catalog/indexing.h"
25+
#include "catalog/pg_inherits_fn.h"
2526
#include "catalog/pg_trigger.h"
2627
#include "catalog/pg_type.h"
2728
#include "commands/tablespace.h"
@@ -709,7 +710,6 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
709710
Relation pathman_config;
710711
Datum values[Natts_pathman_config];
711712
bool isnull[Natts_pathman_config];
712-
bool refresh_part_info;
713713
HeapTuple htup;
714714

715715
Oid expr_type;
@@ -745,13 +745,23 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
745745
}
746746

747747
/* Select partitioning type */
748-
parttype = PG_GETARG_INT32(4);
748+
parttype = PG_GETARG_INT32(3);
749749
if ((parttype != PT_HASH) && (parttype != PT_RANGE))
750750
parttype = PG_ARGISNULL(2) ? PT_HASH : PT_RANGE;
751751

752752
/* Parse and check expression */
753753
expr_datum = plan_partitioning_expression(relid, expression, &expr_type);
754754

755+
/* Expression for range partitions should be hashable */
756+
if (parttype == PT_HASH)
757+
{
758+
TypeCacheEntry *tce;
759+
760+
tce = lookup_type_cache(expr_type, TYPECACHE_HASH_PROC);
761+
if (tce->hash_proc == InvalidOid)
762+
elog(ERROR, "partitioning expression should be hashable");
763+
}
764+
755765
/*
756766
* Initialize columns (partrel, attname, parttype, range_interval).
757767
*/
@@ -790,10 +800,8 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
790800

791801
heap_close(pathman_config, RowExclusiveLock);
792802

793-
/* FIXME: check pg_inherits instead of this argument */
794-
refresh_part_info = PG_GETARG_BOOL(3);
795-
796-
if (refresh_part_info)
803+
/* update caches only if this relation has children */
804+
if (has_subclass(relid))
797805
{
798806
/* Now try to create a PartRelationInfo */
799807
PG_TRY();

0 commit comments

Comments
 (0)