记录未插入指定的Postgres数据库分区

发布于 2025-01-23 09:14:43 字数 6042 浏览 0 评论 0原文

我创建了一个带有命令的范围分区的主分区表:

CREATE TABLE users_range_part (
    user_id SERIAL,
    user_first_name VARCHAR(30) NOT NULL,
    user_last_name VARCHAR(30) NOT NULL,
    user_email_id VARCHAR(50) NOT NULL,
    user_email_validated BOOLEAN DEFAULT FALSE,
    user_password VARCHAR(200),
    user_role VARCHAR(1) NOT NULL DEFAULT 'U', --U and A
    is_active BOOLEAN DEFAULT FALSE,
    created_dt DATE DEFAULT CURRENT_DATE,
    last_updated_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (created_dt, user_id)
) PARTITION BY RANGE(created_dt)

在谁创建了一个默认的子partiton为主分区,该表旨在保持不属于未来儿童分区表范围内的记录,我将创建... 因此,我用命令创建了默认子女党:

CREATE TABLE users_range_part_default
PARTITION OF users_range_part DEFAULT

下一:现在,我的目标是根据我在开始时指定的order_date将每个记录划分

从2016年到2020

为每一个记录,因此我 命令:

import psycopg2

import pandas as pd
from pandas.tseries.offsets import MonthBegin, MonthEnd

months = pd.date_range(start='1/1/2016', end='12/31/2020', freq='1M')

connection = psycopg2.connect(
    host='localhost',
    port='5433',
    database='pat_db',
    user='postgres',
    password='pass'
)
cursor = connection.cursor()
table_name = 'users_range_part'
query = '''
CREATE TABLE {table_name}_{yyyymm}
PARTITION OF {table_name}
FOR VALUES FROM ('{begin_date}') TO ('{end_date}')
'''
for month in months:
    begin_date = month - MonthBegin(1)
    end_date = month + MonthEnd(0)
    print(f'Adding partition for {begin_date} and {end_date}')
    cursor.execute(
        query.format(
            table_name=table_name,
            yyyymm=str(month)[:7].replace('-', ''),
            begin_date=str(begin_date).split(' ')[0],
            end_date=str(end_date).split(' ')[0]
        ), ()
    )
connection.commit()
cursor.close()
connection.close()

我确认了所有这些儿童分区的创建:

tablename                 tableowner
users                     postgres
users_part                postgres
users_part_a              postgres
users_part_default        postgres
users_part_u              postgres
users_range_part          postgres
users_range_part_2017     postgres
users_range_part_2018     postgres
users_range_part_default    postgres
users_range_part_2016     postgres
users_range_part_2019     postgres
users_range_part_2020     postgres
users_range_part_201612     postgres
users_range_part_201701     postgres
users_range_part_201601     postgres
users_range_part_201602     postgres
users_range_part_201603     postgres
users_range_part_201604     postgres
users_range_part_201605     postgres
users_range_part_201606     postgres
users_range_part_201607     postgres
users_range_part_201608     postgres
users_range_part_201609     postgres
users_range_part_201610     postgres
users_range_part_201611     postgres
users_range_part_201702     postgres
users_range_part_201703     postgres
users_range_part_201704     postgres
users_range_part_201705     postgres
users_range_part_201706     postgres
users_range_part_201707     postgres
users_range_part_201708     postgres
users_range_part_201709     postgres
users_range_part_201710     postgres
users_range_part_201711     postgres
users_range_part_201712     postgres
users_range_part_201801     postgres
users_range_part_201802     postgres
users_range_part_201803     postgres
users_range_part_201804     postgres
users_range_part_201805     postgres
users_range_part_201806     postgres
users_range_part_201807     postgres
users_range_part_201808     postgres
users_range_part_201809     postgres
users_range_part_201810     postgres
users_range_part_201811     postgres
users_range_part_201812     postgres
users_range_part_201901     postgres
users_range_part_201902     postgres
users_range_part_201903     postgres
users_range_part_201904     postgres
users_range_part_201905     postgres
users_range_part_201906     postgres
users_range_part_201907     postgres
users_range_part_201908     postgres
users_range_part_201909     postgres
users_range_part_201910     postgres
users_range_part_201911     postgres
users_range_part_201912     postgres
users_range_part_202001     postgres
users_range_part_202002     postgres
users_range_part_202003     postgres
users_range_part_202004     postgres
users_range_part_202005     postgres
users_range_part_202006     postgres
users_range_part_202007     postgres
users_range_part_202008     postgres
users_range_part_202009     postgres
users_range_part_202010     postgres
users_range_part_202011     postgres
users_range_part_202012     postgres

现在:现在是测试我的参与量的时候了,

所以我插入了一些记录

INSERT INTO users_range_part 
    (user_first_name, user_last_name, user_email_id, created_dt)
VALUES 
    ('Scott', 'Tiger', '[email protected]', '2018-10-01'),
    ('Donald', 'Duck', '[email protected]', '2019-02-10'),
    ('Mickey', 'Mouse', '[email protected]', '2017-06-22')

,因为它们都被分区到各自的儿童partitoned表中

,所以显然我的默认子分区表应该有 现在没有任何值

插入新记录:

INSERT INTO users_range_part 
    (user_first_name, user_last_name, user_email_id, created_dt)
VALUES 
    ('Dee', 'Vic', '[email protected]', '2018-10-31'),
    ('Don', 'Jay', '[email protected]', '2019-12-31'),
    ('Mik', 'Mousse', '[email protected]', '2017-09-30')

当我运行此记录时,预期的输出仍是根据其月份分区记录,但记录不会被移动,但仍保留在默认的子分区中... 请帮助

I created a MASTER PARTITION TABLE with RANGE Partition with the command:

CREATE TABLE users_range_part (
    user_id SERIAL,
    user_first_name VARCHAR(30) NOT NULL,
    user_last_name VARCHAR(30) NOT NULL,
    user_email_id VARCHAR(50) NOT NULL,
    user_email_validated BOOLEAN DEFAULT FALSE,
    user_password VARCHAR(200),
    user_role VARCHAR(1) NOT NULL DEFAULT 'U', --U and A
    is_active BOOLEAN DEFAULT FALSE,
    created_dt DATE DEFAULT CURRENT_DATE,
    last_updated_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (created_dt, user_id)
) PARTITION BY RANGE(created_dt)

Afterwhich I created a DEFAULT CHILD PARTITON for the MASTER PARTITION, this table is meant to hold records that dont fall within the range of future CHILD PARTITION TABLE I will create...
SO I created the DEFAULT CHILD PARTITON with the command:

CREATE TABLE users_range_part_default
PARTITION OF users_range_part DEFAULT

NEXT: Now my goal is to partition every record by their month according to the order_date which I specified at the beginning

SO I created Partition for every month starting from 2016 to 2020

I did this with the command:

import psycopg2

import pandas as pd
from pandas.tseries.offsets import MonthBegin, MonthEnd

months = pd.date_range(start='1/1/2016', end='12/31/2020', freq='1M')

connection = psycopg2.connect(
    host='localhost',
    port='5433',
    database='pat_db',
    user='postgres',
    password='pass'
)
cursor = connection.cursor()
table_name = 'users_range_part'
query = '''
CREATE TABLE {table_name}_{yyyymm}
PARTITION OF {table_name}
FOR VALUES FROM ('{begin_date}') TO ('{end_date}')
'''
for month in months:
    begin_date = month - MonthBegin(1)
    end_date = month + MonthEnd(0)
    print(f'Adding partition for {begin_date} and {end_date}')
    cursor.execute(
        query.format(
            table_name=table_name,
            yyyymm=str(month)[:7].replace('-', ''),
            begin_date=str(begin_date).split(' ')[0],
            end_date=str(end_date).split(' ')[0]
        ), ()
    )
connection.commit()
cursor.close()
connection.close()

I confirmed the creation of all this CHILD PARTITIONS:

tablename                 tableowner
users                     postgres
users_part                postgres
users_part_a              postgres
users_part_default        postgres
users_part_u              postgres
users_range_part          postgres
users_range_part_2017     postgres
users_range_part_2018     postgres
users_range_part_default    postgres
users_range_part_2016     postgres
users_range_part_2019     postgres
users_range_part_2020     postgres
users_range_part_201612     postgres
users_range_part_201701     postgres
users_range_part_201601     postgres
users_range_part_201602     postgres
users_range_part_201603     postgres
users_range_part_201604     postgres
users_range_part_201605     postgres
users_range_part_201606     postgres
users_range_part_201607     postgres
users_range_part_201608     postgres
users_range_part_201609     postgres
users_range_part_201610     postgres
users_range_part_201611     postgres
users_range_part_201702     postgres
users_range_part_201703     postgres
users_range_part_201704     postgres
users_range_part_201705     postgres
users_range_part_201706     postgres
users_range_part_201707     postgres
users_range_part_201708     postgres
users_range_part_201709     postgres
users_range_part_201710     postgres
users_range_part_201711     postgres
users_range_part_201712     postgres
users_range_part_201801     postgres
users_range_part_201802     postgres
users_range_part_201803     postgres
users_range_part_201804     postgres
users_range_part_201805     postgres
users_range_part_201806     postgres
users_range_part_201807     postgres
users_range_part_201808     postgres
users_range_part_201809     postgres
users_range_part_201810     postgres
users_range_part_201811     postgres
users_range_part_201812     postgres
users_range_part_201901     postgres
users_range_part_201902     postgres
users_range_part_201903     postgres
users_range_part_201904     postgres
users_range_part_201905     postgres
users_range_part_201906     postgres
users_range_part_201907     postgres
users_range_part_201908     postgres
users_range_part_201909     postgres
users_range_part_201910     postgres
users_range_part_201911     postgres
users_range_part_201912     postgres
users_range_part_202001     postgres
users_range_part_202002     postgres
users_range_part_202003     postgres
users_range_part_202004     postgres
users_range_part_202005     postgres
users_range_part_202006     postgres
users_range_part_202007     postgres
users_range_part_202008     postgres
users_range_part_202009     postgres
users_range_part_202010     postgres
users_range_part_202011     postgres
users_range_part_202012     postgres

Now: It is time to test my PARTITONS

SO I inserted some records

INSERT INTO users_range_part 
    (user_first_name, user_last_name, user_email_id, created_dt)
VALUES 
    ('Scott', 'Tiger', '[email protected]', '2018-10-01'),
    ('Donald', 'Duck', '[email protected]', '2019-02-10'),
    ('Mickey', 'Mouse', '[email protected]', '2017-06-22')

This worked perfectly as they all got partitioned to their respective CHILD PARTITONED TABLE

So obviously my DEFAULT CHILD PARTITION TABLE should have no values

NOW the issue comes with inserting new records:

INSERT INTO users_range_part 
    (user_first_name, user_last_name, user_email_id, created_dt)
VALUES 
    ('Dee', 'Vic', '[email protected]', '2018-10-31'),
    ('Don', 'Jay', '[email protected]', '2019-12-31'),
    ('Mik', 'Mousse', '[email protected]', '2017-09-30')

When I run this, the expected output is to still partition the records according to their months but the records dont get moved but are still kept in the DEFAULT CHILD PARTITION...
Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文