SCSArchiver CSStudio Postgres Partitioning

  • database_owner_ship provided:
    1. Download postgres_partitioning.txt from cs-studio@github
    2. Since the table sample is already created, optionally including FOREIGN KEY constraints,
      • skip DROP TABLE IF EXISTS sample, though you will run into an error
    3. replace archive. prefixes by "$schema." prefix
    4. add client_encoding and SEARCH PATH in front
    5. call pqsl with
      • option "-f" loading modified partitioning
      • option "-a" to show all

%CODE{"bash"}% export PG_Owner=<database_owner, e.g. "postgres"> export PGUSER=<schema_owner, e.g. "archiver"> export PGHOST=%database_host% export PGPORT=%port% export PGDATABASE=%database% schema=%schema% URL="%url%" filename=$(basename $URL ) outputdir=$(mktemp -d); outputfile=$(mktemp -p ${outputdir}) ; cat > ${outputfile} << EOF --- --- SET client_encoding = "UTF-8"; SET SEARCH_PATH TO "$schema"; EOF

cd ${outputdir} && wget -N --no-check-certificate ${URL} && perl -p -e "s/\sarchive\./ $schema./g;" -e "s/OWNER TO postgres;/OWNER TO \"${PGUSER:?PGUSER undefined}\";/g;" -e "s/DROP TABLE IF EXISTS sample;.*?$//g;" ${filename} >> ${outputfile} && echo -e "\nSELECT %schema%.sample_update_partitions('2015-01-01'::timestamp, '%schema%', '$PGUSER', 'week');" >> ${outputfile} && psql -U ${PG_Owner} -a -f ${outputfile} && rm -ir ${outputdir} && cd - %ENDCODE%
Note
if psql -U $(PG_Owner) does not work, unset PGHOST;

crontab :
%CODE{"bash"}% -- This maintenance function automatically creates partitions according to the -- specified interval (e.g. weekly or monthly). The first partition starts at -- and ends a day/week/month/year later. This function has to be -- called regularly (e.g. daily by cron): -- -- @daily psql -a -h %database_host% -p %port% -U <database_owner, e.g. "postgres"> -d %database% -c "SELECT %schema%.sample_update_partitions('2015-01-01'::timestamp, '%schema%', '<database_owner, e.g. "postgres">', 'week');" -- -- This function is based on a more generic version by Nicholas Whittier -- (http://imperialwicket.com/postgresql-automating-monthly-table-partitions). %ENDCODE% Install a cronjob on any relevant machine using .pgpass (q.v. Psql pgwiki.gsi.de #.pgpass)
(german) note about the credentials
... da Sie vorhaben, "psql" für Ihren CronJob zu nutzen, können Sie alle relevanten PostgreSQL-Connection-Daten in Shell-Variablen hinterlegen. Im pgWiki finden Sie dazu eine Anleitung / Empfehlung von uns, wie so ein PostgreSQL-Environment aussehen könnte. Es gibt auch die Datei ".pgpass, wo Sie Passwörter für Ihre Verbindungen hinterlegen können. 1x hinterlegt, bezieht "psql" standardmäßig aus dieser Datei die passenden Passwörter. Wichtig ist nur, dass CronJob unter dem User ausgeführt wird, bei dem die Datei .pgpass hinterlegt ist.
Sie können Ihr Environment sogar soweit "vorbereiten", dass Sie sogar den DBNamen, den Ufernamen, Port, Server, etc. bei Ihrem Cron-Aufruf nicht mehr mit angeben müssen. Die Environment-Variablen geben "psql" vor, wie und wohin er sich per Standard connecten soll.


-- PeterZumbruch - 16 Jan 2018

This topic: DaqSlowControl > HadesDaqDocumentation > DetectorControlSystem > SCSArchiverCSStudio > SCSArchiverCSStudioPostgresPartitioning
Topic revision: 2018-01-16, PeterZumbruch
Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki Send feedback | Imprint | Privacy Policy (in German)