Get table’s row count and size in PostgreSQL


Environment and Prerequisite

  • PostgreSQL


Row Count

  • Use COUNT(*) in query
select count(*) from {table_name};
  • Get estimated value using pg_class
select relname, reltuples from pg_class where relname='{table_name}';
select n.nspname as table_schema, c.relname as table_name, c.reltuples as rows
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where c.relname='{table_name}' and c.relkind = 'r' and n.nspname not in ('information_schema','pg_catalog')
order by c.reltuples desc;


Size

  • Use pg_total_relation_size() function
select pg_size_pretty(pg_total_relation_size('{table_name}'));


Get row count and size using one query

create or replace function count_rows_of_table(table_schema text, table_name text)
returns numeric
language plpgsql
as
$$
declare
 count numeric;
begin
 execute format('select count(*) from %s.%s', table_schema, table_name)
 into count;
 return count;
end;
$$;

select n.nspname as table_schema, c.relname as table_name, c.reltuples as estimated_row_count, count_rows_of_table(n.nspname, c.relname) as exact_row_count
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'r' and n.nspname not in ('information_schema','pg_catalog')
order by c.reltuples desc;


Reference

두 datetime 사이의 날짜들을 가져오자


환경

  • Python


예제

  • 두 datetime 사이의 날짜들 가져오기
import datetime
start_date = datetime.datetime(2021, 11, 15)
end_date = datetime.datetime(2021, 11, 21)

dates = [(start_date + datetime.timedelta(days=day_delta)) for day_delta in range((end_date - start_date).days + 1)]

for date in dates:
    print(date)
2021-11-15 00:00:00
2021-11-16 00:00:00
2021-11-17 00:00:00
2021-11-18 00:00:00
2021-11-19 00:00:00
2021-11-20 00:00:00
2021-11-21 00:00:00
  • 두 datetime 사이의 날짜들을 포맷 변경해서 가져오기
import datetime
start_date = datetime.datetime(2021, 11, 15)
end_date = datetime.datetime(2021, 11, 21)

dates = [(start_date + datetime.timedelta(days=day_delta)).strftime("%Y/%m/%d") for day_delta in range((end_date - start_date).days + 1)]

for date in dates:
    print(date)
2021/11/15
2021/11/16
2021/11/17
2021/11/18
2021/11/19
2021/11/20
2021/11/21


참고자료

Post about getting dates between two datetime dates


Environment and Prerequisite

  • Python


Example

  • Get dates between two datetime dates
import datetime
start_date = datetime.datetime(2021, 11, 15)
end_date = datetime.datetime(2021, 11, 21)

dates = [(start_date + datetime.timedelta(days=day_delta)) for day_delta in range((end_date - start_date).days + 1)]

for date in dates:
    print(date)
2021-11-15 00:00:00
2021-11-16 00:00:00
2021-11-17 00:00:00
2021-11-18 00:00:00
2021-11-19 00:00:00
2021-11-20 00:00:00
2021-11-21 00:00:00
  • Get formatted dates between two datetime dates
import datetime
start_date = datetime.datetime(2021, 11, 15)
end_date = datetime.datetime(2021, 11, 21)

dates = [(start_date + datetime.timedelta(days=day_delta)).strftime("%Y/%m/%d") for day_delta in range((end_date - start_date).days + 1)]

for date in dates:
    print(date)
2021/11/15
2021/11/16
2021/11/17
2021/11/18
2021/11/19
2021/11/20
2021/11/21


Reference

GPG 키를 통해 서명시 “gpg에서 데이터를 서명하는데 실패했습니다.”가 나오는 문제에 대한 해결법


환경

  • Git


문제

  • gpg 키를 사용해 git commit을 작성하는데 아래와 같은 이슈 발생
error: gpg에서 데이터를 서명하는데 실패했습니다.
fatal: 커밋 오브젝트를 쓰는데 실패했습니다


해결법

  • 검색해보니 아래 명령어를 사용하면 비밀번호 입력창이 나오고 문제 해결
  • gpg 키를 통한 서명시 비밀번호가 필요한데 해당 방법이 없어서 발생한 이슈로 추정
    • 해당 gpg 키는 초기설정시 비밀번호가 설정된 상태
export GPG_TTY=$(tty)


참고자료

Post about solving issue which appears “gpg failed to sign the data” when sign with GPG key


Environment and Prerequisite

  • Git


Issue

  • There was an issue when make git commit with gpg key
error: gpg failed to sign the data
fatal: failed to write commit object


Solution

  • By searching on web, found that using below command makes to appear password input box and issue solved
  • It seems like there are no ways to input password when sign with gpg key which needs password
    • That gpg key is set with password in intial setting
export GPG_TTY=$(tty)


Reference